Merge "Move disable of preference 'enotifminoredits'"
[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 public function isCached() {
52 return false;
53 }
54
55 function linkParameters() {
56 return [ '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 = [];
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 public function getQueryInfo() {
86 return [
87 'tables' => [ 'image' ],
88 'fields' => [
89 'title' => 'img_name',
90 'value' => 'img_sha1',
91 'img_user_text',
92 'img_timestamp'
93 ],
94 'conds' => [ 'img_sha1' => $this->hash ]
95 ];
96 }
97
98 public 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 = [
114 'filename' => [
115 'type' => 'text',
116 'name' => 'filename',
117 'label-message' => 'fileduplicatesearch-filename',
118 'id' => 'filename',
119 'size' => 50,
120 'value' => $this->filename,
121 ],
122 ];
123 $hiddenFields = [
124 'title' => $this->getPageTitle()->getPrefixedDBkey(),
125 ];
126 $htmlForm = HTMLForm::factory( 'ooui', $formFields, $this->getContext() );
127 $htmlForm->addHiddenFields( $hiddenFields );
128 $htmlForm->setAction( wfScript() );
129 $htmlForm->setMethod( 'get' );
130 $htmlForm->setSubmitProgressive();
131 $htmlForm->setSubmitTextMsg( $this->msg( 'fileduplicatesearch-submit' ) );
132
133 // The form should be visible always, even if it was submitted (e.g. to perform another action).
134 // To bypass the callback validation of HTMLForm, use prepareForm() and displayForm().
135 $htmlForm->prepareForm()->displayForm( false );
136
137 if ( $this->file ) {
138 $this->hash = $this->file->getSha1();
139 } elseif ( $this->filename !== '' ) {
140 $out->wrapWikiMsg(
141 "<p class='mw-fileduplicatesearch-noresults'>\n$1\n</p>",
142 [ 'fileduplicatesearch-noresults', wfEscapeWikiText( $this->filename ) ]
143 );
144 }
145
146 if ( $this->hash != '' ) {
147 # Show a thumbnail of the file
148 $img = $this->file;
149 if ( $img ) {
150 $thumb = $img->transform( [ 'width' => 120, 'height' => 120 ] );
151 if ( $thumb ) {
152 $out->addModuleStyles( 'mediawiki.special' );
153 $out->addHTML( '<div id="mw-fileduplicatesearch-icon">' .
154 $thumb->toHtml( [ 'desc-link' => false ] ) . '<br />' .
155 $this->msg( 'fileduplicatesearch-info' )->numParams(
156 $img->getWidth(), $img->getHeight() )->params(
157 $this->getLanguage()->formatSize( $img->getSize() ),
158 $img->getMimeType() )->parseAsBlock() .
159 '</div>' );
160 }
161 }
162
163 $dupes = $this->getDupes();
164 $numRows = count( $dupes );
165
166 # Show a short summary
167 if ( $numRows == 1 ) {
168 $out->wrapWikiMsg(
169 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
170 [ 'fileduplicatesearch-result-1', wfEscapeWikiText( $this->filename ) ]
171 );
172 } elseif ( $numRows ) {
173 $out->wrapWikiMsg(
174 "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
175 [ 'fileduplicatesearch-result-n', wfEscapeWikiText( $this->filename ),
176 $this->getLanguage()->formatNum( $numRows - 1 ) ]
177 );
178 }
179
180 $this->doBatchLookups( $dupes );
181 $this->showList( $dupes );
182 }
183 }
184
185 function doBatchLookups( $list ) {
186 $batch = new LinkBatch();
187 /** @var File $file */
188 foreach ( $list as $file ) {
189 $batch->addObj( $file->getTitle() );
190 if ( $file->isLocal() ) {
191 $userName = $file->getUser( 'text' );
192 $batch->add( NS_USER, $userName );
193 $batch->add( NS_USER_TALK, $userName );
194 }
195 }
196
197 $batch->execute();
198 }
199
200 /**
201 *
202 * @param Skin $skin
203 * @param File $result
204 * @return string HTML
205 */
206 function formatResult( $skin, $result ) {
207 global $wgContLang;
208
209 $nt = $result->getTitle();
210 $text = $wgContLang->convert( $nt->getText() );
211 $plink = Linker::link(
212 $nt,
213 htmlspecialchars( $text )
214 );
215
216 $userText = $result->getUser( 'text' );
217 if ( $result->isLocal() ) {
218 $userId = $result->getUser( 'id' );
219 $user = Linker::userLink( $userId, $userText );
220 $user .= '<span style="white-space: nowrap;">';
221 $user .= Linker::userToolLinks( $userId, $userText );
222 $user .= '</span>';
223 } else {
224 $user = htmlspecialchars( $userText );
225 }
226
227 $time = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
228 $result->getTimestamp(), $this->getUser() ) );
229
230 return "$plink . . $user . . $time";
231 }
232
233 /**
234 * Return an array of subpages beginning with $search that this special page will accept.
235 *
236 * @param string $search Prefix to search for
237 * @param int $limit Maximum number of results to return (usually 10)
238 * @param int $offset Number of results to skip (usually 0)
239 * @return string[] Matching subpages
240 */
241 public function prefixSearchSubpages( $search, $limit, $offset ) {
242 $title = Title::newFromText( $search, NS_FILE );
243 if ( !$title || $title->getNamespace() !== NS_FILE ) {
244 // No prefix suggestion outside of file namespace
245 return [];
246 }
247 $searchEngine = SearchEngine::create();
248 $searchEngine->setLimitOffset( $limit, $offset );
249 // Autocomplete subpage the same as a normal search, but just for files
250 $searchEngine->setNamespaces( [ NS_FILE ] );
251 $result = $searchEngine->defaultPrefixSearch( $search );
252
253 return array_map( function ( Title $t ) {
254 // Remove namespace in search suggestion
255 return $t->getText();
256 }, $result );
257 }
258
259 protected function getGroupName() {
260 return 'media';
261 }
262 }