2012-02-29 10:50:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class PageImages {
|
2012-05-08 21:42:07 +00:00
|
|
|
/**
|
|
|
|
* Update this when changing the scoring rules
|
|
|
|
*/
|
|
|
|
const VERSION = 1;
|
|
|
|
/**
|
|
|
|
* Returns true if data for this title should be saved
|
|
|
|
*
|
|
|
|
* @param Title $title
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private static function processThisTitle( Title $title ) {
|
|
|
|
static $flipped = false;
|
|
|
|
if ( $flipped === false ) {
|
|
|
|
global $wgPageImagesNamespaces;
|
|
|
|
$flipped = array_flip( $wgPageImagesNamespaces );
|
|
|
|
}
|
|
|
|
return isset( $flipped[$title->getNamespace()] );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-01 13:29:30 +00:00
|
|
|
/**
|
2012-03-01 14:33:28 +00:00
|
|
|
* ParserMakeImageParams hook handler, saes extended information about images used on page
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserMakeImageParams
|
2012-03-01 13:29:30 +00:00
|
|
|
* @param Title $title
|
|
|
|
* @param File|bool $file
|
|
|
|
* @param array $params
|
|
|
|
* @param Parser $parser
|
2012-03-02 16:45:07 +00:00
|
|
|
* @return bool
|
2012-03-01 13:29:30 +00:00
|
|
|
*/
|
2012-03-01 14:33:28 +00:00
|
|
|
public static function onParserMakeImageParams( Title $title, $file, array &$params, Parser $parser ) {
|
2012-05-08 21:42:07 +00:00
|
|
|
if ( !$file || !self::processThisTitle( $parser->getTitle() ) ) {
|
2012-03-01 13:29:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-02-29 10:50:36 +00:00
|
|
|
$out = $parser->getOutput();
|
|
|
|
if ( !isset( $out->pageImages ) ) {
|
|
|
|
$out->pageImages = array();
|
|
|
|
}
|
|
|
|
$myParams = $params;
|
|
|
|
if ( !isset( $myParams['handler']['width'] ) ) {
|
2012-03-01 12:25:57 +00:00
|
|
|
if ( !isset( $myParams['thumbnail'] ) ) {
|
|
|
|
$myParams['handler']['width'] = $file->getWidth();
|
|
|
|
} else {
|
|
|
|
$myParams['handler']['width'] = 250;
|
|
|
|
}
|
2012-02-29 10:50:36 +00:00
|
|
|
}
|
2012-03-02 16:42:17 +00:00
|
|
|
$myParams['filename'] = $title->getDBkey();
|
2012-03-01 12:25:57 +00:00
|
|
|
$out->pageImages[] = $myParams;
|
2012-02-29 10:50:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-01 14:33:28 +00:00
|
|
|
/**
|
2012-05-08 21:42:07 +00:00
|
|
|
* LinksUpdate hook handler, saves the information in the database
|
2012-03-01 14:33:28 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/LinksUpdate
|
|
|
|
* @param LinksUpdate $lu
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function onLinksUpdate( LinksUpdate $lu ) {
|
2012-05-08 21:42:07 +00:00
|
|
|
if ( !isset( $lu->getParserOutput()->pageImages ) || !self::processThisTitle( $lu->getTitle() ) ) {
|
2012-02-29 10:50:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$images = $lu->getParserOutput()->pageImages;
|
2012-03-01 12:25:57 +00:00
|
|
|
$scores = array();
|
|
|
|
$imagesByExtension = array( 'jpg' => array(), 'jpeg' => array() );
|
|
|
|
$counter = 0;
|
|
|
|
foreach ( $images as $image ) {
|
2012-03-02 16:42:17 +00:00
|
|
|
$fileName = $image['filename'];
|
2012-03-01 12:25:57 +00:00
|
|
|
$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
|
|
|
|
);
|
2012-03-01 13:29:30 +00:00
|
|
|
rsort( $jpegScores );
|
2012-05-08 21:42:07 +00:00
|
|
|
$image = null;
|
|
|
|
$count = 0;
|
|
|
|
$totalScore = 0;
|
2012-03-01 12:25:57 +00:00
|
|
|
foreach ( $scores as $name => $score ) {
|
2012-05-08 21:42:07 +00:00
|
|
|
if ( $score > 0 ) {
|
|
|
|
$count++;
|
|
|
|
$totalScore += $score;
|
|
|
|
if ( !$image || $score > $scores[$image] ) {
|
|
|
|
$image = $name;
|
|
|
|
}
|
2012-03-01 12:25:57 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-08 21:42:07 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
$pageId = $lu->getTitle()->getArticleID( Title::GAID_FOR_UPDATE );
|
|
|
|
if ( is_null( $image ) || !$count ) {
|
|
|
|
$dbw->delete( 'page_images',
|
|
|
|
array( 'pi_page' => $pageId ),
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$dbw->replace( 'page_images', 'pi_page', array(
|
|
|
|
'pi_page' => $pageId,
|
|
|
|
'pi_thumbnail' => $image,
|
|
|
|
'pi_images' => $count,
|
|
|
|
'pi_total_score' => $totalScore,
|
|
|
|
'pi_version' => self::VERSION,
|
|
|
|
),
|
|
|
|
__METHOD__
|
|
|
|
);
|
2012-03-01 12:25:57 +00:00
|
|
|
}
|
2012-03-02 16:45:07 +00:00
|
|
|
|
2012-02-29 10:50:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-01 12:25:57 +00:00
|
|
|
|
2012-05-08 21:42:07 +00:00
|
|
|
/**
|
|
|
|
* ArticleDeleteComplete hook handler
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ArticleDeleteComplete
|
|
|
|
*
|
|
|
|
* @param WikiPage $article
|
|
|
|
* @param User $user
|
|
|
|
* @param string $reason
|
|
|
|
* @param int $id
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function onArticleDeleteComplete( &$article, User &$user, $reason, $id ) {
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
$dbw->delete( 'page_images', array( 'pi_page' => $id ), __METHOD__ );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-08 14:01:00 +00:00
|
|
|
public static function onOpenSearchXml( &$results ) {
|
|
|
|
global $wgPageImagesExpandOpenSearchXml;
|
|
|
|
if ( !$wgPageImagesExpandOpenSearchXml || !count( $results ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-05-08 21:42:07 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2012-03-08 14:01:00 +00:00
|
|
|
$pageIds = array_keys( $results );
|
|
|
|
$api = new ApiMain(
|
|
|
|
new FauxRequest( array(
|
|
|
|
'action' => 'query',
|
|
|
|
'prop' => 'pageimages',
|
|
|
|
'piprop' => 'thumbnail',
|
|
|
|
'pageids' => implode( '|', $pageIds ),
|
2012-03-09 09:49:02 +00:00
|
|
|
'pilimit' => count( $results ),
|
2012-03-08 14:01:00 +00:00
|
|
|
) )
|
|
|
|
);
|
|
|
|
$api->execute();
|
|
|
|
$data = $api->getResultData();
|
|
|
|
foreach ( $pageIds as $id ) {
|
|
|
|
if ( isset( $data['query']['pages'][$id]['thumb'] ) ) {
|
|
|
|
$results[$id]['image'] = $data['query']['pages'][$id]['thumb'];
|
|
|
|
} else {
|
|
|
|
$results[$id]['image'] = null;
|
|
|
|
}
|
|
|
|
}
|
2012-05-08 21:42:07 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LoadExtensionSchemaUpdates hook handler
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/LoadExtensionSchemaUpdates
|
|
|
|
* @param \DatabaseUpdater $updater
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function onLoadExtensionSchemaUpdates( DatabaseUpdater $updater ) {
|
|
|
|
$updater->addExtensionTable( 'page_images', dirname( __FILE__ ) . '/PageImages.sql' );
|
2012-03-08 14:01:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-01 14:33:28 +00:00
|
|
|
/**
|
|
|
|
* Returns score for image, the more the better, if it is less than zero,
|
|
|
|
* the image shouldn't be used for anything
|
|
|
|
* @param array $image: Associative array describing an image
|
|
|
|
* @param int $position: Image order on page
|
|
|
|
* @return int
|
|
|
|
*/
|
2012-03-01 12:25:57 +00:00
|
|
|
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];
|
|
|
|
}
|
2012-03-02 16:42:17 +00:00
|
|
|
$blacklist = self::getBlacklist();
|
|
|
|
if ( isset( $blacklist[$image['filename']] ) ) {
|
|
|
|
$score -= 100;
|
|
|
|
}
|
2012-03-01 12:25:57 +00:00
|
|
|
return $score;
|
|
|
|
}
|
2012-03-02 16:42:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of images blacklisted from influencing this extension's output
|
|
|
|
* @return array: Flipped associative array in format "image BDB key" => int
|
|
|
|
*/
|
|
|
|
private static function getBlacklist() {
|
|
|
|
global $wgPageImagesBlacklist, $wgPageImagesBlacklistExpiry, $wgMemc;
|
|
|
|
static $list = false;
|
|
|
|
if ( $list !== false ) {
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
wfProfileIn( __METHOD__ );
|
|
|
|
$key = wfMemcKey( 'pageimages', 'blacklist' );
|
|
|
|
$list = $wgMemc->get( $key );
|
|
|
|
if ( $list !== false ) {
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
wfDebug( __METHOD__ . "(): cache miss\n" );
|
|
|
|
$list = array();
|
|
|
|
foreach ( $wgPageImagesBlacklist as $source ) {
|
|
|
|
switch ( $source['type'] ) {
|
|
|
|
case 'db':
|
|
|
|
$list = array_merge( $list, self::getDbBlacklist( $source['db'], $source['page'] ) );
|
|
|
|
break;
|
|
|
|
case 'url':
|
|
|
|
$list = array_merge( $list, self::getUrlBlacklist( $source['url'] ) );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new MWException( __METHOD__ . "(): unrecognized image blacklist type '{$source['type']}'" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$list = array_flip( $list );
|
|
|
|
$wgMemc->set( $key, $list, $wgPageImagesBlacklistExpiry );
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns list of images linked by the given blacklist page
|
|
|
|
* @param string|int $dbName: Database name or false for current database
|
|
|
|
* @param string $page
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private static function getDbBlacklist( $dbName, $page ) {
|
|
|
|
wfProfileIn( __METHOD__ );
|
|
|
|
$dbr = wfGetDB( DB_SLAVE, array(), $dbName );
|
|
|
|
$title = Title::newFromText( $page );
|
|
|
|
$list = array();
|
|
|
|
$id = $dbr->selectField( 'page',
|
|
|
|
'page_id',
|
|
|
|
array( 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDBkey() ),
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
if ( $id ) {
|
|
|
|
$res = $dbr->select( 'pagelinks',
|
|
|
|
'pl_title',
|
|
|
|
array( 'pl_from' => $id, 'pl_namespace' => NS_FILE ),
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$list[] = $row->pl_title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns list of images on given remote blacklist page.
|
|
|
|
* Not quite 100% bulletproof due to localised namespaces and so on.
|
|
|
|
* Though if you beat people if they add bad entries to the list... :)
|
|
|
|
* @param string $url
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private static function getUrlBlacklist( $url ) {
|
|
|
|
global $wgFileExtensions;
|
|
|
|
wfProfileIn( __METHOD__ );
|
|
|
|
$list = array();
|
|
|
|
$text = Http::get( $url, 3 );
|
|
|
|
$regex = '/\[\[:([^|\#]*?\.(?:' . implode( '|', $wgFileExtensions ) . '))/i';
|
|
|
|
if ( $text && preg_match_all( $regex, $text, $matches ) ) {
|
|
|
|
foreach ( $matches[1] as $s ) {
|
|
|
|
$t = Title::makeTitleSafe( NS_FILE, $s );
|
|
|
|
if ( $t ) {
|
|
|
|
$list[] = $t->getDBkey();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return $list;
|
|
|
|
}
|
2012-02-29 10:50:36 +00:00
|
|
|
}
|