2d28295a00065a555ab34ef64b432e1c878ac989
[lhc/web/wiklou.git] / includes / specials / SpecialFileDuplicateSearch.php
1 <?php
2 /**
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20 /**
21 * A special page to search for files by hash value as defined in the
22 * img_sha1 field in the image table
23 *
24 * @file
25 * @ingroup SpecialPage
26 *
27 * @author Raimond Spekking, based on Special:MIMESearch by Ævar Arnfjörð Bjarmason
28 */
29
30 /**
31 * Searches the database for files of the requested hash, comparing this with the
32 * 'img_sha1' field in the image table.
33 * @ingroup SpecialPage
34 */
35 class FileDuplicateSearchPage extends QueryPage {
36 var $hash, $filename;
37
38 function FileDuplicateSearchPage( $hash, $filename ) {
39 $this->hash = $hash;
40 $this->filename = $filename;
41 }
42
43 function getName() { return 'FileDuplicateSearch'; }
44 function isExpensive() { return false; }
45 function isSyndicated() { return false; }
46
47 function linkParameters() {
48 return array( 'filename' => $this->filename );
49 }
50
51 function getSQL() {
52 $dbr = wfGetDB( DB_SLAVE );
53 $image = $dbr->tableName( 'image' );
54 $hash = $dbr->addQuotes( $this->hash );
55
56 return "SELECT 'FileDuplicateSearch' AS type,
57 img_name AS title,
58 img_sha1 AS value,
59 img_user_text,
60 img_timestamp
61 FROM $image
62 WHERE img_sha1 = $hash
63 ";
64 }
65
66 function formatResult( $skin, $result ) {
67 global $wgContLang, $wgLang;
68
69 $nt = Title::makeTitle( NS_FILE, $result->title );
70 $text = $wgContLang->convert( $nt->getText() );
71 $plink = $skin->link(
72 Title::newFromText( $nt->getPrefixedText() ),
73 $text
74 );
75
76 $user = $skin->link( Title::makeTitle( NS_USER, $result->img_user_text ), $result->img_user_text );
77 $time = $wgLang->timeanddate( $result->img_timestamp );
78
79 return "$plink . . $user . . $time";
80 }
81 }
82
83 /**
84 * Output the HTML search form, and constructs the FileDuplicateSearch object.
85 */
86 function wfSpecialFileDuplicateSearch( $par = null ) {
87 global $wgRequest, $wgOut, $wgLang, $wgContLang, $wgScript;
88
89 $hash = '';
90 $filename = isset( $par ) ? $par : $wgRequest->getText( 'filename' );
91
92 $title = Title::newFromText( $filename );
93 if( $title && $title->getText() != '' ) {
94 $dbr = wfGetDB( DB_SLAVE );
95 $image = $dbr->tableName( 'image' );
96 $encFilename = $dbr->addQuotes( htmlspecialchars( $title->getDBkey() ) );
97 $sql = "SELECT img_sha1 from $image where img_name = $encFilename";
98 $res = $dbr->query( $sql );
99 $row = $dbr->fetchRow( $res );
100 if( $row !== false ) {
101 $hash = $row[0];
102 }
103 }
104
105 # Create the input form
106 $wgOut->addHTML(
107 Xml::openElement( 'form', array( 'id' => 'fileduplicatesearch', 'method' => 'get', 'action' => $wgScript ) ) .
108 Xml::hidden( 'title', SpecialPage::getTitleFor( 'FileDuplicateSearch' )->getPrefixedDbKey() ) .
109 Xml::openElement( 'fieldset' ) .
110 Xml::element( 'legend', null, wfMsg( 'fileduplicatesearch-legend' ) ) .
111 Xml::inputLabel( wfMsg( 'fileduplicatesearch-filename' ), 'filename', 'filename', 50, $filename ) . ' ' .
112 Xml::submitButton( wfMsg( 'fileduplicatesearch-submit' ) ) .
113 Xml::closeElement( 'fieldset' ) .
114 Xml::closeElement( 'form' )
115 );
116
117 if( $hash != '' ) {
118 $align = $wgContLang->alignEnd();
119
120 # Show a thumbnail of the file
121 $img = wfFindFile( $title );
122 if ( $img ) {
123 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
124 if( $thumb ) {
125 $wgOut->addHTML( '<div style="float:' . $align . '" id="mw-fileduplicatesearch-icon">' .
126 $thumb->toHtml( array( 'desc-link' => false ) ) . '<br />' .
127 wfMsgExt( 'fileduplicatesearch-info', array( 'parse' ),
128 $wgLang->formatNum( $img->getWidth() ),
129 $wgLang->formatNum( $img->getHeight() ),
130 $wgLang->formatSize( $img->getSize() ),
131 $img->getMimeType()
132 ) .
133 '</div>' );
134 }
135 }
136
137 # Do the query
138 $wpp = new FileDuplicateSearchPage( $hash, $filename );
139 list( $limit, $offset ) = wfCheckLimits();
140 $count = $wpp->doQuery( $offset, $limit );
141
142 # Show a short summary
143 if( $count == 1 ) {
144 $wgOut->wrapWikiMsg(
145 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
146 array( 'fileduplicatesearch-result-1', $filename )
147 );
148 } elseif ( $count > 1 ) {
149 $wgOut->wrapWikiMsg(
150 "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
151 array( 'fileduplicatesearch-result-n', $filename, $wgLang->formatNum( $count - 1 ) )
152 );
153 }
154 }
155 }