Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 113 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
SpecialFileDuplicateSearch | |
0.00% |
0 / 112 |
|
0.00% |
0 / 8 |
702 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
getDupes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
showList | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
execute | |
0.00% |
0 / 59 |
|
0.00% |
0 / 1 |
110 | |||
doBatchLookups | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
formatResult | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
20 | |||
prefixSearchSubpages | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Implements Special:FileDuplicateSearch |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; if not, write to the Free Software Foundation, Inc., |
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
19 | * http://www.gnu.org/copyleft/gpl.html |
20 | * |
21 | * @file |
22 | * @ingroup SpecialPage |
23 | * @author Raimond Spekking, based on Special:MIMESearch by Ævar Arnfjörð Bjarmason |
24 | */ |
25 | |
26 | namespace MediaWiki\Specials; |
27 | |
28 | use File; |
29 | use ILanguageConverter; |
30 | use MediaWiki\Cache\LinkBatchFactory; |
31 | use MediaWiki\HTMLForm\HTMLForm; |
32 | use MediaWiki\Languages\LanguageConverterFactory; |
33 | use MediaWiki\Linker\Linker; |
34 | use MediaWiki\SpecialPage\SpecialPage; |
35 | use MediaWiki\Title\Title; |
36 | use RepoGroup; |
37 | use SearchEngineFactory; |
38 | |
39 | /** |
40 | * Searches the database for files of the requested hash, comparing this with the |
41 | * 'img_sha1' field in the image table. |
42 | * |
43 | * @ingroup SpecialPage |
44 | */ |
45 | class SpecialFileDuplicateSearch extends SpecialPage { |
46 | /** |
47 | * @var string The form input hash |
48 | */ |
49 | private $hash = ''; |
50 | |
51 | /** |
52 | * @var string The form input filename |
53 | */ |
54 | private $filename = ''; |
55 | |
56 | /** |
57 | * @var File|null selected reference file, if present |
58 | */ |
59 | private $file = null; |
60 | |
61 | private LinkBatchFactory $linkBatchFactory; |
62 | private RepoGroup $repoGroup; |
63 | private SearchEngineFactory $searchEngineFactory; |
64 | private ILanguageConverter $languageConverter; |
65 | |
66 | /** |
67 | * @param LinkBatchFactory $linkBatchFactory |
68 | * @param RepoGroup $repoGroup |
69 | * @param SearchEngineFactory $searchEngineFactory |
70 | * @param LanguageConverterFactory $languageConverterFactory |
71 | */ |
72 | public function __construct( |
73 | LinkBatchFactory $linkBatchFactory, |
74 | RepoGroup $repoGroup, |
75 | SearchEngineFactory $searchEngineFactory, |
76 | LanguageConverterFactory $languageConverterFactory |
77 | ) { |
78 | parent::__construct( 'FileDuplicateSearch' ); |
79 | $this->linkBatchFactory = $linkBatchFactory; |
80 | $this->repoGroup = $repoGroup; |
81 | $this->searchEngineFactory = $searchEngineFactory; |
82 | $this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() ); |
83 | } |
84 | |
85 | /** |
86 | * Fetch dupes from all connected file repositories. |
87 | * |
88 | * @return File[] |
89 | */ |
90 | private function getDupes() { |
91 | return $this->repoGroup->findBySha1( $this->hash ); |
92 | } |
93 | |
94 | /** |
95 | * @param File[] $dupes |
96 | */ |
97 | private function showList( $dupes ) { |
98 | $html = []; |
99 | $html[] = "<ol class='special'>"; |
100 | |
101 | foreach ( $dupes as $dupe ) { |
102 | $line = $this->formatResult( $dupe ); |
103 | $html[] = "<li>" . $line . "</li>"; |
104 | } |
105 | $html[] = '</ol>'; |
106 | |
107 | $this->getOutput()->addHTML( implode( "\n", $html ) ); |
108 | } |
109 | |
110 | public function execute( $par ) { |
111 | $this->setHeaders(); |
112 | $this->outputHeader(); |
113 | |
114 | $this->filename = $par ?? $this->getRequest()->getText( 'filename' ); |
115 | $this->file = null; |
116 | $this->hash = ''; |
117 | $title = Title::newFromText( $this->filename, NS_FILE ); |
118 | if ( $title && $title->getText() != '' ) { |
119 | $this->file = $this->repoGroup->findFile( $title ); |
120 | } |
121 | |
122 | $out = $this->getOutput(); |
123 | |
124 | # Create the input form |
125 | $formFields = [ |
126 | 'filename' => [ |
127 | 'type' => 'text', |
128 | 'name' => 'filename', |
129 | 'label-message' => 'fileduplicatesearch-filename', |
130 | 'id' => 'filename', |
131 | 'size' => 50, |
132 | 'default' => $this->filename, |
133 | ], |
134 | ]; |
135 | $htmlForm = HTMLForm::factory( 'ooui', $formFields, $this->getContext() ); |
136 | $htmlForm->setTitle( $this->getPageTitle() ); |
137 | $htmlForm->setMethod( 'get' ); |
138 | $htmlForm->setSubmitTextMsg( $this->msg( 'fileduplicatesearch-submit' ) ); |
139 | |
140 | // The form should be visible always, even if it was submitted (e.g. to perform another action). |
141 | // To bypass the callback validation of HTMLForm, use prepareForm() and displayForm(). |
142 | $htmlForm->prepareForm()->displayForm( false ); |
143 | |
144 | if ( $this->file ) { |
145 | $this->hash = $this->file->getSha1(); |
146 | } elseif ( $this->filename !== '' ) { |
147 | $out->wrapWikiMsg( |
148 | "<p class='mw-fileduplicatesearch-noresults'>\n$1\n</p>", |
149 | [ 'fileduplicatesearch-noresults', wfEscapeWikiText( $this->filename ) ] |
150 | ); |
151 | } |
152 | |
153 | if ( $this->hash != '' ) { |
154 | # Show a thumbnail of the file |
155 | $img = $this->file; |
156 | if ( $img ) { |
157 | $thumb = $img->transform( [ 'width' => 120, 'height' => 120 ] ); |
158 | if ( $thumb ) { |
159 | $out->addModuleStyles( 'mediawiki.special' ); |
160 | $out->addHTML( '<div id="mw-fileduplicatesearch-icon">' . |
161 | $thumb->toHtml( [ 'desc-link' => false ] ) . '<br />' . |
162 | $this->msg( 'fileduplicatesearch-info' ) |
163 | ->numParams( $img->getWidth(), $img->getHeight() ) |
164 | ->sizeParams( $img->getSize() ) |
165 | ->params( $img->getMimeType() )->parseAsBlock() . |
166 | '</div>' ); |
167 | } |
168 | } |
169 | |
170 | $dupes = $this->getDupes(); |
171 | $numRows = count( $dupes ); |
172 | |
173 | # Show a short summary |
174 | if ( $numRows == 1 ) { |
175 | $out->wrapWikiMsg( |
176 | "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>", |
177 | [ 'fileduplicatesearch-result-1', wfEscapeWikiText( $this->filename ) ] |
178 | ); |
179 | } elseif ( $numRows ) { |
180 | $out->wrapWikiMsg( |
181 | "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>", |
182 | [ 'fileduplicatesearch-result-n', wfEscapeWikiText( $this->filename ), |
183 | $this->getLanguage()->formatNum( $numRows - 1 ) ] |
184 | ); |
185 | } |
186 | |
187 | $this->doBatchLookups( $dupes ); |
188 | $this->showList( $dupes ); |
189 | } |
190 | } |
191 | |
192 | /** |
193 | * @param File[] $list |
194 | */ |
195 | private function doBatchLookups( $list ) { |
196 | $batch = $this->linkBatchFactory->newLinkBatch(); |
197 | foreach ( $list as $file ) { |
198 | $batch->addObj( $file->getTitle() ); |
199 | if ( $file->isLocal() ) { |
200 | $uploader = $file->getUploader( File::FOR_THIS_USER, $this->getAuthority() ); |
201 | if ( $uploader ) { |
202 | $batch->add( NS_USER, $uploader->getName() ); |
203 | $batch->add( NS_USER_TALK, $uploader->getName() ); |
204 | } |
205 | } |
206 | } |
207 | |
208 | $batch->execute(); |
209 | } |
210 | |
211 | /** |
212 | * @param File $result |
213 | * @return string HTML |
214 | */ |
215 | private function formatResult( $result ) { |
216 | $linkRenderer = $this->getLinkRenderer(); |
217 | $nt = $result->getTitle(); |
218 | $text = $this->languageConverter->convert( $nt->getText() ); |
219 | $plink = $linkRenderer->makeLink( |
220 | $nt, |
221 | $text |
222 | ); |
223 | |
224 | $uploader = $result->getUploader( File::FOR_THIS_USER, $this->getAuthority() ); |
225 | if ( $result->isLocal() && $uploader ) { |
226 | $user = Linker::userLink( $uploader->getId(), $uploader->getName() ); |
227 | $user .= '<span style="white-space: nowrap;">'; |
228 | $user .= Linker::userToolLinks( $uploader->getId(), $uploader->getName() ); |
229 | $user .= '</span>'; |
230 | } elseif ( $uploader ) { |
231 | $user = htmlspecialchars( $uploader->getName() ); |
232 | } else { |
233 | $user = '<span class="history-deleted">' |
234 | . $this->msg( 'rev-deleted-user' )->escaped() . '</span>'; |
235 | } |
236 | |
237 | $time = htmlspecialchars( $this->getLanguage()->userTimeAndDate( |
238 | $result->getTimestamp(), $this->getUser() ) ); |
239 | |
240 | return "$plink . . $user . . $time"; |
241 | } |
242 | |
243 | /** |
244 | * Return an array of subpages beginning with $search that this special page will accept. |
245 | * |
246 | * @param string $search Prefix to search for |
247 | * @param int $limit Maximum number of results to return (usually 10) |
248 | * @param int $offset Number of results to skip (usually 0) |
249 | * @return string[] Matching subpages |
250 | */ |
251 | public function prefixSearchSubpages( $search, $limit, $offset ) { |
252 | $title = Title::newFromText( $search, NS_FILE ); |
253 | if ( !$title || $title->getNamespace() !== NS_FILE ) { |
254 | // No prefix suggestion outside of file namespace |
255 | return []; |
256 | } |
257 | $searchEngine = $this->searchEngineFactory->create(); |
258 | $searchEngine->setLimitOffset( $limit, $offset ); |
259 | // Autocomplete subpage the same as a normal search, but just for files |
260 | $searchEngine->setNamespaces( [ NS_FILE ] ); |
261 | $result = $searchEngine->defaultPrefixSearch( $search ); |
262 | |
263 | return array_map( static function ( Title $t ) { |
264 | // Remove namespace in search suggestion |
265 | return $t->getText(); |
266 | }, $result ); |
267 | } |
268 | |
269 | protected function getGroupName() { |
270 | return 'media'; |
271 | } |
272 | } |
273 | |
274 | /** @deprecated class alias since 1.41 */ |
275 | class_alias( SpecialFileDuplicateSearch::class, 'SpecialFileDuplicateSearch' ); |