3 * A special page to search for files by hash value as defined in the
4 * img_sha1 field in the image table
6 * @addtogroup SpecialPage
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
13 * Searches the database for files of the requested hash, comparing this with the
14 * 'img_sha1' field in the image table.
16 class FileDuplicateSearchPage
extends QueryPage
{
19 function FileDuplicateSearchPage( $hash, $filename ) {
21 $this->filename
= $filename;
24 function getName() { return 'FileDuplicateSearch'; }
25 function isExpensive() { return false; }
26 function isSyndicated() { return false; }
28 function linkParameters() {
29 return array( 'filename' => $this->filename
);
33 $dbr = wfGetDB( DB_SLAVE
);
34 $image = $dbr->tableName( 'image' );
35 $hash = $dbr->addQuotes( $this->hash
);
37 return "SELECT 'FileDuplicateSearch' AS type,
43 WHERE img_sha1 = $hash
47 function formatResult( $skin, $result ) {
48 global $wgContLang, $wgLang;
50 $nt = Title
::makeTitle( NS_IMAGE
, $result->title
);
51 $text = $wgContLang->convert( $nt->getText() );
52 $plink = $skin->makeLink( $nt->getPrefixedText(), $text );
54 $user = $skin->makeLinkObj( Title
::makeTitle( NS_USER
, $result->img_user_text
), $result->img_user_text
);
55 $time = $wgLang->timeanddate( $result->img_timestamp
);
57 return "$plink . . $user . . $time";
62 * Output the HTML search form, and constructs the FileDuplicateSearch object.
64 function wfSpecialFileDuplicateSearch( $par = null ) {
65 global $wgRequest, $wgTitle, $wgOut, $wgLang, $wgContLang;
68 $filename = isset( $par ) ?
$par : $wgRequest->getText( 'filename' );
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 ) {
81 $dbr->freeResult( $res );
84 # Create the input form
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' )
96 $align = $wgContLang->isRtl() ?
'left' : 'right';
98 # Show a thumbnail of the file
99 $img = wfFindFile( $title );
101 $thumb = $img->getThumbnail( 120, 120 );
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() ),
116 $wpp = new FileDuplicateSearchPage( $hash, $filename );
117 list( $limit, $offset ) = wfCheckLimits();
118 $count = $wpp->doQuery( $offset, $limit );
120 # Show a short summary
122 $wgOut->addHTML( '<p class="mw-fileduplicatesearch-result-1">' .
123 wfMsgHtml( 'fileduplicatesearch-result-1', $filename ) .
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 ) ) .