Making WordPress.org

Ticket #6275: 6275.diff

File 6275.diff, 4.9 KB (added by dd32, 3 years ago)
  • plugin-directory/zip/class-builder.php

    class Builder { 
    196196                        return;
    197197                }
    198198
    199199                $plugin_version = $plugin_data['Version'];
    200200                // Catch malformed version strings.
    201201                if ( basename( $plugin_version ) != $plugin_version ) {
    202202                        return;
    203203                }
    204204
    205205                $this->checksum_file = "{$this->tmp_dir}/{$this->slug}/{$this->slug}.{$plugin_version}.checksums.json";
    206206
    207207                // Checkout the Checksum file for this plugin version
    208208                SVN::up( $this->checksum_file );
    209209
    210210                // Existing checksums?
    211                 $existing_json_checksum_file = file_exists( $this->checksum_file );
     211                $existing_json_checksum_file = file_exists( $this->checksum_file ) ? json_decode( file_get_contents( $this->checksum_file ) ) : false;
    212212
    213213                $skip_bad_files = array();
    214214                $checksums      = array();
    215215                foreach ( array(
    216216                        'md5'    => 'md5sum',
    217217                        'sha256' => 'sha256sum',
    218218                ) as $checksum_type => $checksum_bin ) {
    219219                        $checksum_output = array();
    220220                        $this->exec( sprintf(
    221221                                'cd %s && find . -type f -print0 | sort -z | xargs -0 ' . $checksum_bin . ' 2>&1',
    222222                                escapeshellarg( $this->tmp_build_dir . '/' . $this->slug )
    223223                        ), $checksum_output, $return_value );
    224224
    225225                        if ( $return_value ) {
    226226                                // throw new Exception( __METHOD__ . ': Checksum generation failed, return code: ' . $return_value, 503 );
    class Builder { 
    235235                                $checksum = trim( $checksum );
    236236
    237237                                // See https://meta.trac.wordpress.org/ticket/3335 - Filenames like 'Testing Test' truncated to 'Testing'
    238238                                if ( preg_match( '!^(\S+)\s+\S!', $filename, $m ) ) {
    239239                                        $skip_bad_files[ $m[1] ] = true;
    240240                                }
    241241
    242242                                if ( ! isset( $checksums[ $filename ] ) ) {
    243243                                        $checksums[ $filename ] = array(
    244244                                                'md5'    => array(),
    245245                                                'sha256' => array(),
    246246                                        );
    247247                                }
    248248
    249249                                $checksums[ $filename ][ $checksum_type ] = $checksum;
     250
     251                                // If the checksums have already been generated, and this file did not previously exist, it's just been added and therefor is optional.
     252                                if ( ! empty( $existing_json_checksum_file->files ) && ! isset( $existing_json_checksum_file->files[ $filename ] ) ) {
     253                                        $checksums[ $filename ]['optional'] = true;
     254                                }
    250255                        }
    251256                }
    252257
    253258                $json_checksum_file = (object) array(
     259                        'format'  => 2,
    254260                        'plugin'  => $this->slug,
    255261                        'version' => $plugin_version,
    256262                        'source'  => $this->plugin_version_svn_url,
    257263                        'zip'     => 'https://downloads.wordpress.org/plugins/' . basename( $this->zip_file ),
    258264                        'files'   => $checksums,
    259265                );
    260266
    261267                // If the checksum file exists already, merge it into this one.
    262268                if ( $existing_json_checksum_file ) {
    263                         $existing_json_checksum_file = json_decode( file_get_contents( $this->checksum_file ) );
    264 
    265269                        // Sometimes plugin versions exist in multiple tags/zips, include all the SVN urls & ZIP urls
    266270                        foreach ( array( 'source', 'zip' ) as $maybe_different ) {
    267271                                if ( ! empty( $existing_json_checksum_file->{$maybe_different} ) &&
    268272                                        $existing_json_checksum_file->{$maybe_different} != $json_checksum_file->{$maybe_different}
    269273                                ) {
    270274                                        $json_checksum_file->{$maybe_different} = array_unique( array_merge(
    271275                                                (array) $existing_json_checksum_file->{$maybe_different},
    272276                                                (array) $json_checksum_file->{$maybe_different}
    273277                                        ) );
    274278
    275279                                        // Reduce single arrays back to a string when possible.
    276280                                        if ( 1 == count( $json_checksum_file->{$maybe_different} ) ) {
    277281                                                $json_checksum_file->{$maybe_different} = array_shift( $json_checksum_file->{$maybe_different} );
    278282                                        }
    279283                                }
    280284                        }
    281285
    282286                        // Combine Checksums from existing files and the new files
    283287                        foreach ( $existing_json_checksum_file->files as $file => $checksums ) {
    284288
    285289                                if ( ! isset( $json_checksum_file->files[ $file ] ) ) {
    286290                                        if ( isset( $skip_bad_files[ $file ] ) ) {
    287291                                                // See https://meta.trac.wordpress.org/ticket/3335
    288292                                                // This is a partial filename, which shouldn't have been in the checksums.
    289293                                                continue;
    290294                                        }
    291295
    292296                                        // Deleted file, use existing checksums.
    293297                                        $json_checksum_file->files[ $file ] = $checksums;
    294298
     299                                        // Flag the file as optional, it was present, but now it's gone.
     300                                        $json_checksum_file->files[ $file ]['optional'] = true;
     301
    295302                                } elseif ( $checksums !== $json_checksum_file->files[ $file ] ) {
    296303                                        // Checksum has changed, include both in the resulting json file.
    297304                                        foreach ( array( 'md5', 'sha256' ) as $checksum_type ) {
    298305                                                $json_checksum_file->files[ $file ][ $checksum_type ] = array_unique( array_merge(
    299306                                                        (array) $checksums->{$checksum_type}, // May already be an array
    300307                                                        (array) $json_checksum_file->files[ $file ][ $checksum_type ]
    301308                                                ) );
    302309
    303310                                                // Reduce single arrays back to a string when possible.
    304311                                                if ( 1 == count( $json_checksum_file->files[ $file ][ $checksum_type ] ) ) {
    305312                                                        $json_checksum_file->files[ $file ][ $checksum_type ] = array_shift( $json_checksum_file->files[ $file ][ $checksum_type ] );
    306313                                                }
    307314                                        }
    308315                                }
    309316                        }