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_FILE
, $result->title
);
53 $text = $wgContLang->convert( $nt->getText() );
55 Title
::newFromText( $nt->getPrefixedText() ),
59 $user = $skin->link( Title
::makeTitle( NS_USER
, $result->img_user_text
), $result->img_user_text
);
60 $time = $wgLang->timeanddate( $result->img_timestamp
);
62 return "$plink . . $user . . $time";
67 * Output the HTML search form, and constructs the FileDuplicateSearch object.
69 function wfSpecialFileDuplicateSearch( $par = null ) {
70 global $wgRequest, $wgOut, $wgLang, $wgContLang, $wgScript;
73 $filename = isset( $par ) ?
$par : $wgRequest->getText( 'filename' );
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 ) {
86 $dbr->freeResult( $res );
89 # Create the input form
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' )
102 $align = $wgContLang->alignEnd();
104 # Show a thumbnail of the file
105 $img = wfFindFile( $title );
107 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
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() ),
122 $wpp = new FileDuplicateSearchPage( $hash, $filename );
123 list( $limit, $offset ) = wfCheckLimits();
124 $count = $wpp->doQuery( $offset, $limit );
126 # Show a short summary
129 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
130 array( 'fileduplicatesearch-result-1', $filename )
132 } elseif ( $count > 1 ) {
134 "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
135 array( 'fileduplicatesearch-result-n', $filename, $wgLang->formatNum( $count - 1 ) )