MediaWiki
master
Toggle main menu visibility
BadFileLookup.php
Go to the documentation of this file.
1
<?php
2
3
namespace
MediaWiki\Page\File
;
4
5
use
MediaWiki\FileRepo\File\File
;
6
use
MediaWiki\FileRepo\RepoGroup
;
7
use
MediaWiki\HookContainer\HookContainer
;
8
use
MediaWiki\HookContainer\HookRunner
;
9
use
MediaWiki\Linker\LinkTarget
;
10
use
MediaWiki\Title\MalformedTitleException
;
11
use
MediaWiki\Title\TitleParser
;
12
use
Wikimedia\ObjectCache\BagOStuff
;
13
14
class
BadFileLookup
{
16
private
$listCallback;
17
18
private
BagOStuff
$cache;
19
private
RepoGroup
$repoGroup;
20
private
TitleParser
$titleParser;
21
private
HookRunner
$hookRunner;
22
24
private
$badFiles;
25
35
public
function
__construct
(
36
callable $listCallback,
37
BagOStuff
$cache,
38
RepoGroup
$repoGroup,
39
TitleParser
$titleParser,
40
HookContainer
$hookContainer
41
) {
42
$this->listCallback = $listCallback;
43
$this->cache = $cache;
44
$this->repoGroup = $repoGroup;
45
$this->titleParser = $titleParser;
46
$this->hookRunner =
new
HookRunner
( $hookContainer );
47
}
48
62
public
function
isBadFile
(
File
|
string
$file, ?
LinkTarget
$contextTitle =
null
) {
63
// If name was provided instead of File, get File object from repoGroup
64
if
( is_string( $file ) ) {
65
$name = $file;
66
// Handle redirects; callers almost always hit RepoGroup::findFile() anyway,
67
// so just use that method because it has a fast process cache.
68
$file = $this->repoGroup->findFile( $file );
69
// XXX If we don't find the file we also don't replace spaces by underscores or otherwise
70
// validate or normalize the title, is this right?
71
if
( $file ) {
72
$name = $file->getTitle()->getDBkey();
73
}
74
}
else
{
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
$list = ( $this->listCallback )();
86
$key = $this->cache->makeKey(
'bad-image-list'
, sha1( $list ) );
87
$this->badFiles = $this->cache->getWithSetCallback(
88
$key,
89
BagOStuff::TTL_DAY,
90
function
() use ( $list ) {
91
return
$this->buildBadFilesList( $list );
92
}
93
);
94
}
95
96
return
isset( $this->badFiles[$name] ) && ( !$contextTitle ||
97
!isset( $this->badFiles[$name][$contextTitle->getNamespace()][$contextTitle->getDBkey()] ) );
98
}
99
104
private
function
buildBadFilesList(
string
$list ): array {
105
$ret = [];
106
$lines = explode(
"\n"
, $list );
107
foreach
( $lines as $line ) {
108
// List items only
109
if
( !str_starts_with( $line,
'*'
) ) {
110
continue
;
111
}
112
113
// Find all links
114
$m = [];
115
// XXX What is the ':?' doing in the regex? Why not let the TitleParser strip it?
116
if
( !preg_match_all(
'/\[\[:?(.*?)\]\]/'
, $line, $m ) ) {
117
continue
;
118
}
119
120
$fileDBkey =
null
;
121
$exceptions = [];
122
foreach
( $m[1] as $i => $titleText ) {
123
try
{
124
$title = $this->titleParser->parseTitle( $titleText );
125
}
catch
( MalformedTitleException ) {
126
continue
;
127
}
128
if
( $i == 0 ) {
129
$fileDBkey = $title->getDBkey();
130
}
else
{
131
$exceptions[$title->getNamespace()][$title->getDBkey()] =
true
;
132
}
133
}
134
135
if
( $fileDBkey !==
null
) {
136
$ret[$fileDBkey] = $exceptions;
137
}
138
}
139
return
$ret;
140
}
141
}
MediaWiki\FileRepo\File\File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition
File.php:80
MediaWiki\FileRepo\RepoGroup
Prioritized list of file repositories.
Definition
RepoGroup.php:30
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition
HookContainer.php:42
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition
HookRunner.php:601
MediaWiki\Page\File\BadFileLookup
Definition
BadFileLookup.php:14
MediaWiki\Page\File\BadFileLookup\__construct
__construct(callable $listCallback, BagOStuff $cache, RepoGroup $repoGroup, TitleParser $titleParser, HookContainer $hookContainer)
Do not call directly.
Definition
BadFileLookup.php:35
MediaWiki\Page\File\BadFileLookup\isBadFile
isBadFile(File|string $file, ?LinkTarget $contextTitle=null)
Determine if a file exists on the 'bad image list'.
Definition
BadFileLookup.php:62
MediaWiki\Title\MalformedTitleException
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
Definition
MalformedTitleException.php:18
MediaWiki\Title\TitleParser
A title parser service for MediaWiki.
Definition
TitleParser.php:29
Wikimedia\ObjectCache\BagOStuff
Abstract class for any ephemeral data store.
Definition
BagOStuff.php:73
MediaWiki\Linker\LinkTarget
Represents the target of a wiki link.
Definition
LinkTarget.php:19
MediaWiki\Page\File
Definition
BadFileLookup.php:3
includes
Page
File
BadFileLookup.php
Generated on
for MediaWiki by
1.18.0