3 * Implements Special:FileDuplicateSearch
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
22 * @author Raimond Spekking, based on Special:MIMESearch by Ævar Arnfjörð Bjarmason
26 * Searches the database for files of the requested hash, comparing this with the
27 * 'img_sha1' field in the image table.
29 * @ingroup SpecialPage
31 class FileDuplicateSearchPage
extends QueryPage
{
32 protected $hash = '', $filename = '';
35 * @var File $file selected reference file, if present
37 protected $file = null;
39 function __construct( $name = 'FileDuplicateSearch' ) {
40 parent
::__construct( $name );
43 function isSyndicated() {
47 function isCacheable() {
55 function linkParameters() {
56 return array( 'filename' => $this->filename
);
60 * Fetch dupes from all connected file repositories.
62 * @return array Array of File objects
65 return RepoGroup
::singleton()->findBySha1( $this->hash
);
70 * @param array $dupes Array of File objects
72 function showList( $dupes ) {
74 $html[] = $this->openList( 0 );
76 foreach ( $dupes as $dupe ) {
77 $line = $this->formatResult( null, $dupe );
78 $html[] = "<li>" . $line . "</li>";
80 $html[] = $this->closeList();
82 $this->getOutput()->addHtml( implode( "\n", $html ) );
85 function getQueryInfo() {
87 'tables' => array( 'image' ),
89 'title' => 'img_name',
90 'value' => 'img_sha1',
94 'conds' => array( 'img_sha1' => $this->hash
)
98 function execute( $par ) {
100 $this->outputHeader();
102 $this->filename
= $par !== null ?
$par : $this->getRequest()->getText( 'filename' );
105 $title = Title
::newFromText( $this->filename
, NS_FILE
);
106 if ( $title && $title->getText() != '' ) {
107 $this->file
= wfFindFile( $title );
110 $out = $this->getOutput();
112 # Create the input form
116 array( 'id' => 'fileduplicatesearch', 'method' => 'get', 'action' => wfScript() )
118 Html
::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) . "\n" .
119 Html
::openElement( 'fieldset' ) . "\n" .
120 Html
::element( 'legend', null, $this->msg( 'fileduplicatesearch-legend' )->text() ) . "\n" .
122 $this->msg( 'fileduplicatesearch-filename' )->text(),
128 Xml
::submitButton( $this->msg( 'fileduplicatesearch-submit' )->text() ) . "\n" .
129 Html
::closeElement( 'fieldset' ) . "\n" .
130 Html
::closeElement( 'form' )
134 $this->hash
= $this->file
->getSha1();
135 } elseif ( $this->filename
!== '' ) {
137 "<p class='mw-fileduplicatesearch-noresults'>\n$1\n</p>",
138 array( 'fileduplicatesearch-noresults', wfEscapeWikiText( $this->filename
) )
142 if ( $this->hash
!= '' ) {
143 # Show a thumbnail of the file
146 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
148 $out->addHTML( '<div id="mw-fileduplicatesearch-icon">' .
149 $thumb->toHtml( array( 'desc-link' => false ) ) . '<br />' .
150 $this->msg( 'fileduplicatesearch-info' )->numParams(
151 $img->getWidth(), $img->getHeight() )->params(
152 $this->getLanguage()->formatSize( $img->getSize() ),
153 $img->getMimeType() )->parseAsBlock() .
158 $dupes = $this->getDupes();
159 $numRows = count( $dupes );
161 # Show a short summary
162 if ( $numRows == 1 ) {
164 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
165 array( 'fileduplicatesearch-result-1', wfEscapeWikiText( $this->filename
) )
167 } elseif ( $numRows ) {
169 "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
170 array( 'fileduplicatesearch-result-n', wfEscapeWikiText( $this->filename
),
171 $this->getLanguage()->formatNum( $numRows - 1 ) )
175 $this->doBatchLookups( $dupes );
176 $this->showList( $dupes );
180 function doBatchLookups( $list ) {
181 $batch = new LinkBatch();
182 /** @var File $file */
183 foreach ( $list as $file ) {
184 $batch->addObj( $file->getTitle() );
185 if ( $file->isLocal() ) {
186 $userName = $file->getUser( 'text' );
187 $batch->add( NS_USER
, $userName );
188 $batch->add( NS_USER_TALK
, $userName );
198 * @param File $result
201 function formatResult( $skin, $result ) {
204 $nt = $result->getTitle();
205 $text = $wgContLang->convert( $nt->getText() );
206 $plink = Linker
::link(
207 Title
::newFromText( $nt->getPrefixedText() ),
211 $userText = $result->getUser( 'text' );
212 if ( $result->isLocal() ) {
213 $userId = $result->getUser( 'id' );
214 $user = Linker
::userLink( $userId, $userText );
215 $user .= $this->getContext()->msg( 'word-separator' )->plain();
216 $user .= '<span style="white-space: nowrap;">';
217 $user .= Linker
::userToolLinks( $userId, $userText );
220 $user = htmlspecialchars( $userText );
223 $time = $this->getLanguage()->userTimeAndDate( $result->getTimestamp(), $this->getUser() );
225 return "$plink . . $user . . $time";
228 protected function getGroupName() {