jenkins-bot has submitted this change and it was merged.

Change subject: Return assessment level
......................................................................


Return assessment level

Tries to guess image assessment level (quality, featured etc) from Commons 
categories.

Change-Id: I137607bb38ee4ceba23b738fd86cc8c713290874
---
M CommonsMetadata_body.php
1 file changed, 44 insertions(+), 1 deletion(-)

Approvals:
  MarkTraceur: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/CommonsMetadata_body.php b/CommonsMetadata_body.php
index 8caa875..6520062 100755
--- a/CommonsMetadata_body.php
+++ b/CommonsMetadata_body.php
@@ -31,6 +31,19 @@
        );
 
        /**
+        * Mapping of category names to assesment levels. Array keys are 
regexps which will be
+        * matched case-insensitively against category names; the first match 
is returned.
+        * @var array
+        */
+       protected static $assessmentCategories = array(
+               'poty' => '/^pictures of the year \(.*\)/',
+               'potd' => '/^pictures of the day \(.*\)/',
+               'featured' => '/^featured (pictures|sounds) on wikimedia 
commons/',
+               'quality' => '/^quality images/',
+               'valued' => '/^valued images/',
+       );
+
+       /**
         * @param $doc String The html to parse
         * @param String|boolean $lang Language code or false for all langs.
         * @return Array The properties extracted from the page.
@@ -91,8 +104,16 @@
 
                // For now only get the immediate categories
                $categories = self::getCategories( $file );
+
+               $assessments = self::getAssessmentsAndRemoveFromCategories( 
$categories );
+
                $combinedMeta['Categories'] = array(
                        'value' => implode( '|', $categories ),
+                       'source' => 'commons-categories',
+               );
+
+               $combinedMeta['Assessments'] = array(
+                       'value' => implode('|', $assessments),
                        'source' => 'commons-categories',
                );
 
@@ -165,7 +186,29 @@
                                unset( $categories[$i] );
                        }
                }
-               return array_merge( $licenses ); // renumber to avoid holes in 
array
+               $categories = array_merge( $categories ); // renumber to avoid 
holes in array
+               return $licenses;
+       }
+
+       /**
+        * Matches category names to a category => assessment mapping, removes 
the matching categories
+        * and returns the corresponding assessments (valued image, picture of 
the day etc).
+        * @param array $categories a list of human-readable category names.
+        * @return array
+        * FIXME categories do not work with Commons-hosted images due to bug 
56598
+        */
+       protected static function getAssessmentsAndRemoveFromCategories( 
&$categories ) {
+               $assessments = array();
+               foreach ( $categories as $i => $category ) {
+                       foreach ( self::$assessmentCategories as 
$assessmentType => $regexp ) {
+                               if ( preg_match( $regexp, $category ) ) {
+                                       $assessments[] = $assessmentType;
+                                       unset( $categories[$i] );
+                               }
+                       }
+               }
+               $categories = array_merge( $categories ); // renumber to avoid 
holes in array
+               return array_unique($assessments); // potd/poty can happen 
multiple times
        }
 
        /**

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I137607bb38ee4ceba23b738fd86cc8c713290874
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/CommonsMetadata
Gerrit-Branch: master
Gerrit-Owner: GergÅ‘ Tisza <[email protected]>
Gerrit-Reviewer: GergÅ‘ Tisza <[email protected]>
Gerrit-Reviewer: MarkTraceur <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to