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