ParamValidator: Flag as unstable for 1.34
[lhc/web/wiklou.git] / includes / BadFileLookup.php
1 <?php
2
3 namespace MediaWiki;
4
5 use BagOStuff;
6 use Hooks;
7 use MalformedTitleException;
8 use MediaWiki\Linker\LinkTarget;
9 use RepoGroup;
10 use TitleParser;
11
12 class BadFileLookup {
13 /** @var callable Returns contents of blacklist (see comment for isBadFile()) */
14 private $blacklistCallback;
15
16 /** @var BagOStuff Cache of parsed bad image list */
17 private $cache;
18
19 /** @var RepoGroup */
20 private $repoGroup;
21
22 /** @var TitleParser */
23 private $titleParser;
24
25 /** @var array|null Parsed blacklist */
26 private $badFiles;
27
28 /**
29 * Do not call directly. Use MediaWikiServices.
30 *
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
35 */
36 public function __construct(
37 callable $blacklistCallback,
38 BagOStuff $cache,
39 RepoGroup $repoGroup,
40 TitleParser $titleParser
41 ) {
42 $this->blacklistCallback = $blacklistCallback;
43 $this->cache = $cache;
44 $this->repoGroup = $repoGroup;
45 $this->titleParser = $titleParser;
46 }
47
48 /**
49 * Determine if a file exists on the 'bad image list'.
50 *
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.
56 *
57 * @param string $name The file name to check
58 * @param LinkTarget|null $contextTitle The page on which the file occurs, if known
59 * @return bool
60 */
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?
67 if ( $file ) {
68 $name = $file->getTitle()->getDBkey();
69 }
70
71 // Run the extension hook
72 $bad = false;
73 if ( !Hooks::run( 'BadImage', [ $name, &$bad ] ) ) {
74 return (bool)$bad;
75 }
76
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;
82 }
83
84 if ( $this->badFiles === null ) {
85 // Cache miss, build the list now
86 $this->badFiles = [];
87 $lines = explode( "\n", $blacklist );
88 foreach ( $lines as $line ) {
89 // List items only
90 if ( substr( $line, 0, 1 ) !== '*' ) {
91 continue;
92 }
93
94 // Find all links
95 $m = [];
96 // XXX What is the ':?' doing in the regex? Why not let the TitleParser strip it?
97 if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
98 continue;
99 }
100
101 $fileDBkey = null;
102 $exceptions = [];
103 foreach ( $m[1] as $i => $titleText ) {
104 try {
105 $title = $this->titleParser->parseTitle( $titleText );
106 } catch ( MalformedTitleException $e ) {
107 continue;
108 }
109 if ( $i == 0 ) {
110 $fileDBkey = $title->getDBkey();
111 } else {
112 $exceptions[$title->getNamespace()][$title->getDBkey()] = true;
113 }
114 }
115
116 if ( $fileDBkey !== null ) {
117 $this->badFiles[$fileDBkey] = $exceptions;
118 }
119 }
120 $this->cache->set( $key, $this->badFiles, 24 * 60 * 60 );
121 }
122
123 return isset( $this->badFiles[$name] ) && ( !$contextTitle ||
124 !isset( $this->badFiles[$name][$contextTitle->getNamespace()]
125 [$contextTitle->getDBkey()] ) );
126 }
127 }