Making WordPress.org


Ignore:
Timestamp:
12/14/2021 04:39:01 AM (3 years ago)
Author:
dd32
Message:

SVN Watcher: Reports: Avoid some PHP Notices, and compress the versions contributed to table. Instead of showing 3.0, 3.1, .... 5.8 just show 3.0-5.8.

See #5978.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-trac-watcher/admin/reports-page.php

    r11367 r11369  
    66    $url       = add_query_arg( 'page', $_REQUEST['page'], admin_url( 'admin.php' ) );
    77    $what      = $_REQUEST['what'] ?? '';
    8     $version   = $_REQUEST['version'] ?? (WP_CORE_LATEST_RELEASE+0.1);
     8    $version   = $_REQUEST['version'] ?? null;
    99    $revisions = $_REQUEST['revisions'] ?? '';
    1010    $branch    = $_REQUEST['branch'] ?? '';
     11    $is_core   = ( 'core' === $details['slug'] );
     12
     13    // Default to the latest version for core.
     14    if ( $is_core && is_null( $version ) ) {
     15        $version = sprintf( '%.1f', WP_CORE_LATEST_RELEASE + 0.1 );
     16    }
    1117
    1218    $url = add_query_arg( 'version', $version, $url );
     
    2127            <li><a href="<?php echo $url; ?>&what=raw-contributors-and-committers">All Props+Committers matching filter grouped together</a></li>
    2228           
     29            <?php if ( $is_core ) { ?>
    2330            <li><a href="<?php echo $url; ?>&what=versions-contributed">Versions which users have contributed to. Ignores filter.</a></li>
     31            <?php } ?>
    2432        </ol>
    2533
     
    2937
    3038        <?php
    31         if ( 'core' === $details['slug'] ) {
     39        if ( $is_core ) {
    3240            echo '<select name="version"><option value="">Version</option>';
    3341            foreach ( get_wordpress_versions() as $v ) {
     
    5765
    5866        // Revision range.
    59         echo '<input type="text" name="revisions" placeholder="Revs: 1:50 or 1,2,4,5" value="' . esc_attr( $revisions ) .'">';
     67        echo '<input type="text" name="revisions" placeholder="Revs: 1:HEAD or 1,2,4,5" value="' . esc_attr( $revisions ) .'">';
    6068
    6169        echo '<input type="submit" class="button button-primary" value="Filter">';
     
    7078        // Revisions.
    7179        if ( ! empty( $revisions ) ) {
    72             if ( false !== strpos( $revisions, ':' ) || false !== strpos( $revisions, '-' )  ) {
    73                 $where .= $wpdb->prepare( ' AND r.id BETWEEN %d AND %d', preg_split( '![-:]!', $revisions ) );
     80            if (  preg_match( '!(?P<start>\d+)[:-](?P<end>(HEAD|\d+))!', $args['revisions'], $m ) ) {
     81                if ( 'HEAD' === $m['end'] ) {
     82                    $where .= $wpdb->prepare( ' AND r.id > %d', $m['start'] );
     83                } else {
     84                    $where .= $wpdb->prepare( ' AND r.id BETWEEN %d AND %d', $m['start'], $m['end'] );
     85                }
    7486            } elseif ( false !== strpos( $revisions, ',' ) ) {
    7587                $ids = implode( ',', array_map( 'intval', explode( ',', $revisions ) ) );
     
    201213                    );
    202214
     215                    // Compress the versions list down to a smaller range.
     216                    $compress = function( $versions ) {
     217                        $in = $versions;
     218                        $out = [];
     219                        if ( ! is_array( $versions ) ) {
     220                            $versions = preg_split( '![,\s]+!', $versions );
     221                        }
     222
     223                        // Don't try to compress only a few versions.
     224                        if ( count( $versions ) <= 2 ) {
     225                            return $in;
     226                        }
     227
     228                        // [ X.Y => 0, X.Y+1 => 1, ... ]
     229                        $versions = array_flip( $versions );
     230
     231                        $not_a_version = [
     232                            '0.9', '1.1', '1.2', '1.4', '1.7', '1.8', '1.9',
     233                        ];
     234
     235                        // 2.4 + 2.5 are special. 2.4 was skipped, and many users only have a 2.4 or a 2.5 prop. Give them both versions if they have either.
     236                        if ( isset( $versions['2.5'] ) || isset( $versions['2.4'] ) ) {
     237                            $versions['2.5'] = $version['2.4'] = 1;
     238                        }
     239
     240                        $i = 0; // This counter is just here to protect against infinite loops should something go wrong.
     241                        while ( $versions && $i++ < 40 ) {
     242                            $min   = sprintf( '%.1f', min( array_keys( $versions ) ) );
     243                            $max   = sprintf( '%.1f', max( array_keys( $versions ) ) );
     244                            if ( $min === $max ) {
     245                                $out[] = $min;
     246                                break;
     247                            } elseif ( $max - $min < 0.2 ) {
     248                                $out[] = "{$min}-{$max}";
     249                                break;
     250                            }
     251
     252                            foreach ( range( $min, $max+0.1, 0.1 ) as $v ) {
     253                                $v = sprintf( '%.1f', $v );
     254
     255                                if ( in_array( $v, $not_a_version ) ) {
     256                                    unset( $versions[ $v ] );
     257                                    continue;
     258                                }
     259
     260                                if ( ! isset( $versions[ $v ] ) ) {
     261                                    $last = sprintf( '%.1f', $v - 0.1 );
     262
     263                                    if ( $last == $min ) {
     264                                        $out[] = $last;
     265                                        unset( $versions[ $v ] );
     266                                        break;
     267                                    } elseif ( $last - $min < 0.15 ) {
     268                                        // No point doing 1-2, just do 1, 2
     269                                        $out[] = $min;
     270                                        $out[] = $last;
     271                                    } else {
     272                                        $out[] = "{$min}-{$last}";
     273                                    }
     274                                    break;
     275                                } elseif ( $v == $max ) {
     276                                    $out[] = "{$min}-{$max}"; // (5)";
     277                                    unset( $versions[ $v ] );
     278                                    break;
     279                                } else {
     280                                    // no break. We're between a start, and end version.
     281                                }
     282                                unset( $versions[ $v ] );
     283                            }
     284                        }
     285
     286                        return implode( ', ', $out );
     287                    };
     288
    203289                    echo '<table class="widefat striped">';
    204290                    echo '<thead><tr><th>Prop</th><th>Count</th><th>Versions</th></tr></thead>';
     
    223309                            $p->count,
    224310                            esc_attr( $p->versions ),
    225                             $p->versions
     311                            $compress( $p->versions )
    226312                        );
    227313                    }
Note: See TracChangeset for help on using the changeset viewer.