Take ratio into account when considering images

An image might be of a sufficient width but still wildly unsuitable
like https://en.wikipedia.org/wiki/File:Status_iucn3.1_EN.svg

Change-Id: Ib34d8b5cc0b15407ce8cf0caae4df1c63247619b
This commit is contained in:
Max Semenik 2014-02-11 23:59:04 +04:00
parent c0739e0c4d
commit 34437e87da
2 changed files with 58 additions and 8 deletions

View file

@ -73,6 +73,8 @@ class PageImages {
}
}
$myParams['filename'] = $title->getDBkey();
$myParams['fullwidth'] = $file->getWidth();
$myParams['fullheight'] = $file->getHeight();
$out->pageImages[] = $myParams;
return true;
}
@ -87,6 +89,7 @@ class PageImages {
if ( !isset( $lu->getParserOutput()->pageImages ) ) {
return true;
}
wfProfileIn( __METHOD__ );
$images = $lu->getParserOutput()->pageImages;
$scores = array();
$counter = 0;
@ -106,6 +109,7 @@ class PageImages {
if ( $image ) {
$lu->mProperties[self::PROP_NAME] = $image;
}
wfProfileOut( __METHOD__ );
return true;
}
@ -154,23 +158,55 @@ class PageImages {
private static function getScore( array $image, $position ) {
global $wgPageImagesScores;
$score = 0;
foreach ( $wgPageImagesScores['width'] as $maxWidth => $scoreDiff ) {
if ( $image['handler']['width'] <= $maxWidth ) {
$score += $scoreDiff;
break;
}
}
$score = self::scoreFromTable( $image['handler']['width'], $wgPageImagesScores['width'] );
if ( isset( $wgPageImagesScores['position'][$position] ) ) {
$score += $wgPageImagesScores['position'][$position];
}
$ratio = intval( self::getRatio( $image ) * 10 );
$score += self::scoreFromTable( $ratio, $wgPageImagesScores['ratio'] );
$blacklist = self::getBlacklist();
if ( isset( $blacklist[$image['filename']] ) ) {
$score -= 1000;
$score = -1000;
}
return $score;
}
/**
* Returns width/height ratio of an image as displayed or 0 is not available
*
* @param array $image
* @return int
*/
private static function getRatio( array $image ) {
$width = $image['fullwidth'];
$height = $image['fullheight'];
if ( !$width || !$height ) {
return 0;
}
return $width / $height;
}
/**
* Returns score based on table of ranges
*
* @param int|float $value
* @param array $scores
* @return int
*/
private static function scoreFromTable( $value, array $scores ) {
$lastScore = 0;
foreach ( $scores as $boundary => $score ) {
if ( $value <= $boundary ) {
return $score;
}
$lastScore = $score;
}
return $lastScore;
}
/**
* Returns a list of images blacklisted from influencing this extension's output
* @return array: Flipped associative array in format "image BDB key" => int

View file

@ -26,12 +26,26 @@ $wgHooks['OpenSearchXml'][] = 'PageImages::onOpenSearchXml';
$wgAPIPropModules['pageimages'] = 'ApiQueryPageImages';
/**
* Configures how various aspects of image affect its score
*/
$wgPageImagesScores = array(
/** position of image in article */
'position' => array( 8, 6, 4, 3 ),
/** image width */
'width' => array(
99 => -100, // Very small images are usually from maintenace or stub templates
300 => 10,
500 => 5, // Larger images are panoramas, less suitable
501 => 0,
),
/** width/height ratio, in tenths */
'ratio' => array(
3 => -100,
5 => 0,
20 => 5,
30 => 0,
31 => -100,
),
);