2015-12-30 12:41:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PageImages\Hooks;
|
|
|
|
|
2021-12-02 00:18:50 +00:00
|
|
|
use DerivativeContext;
|
|
|
|
use Exception;
|
2015-12-30 12:41:37 +00:00
|
|
|
use File;
|
2021-12-02 00:18:50 +00:00
|
|
|
use FormatMetadata;
|
2024-04-10 18:42:02 +00:00
|
|
|
use MediaWiki\Config\Config;
|
2022-06-15 16:08:43 +00:00
|
|
|
use MediaWiki\Hook\ParserAfterTidyHook;
|
2023-04-22 17:54:54 +00:00
|
|
|
use MediaWiki\Hook\ParserModifyImageHTMLHook;
|
2022-06-15 16:08:43 +00:00
|
|
|
use MediaWiki\Hook\ParserTestGlobalsHook;
|
2022-12-11 13:00:11 +00:00
|
|
|
use MediaWiki\Http\HttpRequestFactory;
|
2023-11-01 17:51:38 +00:00
|
|
|
use MediaWiki\Linker\LinksMigration;
|
2024-04-10 18:42:02 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2022-12-28 14:06:01 +00:00
|
|
|
use MediaWiki\Page\PageReference;
|
2024-01-05 21:37:40 +00:00
|
|
|
use MediaWiki\Parser\ParserOutput;
|
|
|
|
use MediaWiki\Title\TitleFactory;
|
2020-10-22 20:04:23 +00:00
|
|
|
use PageImages\PageImageCandidate;
|
2021-12-02 00:18:50 +00:00
|
|
|
use PageImages\PageImages;
|
2015-12-30 12:41:37 +00:00
|
|
|
use Parser;
|
2022-09-02 22:15:57 +00:00
|
|
|
use RepoGroup;
|
2021-12-02 00:18:50 +00:00
|
|
|
use RuntimeException;
|
2022-09-02 22:15:57 +00:00
|
|
|
use WANObjectCache;
|
2023-06-21 19:54:03 +00:00
|
|
|
use Wikimedia\Rdbms\IConnectionProvider;
|
2015-12-30 12:41:37 +00:00
|
|
|
|
|
|
|
/**
|
2021-12-02 00:18:50 +00:00
|
|
|
* Handlers for parser hooks.
|
|
|
|
*
|
|
|
|
* The ParserModifyImageHTML hook handler collects candidate images, and marks
|
|
|
|
* them with a temporary HTML comment in the parser output.
|
|
|
|
*
|
|
|
|
* The ParserAfterTidy hook handler processes the candidate images, identifying
|
|
|
|
* the best image and the best free image. If $wgPageImagesLeadSectionOnly is
|
|
|
|
* set, images following the first section header are discarded. It removes the
|
|
|
|
* temporary comments and saves the resulting best images to page_props.
|
|
|
|
*
|
|
|
|
* The various query interfaces will retrieve the lead image from page_props.
|
2015-12-30 12:41:37 +00:00
|
|
|
*
|
2018-05-25 04:42:29 +00:00
|
|
|
* @license WTFPL
|
2015-12-30 12:41:37 +00:00
|
|
|
* @author Max Semenik
|
2017-11-24 07:33:49 +00:00
|
|
|
* @author Thiemo Kreuz
|
2015-12-30 12:41:37 +00:00
|
|
|
*/
|
2022-06-15 16:08:43 +00:00
|
|
|
class ParserFileProcessingHookHandlers implements
|
|
|
|
ParserAfterTidyHook,
|
2023-04-22 17:54:54 +00:00
|
|
|
ParserModifyImageHTMLHook,
|
2022-06-15 16:08:43 +00:00
|
|
|
ParserTestGlobalsHook
|
|
|
|
{
|
2022-01-11 04:23:36 +00:00
|
|
|
private const CANDIDATE_REGEX = '/<!--MW-PAGEIMAGES-CANDIDATE-([0-9]+)-->/';
|
|
|
|
|
2024-04-10 18:42:02 +00:00
|
|
|
protected Config $config;
|
2024-02-08 11:09:56 +00:00
|
|
|
private RepoGroup $repoGroup;
|
|
|
|
private WANObjectCache $mainWANObjectCache;
|
|
|
|
private HttpRequestFactory $httpRequestFactory;
|
|
|
|
private IConnectionProvider $connectionProvider;
|
|
|
|
private TitleFactory $titleFactory;
|
2023-11-01 17:51:38 +00:00
|
|
|
private LinksMigration $linksMigration;
|
2023-06-21 21:03:44 +00:00
|
|
|
|
2022-09-02 22:15:57 +00:00
|
|
|
public function __construct(
|
2024-04-10 18:42:02 +00:00
|
|
|
Config $config,
|
2022-09-02 22:15:57 +00:00
|
|
|
RepoGroup $repoGroup,
|
2022-12-11 13:00:11 +00:00
|
|
|
WANObjectCache $mainWANObjectCache,
|
2023-06-21 19:54:03 +00:00
|
|
|
HttpRequestFactory $httpRequestFactory,
|
2023-06-21 21:03:44 +00:00
|
|
|
IConnectionProvider $connectionProvider,
|
2023-11-01 17:51:38 +00:00
|
|
|
TitleFactory $titleFactory,
|
|
|
|
LinksMigration $linksMigration
|
2022-09-02 22:15:57 +00:00
|
|
|
) {
|
2024-04-10 18:42:02 +00:00
|
|
|
$this->config = $config;
|
2022-09-02 22:15:57 +00:00
|
|
|
$this->repoGroup = $repoGroup;
|
|
|
|
$this->mainWANObjectCache = $mainWANObjectCache;
|
2022-12-11 13:00:11 +00:00
|
|
|
$this->httpRequestFactory = $httpRequestFactory;
|
2023-06-21 19:54:03 +00:00
|
|
|
$this->connectionProvider = $connectionProvider;
|
2023-06-21 21:03:44 +00:00
|
|
|
$this->titleFactory = $titleFactory;
|
2023-11-01 17:51:38 +00:00
|
|
|
$this->linksMigration = $linksMigration;
|
2015-12-30 12:41:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 00:59:49 +00:00
|
|
|
/**
|
|
|
|
* @param array &$globals
|
|
|
|
*/
|
2022-06-15 16:08:43 +00:00
|
|
|
public function onParserTestGlobals( &$globals ) {
|
2022-01-24 00:59:49 +00:00
|
|
|
$globals += [
|
|
|
|
'wgPageImagesScores' => [
|
|
|
|
'width' => [
|
|
|
|
200 => 10,
|
|
|
|
1000 => 20
|
|
|
|
],
|
|
|
|
'position' => [],
|
|
|
|
'ratio' => [],
|
|
|
|
'galleryImageWidth' => []
|
|
|
|
],
|
|
|
|
'wgPageImagesLeadSectionOnly' => true
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-12-30 12:41:37 +00:00
|
|
|
/**
|
2022-09-02 22:15:57 +00:00
|
|
|
* ParserModifyImageHTML hook. Save candidate images, and mark them with a
|
|
|
|
* comment so that we can later tell if they were in the lead section.
|
|
|
|
*
|
2021-12-02 00:18:50 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param File $file
|
|
|
|
* @param array $params
|
|
|
|
* @param string &$html
|
2015-12-30 12:41:37 +00:00
|
|
|
*/
|
2022-09-02 22:15:57 +00:00
|
|
|
public function onParserModifyImageHTML(
|
2021-12-02 00:18:50 +00:00
|
|
|
Parser $parser,
|
|
|
|
File $file,
|
|
|
|
array $params,
|
2022-09-02 21:52:59 +00:00
|
|
|
string &$html
|
2022-09-02 22:15:57 +00:00
|
|
|
): void {
|
2022-12-28 14:06:01 +00:00
|
|
|
$page = $parser->getPage();
|
|
|
|
if ( !$page || !$this->processThisTitle( $page ) ) {
|
2015-12-30 12:41:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-02 21:52:59 +00:00
|
|
|
$this->calcWidth( $params, $file );
|
2015-12-30 12:41:37 +00:00
|
|
|
|
2021-12-02 00:18:50 +00:00
|
|
|
$index = $this->addPageImageCandidateToParserOutput(
|
2022-09-02 21:52:59 +00:00
|
|
|
PageImageCandidate::newFromFileAndParams( $file, $params ),
|
2020-10-22 20:04:23 +00:00
|
|
|
$parser->getOutput()
|
|
|
|
);
|
2021-12-02 00:18:50 +00:00
|
|
|
$html .= "<!--MW-PAGEIMAGES-CANDIDATE-$index-->";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-02 22:15:57 +00:00
|
|
|
* ParserAfterTidy hook handler. Remove candidate images which were not in
|
|
|
|
* the lead section.
|
|
|
|
*
|
2021-12-02 00:18:50 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
* @param string &$text
|
|
|
|
*/
|
2022-09-02 22:15:57 +00:00
|
|
|
public function onParserAfterTidy( $parser, &$text ) {
|
2021-12-02 00:18:50 +00:00
|
|
|
$parserOutput = $parser->getOutput();
|
|
|
|
$allImages = $parserOutput->getExtensionData( 'pageImages' );
|
|
|
|
if ( !$allImages ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-03 05:27:16 +00:00
|
|
|
// Find and remove our special comments
|
2021-12-02 00:18:50 +00:00
|
|
|
$images = [];
|
2024-04-10 18:42:02 +00:00
|
|
|
if ( $this->config->get( 'PageImagesLeadSectionOnly' ) ) {
|
2021-12-03 05:27:16 +00:00
|
|
|
$leadEndPos = strpos( $text, '<mw:editsection' );
|
2021-12-02 00:18:50 +00:00
|
|
|
} else {
|
2021-12-03 05:27:16 +00:00
|
|
|
$leadEndPos = false;
|
2021-12-02 00:18:50 +00:00
|
|
|
}
|
2021-12-03 05:27:16 +00:00
|
|
|
$text = preg_replace_callback(
|
|
|
|
self::CANDIDATE_REGEX,
|
|
|
|
static function ( $m ) use ( $allImages, &$images, $leadEndPos ) {
|
|
|
|
$offset = $m[0][1];
|
|
|
|
$id = intval( $m[1][0] );
|
|
|
|
$inLead = $leadEndPos === false || $offset < $leadEndPos;
|
|
|
|
if ( $inLead && isset( $allImages[$id] ) ) {
|
|
|
|
$images[] = PageImageCandidate::newFromArray( $allImages[$id] );
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
},
|
|
|
|
$text, -1, $count, PREG_OFFSET_CAPTURE
|
|
|
|
);
|
2021-12-02 00:18:50 +00:00
|
|
|
|
2023-11-01 17:51:38 +00:00
|
|
|
[ $bestImageName, $freeImageName ] = $this->findBestImages( $images );
|
2021-12-02 00:18:50 +00:00
|
|
|
|
|
|
|
if ( $freeImageName ) {
|
|
|
|
$parserOutput->setPageProperty( PageImages::getPropName( true ), $freeImageName );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only store the image if it's not free. Free image (if any) has already been stored above.
|
|
|
|
if ( $bestImageName && $bestImageName !== $freeImageName ) {
|
|
|
|
$parserOutput->setPageProperty( PageImages::getPropName( false ), $bestImageName );
|
|
|
|
}
|
2022-01-11 04:23:36 +00:00
|
|
|
|
|
|
|
// Strip comments from indicators (T298930)
|
|
|
|
foreach ( $parserOutput->getIndicators() as $id => $value ) {
|
|
|
|
$stripped = preg_replace( self::CANDIDATE_REGEX, '', $value );
|
|
|
|
if ( $stripped !== $value ) {
|
|
|
|
$parserOutput->setIndicator( $id, $stripped );
|
|
|
|
}
|
|
|
|
}
|
2024-02-20 23:10:56 +00:00
|
|
|
// We may have comments in TOC data - Parser::cleanupTocLine strips them for us.
|
2021-12-02 00:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the best images out of an array of candidates
|
|
|
|
*
|
|
|
|
* @param PageImageCandidate[] $images
|
2024-02-08 11:09:56 +00:00
|
|
|
* @return array{string|false,string|false} The best image, and the best free image
|
2021-12-02 00:18:50 +00:00
|
|
|
*/
|
|
|
|
private function findBestImages( array $images ) {
|
2024-02-08 11:12:03 +00:00
|
|
|
if ( !$images ) {
|
2021-12-02 00:18:50 +00:00
|
|
|
return [ false, false ];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the image scores
|
|
|
|
|
|
|
|
$scores = [];
|
|
|
|
$counter = 0;
|
|
|
|
|
|
|
|
foreach ( $images as $image ) {
|
2024-02-08 11:12:03 +00:00
|
|
|
$score = $this->getScore( $image, $counter++ );
|
2021-12-02 00:18:50 +00:00
|
|
|
$fileName = $image->getFileName();
|
2024-02-08 11:12:03 +00:00
|
|
|
$scores[$fileName] = max( $scores[$fileName] ?? -1, $score );
|
2021-12-02 00:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$bestImageName = false;
|
|
|
|
$freeImageName = false;
|
|
|
|
|
|
|
|
foreach ( $scores as $name => $score ) {
|
|
|
|
if ( $score > 0 ) {
|
|
|
|
if ( !$bestImageName || $score > $scores[$bestImageName] ) {
|
|
|
|
$bestImageName = $name;
|
|
|
|
}
|
|
|
|
if ( ( !$freeImageName || $score > $scores[$freeImageName] ) && $this->isImageFree( $name ) ) {
|
|
|
|
$freeImageName = $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [ $bestImageName, $freeImageName ];
|
2020-10-22 20:04:23 +00:00
|
|
|
}
|
2015-12-30 12:41:37 +00:00
|
|
|
|
2020-10-22 20:04:23 +00:00
|
|
|
/**
|
|
|
|
* Adds $image to $parserOutput extension data.
|
|
|
|
*
|
|
|
|
* @param PageImageCandidate $image
|
|
|
|
* @param ParserOutput $parserOutput
|
2021-12-02 00:18:50 +00:00
|
|
|
* @return int
|
2020-10-22 20:04:23 +00:00
|
|
|
*/
|
|
|
|
private function addPageImageCandidateToParserOutput(
|
|
|
|
PageImageCandidate $image,
|
|
|
|
ParserOutput $parserOutput
|
|
|
|
) {
|
|
|
|
$images = $parserOutput->getExtensionData( 'pageImages' ) ?: [];
|
|
|
|
$images[] = $image->jsonSerialize();
|
|
|
|
$parserOutput->setExtensionData( 'pageImages', $images );
|
2021-12-02 00:18:50 +00:00
|
|
|
return count( $images ) - 1;
|
2015-12-30 12:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if data for this title should be saved
|
|
|
|
*
|
2022-12-28 14:06:01 +00:00
|
|
|
* @param PageReference $pageReference
|
2015-12-30 12:41:37 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2022-12-28 14:06:01 +00:00
|
|
|
private function processThisTitle( PageReference $pageReference ) {
|
2024-02-08 11:12:03 +00:00
|
|
|
static $flipped = null;
|
2024-04-10 18:42:02 +00:00
|
|
|
$flipped ??= array_flip( $this->config->get( 'PageImagesNamespaces' ) );
|
2015-12-30 12:41:37 +00:00
|
|
|
|
2022-12-28 14:06:01 +00:00
|
|
|
return isset( $flipped[$pageReference->getNamespace()] );
|
2015-12-30 12:41:37 +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.
|
|
|
|
*
|
2016-03-07 08:47:51 +00:00
|
|
|
* @param array[] &$params
|
2015-12-30 12:41:37 +00:00
|
|
|
* @param File $file
|
|
|
|
*/
|
|
|
|
private function calcWidth( array &$params, File $file ) {
|
|
|
|
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'] )
|
2017-06-20 07:17:38 +00:00
|
|
|
|| isset( $params['frame']['frameless'] )
|
|
|
|
) {
|
2024-04-10 18:42:02 +00:00
|
|
|
$thumbLimits = $this->config->get( MainConfigNames::ThumbLimits );
|
|
|
|
$defaultUserOptions = $this->config->get( MainConfigNames::DefaultUserOptions );
|
|
|
|
$params['handler']['width'] = $thumbLimits[$defaultUserOptions['thumbsize']]
|
2019-03-11 03:22:21 +00:00
|
|
|
?? 250;
|
2015-12-30 12:41:37 +00:00
|
|
|
} else {
|
|
|
|
$params['handler']['width'] = $file->getWidth();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-02 00:18:50 +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 PageImageCandidate $image Associative array describing an image
|
|
|
|
* @param int $position Image order on page
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
protected function getScore( PageImageCandidate $image, $position ) {
|
2024-02-08 11:12:03 +00:00
|
|
|
// Exclude images with class="notpageimage"
|
|
|
|
if ( preg_match( '/(?:^|\s)notpageimage(?=\s|$)/', $image->getFrameClass() ) ) {
|
2021-11-04 23:15:39 +00:00
|
|
|
return -1000;
|
|
|
|
}
|
|
|
|
|
2024-04-10 18:42:02 +00:00
|
|
|
$pageImagesScores = $this->config->get( 'PageImagesScores' );
|
2021-12-02 00:18:50 +00:00
|
|
|
if ( $image->getHandlerWidth() ) {
|
|
|
|
// Standalone image
|
2024-04-10 18:42:02 +00:00
|
|
|
$score = $this->scoreFromTable( $image->getHandlerWidth(), $pageImagesScores['width'] );
|
2021-12-02 00:18:50 +00:00
|
|
|
} else {
|
|
|
|
// From gallery
|
2024-04-10 18:42:02 +00:00
|
|
|
$score = $this->scoreFromTable( $image->getFullWidth(), $pageImagesScores['galleryImageWidth'] );
|
2021-12-02 00:18:50 +00:00
|
|
|
}
|
|
|
|
|
2024-04-10 18:42:02 +00:00
|
|
|
if ( isset( $pageImagesScores['position'][$position] ) ) {
|
|
|
|
$score += $pageImagesScores['position'][$position];
|
2021-12-02 00:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$ratio = intval( $this->getRatio( $image ) * 10 );
|
2024-04-10 18:42:02 +00:00
|
|
|
$score += $this->scoreFromTable( $ratio, $pageImagesScores['ratio'] );
|
2021-12-02 00:18:50 +00:00
|
|
|
|
|
|
|
$denylist = $this->getDenylist();
|
|
|
|
if ( isset( $denylist[$image->getFileName()] ) ) {
|
|
|
|
$score = -1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $score;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns score based on table of ranges
|
|
|
|
*
|
|
|
|
* @param int $value The number that the various bounds are compared against
|
|
|
|
* to calculate the score
|
|
|
|
* @param float[] $scores Table of scores for different ranges of $value
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
protected function scoreFromTable( $value, array $scores ) {
|
|
|
|
$lastScore = 0;
|
|
|
|
|
|
|
|
// The loop stops at the *first* match, and therefore *requires* the input array keys to be
|
|
|
|
// in increasing order.
|
|
|
|
ksort( $scores, SORT_NUMERIC );
|
|
|
|
foreach ( $scores as $upperBoundary => $score ) {
|
|
|
|
$lastScore = $score;
|
|
|
|
|
|
|
|
if ( $value <= $upperBoundary ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !is_numeric( $lastScore ) ) {
|
|
|
|
wfLogWarning( 'The PageImagesScores setting must only contain numeric values!' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return (float)$lastScore;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether image's copyright allows it to be used freely.
|
|
|
|
*
|
|
|
|
* @param string $fileName Name of the image file
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isImageFree( $fileName ) {
|
2022-09-02 22:15:57 +00:00
|
|
|
$file = $this->repoGroup->findFile( $fileName );
|
2021-12-02 00:18:50 +00:00
|
|
|
if ( $file ) {
|
|
|
|
// Process copyright metadata from CommonsMetadata, if present.
|
|
|
|
// Image is considered free if the value is '0' or unset.
|
|
|
|
return empty( $this->fetchFileMetadata( $file )['NonFree']['value'] );
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch file metadata
|
|
|
|
*
|
|
|
|
* @param File $file File to fetch metadata from
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function fetchFileMetadata( $file ) {
|
|
|
|
$format = new FormatMetadata;
|
|
|
|
$context = new DerivativeContext( $format->getContext() );
|
|
|
|
// we don't care about the language, and specifying singleLanguage is slightly faster
|
|
|
|
$format->setSingleLanguage( true );
|
|
|
|
// we don't care about the language, so avoid splitting the cache by selecting English
|
|
|
|
$context->setLanguage( 'en' );
|
|
|
|
$format->setContext( $context );
|
|
|
|
return $format->fetchExtendedMetadata( $file );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-08 11:09:56 +00:00
|
|
|
* Returns width/height ratio of an image as displayed or 0 if not available
|
2021-12-02 00:18:50 +00:00
|
|
|
*
|
2024-02-08 11:09:56 +00:00
|
|
|
* @param PageImageCandidate $image
|
2021-12-02 00:18:50 +00:00
|
|
|
*
|
|
|
|
* @return float|int
|
|
|
|
*/
|
|
|
|
protected function getRatio( PageImageCandidate $image ) {
|
|
|
|
$width = $image->getFullWidth();
|
|
|
|
$height = $image->getFullHeight();
|
2024-02-08 11:12:03 +00:00
|
|
|
return $width > 0 && $height > 0 ? $width / $height : 0;
|
2021-12-02 00:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of images denylisted from influencing this extension's output
|
|
|
|
*
|
|
|
|
* @return int[] Flipped associative array in format "image BDB key" => int
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
protected function getDenylist() {
|
2022-09-02 22:15:57 +00:00
|
|
|
return $this->mainWANObjectCache->getWithSetCallback(
|
|
|
|
$this->mainWANObjectCache->makeKey( 'pageimages-denylist' ),
|
2024-04-10 18:42:02 +00:00
|
|
|
$this->config->get( 'PageImagesDenylistExpiry' ),
|
2021-12-02 00:18:50 +00:00
|
|
|
function () {
|
|
|
|
$list = [];
|
2024-04-10 18:42:02 +00:00
|
|
|
foreach ( $this->config->get( 'PageImagesDenylist' ) as $source ) {
|
2021-12-02 00:18:50 +00:00
|
|
|
switch ( $source['type'] ) {
|
|
|
|
case 'db':
|
|
|
|
$list = array_merge(
|
|
|
|
$list,
|
|
|
|
$this->getDbDenylist( $source['db'], $source['page'] )
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'url':
|
|
|
|
$list = array_merge(
|
|
|
|
$list,
|
|
|
|
$this->getUrlDenylist( $source['url'] )
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new RuntimeException(
|
|
|
|
"unrecognized image denylist type '{$source['type']}'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_flip( $list );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns list of images linked by the given denylist page
|
|
|
|
*
|
2023-06-21 19:54:03 +00:00
|
|
|
* @param string|false $dbName Database name or false for current database
|
2021-12-02 00:18:50 +00:00
|
|
|
* @param string $page
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
private function getDbDenylist( $dbName, $page ) {
|
2023-06-21 21:03:44 +00:00
|
|
|
$title = $this->titleFactory->newFromText( $page );
|
|
|
|
if ( !$title || !$title->canExist() ) {
|
|
|
|
return [];
|
|
|
|
}
|
2021-12-02 00:18:50 +00:00
|
|
|
|
2023-06-21 19:54:03 +00:00
|
|
|
$dbr = $this->connectionProvider->getReplicaDatabase( $dbName );
|
|
|
|
$id = $dbr->newSelectQueryBuilder()
|
|
|
|
->select( 'page_id' )
|
|
|
|
->from( 'page' )
|
|
|
|
->where( [ 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDBkey() ] )
|
|
|
|
->caller( __METHOD__ )->fetchField();
|
|
|
|
if ( !$id ) {
|
|
|
|
return [];
|
2021-12-02 00:18:50 +00:00
|
|
|
}
|
2023-11-01 17:51:38 +00:00
|
|
|
[ $blNamespace, $blTitle ] = $this->linksMigration->getTitleFields( 'pagelinks' );
|
|
|
|
$queryInfo = $this->linksMigration->getQueryInfo( 'pagelinks' );
|
2021-12-02 00:18:50 +00:00
|
|
|
|
2023-06-21 19:54:03 +00:00
|
|
|
return $dbr->newSelectQueryBuilder()
|
2023-11-01 17:51:38 +00:00
|
|
|
->select( $blTitle )
|
|
|
|
->tables( $queryInfo['tables'] )
|
|
|
|
->joinConds( $queryInfo['joins'] )
|
|
|
|
->where( [ 'pl_from' => (int)$id, $blNamespace => NS_FILE ] )
|
2023-06-21 19:54:03 +00:00
|
|
|
->caller( __METHOD__ )->fetchFieldValues();
|
2021-12-02 00:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns list of images on given remote denylist 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 string[]
|
|
|
|
*/
|
|
|
|
private function getUrlDenylist( $url ) {
|
|
|
|
$list = [];
|
2022-12-11 13:00:11 +00:00
|
|
|
$text = $this->httpRequestFactory->get( $url, [ 'timeout' => 3 ], __METHOD__ );
|
2024-04-10 18:42:02 +00:00
|
|
|
$fileExtensions = $this->config->get( 'FileExtensions' );
|
|
|
|
$regex = '/\[\[:([^|\#]*?\.(?:' . implode( '|', $fileExtensions ) . '))/i';
|
2021-12-02 00:18:50 +00:00
|
|
|
|
|
|
|
if ( $text && preg_match_all( $regex, $text, $matches ) ) {
|
|
|
|
foreach ( $matches[1] as $s ) {
|
2023-06-21 21:03:44 +00:00
|
|
|
$t = $this->titleFactory->makeTitleSafe( NS_FILE, $s );
|
2021-12-02 00:18:50 +00:00
|
|
|
|
|
|
|
if ( $t ) {
|
|
|
|
$list[] = $t->getDBkey();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2015-12-30 12:41:37 +00:00
|
|
|
}
|