2012-02-29 10:50:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class PageImages {
|
2013-11-04 22:00:38 +00:00
|
|
|
/**
|
|
|
|
* Page property used to store the page image information
|
|
|
|
*/
|
|
|
|
const PROP_NAME = 'page_image';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns page image for a given title
|
|
|
|
*
|
|
|
|
* @param Title $title: Title to get page image for
|
|
|
|
*
|
2014-06-18 19:58:12 +00:00
|
|
|
* @return File|bool
|
2013-11-04 22:00:38 +00:00
|
|
|
*/
|
|
|
|
public static function getPageImage( Title $title ) {
|
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
|
|
|
$name = $dbr->selectField( 'page_props',
|
|
|
|
'pp_value',
|
|
|
|
array( 'pp_page' => $title->getArticleID(), 'pp_propname' => self::PROP_NAME ),
|
|
|
|
__METHOD__
|
|
|
|
);
|
2014-06-18 19:58:12 +00:00
|
|
|
$file = false;
|
2013-11-04 22:00:38 +00:00
|
|
|
if ( $name ) {
|
|
|
|
$file = wfFindFile( $name );
|
|
|
|
}
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
2012-05-08 21:42:07 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
/**
|
2014-02-20 08:30:35 +00:00
|
|
|
* ParserMakeImageParams hook handler, saves extended information about images used on page
|
2015-10-26 10:41:13 +00:00
|
|
|
*
|
2012-03-01 14:33:28 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserMakeImageParams
|
2015-10-26 10:41:13 +00:00
|
|
|
*
|
2012-03-01 13:29:30 +00:00
|
|
|
* @param Title $title
|
|
|
|
* @param File|bool $file
|
2015-10-26 10:41:13 +00:00
|
|
|
* @param array &$params
|
2012-03-01 13:29:30 +00:00
|
|
|
* @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 ) {
|
2014-06-18 00:23:21 +00:00
|
|
|
self::processFile( $parser, $file, $params );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AfterParserFetchFileAndTitle hook handler, saves information about gallery images
|
2015-10-26 10:41:13 +00:00
|
|
|
*
|
2014-06-18 00:23:21 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param ImageGalleryBase $ig
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function onAfterParserFetchFileAndTitle( Parser $parser, ImageGalleryBase $ig ) {
|
|
|
|
foreach ( $ig->getImages() as $image ) {
|
|
|
|
self::processFile( $parser, $image[0], null );
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Parser $parser
|
|
|
|
* @param File|Title|null $file
|
|
|
|
* @param array|null $handlerParams
|
|
|
|
*/
|
|
|
|
private static function processFile( Parser $parser, $file, $handlerParams ) {
|
2012-05-08 21:42:07 +00:00
|
|
|
if ( !$file || !self::processThisTitle( $parser->getTitle() ) ) {
|
2014-06-18 00:23:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-10-26 10:41:13 +00:00
|
|
|
|
2014-06-18 00:23:21 +00:00
|
|
|
if ( !$file instanceof File ) {
|
|
|
|
$file = wfFindFile( $file );
|
|
|
|
if ( !$file ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-03-01 13:29:30 +00:00
|
|
|
}
|
2015-10-26 10:41:13 +00:00
|
|
|
|
2014-06-18 00:23:21 +00:00
|
|
|
if ( is_array( $handlerParams ) ) {
|
|
|
|
$myParams = $handlerParams;
|
|
|
|
self::calcWidth( $myParams, $file );
|
|
|
|
} else {
|
|
|
|
$myParams = array();
|
|
|
|
}
|
2014-04-18 00:27:00 +00:00
|
|
|
|
2014-06-18 00:23:21 +00:00
|
|
|
$myParams['filename'] = $file->getTitle()->getDBkey();
|
2014-02-11 19:59:04 +00:00
|
|
|
$myParams['fullwidth'] = $file->getWidth();
|
|
|
|
$myParams['fullheight'] = $file->getHeight();
|
2015-10-26 11:05:47 +00:00
|
|
|
|
|
|
|
$out = $parser->getOutput();
|
|
|
|
$pageImages = $out->getExtensionData( 'pageImages' ) ?: array();
|
|
|
|
$pageImages[] = $myParams;
|
|
|
|
$out->setExtensionData( 'pageImages', $pageImages );
|
2012-02-29 10:50:36 +00:00
|
|
|
}
|
|
|
|
|
2014-04-18 00:27:00 +00:00
|
|
|
/**
|
|
|
|
* Estimates image size as displayed if not explicitly provided.
|
|
|
|
* We don't follow the core size calculation algorithm precisely because it's not required and editor's
|
|
|
|
* intentions are more important than the precise number.
|
|
|
|
*
|
2015-10-26 10:41:13 +00:00
|
|
|
* @param array &$params
|
2014-04-18 00:27:00 +00:00
|
|
|
* @param File $file
|
|
|
|
*/
|
|
|
|
private static function calcWidth( array &$params, File $file ) {
|
2014-05-01 23:47:03 +00:00
|
|
|
global $wgThumbLimits, $wgDefaultUserOptions;
|
|
|
|
|
2014-04-18 00:27:00 +00:00
|
|
|
if ( isset( $params['handler']['width'] ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( isset( $params['handler']['height'] ) && $file->getHeight() > 0 ) {
|
|
|
|
$params['handler']['width'] =
|
|
|
|
$file->getWidth() * ( $params['handler']['height'] / $file->getHeight() );
|
|
|
|
} elseif ( isset( $params['frame']['thumbnail'] )
|
|
|
|
|| isset( $params['frame']['thumb'] )
|
|
|
|
|| isset( $params['frame']['frameless'] ) )
|
|
|
|
{
|
2014-06-24 21:05:19 +00:00
|
|
|
$params['handler']['width'] = isset( $wgThumbLimits[$wgDefaultUserOptions['thumbsize']] )
|
|
|
|
? $wgThumbLimits[$wgDefaultUserOptions['thumbsize']]
|
|
|
|
: 250;
|
2014-04-18 00:27:00 +00:00
|
|
|
} else {
|
|
|
|
$params['handler']['width'] = $file->getWidth();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-01 14:33:28 +00:00
|
|
|
/**
|
2012-12-17 19:22:13 +00:00
|
|
|
* LinksUpdate hook handler, sets at most 2 page properties depending on images on page
|
2015-10-26 10:41:13 +00:00
|
|
|
*
|
2012-03-01 14:33:28 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/LinksUpdate
|
2015-10-26 10:41:13 +00:00
|
|
|
*
|
|
|
|
* @param LinksUpdate $linksUpdate
|
2012-03-01 14:33:28 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2015-10-26 10:41:13 +00:00
|
|
|
public static function onLinksUpdate( LinksUpdate $linksUpdate ) {
|
2015-10-26 11:05:47 +00:00
|
|
|
$images = $linksUpdate->getParserOutput()->getExtensionData( 'pageImages' );
|
|
|
|
if ( $images === null ) {
|
2012-02-29 10:50:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-10-26 10:41:13 +00:00
|
|
|
|
2012-03-01 12:25:57 +00:00
|
|
|
$scores = 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
|
|
|
if ( !isset( $scores[$fileName] ) ) {
|
|
|
|
$scores[$fileName] = -1;
|
|
|
|
}
|
|
|
|
$scores[$fileName] = max( $scores[$fileName], self::getScore( $image, $counter++ ) );
|
|
|
|
}
|
2015-10-26 10:41:13 +00:00
|
|
|
|
2012-12-17 19:22:13 +00:00
|
|
|
$image = false;
|
2012-03-01 12:25:57 +00:00
|
|
|
foreach ( $scores as $name => $score ) {
|
2012-12-17 19:22:13 +00:00
|
|
|
if ( $score > 0 && ( !$image || $score > $scores[$image] ) ) {
|
|
|
|
$image = $name;
|
2012-03-01 12:25:57 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-17 19:22:13 +00:00
|
|
|
if ( $image ) {
|
2015-10-26 10:41:13 +00:00
|
|
|
$linksUpdate->mProperties[self::PROP_NAME] = $image;
|
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
|
|
|
|
2014-05-02 06:29:28 +00:00
|
|
|
/**
|
|
|
|
* InfoAction hook handler, adds the page image to the info=action page
|
2015-10-26 10:41:13 +00:00
|
|
|
*
|
2014-05-02 06:29:28 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/InfoAction
|
2015-10-26 10:41:13 +00:00
|
|
|
*
|
2014-05-02 06:29:28 +00:00
|
|
|
* @param IContextSource $context
|
2015-10-26 10:41:13 +00:00
|
|
|
* @param array[] &$pageInfo
|
2014-05-02 06:29:28 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function onInfoAction( IContextSource $context, &$pageInfo ) {
|
|
|
|
global $wgDefaultUserOptions, $wgThumbLimits;
|
|
|
|
|
|
|
|
$imageFile = self::getPageImage( $context->getTitle() );
|
2014-06-18 19:58:12 +00:00
|
|
|
if ( !$imageFile ) {
|
2014-05-02 06:29:28 +00:00
|
|
|
// The page has no image
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$thumbSetting = $context->getUser()->getOption(
|
|
|
|
'thumbsize',
|
|
|
|
$wgDefaultUserOptions['thumbsize']
|
|
|
|
);
|
|
|
|
$thumbSize = $wgThumbLimits[$thumbSetting];
|
|
|
|
|
2014-06-18 19:58:12 +00:00
|
|
|
$thumb = $imageFile->transform( array( 'width' => $thumbSize ) );
|
|
|
|
if ( !$thumb ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$imageHtml = $thumb->toHtml(
|
2014-05-02 06:29:28 +00:00
|
|
|
array(
|
|
|
|
'alt' => $imageFile->getTitle()->getText(),
|
|
|
|
'desc-link' => true,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$pageInfo['header-basic'][] = array(
|
|
|
|
$context->msg( 'pageimages-info-label' ),
|
|
|
|
$imageHtml
|
|
|
|
);
|
2015-01-20 23:37:08 +00:00
|
|
|
|
2014-05-02 06:29:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-08 21:42:07 +00:00
|
|
|
/**
|
2014-11-05 22:18:30 +00:00
|
|
|
* ApiOpenSearchSuggest hook handler, enhances ApiOpenSearch results with this extension's data
|
2015-10-26 10:41:13 +00:00
|
|
|
*
|
|
|
|
* @param array[] &$results
|
2012-05-08 21:42:07 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-11-05 22:18:30 +00:00
|
|
|
public static function onApiOpenSearchSuggest( &$results ) {
|
2012-03-08 14:01:00 +00:00
|
|
|
global $wgPageImagesExpandOpenSearchXml;
|
2015-01-20 23:37:08 +00:00
|
|
|
|
2012-03-08 14:01:00 +00:00
|
|
|
if ( !$wgPageImagesExpandOpenSearchXml || !count( $results ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-10-26 10:41:13 +00:00
|
|
|
|
2012-03-08 14:01:00 +00:00
|
|
|
$pageIds = array_keys( $results );
|
2014-09-19 18:41:48 +00:00
|
|
|
$data = self::getImages( $pageIds, 50 );
|
2012-03-08 14:01:00 +00:00
|
|
|
foreach ( $pageIds as $id ) {
|
2014-09-19 18:41:48 +00:00
|
|
|
if ( isset( $data[$id]['thumbnail'] ) ) {
|
|
|
|
$results[$id]['image'] = $data[$id]['thumbnail'];
|
2012-03-08 14:01:00 +00:00
|
|
|
} else {
|
|
|
|
$results[$id]['image'] = null;
|
|
|
|
}
|
|
|
|
}
|
2015-01-20 23:37:08 +00:00
|
|
|
|
2012-05-08 21:42:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-19 18:41:48 +00:00
|
|
|
/**
|
|
|
|
* SpecialMobileEditWatchlist::images hook handler, adds images to mobile watchlist A-Z view
|
|
|
|
*
|
|
|
|
* @param IContextSource $context
|
2015-10-26 10:41:13 +00:00
|
|
|
* @param array[] $watchlist
|
|
|
|
* @param array[] &$images
|
2014-09-19 18:41:48 +00:00
|
|
|
* @return true
|
|
|
|
*/
|
|
|
|
public static function onSpecialMobileEditWatchlist_images( IContextSource $context, $watchlist,
|
|
|
|
&$images
|
|
|
|
) {
|
|
|
|
$ids = array();
|
|
|
|
foreach ( $watchlist as $ns => $pages ) {
|
|
|
|
foreach ( array_keys( $pages ) as $dbKey ) {
|
|
|
|
$title = Title::makeTitle( $ns, $dbKey );
|
|
|
|
// Getting page ID here is safe because SpecialEditWatchlist::getWatchlistInfo()
|
|
|
|
// uses LinkBatch
|
|
|
|
$id = $title->getArticleID();
|
|
|
|
if ( $id ) {
|
|
|
|
$ids[$id] = $dbKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-26 10:41:13 +00:00
|
|
|
|
2014-09-19 18:41:48 +00:00
|
|
|
$data = self::getImages( array_keys( $ids ) );
|
|
|
|
foreach ( $data as $id => $page ) {
|
|
|
|
if ( isset( $page['pageimage'] ) ) {
|
|
|
|
$images[ $page['ns'] ][ $ids[$id] ] = $page['pageimage'];
|
|
|
|
}
|
|
|
|
}
|
2015-10-26 10:41:13 +00:00
|
|
|
|
2014-09-19 18:41:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns image information for pages with given ids
|
|
|
|
*
|
2015-10-26 10:41:13 +00:00
|
|
|
* @param int[] $pageIds
|
|
|
|
* @param int $size
|
|
|
|
* @return array[]
|
2014-09-19 18:41:48 +00:00
|
|
|
*/
|
|
|
|
private static function getImages( array $pageIds, $size = 0 ) {
|
|
|
|
$request = array(
|
|
|
|
'action' => 'query',
|
|
|
|
'prop' => 'pageimages',
|
|
|
|
'piprop' => 'name',
|
|
|
|
'pageids' => implode( '|', $pageIds ),
|
|
|
|
'pilimit' => 'max',
|
|
|
|
);
|
|
|
|
if ( $size ) {
|
|
|
|
$request['piprop'] = 'thumbnail';
|
|
|
|
$request['pithumbsize'] = $size;
|
|
|
|
}
|
|
|
|
$api = new ApiMain( new FauxRequest( $request ) );
|
|
|
|
$api->execute();
|
2014-12-30 22:20:55 +00:00
|
|
|
if ( defined( 'ApiResult::META_CONTENT' ) ) {
|
2015-04-20 18:41:29 +00:00
|
|
|
return (array)$api->getResult()->getResultData( array( 'query', 'pages' ),
|
|
|
|
array( 'Strip' => 'base' ) );
|
2014-12-30 22:20:55 +00:00
|
|
|
} else {
|
|
|
|
$data = $api->getResultData();
|
|
|
|
if ( isset( $data['query']['pages'] ) ) {
|
|
|
|
return $data['query']['pages'];
|
|
|
|
}
|
|
|
|
return array();
|
2014-09-19 18:41:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2015-10-26 10:41:13 +00:00
|
|
|
*
|
2012-03-01 14:33:28 +00:00
|
|
|
* @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;
|
|
|
|
|
2014-06-18 00:23:21 +00:00
|
|
|
if ( isset( $image['handler'] ) ) {
|
|
|
|
// Standalone image
|
|
|
|
$score = self::scoreFromTable( $image['handler']['width'], $wgPageImagesScores['width'] );
|
|
|
|
} else {
|
|
|
|
// From gallery
|
|
|
|
$score = self::scoreFromTable( $image['fullwidth'], $wgPageImagesScores['galleryImageWidth'] );
|
|
|
|
}
|
2014-02-11 19:59:04 +00:00
|
|
|
|
2012-03-01 12:25:57 +00:00
|
|
|
if ( isset( $wgPageImagesScores['position'][$position] ) ) {
|
|
|
|
$score += $wgPageImagesScores['position'][$position];
|
|
|
|
}
|
2014-02-11 19:59:04 +00:00
|
|
|
|
|
|
|
$ratio = intval( self::getRatio( $image ) * 10 );
|
|
|
|
$score += self::scoreFromTable( $ratio, $wgPageImagesScores['ratio'] );
|
|
|
|
|
2012-03-02 16:42:17 +00:00
|
|
|
$blacklist = self::getBlacklist();
|
|
|
|
if ( isset( $blacklist[$image['filename']] ) ) {
|
2014-02-11 19:59:04 +00:00
|
|
|
$score = -1000;
|
2012-03-02 16:42:17 +00:00
|
|
|
}
|
2015-10-26 10:41:13 +00:00
|
|
|
|
2012-03-01 12:25:57 +00:00
|
|
|
return $score;
|
|
|
|
}
|
2012-03-02 16:42:17 +00:00
|
|
|
|
2014-02-11 19:59:04 +00:00
|
|
|
/**
|
|
|
|
* Returns width/height ratio of an image as displayed or 0 is not available
|
|
|
|
*
|
|
|
|
* @param array $image
|
2015-10-26 10:41:13 +00:00
|
|
|
* @return float
|
2014-02-11 19:59:04 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
*
|
2015-10-26 10:41:13 +00:00
|
|
|
* @param int $value
|
|
|
|
* @param int[] $scores
|
2014-02-11 19:59:04 +00:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2012-03-02 16:42:17 +00:00
|
|
|
/**
|
|
|
|
* Returns a list of images blacklisted from influencing this extension's output
|
2015-10-26 10:41:13 +00:00
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
* @return int[] Flipped associative array in format "image BDB key" => int
|
2012-03-02 16:42:17 +00:00
|
|
|
*/
|
|
|
|
private static function getBlacklist() {
|
|
|
|
global $wgPageImagesBlacklist, $wgPageImagesBlacklistExpiry, $wgMemc;
|
2015-01-20 23:37:08 +00:00
|
|
|
|
2012-03-02 16:42:17 +00:00
|
|
|
static $list = false;
|
|
|
|
if ( $list !== false ) {
|
|
|
|
return $list;
|
|
|
|
}
|
2015-01-20 23:37:08 +00:00
|
|
|
|
2012-03-02 16:42:17 +00:00
|
|
|
$key = wfMemcKey( 'pageimages', 'blacklist' );
|
|
|
|
$list = $wgMemc->get( $key );
|
|
|
|
if ( $list !== false ) {
|
|
|
|
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:
|
2015-01-10 01:57:41 +00:00
|
|
|
throw new Exception( __METHOD__ . "(): unrecognized image blacklist type '{$source['type']}'" );
|
2012-03-02 16:42:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$list = array_flip( $list );
|
|
|
|
$wgMemc->set( $key, $list, $wgPageImagesBlacklistExpiry );
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns list of images linked by the given blacklist page
|
2015-10-26 10:41:13 +00:00
|
|
|
*
|
|
|
|
* @param string|bool $dbName Database name or false for current database
|
2012-03-02 16:42:17 +00:00
|
|
|
* @param string $page
|
2015-10-26 10:41:13 +00:00
|
|
|
* @return string[]
|
2012-03-02 16:42:17 +00:00
|
|
|
*/
|
|
|
|
private static function getDbBlacklist( $dbName, $page ) {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
2015-01-20 23:37:08 +00:00
|
|
|
|
2012-03-02 16:42:17 +00:00
|
|
|
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... :)
|
2015-10-26 10:41:13 +00:00
|
|
|
*
|
2012-03-02 16:42:17 +00:00
|
|
|
* @param string $url
|
2015-10-26 10:41:13 +00:00
|
|
|
* @return string[]
|
2012-03-02 16:42:17 +00:00
|
|
|
*/
|
|
|
|
private static function getUrlBlacklist( $url ) {
|
|
|
|
global $wgFileExtensions;
|
2015-01-20 23:37:08 +00:00
|
|
|
|
2012-03-02 16:42:17 +00:00
|
|
|
$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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-20 23:37:08 +00:00
|
|
|
|
2012-03-02 16:42:17 +00:00
|
|
|
return $list;
|
|
|
|
}
|
2012-02-29 10:50:36 +00:00
|
|
|
}
|