]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/specials/SpecialFileDuplicateSearch.php
MediaWiki 1.16.1-scripts
[autoinstallsdev/mediawiki.git] / includes / specials / SpecialFileDuplicateSearch.php
1 <?php
2 /**
3  * A special page to search for files by hash value as defined in the
4  * img_sha1 field in the image table
5  *
6  * @file
7  * @ingroup SpecialPage
8  *
9  * @author Raimond Spekking, based on Special:MIMESearch by Ævar Arnfjörð Bjarmason
10  * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
11  */
12
13 /**
14  * Searches the database for files of the requested hash, comparing this with the
15  * 'img_sha1' field in the image table.
16  * @ingroup SpecialPage
17  */
18 class FileDuplicateSearchPage extends QueryPage {
19         var $hash, $filename;
20
21         function FileDuplicateSearchPage( $hash, $filename ) {
22                 $this->hash = $hash;
23                 $this->filename = $filename;
24         }
25
26         function getName() { return 'FileDuplicateSearch'; }
27         function isExpensive() { return false; }
28         function isSyndicated() { return false; }
29
30         function linkParameters() {
31                 return array( 'filename' => $this->filename );
32         }
33
34         function getSQL() {
35                 $dbr = wfGetDB( DB_SLAVE );
36                 $image = $dbr->tableName( 'image' );
37                 $hash = $dbr->addQuotes( $this->hash );
38
39                 return "SELECT 'FileDuplicateSearch' AS type,
40                                 img_name AS title,
41                                 img_sha1 AS value,
42                                 img_user_text,
43                                 img_timestamp
44                         FROM $image
45                         WHERE img_sha1 = $hash
46                         ";
47         }
48
49         function formatResult( $skin, $result ) {
50                 global $wgContLang, $wgLang;
51
52                 $nt = Title::makeTitle( NS_FILE, $result->title );
53                 $text = $wgContLang->convert( $nt->getText() );
54                 $plink = $skin->link(
55                         Title::newFromText( $nt->getPrefixedText() ),
56                         $text
57                 );
58
59                 $user = $skin->link( Title::makeTitle( NS_USER, $result->img_user_text ), $result->img_user_text );
60                 $time = $wgLang->timeanddate( $result->img_timestamp );
61
62                 return "$plink . . $user . . $time";
63         }
64 }
65
66 /**
67  * Output the HTML search form, and constructs the FileDuplicateSearch object.
68  */
69 function wfSpecialFileDuplicateSearch( $par = null ) {
70         global $wgRequest, $wgOut, $wgLang, $wgContLang, $wgScript;
71
72         $hash = '';
73         $filename =  isset( $par ) ?  $par : $wgRequest->getText( 'filename' );
74
75         $title = Title::newFromText( $filename );
76         if( $title && $title->getText() != '' ) {
77                 $dbr = wfGetDB( DB_SLAVE );
78                 $image = $dbr->tableName( 'image' );
79                 $encFilename = $dbr->addQuotes( htmlspecialchars( $title->getDBkey() ) );
80                 $sql = "SELECT img_sha1 from $image where img_name = $encFilename";
81                 $res = $dbr->query( $sql );
82                 $row = $dbr->fetchRow( $res );
83                 if( $row !== false ) {
84                         $hash = $row[0];
85                 }
86                 $dbr->freeResult( $res );
87         }
88
89         # Create the input form
90         $wgOut->addHTML(
91                 Xml::openElement( 'form', array( 'id' => 'fileduplicatesearch', 'method' => 'get', 'action' => $wgScript ) ) .
92                 Xml::hidden( 'title', SpecialPage::getTitleFor( 'FileDuplicateSearch' )->getPrefixedDbKey() ) .
93                 Xml::openElement( 'fieldset' ) .
94                 Xml::element( 'legend', null, wfMsg( 'fileduplicatesearch-legend' ) ) .
95                 Xml::inputLabel( wfMsg( 'fileduplicatesearch-filename' ), 'filename', 'filename', 50, $filename ) . ' ' .
96                 Xml::submitButton( wfMsg( 'fileduplicatesearch-submit' ) ) .
97                 Xml::closeElement( 'fieldset' ) .
98                 Xml::closeElement( 'form' )
99         );
100
101         if( $hash != '' ) {
102                 $align = $wgContLang->alignEnd();
103
104                 # Show a thumbnail of the file
105                 $img = wfFindFile( $title );
106                 if ( $img ) {
107                         $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
108                         if( $thumb ) {
109                                 $wgOut->addHTML( '<div style="float:' . $align . '" id="mw-fileduplicatesearch-icon">' .
110                                         $thumb->toHtml( array( 'desc-link' => false ) ) . '<br />' .
111                                         wfMsgExt( 'fileduplicatesearch-info', array( 'parse' ),
112                                                 $wgLang->formatNum( $img->getWidth() ),
113                                                 $wgLang->formatNum( $img->getHeight() ),
114                                                 $wgLang->formatSize( $img->getSize() ),
115                                                 $img->getMimeType()
116                                         ) .
117                                         '</div>' );
118                         }
119                 }
120
121                 # Do the query
122                 $wpp = new FileDuplicateSearchPage( $hash, $filename );
123                 list( $limit, $offset ) = wfCheckLimits();
124                 $count = $wpp->doQuery( $offset, $limit );
125
126                 # Show a short summary
127                 if( $count == 1 ) {
128                         $wgOut->wrapWikiMsg(
129                                 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
130                                 array( 'fileduplicatesearch-result-1', $filename )
131                         );
132                 } elseif ( $count > 1 ) {
133                         $wgOut->wrapWikiMsg(
134                                 "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
135                                 array( 'fileduplicatesearch-result-n', $filename, $wgLang->formatNum( $count - 1 ) )
136                         );
137                 }
138         }
139 }