]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/specials/SpecialFileDuplicateSearch.php
MediaWiki 1.17.1-scripts
[autoinstallsdev/mediawiki.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         var $hash, $filename;
33
34         function __construct( $hash, $filename ) {
35                 $this->hash = $hash;
36                 $this->filename = $filename;
37         }
38
39         function getName() { return 'FileDuplicateSearch'; }
40         function isExpensive() { return false; }
41         function isSyndicated() { return false; }
42
43         function linkParameters() {
44                 return array( 'filename' => $this->filename );
45         }
46
47         function getSQL() {
48                 $dbr = wfGetDB( DB_SLAVE );
49                 $image = $dbr->tableName( 'image' );
50                 $hash = $dbr->addQuotes( $this->hash );
51
52                 return "SELECT 'FileDuplicateSearch' AS type,
53                                 img_name AS title,
54                                 img_sha1 AS value,
55                                 img_user_text,
56                                 img_timestamp
57                         FROM $image
58                         WHERE img_sha1 = $hash
59                         ";
60         }
61
62         function formatResult( $skin, $result ) {
63                 global $wgContLang, $wgLang;
64
65                 $nt = Title::makeTitle( NS_FILE, $result->title );
66                 $text = $wgContLang->convert( $nt->getText() );
67                 $plink = $skin->link(
68                         Title::newFromText( $nt->getPrefixedText() ),
69                         $text
70                 );
71
72                 $user = $skin->link( Title::makeTitle( NS_USER, $result->img_user_text ), $result->img_user_text );
73                 $time = $wgLang->timeanddate( $result->img_timestamp );
74
75                 return "$plink . . $user . . $time";
76         }
77 }
78
79 /**
80  * Output the HTML search form, and constructs the FileDuplicateSearch object.
81  */
82 function wfSpecialFileDuplicateSearch( $par = null ) {
83         global $wgRequest, $wgOut, $wgLang, $wgContLang, $wgScript;
84
85         $hash = '';
86         $filename =  isset( $par ) ?  $par : $wgRequest->getText( 'filename' );
87
88         $title = Title::makeTitleSafe( NS_FILE, $filename );
89         if( $title && $title->getText() != '' ) {
90                 $dbr = wfGetDB( DB_SLAVE );
91                 $image = $dbr->tableName( 'image' );
92                 $encFilename = $dbr->addQuotes( htmlspecialchars( $title->getDBkey() ) );
93                 $sql = "SELECT img_sha1 from $image where img_name = $encFilename";
94                 $res = $dbr->query( $sql );
95                 $row = $dbr->fetchRow( $res );
96                 if( $row !== false ) {
97                         $hash = $row[0];
98                 }
99         }
100
101         # Create the input form
102         $wgOut->addHTML(
103                 Xml::openElement( 'form', array( 'id' => 'fileduplicatesearch', 'method' => 'get', 'action' => $wgScript ) ) .
104                 Html::hidden( 'title', SpecialPage::getTitleFor( 'FileDuplicateSearch' )->getPrefixedDbKey() ) .
105                 Xml::openElement( 'fieldset' ) .
106                 Xml::element( 'legend', null, wfMsg( 'fileduplicatesearch-legend' ) ) .
107                 Xml::inputLabel( wfMsg( 'fileduplicatesearch-filename' ), 'filename', 'filename', 50, $filename ) . ' ' .
108                 Xml::submitButton( wfMsg( 'fileduplicatesearch-submit' ) ) .
109                 Xml::closeElement( 'fieldset' ) .
110                 Xml::closeElement( 'form' )
111         );
112
113         if( $hash != '' ) {
114                 $align = $wgContLang->alignEnd();
115
116                 # Show a thumbnail of the file
117                 $img = wfFindFile( $title );
118                 if ( $img ) {
119                         $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
120                         if( $thumb ) {
121                                 $wgOut->addHTML( '<div style="float:' . $align . '" id="mw-fileduplicatesearch-icon">' .
122                                         $thumb->toHtml( array( 'desc-link' => false ) ) . '<br />' .
123                                         wfMsgExt( 'fileduplicatesearch-info', array( 'parse' ),
124                                                 $wgLang->formatNum( $img->getWidth() ),
125                                                 $wgLang->formatNum( $img->getHeight() ),
126                                                 $wgLang->formatSize( $img->getSize() ),
127                                                 $img->getMimeType()
128                                         ) .
129                                         '</div>' );
130                         }
131                 }
132
133                 # Do the query
134                 $wpp = new FileDuplicateSearchPage( $hash, $filename );
135                 list( $limit, $offset ) = wfCheckLimits();
136                 $count = $wpp->doQuery( $offset, $limit );
137
138                 # Show a short summary
139                 if( $count == 1 ) {
140                         $wgOut->wrapWikiMsg(
141                                 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
142                                 array( 'fileduplicatesearch-result-1', $filename )
143                         );
144                 } elseif ( $count > 1 ) {
145                         $wgOut->wrapWikiMsg(
146                                 "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
147                                 array( 'fileduplicatesearch-result-n', $filename, $wgLang->formatNum( $count - 1 ) )
148                         );
149                 }
150         }
151 }