Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / includes / specials / SpecialFileDuplicateSearch.php
blob4c6593b27236e349c1a5eee9a6cd3affc4715076
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 of File objects
64 function getDupes() {
65 return RepoGroup::singleton()->findBySha1( $this->hash );
68 /**
70 * @param array $dupes 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 global $wgScript;
101 $this->setHeaders();
102 $this->outputHeader();
104 $this->filename = isset( $par ) ? $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 );
112 $out = $this->getOutput();
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->getTitle()->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',
128 $this->filename
129 ) . "\n" .
130 Xml::submitButton( $this->msg( 'fileduplicatesearch-submit' )->text() ) . "\n" .
131 Html::closeElement( 'fieldset' ) . "\n" .
132 Html::closeElement( 'form' )
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 ) )
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>' );
160 $dupes = $this->getDupes();
161 $numRows = count( $dupes );
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 ) )
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 ) )
177 $this->doBatchLookups( $dupes );
178 $this->showList( $dupes );
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 );
194 $batch->execute();
199 * @param Skin $skin
200 * @param File $result
201 * @return string
203 function formatResult( $skin, $result ) {
204 global $wgContLang;
206 $nt = $result->getTitle();
207 $text = $wgContLang->convert( $nt->getText() );
208 $plink = Linker::link(
209 Title::newFromText( $nt->getPrefixedText() ),
210 $text
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 );
225 $time = $this->getLanguage()->userTimeAndDate( $result->getTimestamp(), $this->getUser() );
227 return "$plink . . $user . . $time";
230 protected function getGroupName() {
231 return 'media';