mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/PageImages
synced 2024-11-14 19:34:46 +00:00
Scoring system
This commit is contained in:
parent
6d258671b5
commit
63361c93c3
|
@ -1,16 +1,21 @@
|
|||
<?php
|
||||
|
||||
class PageImages {
|
||||
public static function registerImage( Title $title, File $file, &$params, Parser $parser ) {
|
||||
public static function registerImage( Title $title, File $file, array &$params, Parser $parser ) {
|
||||
$out = $parser->getOutput();
|
||||
if ( !isset( $out->pageImages ) ) {
|
||||
$out->pageImages = array();
|
||||
}
|
||||
$myParams = $params;
|
||||
if ( !isset( $myParams['handler']['width'] ) ) {
|
||||
$myParams['handler']['width'] = $file->getWidth();
|
||||
if ( !isset( $myParams['thumbnail'] ) ) {
|
||||
$myParams['handler']['width'] = $file->getWidth();
|
||||
} else {
|
||||
$myParams['handler']['width'] = 250;
|
||||
}
|
||||
}
|
||||
$out->pageImages[$title->getDBkey()] = $myParams;
|
||||
$myParams['file'] = $title->getDBkey();
|
||||
$out->pageImages[] = $myParams;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -19,6 +24,62 @@ class PageImages {
|
|||
return true;
|
||||
}
|
||||
$images = $lu->getParserOutput()->pageImages;
|
||||
$scores = array();
|
||||
$imagesByExtension = array( 'jpg' => array(), 'jpeg' => array() );
|
||||
$counter = 0;
|
||||
foreach ( $images as $image ) {
|
||||
$fileName = $image['file'];
|
||||
$extension = strtolower( substr( $fileName, strrpos( $fileName, '.' ) + 1 ) );
|
||||
$image['extension'] = $extension;
|
||||
if ( !isset( $imagesByExtension[$extension][$fileName] ) ) {
|
||||
$imagesByExtension[$extension][$fileName] = true;
|
||||
}
|
||||
if ( !isset( $scores[$fileName] ) ) {
|
||||
$scores[$fileName] = -1;
|
||||
}
|
||||
$scores[$fileName] = max( $scores[$fileName], self::getScore( $image, $counter++ ) );
|
||||
}
|
||||
$jpegs = array_merge( array_keys( $imagesByExtension['jpg'] ),
|
||||
array_keys( $imagesByExtension['jpeg'] )
|
||||
);
|
||||
$jpegScores = array_map( function( $name ) use ( $scores ) {
|
||||
return $scores[$name];
|
||||
},
|
||||
$jpegs
|
||||
);
|
||||
arsort( $jpegScores );
|
||||
if ( count( $jpegScores ) && $jpegScores[0] >= 0 ) {
|
||||
$lu->mProperties['has_photos'] = 1;
|
||||
}
|
||||
$image = false;
|
||||
foreach ( $scores as $name => $score ) {
|
||||
if ( $score > 0 && ( !$image || $score > $scores[$image] ) ) {
|
||||
$image = $name;
|
||||
}
|
||||
}
|
||||
if ( $image ) {
|
||||
$lu->mProperties['page_image'] = $image;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static function getScore( array $image, $position ) {
|
||||
global $wgPageImagesScores;
|
||||
|
||||
$score = 0;
|
||||
if ( isset( $wgPageImagesScores['extension'][$image['extension']] ) ) {
|
||||
$score += $wgPageImagesScores['extension'][$image['extension']];
|
||||
}
|
||||
foreach ( $wgPageImagesScores['width'] as $maxWidth => $scoreDiff ) {
|
||||
if ( $image['handler']['width'] <= $maxWidth ) {
|
||||
$score += $scoreDiff;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( isset( $wgPageImagesScores['position'][$position] ) ) {
|
||||
$score += $wgPageImagesScores['position'][$position];
|
||||
}
|
||||
return $score;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,3 +11,18 @@ $wgAutoloadClasses['PageImages'] = "$dir/PageImages.body.php";
|
|||
|
||||
$wgHooks['ParserMakeImageParams'][] = 'PageImages::registerImage';
|
||||
$wgHooks['LinksUpdate'][] = 'PageImages::getProperties';
|
||||
|
||||
$wgPageImagesScores = array(
|
||||
'extension' => array(
|
||||
'jpg' => 5,
|
||||
'jpeg' => 5,
|
||||
'png' => 1,
|
||||
'svg' => 1,
|
||||
),
|
||||
'position' => array( 8, 6, 4, 3 ),
|
||||
'width' => array(
|
||||
99 => -100, // Very small images are usually from maintenace or stub templates
|
||||
300 => 10,
|
||||
500 => 5, // Larger images are panoramas, less suitable
|
||||
),
|
||||
);
|
Loading…
Reference in a new issue