Making WordPress.org


Ignore:
Timestamp:
10/01/2018 08:29:33 PM (6 years ago)
Author:
coffee2code
Message:

Browse Happy: Add fallback to prevent latest browser version number from not being shown.

Doing a WikiQuery ranking based on wikibase:PreferredRank can fail to return a result if no version was marked as preferred, which can sometimes happen. In such cases, rerun the query but rank based on wikibase:NormalRank with no limits and sort result to determine the latest version.

See https://github.com/WordPress/browsehappy/issues/48.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/browsehappy.com/public_html/functions.php

    r7710 r7711  
    8484}
    8585
    86 function browsehappy_fetch_version( $browser, $normalize = true ) {
     86function browsehappy_fetch_version( $browser, $normalize = true, $rank = true ) {
    8787
    8888    $fragment = browsehappy_get_browser_data( $browser )->wikidata;
     
    100100    }
    101101
     102    $rank_type = $rank ? 'PreferredRank' : 'NormalRank';
     103    $limit     = $rank ? 'LIMIT 1' : '';
     104
    102105    // See https://github.com/WordPress/browsehappy/issues/37
    103106    $query = "
     
    105108            wd:{$fragment} p:P348 [
    106109                ps:P348 ?version;
    107                 wikibase:rank wikibase:PreferredRank
     110                wikibase:rank wikibase:{$rank_type}
    108111            ].
    109112        }
    110         LIMIT 1
     113        {$limit}
    111114    ";
    112115
     
    128131        empty( $data ) ||
    129132        empty( $data->results ) ||
    130         ! is_array( $data->results->bindings ) ||
     133        ! is_array( $data->results->bindings )
     134    ) {
     135        return false;
     136    }
     137
     138    if (
    131139        empty( $data->results->bindings[0] ) ||
    132140        empty( $data->results->bindings[0]->version ) ||
    133141        empty( $data->results->bindings[0]->version->value )
    134142    ) {
    135         return false;
     143        if ( $rank ) {
     144            return browsehappy_fetch_version( $browser, $normalize, false );
     145        } else {
     146            return false;
     147        }
     148    }
     149
     150    if ( ! $rank ) {
     151        usort( $data->results->bindings, function( $a, $b ) {
     152            return strcmp( $b->version->value, $a->version->value );
     153        } );
    136154    }
    137155
Note: See TracChangeset for help on using the changeset viewer.