MediaWiki  master
SpecialFileDuplicateSearch.php
Go to the documentation of this file.
1 <?php
2 
29 
37  protected $hash = '', $filename = '';
38 
42  protected $file = null;
43 
46 
48  private $repoGroup;
49 
52 
55 
63  public function __construct(
68  LanguageConverterFactory $languageConverterFactory
69  ) {
70  parent::__construct( 'FileDuplicateSearch' );
71  $this->linkBatchFactory = $linkBatchFactory;
72  $this->repoGroup = $repoGroup;
73  $this->searchEngineFactory = $searchEngineFactory;
74  $this->setDBLoadBalancer( $loadBalancer );
75  $this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() );
76  }
77 
78  public function isSyndicated() {
79  return false;
80  }
81 
82  public function isCacheable() {
83  return false;
84  }
85 
86  public function isCached() {
87  return false;
88  }
89 
90  protected function linkParameters() {
91  return [ 'filename' => $this->filename ];
92  }
93 
99  private function getDupes() {
100  return $this->repoGroup->findBySha1( $this->hash );
101  }
102 
106  private function showList( $dupes ) {
107  $html = [];
108  $html[] = $this->openList( 0 );
109 
110  foreach ( $dupes as $dupe ) {
111  $line = $this->formatResult( null, $dupe );
112  $html[] = "<li>" . $line . "</li>";
113  }
114  $html[] = $this->closeList();
115 
116  $this->getOutput()->addHTML( implode( "\n", $html ) );
117  }
118 
119  public function getQueryInfo() {
120  $imgQuery = LocalFile::getQueryInfo();
121  return [
122  'tables' => $imgQuery['tables'],
123  'fields' => [
124  'title' => 'img_name',
125  'value' => 'img_sha1',
126  'img_user_text' => $imgQuery['fields']['img_user_text'],
127  'img_timestamp'
128  ],
129  'conds' => [ 'img_sha1' => $this->hash ],
130  'join_conds' => $imgQuery['joins'],
131  ];
132  }
133 
134  public function execute( $par ) {
135  $this->setHeaders();
136  $this->outputHeader();
137 
138  $this->filename = $par ?? $this->getRequest()->getText( 'filename' );
139  $this->file = null;
140  $this->hash = '';
141  $title = Title::newFromText( $this->filename, NS_FILE );
142  if ( $title && $title->getText() != '' ) {
143  $this->file = $this->repoGroup->findFile( $title );
144  }
145 
146  $out = $this->getOutput();
147 
148  # Create the input form
149  $formFields = [
150  'filename' => [
151  'type' => 'text',
152  'name' => 'filename',
153  'label-message' => 'fileduplicatesearch-filename',
154  'id' => 'filename',
155  'size' => 50,
156  'default' => $this->filename,
157  ],
158  ];
159  $hiddenFields = [
160  'title' => $this->getPageTitle()->getPrefixedDBkey(),
161  ];
162  $htmlForm = HTMLForm::factory( 'ooui', $formFields, $this->getContext() );
163  $htmlForm->addHiddenFields( $hiddenFields );
164  $htmlForm->setAction( wfScript() );
165  $htmlForm->setMethod( 'get' );
166  $htmlForm->setSubmitTextMsg( $this->msg( 'fileduplicatesearch-submit' ) );
167 
168  // The form should be visible always, even if it was submitted (e.g. to perform another action).
169  // To bypass the callback validation of HTMLForm, use prepareForm() and displayForm().
170  $htmlForm->prepareForm()->displayForm( false );
171 
172  if ( $this->file ) {
173  $this->hash = $this->file->getSha1();
174  } elseif ( $this->filename !== '' ) {
175  $out->wrapWikiMsg(
176  "<p class='mw-fileduplicatesearch-noresults'>\n$1\n</p>",
177  [ 'fileduplicatesearch-noresults', wfEscapeWikiText( $this->filename ) ]
178  );
179  }
180 
181  if ( $this->hash != '' ) {
182  # Show a thumbnail of the file
183  $img = $this->file;
184  if ( $img ) {
185  $thumb = $img->transform( [ 'width' => 120, 'height' => 120 ] );
186  if ( $thumb ) {
187  $out->addModuleStyles( 'mediawiki.special' );
188  $out->addHTML( '<div id="mw-fileduplicatesearch-icon">' .
189  $thumb->toHtml( [ 'desc-link' => false ] ) . '<br />' .
190  $this->msg( 'fileduplicatesearch-info' )->numParams(
191  $img->getWidth(), $img->getHeight() )->params(
192  $this->getLanguage()->formatSize( $img->getSize() ),
193  $img->getMimeType() )->parseAsBlock() .
194  '</div>' );
195  }
196  }
197 
198  $dupes = $this->getDupes();
199  $numRows = count( $dupes );
200 
201  # Show a short summary
202  if ( $numRows == 1 ) {
203  $out->wrapWikiMsg(
204  "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
205  [ 'fileduplicatesearch-result-1', wfEscapeWikiText( $this->filename ) ]
206  );
207  } elseif ( $numRows ) {
208  $out->wrapWikiMsg(
209  "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
210  [ 'fileduplicatesearch-result-n', wfEscapeWikiText( $this->filename ),
211  $this->getLanguage()->formatNum( $numRows - 1 ) ]
212  );
213  }
214 
215  $this->doBatchLookups( $dupes );
216  $this->showList( $dupes );
217  }
218  }
219 
223  private function doBatchLookups( $list ) {
224  $batch = $this->linkBatchFactory->newLinkBatch();
225  foreach ( $list as $file ) {
226  $batch->addObj( $file->getTitle() );
227  if ( $file->isLocal() ) {
228  $userName = $file->getUser( 'text' );
229  $batch->add( NS_USER, $userName );
230  $batch->add( NS_USER_TALK, $userName );
231  }
232  }
233 
234  $batch->execute();
235  }
236 
243  public function formatResult( $skin, $result ) {
244  $linkRenderer = $this->getLinkRenderer();
245  $nt = $result->getTitle();
246  $text = $this->languageConverter->convert( $nt->getText() );
247  $plink = $linkRenderer->makeLink(
248  $nt,
249  $text
250  );
251 
252  $userText = $result->getUser( 'text' );
253  if ( $result->isLocal() ) {
254  $userId = $result->getUser( 'id' );
255  $user = Linker::userLink( $userId, $userText );
256  $user .= '<span style="white-space: nowrap;">';
257  $user .= Linker::userToolLinks( $userId, $userText );
258  $user .= '</span>';
259  } else {
260  $user = htmlspecialchars( $userText );
261  }
262 
263  $time = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
264  $result->getTimestamp(), $this->getUser() ) );
265 
266  return "$plink . . $user . . $time";
267  }
268 
277  public function prefixSearchSubpages( $search, $limit, $offset ) {
278  $title = Title::newFromText( $search, NS_FILE );
279  if ( !$title || $title->getNamespace() !== NS_FILE ) {
280  // No prefix suggestion outside of file namespace
281  return [];
282  }
283  $searchEngine = $this->searchEngineFactory->create();
284  $searchEngine->setLimitOffset( $limit, $offset );
285  // Autocomplete subpage the same as a normal search, but just for files
286  $searchEngine->setNamespaces( [ NS_FILE ] );
287  $result = $searchEngine->defaultPrefixSearch( $search );
288 
289  return array_map( function ( Title $t ) {
290  // Remove namespace in search suggestion
291  return $t->getText();
292  }, $result );
293  }
294 
295  protected function getGroupName() {
296  return 'media';
297  }
298 }
SpecialPage\$linkRenderer
LinkRenderer null $linkRenderer
Definition: SpecialPage.php:79
SpecialFileDuplicateSearch\getDupes
getDupes()
Fetch dupes from all connected file repositories.
Definition: SpecialFileDuplicateSearch.php:99
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:742
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:900
SpecialFileDuplicateSearch\formatResult
formatResult( $skin, $result)
Definition: SpecialFileDuplicateSearch.php:243
SpecialFileDuplicateSearch\$languageConverter
ILanguageConverter $languageConverter
Definition: SpecialFileDuplicateSearch.php:54
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:328
Linker\userLink
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:896
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:788
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:941
File\getUser
getUser( $type='text')
Returns ID or name of user who uploaded the file STUB.
Definition: File.php:599
SpecialFileDuplicateSearch\isCached
isCached()
Whether or not the output of the page in question is retrieved from the database cache.
Definition: SpecialFileDuplicateSearch.php:86
NS_FILE
const NS_FILE
Definition: Defines.php:75
SpecialFileDuplicateSearch\$searchEngineFactory
SearchEngineFactory $searchEngineFactory
Definition: SpecialFileDuplicateSearch.php:51
QueryPage\openList
openList( $offset)
Definition: QueryPage.php:823
SearchEngineFactory
Factory class for SearchEngine.
Definition: SearchEngineFactory.php:13
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:818
SpecialFileDuplicateSearch\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialFileDuplicateSearch.php:295
QueryPage
This is a class for doing query pages; since they're almost all the same, we factor out some of the f...
Definition: QueryPage.php:41
MediaWiki\Languages\LanguageConverterFactory
An interface for creating language converters.
Definition: LanguageConverterFactory.php:44
SpecialFileDuplicateSearch
Searches the database for files of the requested hash, comparing this with the 'img_sha1' field in th...
Definition: SpecialFileDuplicateSearch.php:36
QueryPage\$offset
int $offset
The offset and limit in use, as passed to the query() function.
Definition: QueryPage.php:46
QueryPage\$limit
int $limit
Definition: QueryPage.php:49
QueryPage\closeList
closeList()
Definition: QueryPage.php:830
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:63
SpecialFileDuplicateSearch\linkParameters
linkParameters()
If using extra form wheely-dealies, return a set of parameters here as an associative array.
Definition: SpecialFileDuplicateSearch.php:90
MediaWiki\Languages\LanguageConverterFactory\getLanguageConverter
getLanguageConverter( $language=null)
Provide a LanguageConverter for given language.
Definition: LanguageConverterFactory.php:121
wfScript
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Definition: GlobalFunctions.php:2515
MediaWiki\Cache\LinkBatchFactory
Definition: LinkBatchFactory.php:38
SpecialFileDuplicateSearch\execute
execute( $par)
This is the actual workhorse.
Definition: SpecialFileDuplicateSearch.php:134
$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:616
SpecialFileDuplicateSearch\$filename
$filename
Definition: SpecialFileDuplicateSearch.php:37
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:762
SpecialFileDuplicateSearch\__construct
__construct(LinkBatchFactory $linkBatchFactory, RepoGroup $repoGroup, SearchEngineFactory $searchEngineFactory, ILoadBalancer $loadBalancer, LanguageConverterFactory $languageConverterFactory)
Definition: SpecialFileDuplicateSearch.php:63
QueryPage\$numRows
int $numRows
The number of rows returned by the query.
Definition: QueryPage.php:58
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:72
ILanguageConverter
The shared interface for all language converters.
Definition: ILanguageConverter.php:28
$line
$line
Definition: mcc.php:119
SpecialFileDuplicateSearch\getQueryInfo
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
Definition: SpecialFileDuplicateSearch.php:119
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:778
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1494
QueryPage\setDBLoadBalancer
setDBLoadBalancer(ILoadBalancer $loadBalancer)
Definition: QueryPage.php:873
SpecialFileDuplicateSearch\isSyndicated
isSyndicated()
Sometime we don't want to build rss / atom feeds.
Definition: SpecialFileDuplicateSearch.php:78
SpecialFileDuplicateSearch\showList
showList( $dupes)
Definition: SpecialFileDuplicateSearch.php:106
SpecialFileDuplicateSearch\$linkBatchFactory
LinkBatchFactory $linkBatchFactory
Definition: SpecialFileDuplicateSearch.php:45
File\transform
transform( $params, $flags=0)
Transform a media file Stable to override.
Definition: File.php:1127
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:1016
File\getTitle
getTitle()
Return the associated title object.
Definition: File.php:345
LocalFile\getQueryInfo
static getQueryInfo(array $options=[])
Return the tables, fields, and join conditions to be selected to create a new localfile object.
Definition: LocalFile.php:227
Title
Represents a title within MediaWiki.
Definition: Title.php:41
SpecialFileDuplicateSearch\$file
File $file
selected reference file, if present
Definition: SpecialFileDuplicateSearch.php:42
SpecialFileDuplicateSearch\prefixSearchSubpages
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
Definition: SpecialFileDuplicateSearch.php:277
File\isLocal
isLocal()
Returns true if the file comes from the local file repository.
Definition: File.php:1927
SpecialFileDuplicateSearch\isCacheable
isCacheable()
Is the output of this query cacheable? Non-cacheable expensive pages will be disabled in miser mode a...
Definition: SpecialFileDuplicateSearch.php:82
RepoGroup
Prioritized list of file repositories.
Definition: RepoGroup.php:31
NS_USER
const NS_USER
Definition: Defines.php:71
SpecialFileDuplicateSearch\$repoGroup
RepoGroup $repoGroup
Definition: SpecialFileDuplicateSearch.php:48
QueryPage\$loadBalancer
ILoadBalancer null $loadBalancer
Definition: QueryPage.php:71
SpecialFileDuplicateSearch\$hash
$hash
Definition: SpecialFileDuplicateSearch.php:37
SpecialPage\getContentLanguage
getContentLanguage()
Shortcut to get content language.
Definition: SpecialPage.php:828
$t
$t
Definition: testCompression.php:74
SpecialFileDuplicateSearch\doBatchLookups
doBatchLookups( $list)
Definition: SpecialFileDuplicateSearch.php:223
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:316
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:707
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81