* Use local context instead of global variables
[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 global $wgOut;
66 $skin = $this->getSkin();
67
68 $html = array();
69 $html[] = $this->openList( 0 );
70
71 foreach ( $dupes as $dupe ) {
72 $line = $this->formatResult( $skin, $dupe );
73 $html[] = "<li>" . $line . "</li>";
74 }
75 $html[] = $this->closeList();
76
77 $wgOut->addHtml( implode( "\n", $html ) );
78 }
79
80 function getQueryInfo() {
81 return array(
82 'tables' => array( 'image' ),
83 'fields' => array(
84 'img_name AS title',
85 'img_sha1 AS value',
86 'img_user_text',
87 'img_timestamp'
88 ),
89 'conds' => array( 'img_sha1' => $this->hash )
90 );
91 }
92
93 function execute( $par ) {
94 global $wgRequest, $wgOut, $wgLang, $wgScript;
95
96 $this->setHeaders();
97 $this->outputHeader();
98
99 $this->filename = isset( $par ) ? $par : $wgRequest->getText( 'filename' );
100 $this->file = null;
101 $this->hash = '';
102 $title = Title::newFromText( $this->filename, NS_FILE );
103 if( $title && $title->getText() != '' ) {
104 $this->file = wfFindFile( $title );
105 }
106
107 # Create the input form
108 $wgOut->addHTML(
109 Xml::openElement( 'form', array( 'id' => 'fileduplicatesearch', 'method' => 'get', 'action' => $wgScript ) ) .
110 Html::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
111 Xml::openElement( 'fieldset' ) .
112 Xml::element( 'legend', null, wfMsg( 'fileduplicatesearch-legend' ) ) .
113 Xml::inputLabel( wfMsg( 'fileduplicatesearch-filename' ), 'filename', 'filename', 50, $this->filename ) . ' ' .
114 Xml::submitButton( wfMsg( 'fileduplicatesearch-submit' ) ) .
115 Xml::closeElement( 'fieldset' ) .
116 Xml::closeElement( 'form' )
117 );
118
119 if( $this->file ) {
120 $this->hash = $this->file->getSha1();
121 } elseif( $this->filename !== '' ) {
122 $wgOut->wrapWikiMsg(
123 "<p class='mw-fileduplicatesearch-noresults'>\n$1\n</p>",
124 array( 'fileduplicatesearch-noresults', wfEscapeWikiText( $this->filename ) )
125 );
126 }
127
128 if( $this->hash != '' ) {
129 # Show a thumbnail of the file
130 $img = $this->file;
131 if ( $img ) {
132 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
133 if( $thumb ) {
134 $wgOut->addHTML( '<div class="mw-float-end" id="mw-fileduplicatesearch-icon">' .
135 $thumb->toHtml( array( 'desc-link' => false ) ) . '<br />' .
136 wfMsgExt( 'fileduplicatesearch-info', array( 'parse' ),
137 $wgLang->formatNum( $img->getWidth() ),
138 $wgLang->formatNum( $img->getHeight() ),
139 $wgLang->formatSize( $img->getSize() ),
140 $img->getMimeType()
141 ) .
142 '</div>' );
143 }
144 }
145
146 $dupes = $this->getDupes();
147 $numRows = count( $dupes );
148
149 # Show a short summary
150 if( $numRows == 1 ) {
151 $wgOut->wrapWikiMsg(
152 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
153 array( 'fileduplicatesearch-result-1', wfEscapeWikiText( $this->filename ) )
154 );
155 } elseif ( $numRows ) {
156 $wgOut->wrapWikiMsg(
157 "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
158 array( 'fileduplicatesearch-result-n', wfEscapeWikiText( $this->filename ),
159 $wgLang->formatNum( $numRows - 1 ) )
160 );
161 }
162
163 $this->showList( $dupes );
164 }
165 }
166
167 /**
168 *
169 * @param Skin $skin
170 * @param File $result
171 * @return string
172 */
173 function formatResult( $skin, $result ) {
174 global $wgContLang, $wgLang;
175
176 $nt = $result->getTitle();
177 $text = $wgContLang->convert( $nt->getText() );
178 $plink = $skin->link(
179 Title::newFromText( $nt->getPrefixedText() ),
180 $text
181 );
182
183 $userText = $result->getUser( 'text' );
184 $user = $skin->link( Title::makeTitle( NS_USER, $userText ), $userText );
185 $time = $wgLang->timeanddate( $result->getTimestamp() );
186
187 return "$plink . . $user . . $time";
188 }
189 }