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() { return false; }
44 function isCacheable() { return false; }
45 function isCached() { return false; }
47 function linkParameters() {
48 return array( 'filename' => $this->filename
);
52 * Fetch dupes from all connected file repositories.
54 * @return Array of File objects
57 return RepoGroup
::singleton()->findBySha1( $this->hash
);
62 * @param $dupes Array of File objects
64 function showList( $dupes ) {
66 $html[] = $this->openList( 0 );
68 foreach ( $dupes as $dupe ) {
69 $line = $this->formatResult( null, $dupe );
70 $html[] = "<li>" . $line . "</li>";
72 $html[] = $this->closeList();
74 $this->getOutput()->addHtml( implode( "\n", $html ) );
77 function getQueryInfo() {
79 'tables' => array( 'image' ),
86 'conds' => array( 'img_sha1' => $this->hash
)
90 function execute( $par ) {
94 $this->outputHeader();
96 $this->filename
= isset( $par ) ?
$par : $this->getRequest()->getText( 'filename' );
99 $title = Title
::newFromText( $this->filename
, NS_FILE
);
100 if( $title && $title->getText() != '' ) {
101 $this->file
= wfFindFile( $title );
104 $out = $this->getOutput();
106 # Create the input form
108 Xml
::openElement( 'form', array( 'id' => 'fileduplicatesearch', 'method' => 'get', 'action' => $wgScript ) ) .
109 Html
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
110 Xml
::openElement( 'fieldset' ) .
111 Xml
::element( 'legend', null, $this->msg( 'fileduplicatesearch-legend' )->text() ) .
112 Xml
::inputLabel( $this->msg( 'fileduplicatesearch-filename' )->text(), 'filename', 'filename', 50, $this->filename
) . ' ' .
113 Xml
::submitButton( $this->msg( 'fileduplicatesearch-submit' )->text() ) .
114 Xml
::closeElement( 'fieldset' ) .
115 Xml
::closeElement( 'form' )
119 $this->hash
= $this->file
->getSha1();
120 } elseif( $this->filename
!== '' ) {
122 "<p class='mw-fileduplicatesearch-noresults'>\n$1\n</p>",
123 array( 'fileduplicatesearch-noresults', wfEscapeWikiText( $this->filename
) )
127 if( $this->hash
!= '' ) {
128 # Show a thumbnail of the file
131 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
133 $out->addHTML( '<div id="mw-fileduplicatesearch-icon">' .
134 $thumb->toHtml( array( 'desc-link' => false ) ) . '<br />' .
135 $this->msg( 'fileduplicatesearch-info' )->numParams(
136 $img->getWidth(), $img->getHeight() )->params(
137 $this->getLanguage()->formatSize( $img->getSize() ),
138 $img->getMimeType() )->parseAsBlock() .
143 $dupes = $this->getDupes();
144 $numRows = count( $dupes );
146 # Show a short summary
147 if( $numRows == 1 ) {
149 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
150 array( 'fileduplicatesearch-result-1', wfEscapeWikiText( $this->filename
) )
152 } elseif ( $numRows ) {
154 "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
155 array( 'fileduplicatesearch-result-n', wfEscapeWikiText( $this->filename
),
156 $this->getLanguage()->formatNum( $numRows - 1 ) )
160 $this->showList( $dupes );
167 * @param File $result
170 function formatResult( $skin, $result ) {
173 $nt = $result->getTitle();
174 $text = $wgContLang->convert( $nt->getText() );
175 $plink = Linker
::link(
176 Title
::newFromText( $nt->getPrefixedText() ),
180 $userText = $result->getUser( 'text' );
181 $user = Linker
::link( Title
::makeTitle( NS_USER
, $userText ), $userText );
182 $time = $this->getLanguage()->userTimeAndDate( $result->getTimestamp(), $this->getUser() );
184 return "$plink . . $user . . $time";