MediaWiki REL1_37
SpecialFileDuplicateSearch.php
Go to the documentation of this file.
1<?php
2
28
39 private $hash = '';
40
44 private $filename = '';
45
49 private $file = null;
50
53
55 private $repoGroup;
56
59
62
69 public function __construct(
73 LanguageConverterFactory $languageConverterFactory
74 ) {
75 parent::__construct( 'FileDuplicateSearch' );
76 $this->linkBatchFactory = $linkBatchFactory;
77 $this->repoGroup = $repoGroup;
78 $this->searchEngineFactory = $searchEngineFactory;
79 $this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() );
80 }
81
87 private function getDupes() {
88 return $this->repoGroup->findBySha1( $this->hash );
89 }
90
94 private function showList( $dupes ) {
95 $html = [];
96 $html[] = "<ol class='special'>";
97
98 foreach ( $dupes as $dupe ) {
99 $line = $this->formatResult( $dupe );
100 $html[] = "<li>" . $line . "</li>";
101 }
102 $html[] = '</ol>';
103
104 $this->getOutput()->addHTML( implode( "\n", $html ) );
105 }
106
107 public function execute( $par ) {
108 $this->setHeaders();
109 $this->outputHeader();
110
111 $this->filename = $par ?? $this->getRequest()->getText( 'filename' );
112 $this->file = null;
113 $this->hash = '';
114 $title = Title::newFromText( $this->filename, NS_FILE );
115 if ( $title && $title->getText() != '' ) {
116 $this->file = $this->repoGroup->findFile( $title );
117 }
118
119 $out = $this->getOutput();
120
121 # Create the input form
122 $formFields = [
123 'filename' => [
124 'type' => 'text',
125 'name' => 'filename',
126 'label-message' => 'fileduplicatesearch-filename',
127 'id' => 'filename',
128 'size' => 50,
129 'default' => $this->filename,
130 ],
131 ];
132 $hiddenFields = [
133 'title' => $this->getPageTitle()->getPrefixedDBkey(),
134 ];
135 $htmlForm = HTMLForm::factory( 'ooui', $formFields, $this->getContext() );
136 $htmlForm->addHiddenFields( $hiddenFields );
137 $htmlForm->setAction( wfScript() );
138 $htmlForm->setMethod( 'get' );
139 $htmlForm->setSubmitTextMsg( $this->msg( 'fileduplicatesearch-submit' ) );
140
141 // The form should be visible always, even if it was submitted (e.g. to perform another action).
142 // To bypass the callback validation of HTMLForm, use prepareForm() and displayForm().
143 $htmlForm->prepareForm()->displayForm( false );
144
145 if ( $this->file ) {
146 $this->hash = $this->file->getSha1();
147 } elseif ( $this->filename !== '' ) {
148 $out->wrapWikiMsg(
149 "<p class='mw-fileduplicatesearch-noresults'>\n$1\n</p>",
150 [ 'fileduplicatesearch-noresults', wfEscapeWikiText( $this->filename ) ]
151 );
152 }
153
154 if ( $this->hash != '' ) {
155 # Show a thumbnail of the file
156 $img = $this->file;
157 if ( $img ) {
158 $thumb = $img->transform( [ 'width' => 120, 'height' => 120 ] );
159 if ( $thumb ) {
160 $out->addModuleStyles( 'mediawiki.special' );
161 $out->addHTML( '<div id="mw-fileduplicatesearch-icon">' .
162 $thumb->toHtml( [ 'desc-link' => false ] ) . '<br />' .
163 $this->msg( 'fileduplicatesearch-info' )->numParams(
164 $img->getWidth(), $img->getHeight() )->params(
165 $this->getLanguage()->formatSize( $img->getSize() ),
166 $img->getMimeType() )->parseAsBlock() .
167 '</div>' );
168 }
169 }
170
171 $dupes = $this->getDupes();
172 $numRows = count( $dupes );
173
174 # Show a short summary
175 if ( $numRows == 1 ) {
176 $out->wrapWikiMsg(
177 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
178 [ 'fileduplicatesearch-result-1', wfEscapeWikiText( $this->filename ) ]
179 );
180 } elseif ( $numRows ) {
181 $out->wrapWikiMsg(
182 "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
183 [ 'fileduplicatesearch-result-n', wfEscapeWikiText( $this->filename ),
184 $this->getLanguage()->formatNum( $numRows - 1 ) ]
185 );
186 }
187
188 $this->doBatchLookups( $dupes );
189 $this->showList( $dupes );
190 }
191 }
192
196 private function doBatchLookups( $list ) {
197 $batch = $this->linkBatchFactory->newLinkBatch();
198 foreach ( $list as $file ) {
199 $batch->addObj( $file->getTitle() );
200 if ( $file->isLocal() ) {
201 $uploader = $file->getUploader( File::FOR_THIS_USER, $this->getAuthority() );
202 if ( $uploader ) {
203 $batch->add( NS_USER, $uploader->getName() );
204 $batch->add( NS_USER_TALK, $uploader->getName() );
205 }
206 }
207 }
208
209 $batch->execute();
210 }
211
216 private function formatResult( $result ) {
218 $nt = $result->getTitle();
219 $text = $this->languageConverter->convert( $nt->getText() );
220 $plink = $linkRenderer->makeLink(
221 $nt,
222 $text
223 );
224
225 $uploader = $result->getUploader( File::FOR_THIS_USER, $this->getAuthority() );
226 if ( $result->isLocal() && $uploader ) {
227 $user = Linker::userLink( $uploader->getId(), $uploader->getName() );
228 $user .= '<span style="white-space: nowrap;">';
229 $user .= Linker::userToolLinks( $uploader->getId(), $uploader->getName() );
230 $user .= '</span>';
231 } elseif ( $uploader ) {
232 $user = htmlspecialchars( $uploader->getName() );
233 } else {
234 $user = '<span class="history-deleted">'
235 . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
236 }
237
238 $time = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
239 $result->getTimestamp(), $this->getUser() ) );
240
241 return "$plink . . $user . . $time";
242 }
243
252 public function prefixSearchSubpages( $search, $limit, $offset ) {
253 $title = Title::newFromText( $search, NS_FILE );
254 if ( !$title || $title->getNamespace() !== NS_FILE ) {
255 // No prefix suggestion outside of file namespace
256 return [];
257 }
258 $searchEngine = $this->searchEngineFactory->create();
259 $searchEngine->setLimitOffset( $limit, $offset );
260 // Autocomplete subpage the same as a normal search, but just for files
261 $searchEngine->setNamespaces( [ NS_FILE ] );
262 $result = $searchEngine->defaultPrefixSearch( $search );
263
264 return array_map( static function ( Title $t ) {
265 // Remove namespace in search suggestion
266 return $t->getText();
267 }, $result );
268 }
269
270 protected function getGroupName() {
271 return 'media';
272 }
273}
const NS_USER
Definition Defines.php:66
const NS_FILE
Definition Defines.php:70
const NS_USER_TALK
Definition Defines.php:67
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:66
isLocal()
Returns true if the file comes from the local file repository.
Definition File.php:2014
getUploader(int $audience=self::FOR_PUBLIC, Authority $performer=null)
Get the identity of the file uploader.
Definition File.php:2284
getTitle()
Return the associated title object.
Definition File.php:360
transform( $params, $flags=0)
Transform a media file.
Definition File.php:1189
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition Linker.php:1064
static userToolLinks( $userId, $userText, $redContribsWhenNoEdits=false, $flags=0, $edits=null, $useParentheses=true)
Generate standard user tool links (talk, contributions, block link, etc.)
Definition Linker.php:1109
An interface for creating language converters.
getLanguageConverter( $language=null)
Provide a LanguageConverter for given language.
makeLink( $target, $text=null, array $extraAttribs=[], array $query=[])
Prioritized list of file repositories.
Definition RepoGroup.php:33
Factory class for SearchEngine.
Searches the database for files of the requested hash, comparing this with the 'img_sha1' field in th...
getDupes()
Fetch dupes from all connected file repositories.
execute( $par)
Default execute method Checks user permissions.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
__construct(LinkBatchFactory $linkBatchFactory, RepoGroup $repoGroup, SearchEngineFactory $searchEngineFactory, LanguageConverterFactory $languageConverterFactory)
string $filename
The form input filename.
File $file
selected reference file, if present
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
Parent class for all special pages.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getContext()
Gets the context this SpecialPage is executed in.
LinkRenderer null $linkRenderer
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getAuthority()
Shortcut to get the Authority executing this instance.
getRequest()
Get the WebRequest being used for this instance.
getPageTitle( $subpage=false)
Get a self-referential title object.
getLanguage()
Shortcut to get user's language.
getContentLanguage()
Shortcut to get content language.
Represents a title within MediaWiki.
Definition Title.php:48
The shared interface for all language converters.
$line
Definition mcc.php:119