Merge "SkinTemplate: extract formatLanguageName() from outputPage()"
[lhc/web/wiklou.git] / includes / specials / SpecialFileDuplicateSearch.php
1 <?php
2 /**
3 * Implements Special:FileDuplicateSearch
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 * @author Raimond Spekking, based on Special:MIMESearch by Ævar Arnfjörð Bjarmason
23 */
24
25 /**
26 * Searches the database for files of the requested hash, comparing this with the
27 * 'img_sha1' field in the image table.
28 *
29 * @ingroup SpecialPage
30 */
31 class FileDuplicateSearchPage extends QueryPage {
32 protected $hash = '', $filename = '';
33
34 /**
35 * @var File $file selected reference file, if present
36 */
37 protected $file = null;
38
39 function __construct( $name = 'FileDuplicateSearch' ) {
40 parent::__construct( $name );
41 }
42
43 function isSyndicated() {
44 return false;
45 }
46
47 function isCacheable() {
48 return false;
49 }
50
51 function isCached() {
52 return false;
53 }
54
55 function linkParameters() {
56 return array( 'filename' => $this->filename );
57 }
58
59 /**
60 * Fetch dupes from all connected file repositories.
61 *
62 * @return Array of File objects
63 */
64 function getDupes() {
65 return RepoGroup::singleton()->findBySha1( $this->hash );
66 }
67
68 /**
69 *
70 * @param $dupes Array of File objects
71 */
72 function showList( $dupes ) {
73 $html = array();
74 $html[] = $this->openList( 0 );
75
76 foreach ( $dupes as $dupe ) {
77 $line = $this->formatResult( null, $dupe );
78 $html[] = "<li>" . $line . "</li>";
79 }
80 $html[] = $this->closeList();
81
82 $this->getOutput()->addHtml( implode( "\n", $html ) );
83 }
84
85 function getQueryInfo() {
86 return array(
87 'tables' => array( 'image' ),
88 'fields' => array(
89 'title' => 'img_name',
90 'value' => 'img_sha1',
91 'img_user_text',
92 'img_timestamp'
93 ),
94 'conds' => array( 'img_sha1' => $this->hash )
95 );
96 }
97
98 function execute( $par ) {
99 global $wgScript;
100
101 $this->setHeaders();
102 $this->outputHeader();
103
104 $this->filename = isset( $par ) ? $par : $this->getRequest()->getText( 'filename' );
105 $this->file = null;
106 $this->hash = '';
107 $title = Title::newFromText( $this->filename, NS_FILE );
108 if( $title && $title->getText() != '' ) {
109 $this->file = wfFindFile( $title );
110 }
111
112 $out = $this->getOutput();
113
114 # Create the input form
115 $out->addHTML(
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' )
124 );
125
126 if( $this->file ) {
127 $this->hash = $this->file->getSha1();
128 } elseif( $this->filename !== '' ) {
129 $out->wrapWikiMsg(
130 "<p class='mw-fileduplicatesearch-noresults'>\n$1\n</p>",
131 array( 'fileduplicatesearch-noresults', wfEscapeWikiText( $this->filename ) )
132 );
133 }
134
135 if( $this->hash != '' ) {
136 # Show a thumbnail of the file
137 $img = $this->file;
138 if ( $img ) {
139 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
140 if( $thumb ) {
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() .
147 '</div>' );
148 }
149 }
150
151 $dupes = $this->getDupes();
152 $numRows = count( $dupes );
153
154 # Show a short summary
155 if( $numRows == 1 ) {
156 $out->wrapWikiMsg(
157 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
158 array( 'fileduplicatesearch-result-1', wfEscapeWikiText( $this->filename ) )
159 );
160 } elseif ( $numRows ) {
161 $out->wrapWikiMsg(
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 ) )
165 );
166 }
167
168 $this->doBatchLookups( $dupes );
169 $this->showList( $dupes );
170 }
171 }
172
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 );
181 }
182 }
183 $batch->execute();
184 }
185
186 /**
187 *
188 * @param Skin $skin
189 * @param File $result
190 * @return string
191 */
192 function formatResult( $skin, $result ) {
193 global $wgContLang;
194
195 $nt = $result->getTitle();
196 $text = $wgContLang->convert( $nt->getText() );
197 $plink = Linker::link(
198 Title::newFromText( $nt->getPrefixedText() ),
199 $text
200 );
201
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 );
209 $user .= '</span>';
210 } else {
211 $user = htmlspecialchars( $userText );
212 }
213
214 $time = $this->getLanguage()->userTimeAndDate( $result->getTimestamp(), $this->getUser() );
215
216 return "$plink . . $user . . $time";
217 }
218 }