7 use MalformedTitleException
;
8 use MediaWiki\Linker\LinkTarget
;
13 /** @var callable Returns contents of blacklist (see comment for isBadFile()) */
14 private $blacklistCallback;
16 /** @var BagOStuff Cache of parsed bad image list */
22 /** @var TitleParser */
25 /** @var array|null Parsed blacklist */
29 * Do not call directly. Use MediaWikiServices.
31 * @param callable $blacklistCallback Callback that returns wikitext of a file blacklist
32 * @param BagOStuff $cache For caching parsed versions of the blacklist
33 * @param RepoGroup $repoGroup
34 * @param TitleParser $titleParser
36 public function __construct(
37 callable
$blacklistCallback,
40 TitleParser
$titleParser
42 $this->blacklistCallback
= $blacklistCallback;
43 $this->cache
= $cache;
44 $this->repoGroup
= $repoGroup;
45 $this->titleParser
= $titleParser;
49 * Determine if a file exists on the 'bad image list'.
51 * The format of MediaWiki:Bad_image_list is as follows:
52 * * Only list items (lines starting with "*") are considered
53 * * The first link on a line must be a link to a bad file
54 * * Any subsequent links on the same line are considered to be exceptions,
55 * i.e. articles where the file may occur inline.
57 * @param string $name The file name to check
58 * @param LinkTarget|null $contextTitle The page on which the file occurs, if known
61 public function isBadFile( $name, LinkTarget
$contextTitle = null ) {
62 // Handle redirects; callers almost always hit wfFindFile() anyway, so just use that method
63 // because it has a fast process cache.
64 $file = $this->repoGroup
->findFile( $name );
65 // XXX If we don't find the file we also don't replace spaces by underscores or otherwise
66 // validate or normalize the title, is this right?
68 $name = $file->getTitle()->getDBkey();
71 // Run the extension hook
73 if ( !Hooks
::run( 'BadImage', [ $name, &$bad ] ) ) {
77 if ( $this->badFiles
=== null ) {
78 // Not used before in this request, try the cache
79 $blacklist = ( $this->blacklistCallback
)();
80 $key = $this->cache
->makeKey( 'bad-image-list', sha1( $blacklist ) );
81 $this->badFiles
= $this->cache
->get( $key ) ?
: null;
84 if ( $this->badFiles
=== null ) {
85 // Cache miss, build the list now
87 $lines = explode( "\n", $blacklist );
88 foreach ( $lines as $line ) {
90 if ( substr( $line, 0, 1 ) !== '*' ) {
96 // XXX What is the ':?' doing in the regex? Why not let the TitleParser strip it?
97 if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
103 foreach ( $m[1] as $i => $titleText ) {
105 $title = $this->titleParser
->parseTitle( $titleText );
106 } catch ( MalformedTitleException
$e ) {
110 $fileDBkey = $title->getDBkey();
112 $exceptions[$title->getNamespace()][$title->getDBkey()] = true;
116 if ( $fileDBkey !== null ) {
117 $this->badFiles
[$fileDBkey] = $exceptions;
120 $this->cache
->set( $key, $this->badFiles
, 24 * 60 * 60 );
123 return isset( $this->badFiles
[$name] ) && ( !$contextTitle ||
124 !isset( $this->badFiles
[$name][$contextTitle->getNamespace()]
125 [$contextTitle->getDBkey()] ) );