MarkTraceur has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/311737

Change subject: Do useful things for other image types
......................................................................

Do useful things for other image types

Wikidata items sometimes don't have P18 (image), but have P41 (flag image) or
some other representative image.

So, we now have a list of preferences, starting with P18, all the way down to
P15 (route map), and we also check all other properties pointing to a 
representative
image as determined by the following WDQS query:

select ?prop where{?prop wdt:P31 wd:Q26940804.}

This means that e.g. Q155 (Brazil), which has a flag image, looks nice.

Bug: T146118
Change-Id: I251c1aeaddbeb53d0937067cd9afe931d5b613e2
---
M ApiFileAnnotations.php
1 file changed, 66 insertions(+), 10 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/FileAnnotations 
refs/changes/37/311737/1

diff --git a/ApiFileAnnotations.php b/ApiFileAnnotations.php
index fe090a5..35e8e2e 100644
--- a/ApiFileAnnotations.php
+++ b/ApiFileAnnotations.php
@@ -29,6 +29,8 @@
        const MIN_CACHE_TTL = WANObjectCache::TTL_MINUTE;
        const MAX_CACHE_TTL = WANObjectCache::TTL_DAY;
 
+       const LONG_CACHE_TTL = WANObjectCache::TTL_MONTH;
+
        public function __construct( $query, $moduleName ) {
                parent::__construct( $query, $moduleName, 'fa' );
        }
@@ -230,6 +232,51 @@
                );
        }
 
+       protected function getWikidataImageProps() {
+               $cache = ObjectCache::getMainWANInstance();
+               $cacheKey = $cache->makeKey( 'fileannotations', 
'wikidataimageprops' );
+
+               return $cache->getWithSetCallback(
+                       $cacheKey,
+                       self::LONG_CACHE_TTL,
+                       function ( $oldValue, &$ttl, array &$setOpts, $oldAsOf )
+                       use ( $cache ) {
+                               $client = new MultiHttpClient( [] );
+
+                               $response = $client->run( [
+                                       'method' => 'GET',
+                                       'url' => 
'https://query.wikidata.org/sparql',
+                                       'query' => [
+                                               'format' => 'json',
+                                               'query' => 'select ?prop 
where{?prop wdt:P31 wd:Q26940804.}',
+                                       ],
+                               ] );
+
+                               if ( $response['code'] == 200 ) {
+                                       $propsData = json_decode( 
$response['body'], true );
+                                       $props = 
$propsData['results']['bindings'];
+                                       $propArr = [];
+
+                                       foreach ( $props as $prop ) {
+                                               $uri = $prop['prop']['value'];
+                                               $propMatches = [];
+                                               $propMatch = preg_match(
+                                                       '%P\d+%',
+                                                       $uri,
+                                                       $propMatches
+                                               );
+                                               $propArr[] = $propMatches[0];
+                                       }
+
+                                       return $propArr;
+                               }
+
+                               $ttl = $cache::TTL_UNCACHEABLE;
+                               return [];
+                       }
+               );
+       }
+
        protected function renderWikidataAnnotation( $wdMatches ) {
                $entityId = $wdMatches[2];
                $currentLang = $this->getLanguage()->getCode();
@@ -273,17 +320,26 @@
 
                                $imageHtml = null;
 
-                               foreach ( $claims as $claimid => $claim ) {
-                                       switch ( $claimid ) {
-                                               case 'P18':
-                                                       // Main image. Fetch 
imageinfo and render.
-                                                       $imageHtml = 
$this->renderWdImage(
-                                                               
$claim[0]['mainsnak']['datavalue']['value']
-                                                       );
-                                                       break;
+                               $imageProps = $this->getWikidataImageProps();
 
-                                               default:
-                                                       continue;
+                               $imagePropPreference = array_merge( [
+                                       'P18', // image
+                                       'P41', // flag image
+                                       'P158', // seal image
+                                       'P94', // coat of arms image
+                                       'P154', // logo image
+                                       'P2716', // collage image
+                                       'P2713', // sectional view
+                                       'P2910', // icon
+                                       'P15', // route map
+                               ], $imageProps );
+
+                               foreach ( $imagePropPreference as $prop ) {
+                                       if ( isset( $claims[$prop] ) ) {
+                                               $imageHtml = 
$this->renderWdImage(
+                                                       
$claims[$prop][0]['mainsnak']['datavalue']['value']
+                                               );
+                                               break;
                                        }
                                }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/311737
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I251c1aeaddbeb53d0937067cd9afe931d5b613e2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/FileAnnotations
Gerrit-Branch: master
Gerrit-Owner: MarkTraceur <mholmqu...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to