MediaWiki  master
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 ) {
217  $linkRenderer = $this->getLinkRenderer();
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 }
SpecialPage\$linkRenderer
LinkRenderer null $linkRenderer
Definition: SpecialPage.php:80
SpecialFileDuplicateSearch\getDupes
getDupes()
Fetch dupes from all connected file repositories.
Definition: SpecialFileDuplicateSearch.php:87
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:744
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:912
SpecialFileDuplicateSearch\$languageConverter
ILanguageConverter $languageConverter
Definition: SpecialFileDuplicateSearch.php:61
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:382
Linker\userLink
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:1064
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:790
File\getUploader
getUploader(int $audience=self::FOR_PUBLIC, Authority $performer=null)
Get the identity of the file uploader.
Definition: File.php:2284
Linker\userToolLinks
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
SpecialFileDuplicateSearch\$searchEngineFactory
SearchEngineFactory $searchEngineFactory
Definition: SpecialFileDuplicateSearch.php:58
SearchEngineFactory
Factory class for SearchEngine.
Definition: SearchEngineFactory.php:12
SpecialPage\getAuthority
getAuthority()
Shortcut to get the Authority executing this instance.
Definition: SpecialPage.php:810
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:830
SpecialFileDuplicateSearch\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialFileDuplicateSearch.php:270
MediaWiki\Languages\LanguageConverterFactory
An interface for creating language converters.
Definition: LanguageConverterFactory.php:46
SpecialFileDuplicateSearch
Searches the database for files of the requested hash, comparing this with the 'img_sha1' field in th...
Definition: SpecialFileDuplicateSearch.php:35
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:66
SpecialFileDuplicateSearch\__construct
__construct(LinkBatchFactory $linkBatchFactory, RepoGroup $repoGroup, SearchEngineFactory $searchEngineFactory, LanguageConverterFactory $languageConverterFactory)
Definition: SpecialFileDuplicateSearch.php:69
MediaWiki\Languages\LanguageConverterFactory\getLanguageConverter
getLanguageConverter( $language=null)
Provide a LanguageConverter for given language.
Definition: LanguageConverterFactory.php:125
wfScript
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Definition: GlobalFunctions.php:2285
MediaWiki\Cache\LinkBatchFactory
Definition: LinkBatchFactory.php:39
SpecialFileDuplicateSearch\$filename
string $filename
The form input filename.
Definition: SpecialFileDuplicateSearch.php:44
SpecialFileDuplicateSearch\execute
execute( $par)
Default execute method Checks user permissions.
Definition: SpecialFileDuplicateSearch.php:107
$title
$title
Definition: testCompression.php:38
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:618
File\FOR_THIS_USER
const FOR_THIS_USER
Definition: File.php:87
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:764
ILanguageConverter
The shared interface for all language converters.
Definition: ILanguageConverter.php:29
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:43
$line
$line
Definition: mcc.php:119
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:780
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1459
NS_USER
const NS_USER
Definition: Defines.php:66
SpecialFileDuplicateSearch\showList
showList( $dupes)
Definition: SpecialFileDuplicateSearch.php:94
SpecialFileDuplicateSearch\$linkBatchFactory
LinkBatchFactory $linkBatchFactory
Definition: SpecialFileDuplicateSearch.php:52
File\transform
transform( $params, $flags=0)
Transform a media file.
Definition: File.php:1189
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:1028
File\getTitle
getTitle()
Return the associated title object.
Definition: File.php:360
Title
Represents a title within MediaWiki.
Definition: Title.php:47
SpecialFileDuplicateSearch\$hash
string $hash
The form input hash.
Definition: SpecialFileDuplicateSearch.php:39
SpecialFileDuplicateSearch\prefixSearchSubpages
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
Definition: SpecialFileDuplicateSearch.php:252
File\isLocal
isLocal()
Returns true if the file comes from the local file repository.
Definition: File.php:2014
SpecialFileDuplicateSearch\$file
File null $file
selected reference file, if present
Definition: SpecialFileDuplicateSearch.php:49
RepoGroup
Prioritized list of file repositories.
Definition: RepoGroup.php:32
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:67
SpecialFileDuplicateSearch\$repoGroup
RepoGroup $repoGroup
Definition: SpecialFileDuplicateSearch.php:55
SpecialPage\getContentLanguage
getContentLanguage()
Shortcut to get content language.
Definition: SpecialPage.php:840
$t
$t
Definition: testCompression.php:74
NS_FILE
const NS_FILE
Definition: Defines.php:70
SpecialFileDuplicateSearch\doBatchLookups
doBatchLookups( $list)
Definition: SpecialFileDuplicateSearch.php:196
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:326
SpecialPage\outputHeader
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
Definition: SpecialPage.php:709
SpecialFileDuplicateSearch\formatResult
formatResult( $result)
Definition: SpecialFileDuplicateSearch.php:216