Merge "Special:Search: Remove token from URL when saving settings"
[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 Array of File objects
63 */
64 function getDupes() {
65 return RepoGroup::singleton()->findBySha1( $this->hash );
66 }
67
68 /**
69 *
70 * @param array $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 $this->setHeaders();
100 $this->outputHeader();
101
102 $this->filename = $par !== null ? $par : $this->getRequest()->getText( 'filename' );
103 $this->file = null;
104 $this->hash = '';
105 $title = Title::newFromText( $this->filename, NS_FILE );
106 if ( $title && $title->getText() != '' ) {
107 $this->file = wfFindFile( $title );
108 }
109
110 $out = $this->getOutput();
111
112 # Create the input form
113 $out->addHTML(
114 Html::openElement(
115 'form',
116 array( 'id' => 'fileduplicatesearch', 'method' => 'get', 'action' => wfScript() )
117 ) . "\n" .
118 Html::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) . "\n" .
119 Html::openElement( 'fieldset' ) . "\n" .
120 Html::element( 'legend', null, $this->msg( 'fileduplicatesearch-legend' )->text() ) . "\n" .
121 Xml::inputLabel(
122 $this->msg( 'fileduplicatesearch-filename' )->text(),
123 'filename',
124 'filename',
125 50,
126 $this->filename
127 ) . "\n" .
128 Xml::submitButton( $this->msg( 'fileduplicatesearch-submit' )->text() ) . "\n" .
129 Html::closeElement( 'fieldset' ) . "\n" .
130 Html::closeElement( 'form' )
131 );
132
133 if ( $this->file ) {
134 $this->hash = $this->file->getSha1();
135 } elseif ( $this->filename !== '' ) {
136 $out->wrapWikiMsg(
137 "<p class='mw-fileduplicatesearch-noresults'>\n$1\n</p>",
138 array( 'fileduplicatesearch-noresults', wfEscapeWikiText( $this->filename ) )
139 );
140 }
141
142 if ( $this->hash != '' ) {
143 # Show a thumbnail of the file
144 $img = $this->file;
145 if ( $img ) {
146 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
147 if ( $thumb ) {
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() .
154 '</div>' );
155 }
156 }
157
158 $dupes = $this->getDupes();
159 $numRows = count( $dupes );
160
161 # Show a short summary
162 if ( $numRows == 1 ) {
163 $out->wrapWikiMsg(
164 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
165 array( 'fileduplicatesearch-result-1', wfEscapeWikiText( $this->filename ) )
166 );
167 } elseif ( $numRows ) {
168 $out->wrapWikiMsg(
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 ) )
172 );
173 }
174
175 $this->doBatchLookups( $dupes );
176 $this->showList( $dupes );
177 }
178 }
179
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 );
189 }
190 }
191
192 $batch->execute();
193 }
194
195 /**
196 *
197 * @param Skin $skin
198 * @param File $result
199 * @return string
200 */
201 function formatResult( $skin, $result ) {
202 global $wgContLang;
203
204 $nt = $result->getTitle();
205 $text = $wgContLang->convert( $nt->getText() );
206 $plink = Linker::link(
207 Title::newFromText( $nt->getPrefixedText() ),
208 $text
209 );
210
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 );
218 $user .= '</span>';
219 } else {
220 $user = htmlspecialchars( $userText );
221 }
222
223 $time = $this->getLanguage()->userTimeAndDate( $result->getTimestamp(), $this->getUser() );
224
225 return "$plink . . $user . . $time";
226 }
227
228 protected function getGroupName() {
229 return 'media';
230 }
231 }