MediaWiki master
BadFileLookup.php
Go to the documentation of this file.
1<?php
2
4
13
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}
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:80
Prioritized list of file repositories.
Definition RepoGroup.php:30
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
__construct(callable $listCallback, BagOStuff $cache, RepoGroup $repoGroup, TitleParser $titleParser, HookContainer $hookContainer)
Do not call directly.
isBadFile(File|string $file, ?LinkTarget $contextTitle=null)
Determine if a file exists on the 'bad image list'.
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
A title parser service for MediaWiki.
Abstract class for any ephemeral data store.
Definition BagOStuff.php:73
Represents the target of a wiki link.