3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Specials
;
24 use MediaWiki\Cache\LinkBatchFactory
;
25 use MediaWiki\HTMLForm\HTMLForm
;
26 use MediaWiki\Language\ILanguageConverter
;
27 use MediaWiki\Languages\LanguageConverterFactory
;
28 use MediaWiki\Linker\Linker
;
29 use MediaWiki\SpecialPage\SpecialPage
;
30 use MediaWiki\Title\Title
;
32 use SearchEngineFactory
;
35 * Search the database for files of the requested hash, comparing this with the
36 * 'img_sha1' field in the image table.
38 * @ingroup SpecialPage
39 * @author Raimond Spekking, based on Special:MIMESearch by Ævar Arnfjörð Bjarmason
41 class SpecialFileDuplicateSearch
extends SpecialPage
{
43 * @var string The form input hash
48 * @var string The form input filename
50 private $filename = '';
53 * @var File|null selected reference file, if present
57 private LinkBatchFactory
$linkBatchFactory;
58 private RepoGroup
$repoGroup;
59 private SearchEngineFactory
$searchEngineFactory;
60 private ILanguageConverter
$languageConverter;
63 * @param LinkBatchFactory $linkBatchFactory
64 * @param RepoGroup $repoGroup
65 * @param SearchEngineFactory $searchEngineFactory
66 * @param LanguageConverterFactory $languageConverterFactory
68 public function __construct(
69 LinkBatchFactory
$linkBatchFactory,
71 SearchEngineFactory
$searchEngineFactory,
72 LanguageConverterFactory
$languageConverterFactory
74 parent
::__construct( 'FileDuplicateSearch' );
75 $this->linkBatchFactory
= $linkBatchFactory;
76 $this->repoGroup
= $repoGroup;
77 $this->searchEngineFactory
= $searchEngineFactory;
78 $this->languageConverter
= $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() );
82 * Fetch dupes from all connected file repositories.
86 private function getDupes() {
87 return $this->repoGroup
->findBySha1( $this->hash
);
91 * @param File[] $dupes
93 private function showList( $dupes ) {
95 $html[] = "<ol class='special'>";
97 foreach ( $dupes as $dupe ) {
98 $line = $this->formatResult( $dupe );
99 $html[] = "<li>" . $line . "</li>";
103 $this->getOutput()->addHTML( implode( "\n", $html ) );
106 public function execute( $par ) {
108 $this->outputHeader();
110 $this->filename
= $par ??
$this->getRequest()->getText( 'filename' );
113 $title = Title
::newFromText( $this->filename
, NS_FILE
);
114 if ( $title && $title->getText() != '' ) {
115 $this->file
= $this->repoGroup
->findFile( $title );
118 $out = $this->getOutput();
120 # Create the input form
124 'name' => 'filename',
125 'label-message' => 'fileduplicatesearch-filename',
128 'default' => $this->filename
,
131 $htmlForm = HTMLForm
::factory( 'ooui', $formFields, $this->getContext() );
132 $htmlForm->setTitle( $this->getPageTitle() );
133 $htmlForm->setMethod( 'get' );
134 $htmlForm->setSubmitTextMsg( $this->msg( 'fileduplicatesearch-submit' ) );
136 // The form should be visible always, even if it was submitted (e.g. to perform another action).
137 // To bypass the callback validation of HTMLForm, use prepareForm() and displayForm().
138 $htmlForm->prepareForm()->displayForm( false );
141 $this->hash
= $this->file
->getSha1();
142 } elseif ( $this->filename
!== '' ) {
144 "<p class='mw-fileduplicatesearch-noresults'>\n$1\n</p>",
145 [ 'fileduplicatesearch-noresults', wfEscapeWikiText( $this->filename
) ]
149 if ( $this->hash
!= '' ) {
150 # Show a thumbnail of the file
153 $thumb = $img->transform( [ 'width' => 120, 'height' => 120 ] );
155 $out->addModuleStyles( 'mediawiki.special' );
156 $out->addHTML( '<div id="mw-fileduplicatesearch-icon">' .
157 $thumb->toHtml( [ 'desc-link' => false ] ) . '<br />' .
158 $this->msg( 'fileduplicatesearch-info' )
159 ->numParams( $img->getWidth(), $img->getHeight() )
160 ->sizeParams( $img->getSize() )
161 ->params( $img->getMimeType() )->parseAsBlock() .
166 $dupes = $this->getDupes();
167 $numRows = count( $dupes );
169 # Show a short summary
170 if ( $numRows == 1 ) {
172 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
173 [ 'fileduplicatesearch-result-1', wfEscapeWikiText( $this->filename
) ]
175 } elseif ( $numRows ) {
177 "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
178 [ 'fileduplicatesearch-result-n', wfEscapeWikiText( $this->filename
),
179 $this->getLanguage()->formatNum( $numRows - 1 ) ]
183 $this->doBatchLookups( $dupes );
184 $this->showList( $dupes );
189 * @param File[] $list
191 private function doBatchLookups( $list ) {
192 $batch = $this->linkBatchFactory
->newLinkBatch();
193 foreach ( $list as $file ) {
194 $batch->addObj( $file->getTitle() );
195 if ( $file->isLocal() ) {
196 $uploader = $file->getUploader( File
::FOR_THIS_USER
, $this->getAuthority() );
198 $batch->add( NS_USER
, $uploader->getName() );
199 $batch->add( NS_USER_TALK
, $uploader->getName() );
208 * @param File $result
209 * @return string HTML
211 private function formatResult( $result ) {
212 $linkRenderer = $this->getLinkRenderer();
213 $nt = $result->getTitle();
214 $text = $this->languageConverter
->convert( $nt->getText() );
215 $plink = $linkRenderer->makeLink(
220 $uploader = $result->getUploader( File
::FOR_THIS_USER
, $this->getAuthority() );
221 if ( $result->isLocal() && $uploader ) {
222 $user = Linker
::userLink( $uploader->getId(), $uploader->getName() );
223 $user .= '<span style="white-space: nowrap;">';
224 $user .= Linker
::userToolLinks( $uploader->getId(), $uploader->getName() );
226 } elseif ( $uploader ) {
227 $user = htmlspecialchars( $uploader->getName() );
229 $user = '<span class="history-deleted">'
230 . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
233 $time = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
234 $result->getTimestamp(), $this->getUser() ) );
236 return "$plink . . $user . . $time";
240 * Return an array of subpages beginning with $search that this special page will accept.
242 * @param string $search Prefix to search for
243 * @param int $limit Maximum number of results to return (usually 10)
244 * @param int $offset Number of results to skip (usually 0)
245 * @return string[] Matching subpages
247 public function prefixSearchSubpages( $search, $limit, $offset ) {
248 $title = Title
::newFromText( $search, NS_FILE
);
249 if ( !$title ||
$title->getNamespace() !== NS_FILE
) {
250 // No prefix suggestion outside of file namespace
253 $searchEngine = $this->searchEngineFactory
->create();
254 $searchEngine->setLimitOffset( $limit, $offset );
255 // Autocomplete subpage the same as a normal search, but just for files
256 $searchEngine->setNamespaces( [ NS_FILE
] );
257 $result = $searchEngine->defaultPrefixSearch( $search );
259 return array_map( static function ( Title
$t ) {
260 // Remove namespace in search suggestion
261 return $t->getText();
265 protected function getGroupName() {
270 /** @deprecated class alias since 1.41 */
271 class_alias( SpecialFileDuplicateSearch
::class, 'SpecialFileDuplicateSearch' );