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 of File objects
65 return RepoGroup
::singleton()->findBySha1( $this->hash
);
70 * @param array $dupes 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 ) {
102 $this->outputHeader();
104 $this->filename
= isset( $par ) ?
$par : $this->getRequest()->getText( 'filename' );
107 $title = Title
::newFromText( $this->filename
, NS_FILE
);
108 if( $title && $title->getText() != '' ) {
109 $this->file
= wfFindFile( $title );
112 $out = $this->getOutput();
114 # Create the input form
116 Xml
::openElement( 'form', array( 'id' => 'fileduplicatesearch', 'method' => 'get', 'action' => $wgScript ) ) .
117 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
118 Xml
::openElement( 'fieldset' ) .
119 Xml
::element( 'legend', null, $this->msg( 'fileduplicatesearch-legend' )->text() ) .
120 Xml
::inputLabel( $this->msg( 'fileduplicatesearch-filename' )->text(), 'filename', 'filename', 50, $this->filename
) . ' ' .
121 Xml
::submitButton( $this->msg( 'fileduplicatesearch-submit' )->text() ) .
122 Xml
::closeElement( 'fieldset' ) .
123 Xml
::closeElement( 'form' )
127 $this->hash
= $this->file
->getSha1();
128 } elseif( $this->filename
!== '' ) {
130 "<p class='mw-fileduplicatesearch-noresults'>\n$1\n</p>",
131 array( 'fileduplicatesearch-noresults', wfEscapeWikiText( $this->filename
) )
135 if( $this->hash
!= '' ) {
136 # Show a thumbnail of the file
139 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
141 $out->addHTML( '<div id="mw-fileduplicatesearch-icon">' .
142 $thumb->toHtml( array( 'desc-link' => false ) ) . '<br />' .
143 $this->msg( 'fileduplicatesearch-info' )->numParams(
144 $img->getWidth(), $img->getHeight() )->params(
145 $this->getLanguage()->formatSize( $img->getSize() ),
146 $img->getMimeType() )->parseAsBlock() .
151 $dupes = $this->getDupes();
152 $numRows = count( $dupes );
154 # Show a short summary
155 if( $numRows == 1 ) {
157 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
158 array( 'fileduplicatesearch-result-1', wfEscapeWikiText( $this->filename
) )
160 } elseif ( $numRows ) {
162 "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
163 array( 'fileduplicatesearch-result-n', wfEscapeWikiText( $this->filename
),
164 $this->getLanguage()->formatNum( $numRows - 1 ) )
168 $this->doBatchLookups( $dupes );
169 $this->showList( $dupes );
173 function doBatchLookups( $list ) {
174 $batch = new LinkBatch();
175 foreach( $list as $file ) {
176 $batch->addObj( $file->getTitle() );
177 if( $file->isLocal() ) {
178 $userName = $file->getUser( 'text' );
179 $batch->add( NS_USER
, $userName );
180 $batch->add( NS_USER_TALK
, $userName );
189 * @param File $result
192 function formatResult( $skin, $result ) {
195 $nt = $result->getTitle();
196 $text = $wgContLang->convert( $nt->getText() );
197 $plink = Linker
::link(
198 Title
::newFromText( $nt->getPrefixedText() ),
202 $userText = $result->getUser( 'text' );
203 if ( $result->isLocal() ) {
204 $userId = $result->getUser( 'id' );
205 $user = Linker
::userLink( $userId, $userText );
206 $user .= $this->getContext()->msg( 'word-separator' )->plain();
207 $user .= '<span style="white-space: nowrap;">';
208 $user .= Linker
::userToolLinks( $userId, $userText );
211 $user = htmlspecialchars( $userText );
214 $time = $this->getLanguage()->userTimeAndDate( $result->getTimestamp(), $this->getUser() );
216 return "$plink . . $user . . $time";
219 protected function getGroupName() {