3 * A special page to search for files by hash value as defined in the
4 * img_sha1 field in the image table
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
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
18 class FileDuplicateSearchPage
extends QueryPage
{
21 function FileDuplicateSearchPage( $hash, $filename ) {
23 $this->filename
= $filename;
26 function getName() { return 'FileDuplicateSearch'; }
27 function isExpensive() { return false; }
28 function isSyndicated() { return false; }
30 function linkParameters() {
31 return array( 'filename' => $this->filename
);
35 $dbr = wfGetDB( DB_SLAVE
);
36 $image = $dbr->tableName( 'image' );
37 $hash = $dbr->addQuotes( $this->hash
);
39 return "SELECT 'FileDuplicateSearch' AS type,
45 WHERE img_sha1 = $hash
49 function formatResult( $skin, $result ) {
50 global $wgContLang, $wgLang;
52 $nt = Title
::makeTitle( NS_IMAGE
, $result->title
);
53 $text = $wgContLang->convert( $nt->getText() );
54 $plink = $skin->makeLink( $nt->getPrefixedText(), $text );
56 $user = $skin->makeLinkObj( Title
::makeTitle( NS_USER
, $result->img_user_text
), $result->img_user_text
);
57 $time = $wgLang->timeanddate( $result->img_timestamp
);
59 return "$plink . . $user . . $time";
64 * Output the HTML search form, and constructs the FileDuplicateSearch object.
66 function wfSpecialFileDuplicateSearch( $par = null ) {
67 global $wgRequest, $wgTitle, $wgOut, $wgLang, $wgContLang;
70 $filename = isset( $par ) ?
$par : $wgRequest->getText( 'filename' );
72 $title = Title
::newFromText( $filename );
73 if( $title && $title->getText() != '' ) {
74 $dbr = wfGetDB( DB_SLAVE
);
75 $image = $dbr->tableName( 'image' );
76 $encFilename = $dbr->addQuotes( htmlspecialchars( $title->getDbKey() ) );
77 $sql = "SELECT img_sha1 from $image where img_name = $encFilename";
78 $res = $dbr->query( $sql );
79 $row = $dbr->fetchRow( $res );
80 if( $row !== false ) {
83 $dbr->freeResult( $res );
86 # Create the input form
88 Xml
::openElement( 'form', array( 'id' => 'fileduplicatesearch', 'method' => 'get', 'action' => $wgTitle->getLocalUrl() ) ) .
89 Xml
::openElement( 'fieldset' ) .
90 Xml
::element( 'legend', null, wfMsg( 'fileduplicatesearch-legend' ) ) .
91 Xml
::inputLabel( wfMsg( 'fileduplicatesearch-filename' ), 'filename', 'filename', 50, $filename ) . ' ' .
92 Xml
::submitButton( wfMsg( 'fileduplicatesearch-submit' ) ) .
93 Xml
::closeElement( 'fieldset' ) .
94 Xml
::closeElement( 'form' )
98 $align = $wgContLang->isRtl() ?
'left' : 'right';
100 # Show a thumbnail of the file
101 $img = wfFindFile( $title );
103 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
105 $wgOut->addHTML( '<div style="float:' . $align . '" id="mw-fileduplicatesearch-icon">' .
106 $thumb->toHtml( array( 'desc-link' => false ) ) . '<br />' .
107 wfMsgExt( 'fileduplicatesearch-info', array( 'parse' ),
108 $wgLang->formatNum( $img->getWidth() ),
109 $wgLang->formatNum( $img->getHeight() ),
110 $wgLang->formatSize( $img->getSize() ),
118 $wpp = new FileDuplicateSearchPage( $hash, $filename );
119 list( $limit, $offset ) = wfCheckLimits();
120 $count = $wpp->doQuery( $offset, $limit );
122 # Show a short summary
124 $wgOut->addHTML( '<p class="mw-fileduplicatesearch-result-1">' .
125 wfMsgHtml( 'fileduplicatesearch-result-1', $filename ) .
128 } elseif ( $count > 1 ) {
129 $wgOut->addHTML( '<p class="mw-fileduplicatesearch-result-n">' .
130 wfMsgExt( 'fileduplicatesearch-result-n', array( 'parseinline' ), $filename, $wgLang->formatNum( $count - 1 ) ) .