Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / specials / SpecialFileDuplicateSearch.php
blob607b4f6f12a5f1d32c2d305db4d24c1bb9a1cf29
1 <?php
2 /**
3 * Implements Special:FileDuplicateSearch
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
22 * @author Raimond Spekking, based on Special:MIMESearch by Ævar Arnfjörð Bjarmason
25 /**
26 * Searches the database for files of the requested hash, comparing this with the
27 * 'img_sha1' field in the image table.
29 * @ingroup SpecialPage
31 class FileDuplicateSearchPage extends QueryPage {
32 protected $hash = '', $filename = '';
34 /**
35 * @var File $file selected reference file, if present
37 protected $file = null;
39 function __construct( $name = 'FileDuplicateSearch' ) {
40 parent::__construct( $name );
43 function isSyndicated() {
44 return false;
47 function isCacheable() {
48 return false;
51 function isCached() {
52 return false;
55 function linkParameters() {
56 return array( 'filename' => $this->filename );
59 /**
60 * Fetch dupes from all connected file repositories.
62 * @return array Array of File objects
64 function getDupes() {
65 return RepoGroup::singleton()->findBySha1( $this->hash );
68 /**
70 * @param array $dupes Array of File objects
72 function showList( $dupes ) {
73 $html = array();
74 $html[] = $this->openList( 0 );
76 foreach ( $dupes as $dupe ) {
77 $line = $this->formatResult( null, $dupe );
78 $html[] = "<li>" . $line . "</li>";
80 $html[] = $this->closeList();
82 $this->getOutput()->addHtml( implode( "\n", $html ) );
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'
94 'conds' => array( 'img_sha1' => $this->hash )
98 function execute( $par ) {
99 $this->setHeaders();
100 $this->outputHeader();
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 );
110 $out = $this->getOutput();
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',
126 $this->filename
127 ) . "\n" .
128 Xml::submitButton( $this->msg( 'fileduplicatesearch-submit' )->text() ) . "\n" .
129 Html::closeElement( 'fieldset' ) . "\n" .
130 Html::closeElement( 'form' )
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 ) )
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>' );
158 $dupes = $this->getDupes();
159 $numRows = count( $dupes );
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 ) )
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 ) )
175 $this->doBatchLookups( $dupes );
176 $this->showList( $dupes );
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 );
192 $batch->execute();
197 * @param Skin $skin
198 * @param File $result
199 * @return string HTML
201 function formatResult( $skin, $result ) {
202 global $wgContLang;
204 $nt = $result->getTitle();
205 $text = $wgContLang->convert( $nt->getText() );
206 $plink = Linker::link(
207 $nt,
208 htmlspecialchars( $text )
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' )->escaped();
216 $user .= '<span style="white-space: nowrap;">';
217 $user .= Linker::userToolLinks( $userId, $userText );
218 $user .= '</span>';
219 } else {
220 $user = htmlspecialchars( $userText );
223 $time = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
224 $result->getTimestamp(), $this->getUser() ) );
226 return "$plink . . $user . . $time";
229 protected function getGroupName() {
230 return 'media';