MediaWiki REL1_35
BadFileLookup.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki;
4
5use BagOStuff;
10use RepoGroup;
11use TitleParser;
12
16
18 private $cache;
19
21 private $repoGroup;
22
24 private $titleParser;
25
27 private $badFiles;
28
30 private $hookRunner;
31
41 public function __construct(
42 callable $listCallback,
44 RepoGroup $repoGroup,
45 TitleParser $titleParser,
46 HookContainer $hookContainer
47 ) {
48 $this->listCallback = $listCallback;
49 $this->cache = $cache;
50 $this->repoGroup = $repoGroup;
51 $this->titleParser = $titleParser;
52 $this->hookRunner = new HookRunner( $hookContainer );
53 }
54
68 public function isBadFile( $name, LinkTarget $contextTitle = null ) {
69 // Handle redirects; callers almost always hit RepoGroup::findFile() anyway,
70 // so just use that method because it has a fast process cache.
71 $file = $this->repoGroup->findFile( $name );
72 // XXX If we don't find the file we also don't replace spaces by underscores or otherwise
73 // validate or normalize the title, is this right?
74 if ( $file ) {
75 $name = $file->getTitle()->getDBkey();
76 }
77
78 // Run the extension hook
79 $bad = false;
80 if ( !$this->hookRunner->onBadImage( $name, $bad ) ) {
81 return (bool)$bad;
82 }
83
84 if ( $this->badFiles === null ) {
85 // Not used before in this request, try the cache
86 $list = ( $this->listCallback )();
87 $key = $this->cache->makeKey( 'bad-image-list', sha1( $list ) );
88 $this->badFiles = $this->cache->get( $key ) ?: null;
89 }
90
91 if ( $this->badFiles === null ) {
92 // Cache miss, build the list now
93 $this->badFiles = [];
94 $lines = explode( "\n", $list );
95 foreach ( $lines as $line ) {
96 // List items only
97 if ( substr( $line, 0, 1 ) !== '*' ) {
98 continue;
99 }
100
101 // Find all links
102 $m = [];
103 // XXX What is the ':?' doing in the regex? Why not let the TitleParser strip it?
104 if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
105 continue;
106 }
107
108 $fileDBkey = null;
109 $exceptions = [];
110 foreach ( $m[1] as $i => $titleText ) {
111 try {
112 $title = $this->titleParser->parseTitle( $titleText );
113 } catch ( MalformedTitleException $e ) {
114 continue;
115 }
116 if ( $i == 0 ) {
117 $fileDBkey = $title->getDBkey();
118 } else {
119 $exceptions[$title->getNamespace()][$title->getDBkey()] = true;
120 }
121 }
122
123 if ( $fileDBkey !== null ) {
124 $this->badFiles[$fileDBkey] = $exceptions;
125 }
126 }
127 $this->cache->set( $key, $this->badFiles, 24 * 60 * 60 );
128 }
129
130 return isset( $this->badFiles[$name] ) && ( !$contextTitle ||
131 !isset( $this->badFiles[$name][$contextTitle->getNamespace()]
132 [$contextTitle->getDBkey()] ) );
133 }
134}
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:71
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
__construct(callable $listCallback, BagOStuff $cache, RepoGroup $repoGroup, TitleParser $titleParser, HookContainer $hookContainer)
Do not call directly.
array null $badFiles
Parsed bad file list.
callable $listCallback
Returns contents of bad file list (see comment for isBadFile())
isBadFile( $name, LinkTarget $contextTitle=null)
Determine if a file exists on the 'bad image list'.
BagOStuff $cache
Cache of parsed bad image list.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Prioritized list of file repositories.
Definition RepoGroup.php:31
A title parser service for MediaWiki.
$line
Definition mcc.php:119
$cache
Definition mcc.php:33
A helper class for throttling authentication attempts.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42
if(!file_exists( $CREDITS)) $lines