2008-03-19 16:58:56 +00:00
|
|
|
<?php
|
2019-05-28 14:04:23 +00:00
|
|
|
|
2010-06-21 12:59:04 +00:00
|
|
|
/**
|
2010-08-14 19:19:41 +00:00
|
|
|
* Implements Special:FileDuplicateSearch
|
2010-06-21 12:59:04 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2010-06-21 13:16:32 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2010-06-21 12:59:04 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2008-03-19 16:58:56 +00:00
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup SpecialPage
|
2008-03-19 16:58:56 +00:00
|
|
|
* @author Raimond Spekking, based on Special:MIMESearch by Ævar Arnfjörð Bjarmason
|
|
|
|
|
*/
|
|
|
|
|
|
2020-10-16 17:52:27 +00:00
|
|
|
use MediaWiki\Cache\LinkBatchFactory;
|
2020-10-29 19:58:32 +00:00
|
|
|
use MediaWiki\Languages\LanguageConverterFactory;
|
2020-10-16 17:52:27 +00:00
|
|
|
|
2008-03-19 16:58:56 +00:00
|
|
|
/**
|
|
|
|
|
* Searches the database for files of the requested hash, comparing this with the
|
|
|
|
|
* 'img_sha1' field in the image table.
|
2010-08-14 19:19:41 +00:00
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup SpecialPage
|
2008-03-19 16:58:56 +00:00
|
|
|
*/
|
2021-04-08 03:57:15 +00:00
|
|
|
class SpecialFileDuplicateSearch extends SpecialPage {
|
|
|
|
|
/**
|
|
|
|
|
* @var string The form input hash
|
|
|
|
|
*/
|
|
|
|
|
private $hash = '';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string The form input filename
|
|
|
|
|
*/
|
|
|
|
|
private $filename = '';
|
2011-02-07 02:35:35 +00:00
|
|
|
|
|
|
|
|
/**
|
2021-09-22 20:16:51 +00:00
|
|
|
* @var File|null selected reference file, if present
|
2011-02-07 02:35:35 +00:00
|
|
|
*/
|
2021-04-08 03:57:15 +00:00
|
|
|
private $file = null;
|
2008-03-19 16:58:56 +00:00
|
|
|
|
2020-10-16 17:52:27 +00:00
|
|
|
/** @var LinkBatchFactory */
|
|
|
|
|
private $linkBatchFactory;
|
|
|
|
|
|
|
|
|
|
/** @var RepoGroup */
|
|
|
|
|
private $repoGroup;
|
|
|
|
|
|
|
|
|
|
/** @var SearchEngineFactory */
|
|
|
|
|
private $searchEngineFactory;
|
|
|
|
|
|
2020-10-29 19:58:32 +00:00
|
|
|
/** @var ILanguageConverter */
|
|
|
|
|
private $languageConverter;
|
|
|
|
|
|
2020-10-16 17:52:27 +00:00
|
|
|
/**
|
|
|
|
|
* @param LinkBatchFactory $linkBatchFactory
|
|
|
|
|
* @param RepoGroup $repoGroup
|
|
|
|
|
* @param SearchEngineFactory $searchEngineFactory
|
2020-10-29 19:58:32 +00:00
|
|
|
* @param LanguageConverterFactory $languageConverterFactory
|
2020-10-16 17:52:27 +00:00
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
LinkBatchFactory $linkBatchFactory,
|
|
|
|
|
RepoGroup $repoGroup,
|
2020-10-21 19:33:52 +00:00
|
|
|
SearchEngineFactory $searchEngineFactory,
|
2020-10-29 19:58:32 +00:00
|
|
|
LanguageConverterFactory $languageConverterFactory
|
2020-10-16 17:52:27 +00:00
|
|
|
) {
|
|
|
|
|
parent::__construct( 'FileDuplicateSearch' );
|
|
|
|
|
$this->linkBatchFactory = $linkBatchFactory;
|
|
|
|
|
$this->repoGroup = $repoGroup;
|
|
|
|
|
$this->searchEngineFactory = $searchEngineFactory;
|
2020-10-29 19:58:32 +00:00
|
|
|
$this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() );
|
2008-03-19 16:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
2011-02-07 02:35:35 +00:00
|
|
|
/**
|
|
|
|
|
* Fetch dupes from all connected file repositories.
|
|
|
|
|
*
|
2020-10-28 10:01:33 +00:00
|
|
|
* @return File[]
|
2011-02-07 02:35:35 +00:00
|
|
|
*/
|
2020-05-18 01:16:17 +00:00
|
|
|
private function getDupes() {
|
2020-10-16 17:52:27 +00:00
|
|
|
return $this->repoGroup->findBySha1( $this->hash );
|
2011-02-07 02:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-10-28 10:01:33 +00:00
|
|
|
* @param File[] $dupes
|
2011-02-07 02:35:35 +00:00
|
|
|
*/
|
2020-05-18 01:16:17 +00:00
|
|
|
private function showList( $dupes ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$html = [];
|
2021-04-08 03:57:15 +00:00
|
|
|
$html[] = "<ol class='special'>";
|
2011-02-07 02:35:35 +00:00
|
|
|
|
2011-02-18 01:10:38 +00:00
|
|
|
foreach ( $dupes as $dupe ) {
|
2021-04-08 03:57:15 +00:00
|
|
|
$line = $this->formatResult( $dupe );
|
2011-02-07 02:35:35 +00:00
|
|
|
$html[] = "<li>" . $line . "</li>";
|
|
|
|
|
}
|
2021-04-08 03:57:15 +00:00
|
|
|
$html[] = '</ol>';
|
2011-02-07 02:35:35 +00:00
|
|
|
|
2016-03-18 13:55:54 +00:00
|
|
|
$this->getOutput()->addHTML( implode( "\n", $html ) );
|
2011-02-07 02:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-20 13:02:54 +00:00
|
|
|
public function execute( $par ) {
|
2010-12-22 14:16:25 +00:00
|
|
|
$this->setHeaders();
|
|
|
|
|
$this->outputHeader();
|
2011-03-18 20:37:11 +00:00
|
|
|
|
2018-06-20 05:26:57 +00:00
|
|
|
$this->filename = $par ?? $this->getRequest()->getText( 'filename' );
|
2011-02-07 02:35:35 +00:00
|
|
|
$this->file = null;
|
2010-12-22 14:16:25 +00:00
|
|
|
$this->hash = '';
|
2011-02-07 02:35:35 +00:00
|
|
|
$title = Title::newFromText( $this->filename, NS_FILE );
|
2013-04-14 17:27:14 +00:00
|
|
|
if ( $title && $title->getText() != '' ) {
|
2020-10-16 17:52:27 +00:00
|
|
|
$this->file = $this->repoGroup->findFile( $title );
|
2010-12-22 14:16:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-08-05 14:58:37 +00:00
|
|
|
$out = $this->getOutput();
|
|
|
|
|
|
2010-12-22 14:16:25 +00:00
|
|
|
# Create the input form
|
2016-02-17 09:09:32 +00:00
|
|
|
$formFields = [
|
|
|
|
|
'filename' => [
|
2014-10-31 12:05:32 +00:00
|
|
|
'type' => 'text',
|
|
|
|
|
'name' => 'filename',
|
|
|
|
|
'label-message' => 'fileduplicatesearch-filename',
|
|
|
|
|
'id' => 'filename',
|
|
|
|
|
'size' => 50,
|
2018-07-25 19:38:16 +00:00
|
|
|
'default' => $this->filename,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
$hiddenFields = [
|
2016-03-19 00:08:06 +00:00
|
|
|
'title' => $this->getPageTitle()->getPrefixedDBkey(),
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-02-28 11:19:03 +00:00
|
|
|
$htmlForm = HTMLForm::factory( 'ooui', $formFields, $this->getContext() );
|
2014-10-31 12:05:32 +00:00
|
|
|
$htmlForm->addHiddenFields( $hiddenFields );
|
|
|
|
|
$htmlForm->setAction( wfScript() );
|
|
|
|
|
$htmlForm->setMethod( 'get' );
|
|
|
|
|
$htmlForm->setSubmitTextMsg( $this->msg( 'fileduplicatesearch-submit' ) );
|
|
|
|
|
|
|
|
|
|
// The form should be visible always, even if it was submitted (e.g. to perform another action).
|
|
|
|
|
// To bypass the callback validation of HTMLForm, use prepareForm() and displayForm().
|
|
|
|
|
$htmlForm->prepareForm()->displayForm( false );
|
2010-12-22 14:16:25 +00:00
|
|
|
|
2013-04-14 17:27:14 +00:00
|
|
|
if ( $this->file ) {
|
2011-02-07 02:35:35 +00:00
|
|
|
$this->hash = $this->file->getSha1();
|
2013-04-14 17:27:14 +00:00
|
|
|
} elseif ( $this->filename !== '' ) {
|
2011-08-05 14:58:37 +00:00
|
|
|
$out->wrapWikiMsg(
|
2011-02-07 02:35:35 +00:00
|
|
|
"<p class='mw-fileduplicatesearch-noresults'>\n$1\n</p>",
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'fileduplicatesearch-noresults', wfEscapeWikiText( $this->filename ) ]
|
2011-02-07 02:35:35 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-14 17:27:14 +00:00
|
|
|
if ( $this->hash != '' ) {
|
2010-12-22 14:16:25 +00:00
|
|
|
# Show a thumbnail of the file
|
2011-02-07 02:35:35 +00:00
|
|
|
$img = $this->file;
|
2010-12-22 14:16:25 +00:00
|
|
|
if ( $img ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$thumb = $img->transform( [ 'width' => 120, 'height' => 120 ] );
|
2013-04-14 17:27:14 +00:00
|
|
|
if ( $thumb ) {
|
2015-10-10 06:53:32 +00:00
|
|
|
$out->addModuleStyles( 'mediawiki.special' );
|
2011-09-07 05:23:16 +00:00
|
|
|
$out->addHTML( '<div id="mw-fileduplicatesearch-icon">' .
|
2016-02-17 09:09:32 +00:00
|
|
|
$thumb->toHtml( [ 'desc-link' => false ] ) . '<br />' .
|
2011-12-24 17:03:59 +00:00
|
|
|
$this->msg( 'fileduplicatesearch-info' )->numParams(
|
|
|
|
|
$img->getWidth(), $img->getHeight() )->params(
|
2013-04-14 17:27:14 +00:00
|
|
|
$this->getLanguage()->formatSize( $img->getSize() ),
|
|
|
|
|
$img->getMimeType() )->parseAsBlock() .
|
2010-12-22 14:16:25 +00:00
|
|
|
'</div>' );
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-03-19 16:58:56 +00:00
|
|
|
|
2011-02-07 02:35:35 +00:00
|
|
|
$dupes = $this->getDupes();
|
|
|
|
|
$numRows = count( $dupes );
|
2010-12-22 14:16:25 +00:00
|
|
|
|
|
|
|
|
# Show a short summary
|
2013-04-14 17:27:14 +00:00
|
|
|
if ( $numRows == 1 ) {
|
2011-08-05 14:58:37 +00:00
|
|
|
$out->wrapWikiMsg(
|
2010-12-22 14:16:25 +00:00
|
|
|
"<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'fileduplicatesearch-result-1', wfEscapeWikiText( $this->filename ) ]
|
2010-12-22 14:16:25 +00:00
|
|
|
);
|
2011-02-07 02:35:35 +00:00
|
|
|
} elseif ( $numRows ) {
|
2011-08-05 14:58:37 +00:00
|
|
|
$out->wrapWikiMsg(
|
2010-12-22 14:16:25 +00:00
|
|
|
"<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'fileduplicatesearch-result-n', wfEscapeWikiText( $this->filename ),
|
|
|
|
|
$this->getLanguage()->formatNum( $numRows - 1 ) ]
|
2010-12-22 14:16:25 +00:00
|
|
|
);
|
|
|
|
|
}
|
2011-02-07 02:35:35 +00:00
|
|
|
|
2012-07-06 21:48:08 +00:00
|
|
|
$this->doBatchLookups( $dupes );
|
2011-02-07 02:35:35 +00:00
|
|
|
$this->showList( $dupes );
|
2010-12-22 14:16:25 +00:00
|
|
|
}
|
2008-03-19 16:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
2020-10-28 10:01:33 +00:00
|
|
|
/**
|
|
|
|
|
* @param File[] $list
|
|
|
|
|
*/
|
2020-05-18 01:16:17 +00:00
|
|
|
private function doBatchLookups( $list ) {
|
2020-10-16 17:52:27 +00:00
|
|
|
$batch = $this->linkBatchFactory->newLinkBatch();
|
2013-04-14 17:27:14 +00:00
|
|
|
foreach ( $list as $file ) {
|
2012-07-06 21:48:08 +00:00
|
|
|
$batch->addObj( $file->getTitle() );
|
2013-04-14 17:27:14 +00:00
|
|
|
if ( $file->isLocal() ) {
|
2021-05-27 16:56:43 +00:00
|
|
|
$uploader = $file->getUploader( File::FOR_THIS_USER, $this->getAuthority() );
|
|
|
|
|
if ( $uploader ) {
|
|
|
|
|
$batch->add( NS_USER, $uploader->getName() );
|
|
|
|
|
$batch->add( NS_USER_TALK, $uploader->getName() );
|
|
|
|
|
}
|
2012-07-06 21:48:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-04-14 17:27:14 +00:00
|
|
|
|
2012-07-06 21:48:08 +00:00
|
|
|
$batch->execute();
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-07 02:35:35 +00:00
|
|
|
/**
|
|
|
|
|
* @param File $result
|
2015-01-27 20:47:55 +00:00
|
|
|
* @return string HTML
|
2011-02-07 02:35:35 +00:00
|
|
|
*/
|
2021-04-08 03:57:15 +00:00
|
|
|
private function formatResult( $result ) {
|
2016-11-30 22:56:33 +00:00
|
|
|
$linkRenderer = $this->getLinkRenderer();
|
2011-02-07 02:35:35 +00:00
|
|
|
$nt = $result->getTitle();
|
2020-10-29 19:58:32 +00:00
|
|
|
$text = $this->languageConverter->convert( $nt->getText() );
|
2016-11-30 22:56:33 +00:00
|
|
|
$plink = $linkRenderer->makeLink(
|
2015-01-27 20:47:55 +00:00
|
|
|
$nt,
|
2020-10-16 17:52:27 +00:00
|
|
|
$text
|
2009-06-06 17:00:20 +00:00
|
|
|
);
|
2008-03-19 16:58:56 +00:00
|
|
|
|
2021-05-27 16:56:43 +00:00
|
|
|
$uploader = $result->getUploader( File::FOR_THIS_USER, $this->getAuthority() );
|
|
|
|
|
if ( $result->isLocal() && $uploader ) {
|
|
|
|
|
$user = Linker::userLink( $uploader->getId(), $uploader->getName() );
|
2012-07-06 21:48:08 +00:00
|
|
|
$user .= '<span style="white-space: nowrap;">';
|
2021-05-27 16:56:43 +00:00
|
|
|
$user .= Linker::userToolLinks( $uploader->getId(), $uploader->getName() );
|
2012-07-06 21:48:08 +00:00
|
|
|
$user .= '</span>';
|
2021-05-27 16:56:43 +00:00
|
|
|
} elseif ( $uploader ) {
|
|
|
|
|
$user = htmlspecialchars( $uploader->getName() );
|
2012-07-06 21:48:08 +00:00
|
|
|
} else {
|
2021-05-27 16:56:43 +00:00
|
|
|
$user = '<span class="history-deleted">'
|
|
|
|
|
. $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
|
2012-07-06 21:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
2015-01-27 20:47:55 +00:00
|
|
|
$time = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
|
|
|
|
|
$result->getTimestamp(), $this->getUser() ) );
|
2008-03-19 16:58:56 +00:00
|
|
|
|
|
|
|
|
return "$plink . . $user . . $time";
|
|
|
|
|
}
|
2013-03-07 20:15:54 +00:00
|
|
|
|
2015-12-06 15:06:01 +00:00
|
|
|
/**
|
|
|
|
|
* Return an array of subpages beginning with $search that this special page will accept.
|
|
|
|
|
*
|
|
|
|
|
* @param string $search Prefix to search for
|
|
|
|
|
* @param int $limit Maximum number of results to return (usually 10)
|
|
|
|
|
* @param int $offset Number of results to skip (usually 0)
|
|
|
|
|
* @return string[] Matching subpages
|
|
|
|
|
*/
|
|
|
|
|
public function prefixSearchSubpages( $search, $limit, $offset ) {
|
2015-12-28 22:10:12 +00:00
|
|
|
$title = Title::newFromText( $search, NS_FILE );
|
2015-12-13 17:39:14 +00:00
|
|
|
if ( !$title || $title->getNamespace() !== NS_FILE ) {
|
|
|
|
|
// No prefix suggestion outside of file namespace
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2015-12-06 15:06:01 +00:00
|
|
|
}
|
2020-10-16 17:52:27 +00:00
|
|
|
$searchEngine = $this->searchEngineFactory->create();
|
2016-04-03 11:45:55 +00:00
|
|
|
$searchEngine->setLimitOffset( $limit, $offset );
|
2015-12-06 15:06:01 +00:00
|
|
|
// Autocomplete subpage the same as a normal search, but just for files
|
2016-04-03 11:45:55 +00:00
|
|
|
$searchEngine->setNamespaces( [ NS_FILE ] );
|
|
|
|
|
$result = $searchEngine->defaultPrefixSearch( $search );
|
2015-12-06 15:06:01 +00:00
|
|
|
|
2021-02-10 22:31:02 +00:00
|
|
|
return array_map( static function ( Title $t ) {
|
2015-12-06 15:06:01 +00:00
|
|
|
// Remove namespace in search suggestion
|
|
|
|
|
return $t->getText();
|
|
|
|
|
}, $result );
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-07 20:15:54 +00:00
|
|
|
protected function getGroupName() {
|
|
|
|
|
return 'media';
|
|
|
|
|
}
|
2008-03-19 16:58:56 +00:00
|
|
|
}
|