MediaWiki  master
BadFileLookup.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki;
4 
5 use BagOStuff;
10 use RepoGroup;
11 use TitleParser;
12 
15  private $listCallback;
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 }
MediaWiki\BadFileLookup
Definition: BadFileLookup.php:13
MediaWiki\BadFileLookup\$listCallback
callable $listCallback
Returns contents of bad file list (see comment for isBadFile())
Definition: BadFileLookup.php:15
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:71
MediaWiki\BadFileLookup\$titleParser
TitleParser $titleParser
Definition: BadFileLookup.php:24
MediaWiki\BadFileLookup\$badFiles
array null $badFiles
Parsed bad file list.
Definition: BadFileLookup.php:27
MediaWiki\BadFileLookup\isBadFile
isBadFile( $name, LinkTarget $contextTitle=null)
Determine if a file exists on the 'bad image list'.
Definition: BadFileLookup.php:68
MediaWiki
A helper class for throttling authentication attempts.
$title
$title
Definition: testCompression.php:38
TitleParser
A title parser service for MediaWiki.
Definition: TitleParser.php:33
MediaWiki\BadFileLookup\$repoGroup
RepoGroup $repoGroup
Definition: BadFileLookup.php:21
$line
$line
Definition: mcc.php:119
$lines
if(!file_exists( $CREDITS)) $lines
Definition: updateCredits.php:49
$cache
$cache
Definition: mcc.php:33
MalformedTitleException
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
Definition: MalformedTitleException.php:26
RepoGroup
Prioritized list of file repositories.
Definition: RepoGroup.php:31
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition: HookContainer.php:44
MediaWiki\BadFileLookup\__construct
__construct(callable $listCallback, BagOStuff $cache, RepoGroup $repoGroup, TitleParser $titleParser, HookContainer $hookContainer)
Do not call directly.
Definition: BadFileLookup.php:41
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:570
MediaWiki\BadFileLookup\$hookRunner
HookRunner $hookRunner
Definition: BadFileLookup.php:30
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
MediaWiki\BadFileLookup\$cache
BagOStuff $cache
Cache of parsed bad image list.
Definition: BadFileLookup.php:18