Switch some HTMLForms in special pages to OOUI
[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 $formFields = array(
114 'filename' => array(
115 'type' => 'text',
116 'name' => 'filename',
117 'label-message' => 'fileduplicatesearch-filename',
118 'id' => 'filename',
119 'size' => 50,
120 'value' => $this->filename,
121 'cssclass' => 'mw-ui-input-inline'
122 ),
123 );
124 $hiddenFields = array(
125 'title' => $this->getPageTitle()->getPrefixedDBKey(),
126 );
127 $htmlForm = HTMLForm::factory( 'inline', $formFields, $this->getContext() );
128 $htmlForm->addHiddenFields( $hiddenFields );
129 $htmlForm->setAction( wfScript() );
130 $htmlForm->setMethod( 'get' );
131 $htmlForm->setSubmitProgressive();
132 $htmlForm->setSubmitTextMsg( $this->msg( 'fileduplicatesearch-submit' ) );
133 $htmlForm->setWrapperLegendMsg( 'fileduplicatesearch-legend' );
134
135 // The form should be visible always, even if it was submitted (e.g. to perform another action).
136 // To bypass the callback validation of HTMLForm, use prepareForm() and displayForm().
137 $htmlForm->prepareForm()->displayForm( false );
138
139 if ( $this->file ) {
140 $this->hash = $this->file->getSha1();
141 } elseif ( $this->filename !== '' ) {
142 $out->wrapWikiMsg(
143 "<p class='mw-fileduplicatesearch-noresults'>\n$1\n</p>",
144 array( 'fileduplicatesearch-noresults', wfEscapeWikiText( $this->filename ) )
145 );
146 }
147
148 if ( $this->hash != '' ) {
149 # Show a thumbnail of the file
150 $img = $this->file;
151 if ( $img ) {
152 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
153 if ( $thumb ) {
154 $out->addHTML( '<div id="mw-fileduplicatesearch-icon">' .
155 $thumb->toHtml( array( 'desc-link' => false ) ) . '<br />' .
156 $this->msg( 'fileduplicatesearch-info' )->numParams(
157 $img->getWidth(), $img->getHeight() )->params(
158 $this->getLanguage()->formatSize( $img->getSize() ),
159 $img->getMimeType() )->parseAsBlock() .
160 '</div>' );
161 }
162 }
163
164 $dupes = $this->getDupes();
165 $numRows = count( $dupes );
166
167 # Show a short summary
168 if ( $numRows == 1 ) {
169 $out->wrapWikiMsg(
170 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
171 array( 'fileduplicatesearch-result-1', wfEscapeWikiText( $this->filename ) )
172 );
173 } elseif ( $numRows ) {
174 $out->wrapWikiMsg(
175 "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
176 array( 'fileduplicatesearch-result-n', wfEscapeWikiText( $this->filename ),
177 $this->getLanguage()->formatNum( $numRows - 1 ) )
178 );
179 }
180
181 $this->doBatchLookups( $dupes );
182 $this->showList( $dupes );
183 }
184 }
185
186 function doBatchLookups( $list ) {
187 $batch = new LinkBatch();
188 /** @var File $file */
189 foreach ( $list as $file ) {
190 $batch->addObj( $file->getTitle() );
191 if ( $file->isLocal() ) {
192 $userName = $file->getUser( 'text' );
193 $batch->add( NS_USER, $userName );
194 $batch->add( NS_USER_TALK, $userName );
195 }
196 }
197
198 $batch->execute();
199 }
200
201 /**
202 *
203 * @param Skin $skin
204 * @param File $result
205 * @return string HTML
206 */
207 function formatResult( $skin, $result ) {
208 global $wgContLang;
209
210 $nt = $result->getTitle();
211 $text = $wgContLang->convert( $nt->getText() );
212 $plink = Linker::link(
213 $nt,
214 htmlspecialchars( $text )
215 );
216
217 $userText = $result->getUser( 'text' );
218 if ( $result->isLocal() ) {
219 $userId = $result->getUser( 'id' );
220 $user = Linker::userLink( $userId, $userText );
221 $user .= '<span style="white-space: nowrap;">';
222 $user .= Linker::userToolLinks( $userId, $userText );
223 $user .= '</span>';
224 } else {
225 $user = htmlspecialchars( $userText );
226 }
227
228 $time = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
229 $result->getTimestamp(), $this->getUser() ) );
230
231 return "$plink . . $user . . $time";
232 }
233
234 protected function getGroupName() {
235 return 'media';
236 }
237 }