Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull version numbers from Wikidata #39

Merged
merged 2 commits into from Aug 7, 2017
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -10,6 +10,7 @@ function browsehappy_get_browser_data( $browser = false ) {
'name' => 'Google Chrome',
'long_name' => 'Google Chrome',
'wikipedia' => 'Google_Chrome',
'wikidata' => 'Q777',
'normalized' => 1, // just first number
'facebook' => 'googlechrome',
'url' => 'https://www.google.com/chrome',
@@ -19,6 +20,7 @@ function browsehappy_get_browser_data( $browser = false ) {
'name' => 'Mozilla Firefox',
'long_name' => 'Mozilla Firefox',
'wikipedia' => 'Firefox',
'wikidata' => 'Q698',
'normalized' => 1.5, // include second number if non-zero
'facebook' => 'Firefox',
'url' => 'https://www.firefox.com/',
@@ -28,6 +30,7 @@ function browsehappy_get_browser_data( $browser = false ) {
'name' => 'Safari',
'long_name' => 'Apple Safari',
'wikipedia' => 'Safari',
'wikidata' => 'Q35773',
'normalized' => 1.5, // include second number if non-zero
'facebook' => false,
'url' => 'https://www.apple.com/safari/',
@@ -37,6 +40,7 @@ function browsehappy_get_browser_data( $browser = false ) {
'name' => 'Opera',
'long_name' => 'Opera',
'wikipedia' => 'Opera',
'wikidata' => 'Q41242',
'normalized' => 1, // just first number
'facebook' => 'Opera',
'url' => 'http://www.opera.com/',
@@ -46,6 +50,7 @@ function browsehappy_get_browser_data( $browser = false ) {
'name' => 'Microsoft Edge',
'long_name' => 'Microsoft Edge',
'wikipedia' => 'Microsoft_Edge',
'wikidata' => 'Q18698690',
'normalized' => 1, // just first number
'facebook' => 'MicrosoftEdge',
'url' => 'https://www.microsoft.com/en-us/windows/microsoft-edge',
@@ -55,6 +60,7 @@ function browsehappy_get_browser_data( $browser = false ) {
'name' => 'Internet Explorer',
'long_name' => 'Microsoft Internet Explorer',
'wikipedia' => 'Internet_Explorer',
'wikidata' => 'Q1575',
'normalized' => 1, // just first number
'facebook' => 'internetexplorer',
'url' => 'http://windows.microsoft.com/ie',
@@ -79,63 +85,66 @@ function browsehappy_echo_version( $browser ) {
function browsehappy_fetch_version( $browser, $normalize = true ) {
$fragment = browsehappy_get_browser_data( $browser )->wikipedia;
if ( ! $fragment )
$fragment = browsehappy_get_browser_data( $browser )->wikidata;
if ( ! $fragment ) {
return false;
}
// Unexpiring transients are autoloaded. We expire these manually on cron instead.
$stored_version = get_transient( 'browsehappy_version_' . $browser );
if ( false !== $stored_version ) {
if ( $normalize )
if ( $normalize ) {
return browsehappy_normalize_version( $browser, $stored_version );
}
return $stored_version;
}
$url = 'https://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=php&titles=Template:Latest_stable_software_release/';
$url .= $fragment;
// See https://github.com/WordPress/browsehappy/issues/37
$query = "
SELECT ?version WHERE {
wd:{$fragment} p:P348 [
ps:P348 ?version;
pq:P548 wd:Q12355314;
wikibase:rank wikibase:PreferredRank
].
}
LIMIT 1
";
$response = wp_remote_get( $url );
$request = wp_remote_get( add_query_arg(
array(
'format' => 'json',
'query' => rawurlencode( $query ),
),
'https://query.wikidata.org/bigdata/namespace/wdq/sparql'
) );
if ( is_wp_error( $response ) )
if ( is_wp_error( $request ) ) {
return false;
}
if ( ! $content = wp_remote_retrieve_body( $response ) )
return false;
$data = json_decode( wp_remote_retrieve_body( $request ) );
if ( ! is_serialized( $content ) )
if (
empty( $data ) ||
empty( $data->results ) ||
! is_array( $data->results->bindings ) ||
empty( $data->results->bindings[0] ) ||
empty( $data->results->bindings[0]->version ) ||
empty( $data->results->bindings[0]->version->value )
) {
return false;
$content = maybe_unserialize( $content );
$page = array_pop( $content['query']['pages'] );
$raw_data = explode( "\n", $page['revisions'][0]['*'] );
$version = false;
foreach( $raw_data as $data ) {
$data = trim( $data, '| ' );
if ( false !== strpos( $data, 'Android' ) || false !== strpos( $data, 'iOS' ) )
continue;
if ( false !== strpos( $data, 'Linux' ) && false === strpos( $data, 'Mac OS X' ) && false === strpos( $data, 'Windows' ) && false === strpos( $data, 'Microsoft' ) )
continue;
if ( ( false !== $pos = strpos( $data, 'latest_release_version' ) ) || ( false !== $pos = strpos( $data, 'latest release version' ) ) ) {
if ( $pos )
$data = substr( $data, $pos );
$version = trim( str_replace( array( 'latest_release_version', 'latest release version', '=' ), '', $data ), '| ' ) . " ";
$version = str_replace( "'''Mac OS X''' and '''Microsoft Windows'''<br />", '', $version );
$version = str_replace( "'''Windows 10'''<br>", '', $version );
$version = substr( $version, 0, strpos( $version, ' ' ) );
break;
}
}
if ( false === $version )
return false;
$version = $data->results->bindings[0]->version->value;
$version = preg_replace( '/[^0-9\.]/', '', $version );
set_transient( 'browsehappy_version_' . $browser, $version );
if ( $normalize )
if ( $normalize ) {
return browsehappy_normalize_version( $browser, $version );
}
return $version;
}
@@ -262,4 +271,4 @@ function browsehappy_locale_notice() {
<p><?php printf( __( 'Browse Happy is also available in English. <a href="%s">Click here to change the language to English</a>.', 'browsehappy' ), '/?locale=en' ); ?></p>
</div>
<?php
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.