Movatterモバイル変換


[0]ホーム

URL:


MediaWiki master
BadFileLookup.php
Go to the documentation of this file.
1<?php
2
3namespaceMediaWiki\Page\File;
4
5useMediaWiki\FileRepo\RepoGroup;
6useMediaWiki\HookContainer\HookContainer;
7useMediaWiki\HookContainer\HookRunner;
8useMediaWiki\Linker\LinkTarget;
9useMediaWiki\Title\MalformedTitleException;
10useMediaWiki\Title\TitleParser;
11useWikimedia\ObjectCache\BagOStuff;
12
13classBadFileLookup {
15private $listCallback;
16
17privateBagOStuff $cache;
18privateRepoGroup $repoGroup;
19privateTitleParser $titleParser;
20privateHookRunner $hookRunner;
21
23private $badFiles;
24
34publicfunction__construct(
35 callable $listCallback,
36BagOStuff $cache,
37RepoGroup $repoGroup,
38TitleParser $titleParser,
39HookContainer $hookContainer
40 ) {
41 $this->listCallback = $listCallback;
42 $this->cache = $cache;
43 $this->repoGroup = $repoGroup;
44 $this->titleParser = $titleParser;
45 $this->hookRunner =newHookRunner( $hookContainer );
46 }
47
61publicfunctionisBadFile( $name, ?LinkTarget $contextTitle =null ) {
62// Handle redirects; callers almost always hit RepoGroup::findFile() anyway,
63// so just use that method 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?
67if ( $file ) {
68 $name = $file->getTitle()->getDBkey();
69 }
70
71// Run the extension hook
72 $bad =false;
73if ( !$this->hookRunner->onBadImage( $name, $bad ) ) {
74return (bool)$bad;
75 }
76
77if ( $this->badFiles ===null ) {
78 $list = ( $this->listCallback )();
79 $key = $this->cache->makeKey('bad-image-list', sha1( $list ) );
80 $this->badFiles = $this->cache->getWithSetCallback(
81 $key,
82 BagOStuff::TTL_DAY,
83function () use ( $list ) {
84return $this->buildBadFilesList( $list );
85 }
86 );
87 }
88
89return isset( $this->badFiles[$name] ) && ( !$contextTitle ||
90 !isset( $this->badFiles[$name][$contextTitle->getNamespace()][$contextTitle->getDBkey()] ) );
91 }
92
97privatefunction buildBadFilesList(string $list ): array {
98 $ret = [];
99 $lines = explode("\n", $list );
100foreach ( $lines as $line ) {
101// List items only
102if ( !str_starts_with( $line,'*' ) ) {
103continue;
104 }
105
106// Find all links
107 $m = [];
108// XXX What is the ':?' doing in the regex? Why not let the TitleParser strip it?
109if ( !preg_match_all('/\[\[:?(.*?)\]\]/', $line, $m ) ) {
110continue;
111 }
112
113 $fileDBkey =null;
114 $exceptions = [];
115foreach ( $m[1] as $i => $titleText ) {
116try {
117 $title = $this->titleParser->parseTitle( $titleText );
118 }catch ( MalformedTitleException ) {
119continue;
120 }
121if ( $i == 0 ) {
122 $fileDBkey = $title->getDBkey();
123 }else {
124 $exceptions[$title->getNamespace()][$title->getDBkey()] =true;
125 }
126 }
127
128if ( $fileDBkey !==null ) {
129 $ret[$fileDBkey] = $exceptions;
130 }
131 }
132return $ret;
133 }
134}
MediaWiki\FileRepo\RepoGroup
Prioritized list of file repositories.
DefinitionRepoGroup.php:38
MediaWiki\HookContainer\HookContainer
HookContainer class.
DefinitionHookContainer.php:57
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
DefinitionHookRunner.php:592
MediaWiki\Page\File\BadFileLookup
DefinitionBadFileLookup.php:13
MediaWiki\Page\File\BadFileLookup\__construct
__construct(callable $listCallback, BagOStuff $cache, RepoGroup $repoGroup, TitleParser $titleParser, HookContainer $hookContainer)
Do not call directly.
DefinitionBadFileLookup.php:34
MediaWiki\Page\File\BadFileLookup\isBadFile
isBadFile( $name, ?LinkTarget $contextTitle=null)
Determine if a file exists on the 'bad image list'.
DefinitionBadFileLookup.php:61
MediaWiki\Title\MalformedTitleException
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
DefinitionMalformedTitleException.php:32
MediaWiki\Title\TitleParser
A title parser service for MediaWiki.
DefinitionTitleParser.php:43
Wikimedia\ObjectCache\BagOStuff
Abstract class for any ephemeral data store.
DefinitionBagOStuff.php:87
MediaWiki\Linker\LinkTarget
Represents the target of a wiki link.
DefinitionLinkTarget.php:33
MediaWiki\Page\File
DefinitionBadFileLookup.php:3

[8]ページ先頭

©2009-2025 Movatter.jp