moved release notes for r113737 to 1.19 (a 1.19-regression fix)
[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() { return false; }
44 function isCacheable() { return false; }
45 function isCached() { return false; }
46
47 function linkParameters() {
48 return array( 'filename' => $this->filename );
49 }
50
51 /**
52 * Fetch dupes from all connected file repositories.
53 *
54 * @return Array of File objects
55 */
56 function getDupes() {
57 return RepoGroup::singleton()->findBySha1( $this->hash );
58 }
59
60 /**
61 *
62 * @param $dupes Array of File objects
63 */
64 function showList( $dupes ) {
65 $html = array();
66 $html[] = $this->openList( 0 );
67
68 foreach ( $dupes as $dupe ) {
69 $line = $this->formatResult( null, $dupe );
70 $html[] = "<li>" . $line . "</li>";
71 }
72 $html[] = $this->closeList();
73
74 $this->getOutput()->addHtml( implode( "\n", $html ) );
75 }
76
77 function getQueryInfo() {
78 return array(
79 'tables' => array( 'image' ),
80 'fields' => array(
81 'img_name AS title',
82 'img_sha1 AS value',
83 'img_user_text',
84 'img_timestamp'
85 ),
86 'conds' => array( 'img_sha1' => $this->hash )
87 );
88 }
89
90 function execute( $par ) {
91 global $wgScript;
92
93 $this->setHeaders();
94 $this->outputHeader();
95
96 $this->filename = isset( $par ) ? $par : $this->getRequest()->getText( 'filename' );
97 $this->file = null;
98 $this->hash = '';
99 $title = Title::newFromText( $this->filename, NS_FILE );
100 if( $title && $title->getText() != '' ) {
101 $this->file = wfFindFile( $title );
102 }
103
104 $out = $this->getOutput();
105
106 # Create the input form
107 $out->addHTML(
108 Xml::openElement( 'form', array( 'id' => 'fileduplicatesearch', 'method' => 'get', 'action' => $wgScript ) ) .
109 Html::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
110 Xml::openElement( 'fieldset' ) .
111 Xml::element( 'legend', null, $this->msg( 'fileduplicatesearch-legend' )->text() ) .
112 Xml::inputLabel( $this->msg( 'fileduplicatesearch-filename' )->text(), 'filename', 'filename', 50, $this->filename ) . ' ' .
113 Xml::submitButton( $this->msg( 'fileduplicatesearch-submit' )->text() ) .
114 Xml::closeElement( 'fieldset' ) .
115 Xml::closeElement( 'form' )
116 );
117
118 if( $this->file ) {
119 $this->hash = $this->file->getSha1();
120 } elseif( $this->filename !== '' ) {
121 $out->wrapWikiMsg(
122 "<p class='mw-fileduplicatesearch-noresults'>\n$1\n</p>",
123 array( 'fileduplicatesearch-noresults', wfEscapeWikiText( $this->filename ) )
124 );
125 }
126
127 if( $this->hash != '' ) {
128 # Show a thumbnail of the file
129 $img = $this->file;
130 if ( $img ) {
131 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
132 if( $thumb ) {
133 $out->addHTML( '<div id="mw-fileduplicatesearch-icon">' .
134 $thumb->toHtml( array( 'desc-link' => false ) ) . '<br />' .
135 $this->msg( 'fileduplicatesearch-info' )->numParams(
136 $img->getWidth(), $img->getHeight() )->params(
137 $this->getLanguage()->formatSize( $img->getSize() ),
138 $img->getMimeType() )->parseAsBlock() .
139 '</div>' );
140 }
141 }
142
143 $dupes = $this->getDupes();
144 $numRows = count( $dupes );
145
146 # Show a short summary
147 if( $numRows == 1 ) {
148 $out->wrapWikiMsg(
149 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
150 array( 'fileduplicatesearch-result-1', wfEscapeWikiText( $this->filename ) )
151 );
152 } elseif ( $numRows ) {
153 $out->wrapWikiMsg(
154 "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
155 array( 'fileduplicatesearch-result-n', wfEscapeWikiText( $this->filename ),
156 $this->getLanguage()->formatNum( $numRows - 1 ) )
157 );
158 }
159
160 $this->showList( $dupes );
161 }
162 }
163
164 /**
165 *
166 * @param Skin $skin
167 * @param File $result
168 * @return string
169 */
170 function formatResult( $skin, $result ) {
171 global $wgContLang;
172
173 $nt = $result->getTitle();
174 $text = $wgContLang->convert( $nt->getText() );
175 $plink = Linker::link(
176 Title::newFromText( $nt->getPrefixedText() ),
177 $text
178 );
179
180 $userText = $result->getUser( 'text' );
181 $user = Linker::link( Title::makeTitle( NS_USER, $userText ), $userText );
182 $time = $this->getLanguage()->userTimeAndDate( $result->getTimestamp(), $this->getUser() );
183
184 return "$plink . . $user . . $time";
185 }
186 }