Localisation updates for core and extension messages from translatewiki.net (2011...
[mediawiki.git] / includes / specials / SpecialFileDuplicateSearch.php
blobaf93070cb93db372d09c873c2ea6bff71e489445
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
22 * @author Raimond Spekking, based on Special:MIMESearch by Ævar Arnfjörð Bjarmason
25 /**
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 = '';
34 /**
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 );
51 /**
52 * Fetch dupes from all connected file repositories.
54 * @return Array of File objects
56 function getDupes() {
57 return RepoGroup::singleton()->findBySha1( $this->hash );
60 /**
62 * @param Array of File objects $dupes
64 function showList( $dupes ) {
65 global $wgUser, $wgOut;
66 $skin = $wgUser->getSkin();
68 $html = array();
69 $html[] = $this->openList( 0 );
71 foreach ( $dupes as $dupe ) {
72 $line = $this->formatResult( $skin, $dupe );
73 $html[] = "<li>" . $line . "</li>";
75 $html[] = $this->closeList();
77 $wgOut->addHtml( implode( "\n", $html ) );
80 function getQueryInfo() {
81 return array(
82 'tables' => array( 'image' ),
83 'fields' => array(
84 'img_name AS title',
85 'img_sha1 AS value',
86 'img_user_text',
87 'img_timestamp'
89 'conds' => array( 'img_sha1' => $this->hash )
93 function execute( $par ) {
94 global $wgRequest, $wgOut, $wgLang, $wgContLang, $wgScript;
96 $this->setHeaders();
97 $this->outputHeader();
99 $this->filename = isset( $par ) ? $par : $wgRequest->getText( 'filename' );
100 $this->file = null;
101 $this->hash = '';
102 $title = Title::newFromText( $this->filename, NS_FILE );
103 if( $title && $title->getText() != '' ) {
104 $this->file = wfFindFile( $title );
107 # Create the input form
108 $wgOut->addHTML(
109 Xml::openElement( 'form', array( 'id' => 'fileduplicatesearch', 'method' => 'get', 'action' => $wgScript ) ) .
110 Html::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
111 Xml::openElement( 'fieldset' ) .
112 Xml::element( 'legend', null, wfMsg( 'fileduplicatesearch-legend' ) ) .
113 Xml::inputLabel( wfMsg( 'fileduplicatesearch-filename' ), 'filename', 'filename', 50, $this->filename ) . ' ' .
114 Xml::submitButton( wfMsg( 'fileduplicatesearch-submit' ) ) .
115 Xml::closeElement( 'fieldset' ) .
116 Xml::closeElement( 'form' )
119 if( $this->file ) {
120 $this->hash = $this->file->getSha1();
121 } else {
122 $wgOut->wrapWikiMsg(
123 "<p class='mw-fileduplicatesearch-noresults'>\n$1\n</p>",
124 array( 'fileduplicatesearch-noresults', wfEscapeWikiText( $this->filename ) )
128 if( $this->hash != '' ) {
129 $align = $wgContLang->alignEnd();
131 # Show a thumbnail of the file
132 $img = $this->file;
133 if ( $img ) {
134 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
135 if( $thumb ) {
136 $wgOut->addHTML( '<div style="float:' . $align . '" id="mw-fileduplicatesearch-icon">' .
137 $thumb->toHtml( array( 'desc-link' => false ) ) . '<br />' .
138 wfMsgExt( 'fileduplicatesearch-info', array( 'parse' ),
139 $wgLang->formatNum( $img->getWidth() ),
140 $wgLang->formatNum( $img->getHeight() ),
141 $wgLang->formatSize( $img->getSize() ),
142 $img->getMimeType()
144 '</div>' );
148 $dupes = $this->getDupes();
149 $numRows = count( $dupes );
151 # Show a short summary
152 if( $numRows == 1 ) {
153 $wgOut->wrapWikiMsg(
154 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
155 array( 'fileduplicatesearch-result-1', wfEscapeWikiText( $this->filename ) )
157 } elseif ( $numRows ) {
158 $wgOut->wrapWikiMsg(
159 "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
160 array( 'fileduplicatesearch-result-n', wfEscapeWikiText( $this->filename ),
161 $wgLang->formatNum( $numRows - 1 ) )
165 $this->showList( $dupes );
171 * @param Skin $skin
172 * @param File $result
173 * @return string
175 function formatResult( $skin, $result ) {
176 global $wgContLang, $wgLang;
178 $nt = $result->getTitle();
179 $text = $wgContLang->convert( $nt->getText() );
180 $plink = $skin->link(
181 Title::newFromText( $nt->getPrefixedText() ),
182 $text
185 $userText = $result->getUser( 'text' );
186 $user = $skin->link( Title::makeTitle( NS_USER, $userText ), $userText );
187 $time = $wgLang->timeanddate( $result->getTimestamp() );
189 return "$plink . . $user . . $time";