Merge "Adding header font change to release notes for 1.24"
[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 array $dupes 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 = $par !== null ? $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 Html::openElement(
117 'form',
118 array( 'id' => 'fileduplicatesearch', 'method' => 'get', 'action' => $wgScript )
119 ) . "\n" .
120 Html::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) . "\n" .
121 Html::openElement( 'fieldset' ) . "\n" .
122 Html::element( 'legend', null, $this->msg( 'fileduplicatesearch-legend' )->text() ) . "\n" .
123 Xml::inputLabel(
124 $this->msg( 'fileduplicatesearch-filename' )->text(),
125 'filename',
126 'filename',
127 50,
128 $this->filename
129 ) . "\n" .
130 Xml::submitButton( $this->msg( 'fileduplicatesearch-submit' )->text() ) . "\n" .
131 Html::closeElement( 'fieldset' ) . "\n" .
132 Html::closeElement( 'form' )
133 );
134
135 if ( $this->file ) {
136 $this->hash = $this->file->getSha1();
137 } elseif ( $this->filename !== '' ) {
138 $out->wrapWikiMsg(
139 "<p class='mw-fileduplicatesearch-noresults'>\n$1\n</p>",
140 array( 'fileduplicatesearch-noresults', wfEscapeWikiText( $this->filename ) )
141 );
142 }
143
144 if ( $this->hash != '' ) {
145 # Show a thumbnail of the file
146 $img = $this->file;
147 if ( $img ) {
148 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
149 if ( $thumb ) {
150 $out->addHTML( '<div id="mw-fileduplicatesearch-icon">' .
151 $thumb->toHtml( array( 'desc-link' => false ) ) . '<br />' .
152 $this->msg( 'fileduplicatesearch-info' )->numParams(
153 $img->getWidth(), $img->getHeight() )->params(
154 $this->getLanguage()->formatSize( $img->getSize() ),
155 $img->getMimeType() )->parseAsBlock() .
156 '</div>' );
157 }
158 }
159
160 $dupes = $this->getDupes();
161 $numRows = count( $dupes );
162
163 # Show a short summary
164 if ( $numRows == 1 ) {
165 $out->wrapWikiMsg(
166 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
167 array( 'fileduplicatesearch-result-1', wfEscapeWikiText( $this->filename ) )
168 );
169 } elseif ( $numRows ) {
170 $out->wrapWikiMsg(
171 "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
172 array( 'fileduplicatesearch-result-n', wfEscapeWikiText( $this->filename ),
173 $this->getLanguage()->formatNum( $numRows - 1 ) )
174 );
175 }
176
177 $this->doBatchLookups( $dupes );
178 $this->showList( $dupes );
179 }
180 }
181
182 function doBatchLookups( $list ) {
183 $batch = new LinkBatch();
184 /** @var File $file */
185 foreach ( $list as $file ) {
186 $batch->addObj( $file->getTitle() );
187 if ( $file->isLocal() ) {
188 $userName = $file->getUser( 'text' );
189 $batch->add( NS_USER, $userName );
190 $batch->add( NS_USER_TALK, $userName );
191 }
192 }
193
194 $batch->execute();
195 }
196
197 /**
198 *
199 * @param Skin $skin
200 * @param File $result
201 * @return string
202 */
203 function formatResult( $skin, $result ) {
204 global $wgContLang;
205
206 $nt = $result->getTitle();
207 $text = $wgContLang->convert( $nt->getText() );
208 $plink = Linker::link(
209 Title::newFromText( $nt->getPrefixedText() ),
210 $text
211 );
212
213 $userText = $result->getUser( 'text' );
214 if ( $result->isLocal() ) {
215 $userId = $result->getUser( 'id' );
216 $user = Linker::userLink( $userId, $userText );
217 $user .= $this->getContext()->msg( 'word-separator' )->plain();
218 $user .= '<span style="white-space: nowrap;">';
219 $user .= Linker::userToolLinks( $userId, $userText );
220 $user .= '</span>';
221 } else {
222 $user = htmlspecialchars( $userText );
223 }
224
225 $time = $this->getLanguage()->userTimeAndDate( $result->getTimestamp(), $this->getUser() );
226
227 return "$plink . . $user . . $time";
228 }
229
230 protected function getGroupName() {
231 return 'media';
232 }
233 }