Gergő Tisza has uploaded a new change for review.

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

Change subject: Weight images by copyright status
......................................................................

Weight images by copyright status

Bug: T124225
Change-Id: I21111ecbc80ded864806a2a93002f3254c3c68a9
---
M PageImages.php
M includes/PageImages.php
M tests/phpunit/PageImagesTest.php
3 files changed, 54 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/PageImages 
refs/changes/96/266196/1

diff --git a/PageImages.php b/PageImages.php
index 81e355c..80d13f3 100644
--- a/PageImages.php
+++ b/PageImages.php
@@ -63,6 +63,9 @@
                30 => 0,
                31 => -100,
        ),
+       'rights' => array(
+               'nonfree' => -100, // don't show nonfree images
+       ),
 );
 
 $wgPageImagesBlacklist = array(
diff --git a/includes/PageImages.php b/includes/PageImages.php
index b986fd8..008e4cb 100644
--- a/includes/PageImages.php
+++ b/includes/PageImages.php
@@ -333,6 +333,8 @@
        private static function getScore( array $image, $position ) {
                global $wgPageImagesScores;
 
+               $image += self::getMetadata( wfFindFile( $image['filename'] ) );
+
                if ( isset( $image['handler'] ) ) {
                        // Standalone image
                        $score = self::scoreFromTable( 
$image['handler']['width'], $wgPageImagesScores['width'] );
@@ -348,6 +350,10 @@
                $ratio = intval( self::getRatio( $image ) * 10 );
                $score += self::scoreFromTable( $ratio, 
$wgPageImagesScores['ratio'] );
 
+               if ( isset( $image['rights'] ) && isset( 
$wgPageImagesScores['rights'][$image['rights']] ) ) {
+                       $score += 
$wgPageImagesScores['rights'][$image['rights']];
+               }
+
                $blacklist = self::getBlacklist();
                if ( isset( $blacklist[$image['filename']] ) ) {
                        $score = -1000;
@@ -357,6 +363,28 @@
        }
 
        /**
+        * Return some file metadata (only what's relevant to page image 
scores).
+        * @param File $file
+        * @return array
+        */
+       private static function getMetadata( File $file ) {
+               $format = new FormatMetadata;
+               $context = new DerivativeContext( $format->getContext() );
+               $format->setSingleLanguage( true ); // we don't care and it's 
slightly faster
+               $context->setLanguage( 'en' ); // we don't care so avoid 
splitting the cache
+               $format->setContext( $context );
+               $extmetadata = $format->fetchExtendedMetadata( $file );
+               $processedMetadata = array();
+
+               // process copyright metadata from CommonsMetadata, if present
+               if ( !empty( $extmetadata['NonFree']['value'] ) ) { // not '0' 
or unset
+                       $processedMetadata['rights'] = 'nonfree';
+               }
+
+               return $processedMetadata;
+       }
+
+       /**
         * Returns width/height ratio of an image as displayed or 0 is not 
available
         *
         * @param array $image
diff --git a/tests/phpunit/PageImagesTest.php b/tests/phpunit/PageImagesTest.php
index 6efb247..3f3cfc3 100644
--- a/tests/phpunit/PageImagesTest.php
+++ b/tests/phpunit/PageImagesTest.php
@@ -3,6 +3,7 @@
 namespace PageImages\Tests;
 
 use MediaWikiTestCase;
+use TestingAccessWrapper;
 use PageImages;
 use ParserOutput;
 use Title;
@@ -52,4 +53,26 @@
                $this->assertSame( 'A.jpg', 
$linksUpdate->mProperties[PageImages::PROP_NAME] );
        }
 
+       /**
+        * @dataProvider provideGetScore
+        */
+       public function testGetScore( $image, $position, $expectedScore ) {
+               $pageImages = TestingAccessWrapper::newFromClass( 'PageImages' 
);
+               $score = $pageImages->getScore( $image, $position );
+               $this->assertSame( $expectedScore, $score );
+       }
+
+       public function provideGetScore() {
+               $image = array(
+                       'filename' => 'Example.png',
+                       'fullwidth' => 100,
+                       'fullheight' => 100,
+               );
+               return array(
+                       array( $image, 10, 5 ),
+                       array( $image, 0, 13 ),
+                       array( $image + array( 'rights' => 'nonfree' ), 10, -95 
),
+               );
+       }
+
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I21111ecbc80ded864806a2a93002f3254c3c68a9
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/PageImages
Gerrit-Branch: master
Gerrit-Owner: Gergő Tisza <gti...@wikimedia.org>

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

Reply via email to