* (bug 1459) Search for duplicate files by hash: Special:FileDuplicateSearch
authorRaimond Spekking <raymond@users.mediawiki.org>
Wed, 19 Mar 2008 16:58:56 +0000 (16:58 +0000)
committerRaimond Spekking <raymond@users.mediawiki.org>
Wed, 19 Mar 2008 16:58:56 +0000 (16:58 +0000)
* Add a link on the image description page to Special:FileDuplicateSearch/filename.ext

RELEASE-NOTES
includes/AutoLoader.php
includes/ImagePage.php
includes/SpecialFileDuplicateSearch.php [new file with mode: 0644]
includes/SpecialPage.php
languages/messages/MessagesEn.php
maintenance/language/messages.inc

index 63cfdd6..626fd5c 100644 (file)
@@ -48,6 +48,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 * Add updatelog table to reliably permit updates that don't change the schema
 * Add category table to allow better tracking of category membership counts
 ** (bug 1212) Give correct membership counts on the pages of large categories
+* (bug 1459) Search for duplicate files by hash: Special:FileDuplicateSearch
 
 === Bug fixes in 1.13 ===
 
index c702586..cd315cd 100644 (file)
@@ -91,6 +91,7 @@ function __autoload($className) {
                'FewestrevisionsPage' => 'includes/SpecialFewestrevisions.php',
                'FileDeleteForm' => 'includes/FileDeleteForm.php',
                'FileDependency' => 'includes/CacheDependency.php',
+               'FileDuplicateSearch' => 'includes/SpecialFileDuplicateSearch.php',
                'FileRevertForm' => 'includes/FileRevertForm.php',
                'FileStore' => 'includes/FileStore.php',
                'FormatExif' => 'includes/Exif.php',
index a39da0a..0ff75d2 100644 (file)
@@ -397,7 +397,11 @@ EOT
                        $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
                        $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
                }
-               
+
+               # Link to Special:FileDuplicateSearch
+               $dupeLink = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'FileDuplicateSearch', $this->mTitle->getDBkey() ), wfMsgHtml( 'imagepage-searchdupe' ) );
+               $wgOut->addHtml( "<li>{$dupeLink}</li>" );
+
                # External editing link
                $elink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
                $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
diff --git a/includes/SpecialFileDuplicateSearch.php b/includes/SpecialFileDuplicateSearch.php
new file mode 100644 (file)
index 0000000..c48ffad
--- /dev/null
@@ -0,0 +1,133 @@
+<?php
+/**
+ * A special page to search for files by hash value as defined in the
+ * img_sha1 field in the image table
+ *
+ * @addtogroup SpecialPage
+ *
+ * @author Raimond Spekking, based on Special:MIMESearch by Ævar Arnfjörð Bjarmason
+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
+ */
+
+/**
+ * Searches the database for files of the requested hash, comparing this with the
+ * 'img_sha1' field in the image table.
+ */
+class FileDuplicateSearchPage extends QueryPage {
+       var $hash, $filename;
+
+       function FileDuplicateSearchPage( $hash, $filename ) {
+               $this->hash = $hash;
+               $this->filename = $filename;
+       }
+
+       function getName() { return 'FileDuplicateSearch'; }
+       function isExpensive() { return false; }
+       function isSyndicated() { return false; }
+
+       function linkParameters() {
+               return array( 'filename' => $this->filename );
+       }
+
+       function getSQL() {
+               $dbr = wfGetDB( DB_SLAVE );
+               $image = $dbr->tableName( 'image' );
+               $hash = $dbr->addQuotes( $this->hash );
+
+               return "SELECT 'FileDuplicateSearch' AS type,
+                               img_name AS title,
+                               img_sha1 AS value,
+                               img_user_text,
+                               img_timestamp
+                       FROM $image
+                       WHERE img_sha1 = $hash
+                       ";
+       }
+
+       function formatResult( $skin, $result ) {
+               global $wgContLang, $wgLang;
+
+               $nt = Title::makeTitle( NS_IMAGE, $result->title );
+               $text = $wgContLang->convert( $nt->getText() );
+               $plink = $skin->makeLink( $nt->getPrefixedText(), $text );
+
+               $user = $skin->makeLinkObj( Title::makeTitle( NS_USER, $result->img_user_text ), $result->img_user_text );
+               $time = $wgLang->timeanddate( $result->img_timestamp );
+
+               return "$plink . . $user . . $time";
+       }
+}
+
+/**
+ * Output the HTML search form, and constructs the FileDuplicateSearch object.
+ */
+function wfSpecialFileDuplicateSearch( $par = null ) {
+       global $wgRequest, $wgTitle, $wgOut, $wgLang, $wgContLang;
+
+       $hash = '';
+       $filename =  isset( $par ) ?  $par : $wgRequest->getText( 'filename' );
+
+       $title = Title::newFromText( $filename );
+       if( $title && $title->getText() != '' ) {
+               $dbr = wfGetDB( DB_SLAVE );
+               $image = $dbr->tableName( 'image' );
+               $encFilename = $dbr->addQuotes( htmlspecialchars( $title->getDbKey() ) );
+               $sql = "SELECT img_sha1 from $image where img_name = $encFilename";
+               $res = $dbr->query( $sql );
+               $row = $dbr->fetchRow( $res );
+               if( $row !== false ) {
+                       $hash = $row[0];
+               }
+               $dbr->freeResult( $res );
+       }
+
+       # Create the input form
+       $wgOut->addHTML(
+               Xml::openElement( 'form', array( 'id' => 'fileduplicatesearch', 'method' => 'get', 'action' => $wgTitle->getLocalUrl() ) ) .
+               Xml::openElement( 'fieldset' ) .
+               Xml::element( 'legend', null, wfMsg( 'fileduplicatesearch-legend' ) ) .
+               Xml::inputLabel( wfMsg( 'fileduplicatesearch-filename' ), 'filename', 'filename', 50, $filename ) . ' ' .
+               Xml::submitButton( wfMsg( 'fileduplicatesearch-submit' ) ) .
+               Xml::closeElement( 'fieldset' ) .
+               Xml::closeElement( 'form' )
+       );
+
+       if( $hash != '' ) {
+               $align = $wgContLang->isRtl() ? 'left' : 'right';
+
+               # Show a thumbnail of the file
+               $img = wfFindFile( $title );
+               if ( $img ) {
+                       $thumb = $img->getThumbnail( 120, 120 );
+                       if( $thumb ) {
+                               $wgOut->addHTML( '<div style="float:' . $align . '" id="mw-fileduplicatesearch-icon">' .
+                                       $thumb->toHtml( array( 'desc-link' => false ) ) . '<br />' .
+                                       wfMsgExt( 'fileduplicatesearch-info', array( 'parse' ), 
+                                               $wgLang->formatNum( $img->getWidth() ),
+                                               $wgLang->formatNum( $img->getHeight() ),
+                                               $wgLang->formatSize( $img->getSize() ),
+                                               $img->getMimeType()
+                                       ) .
+                                       '</div>' );
+                       }
+               }
+
+               # Do the query
+               $wpp = new FileDuplicateSearchPage( $hash, $filename );
+               list( $limit, $offset ) = wfCheckLimits();
+               $count = $wpp->doQuery( $offset, $limit );
+
+               # Show a short summary
+               if( $count == 1 ) {
+                       $wgOut->addHTML( '<p class="mw-fileduplicatesearch-result-1">' .
+                               wfMsgHtml( 'fileduplicatesearch-result-1', $filename ) .
+                               '</p>'
+                       );
+               } elseif ( $count > 1 ) {
+                       $wgOut->addHTML( '<p class="mw-fileduplicatesearch-result-n">' .
+                               wfMsgExt( 'fileduplicatesearch-result-n', array( 'parseinline' ), $filename, $wgLang->formatNum( $count - 1 ) ) .
+                               '</p>'
+                       );
+               }
+       }
+}
index a1890b9..c9380ba 100644 (file)
@@ -134,6 +134,7 @@ class SpecialPage
                'Unlockdb'                  => array( 'SpecialPage', 'Unlockdb', 'siteadmin' ),
                'Userrights'                => 'UserrightsPage',
                'MIMEsearch'                => array( 'SpecialPage', 'MIMEsearch' ),
+               'FileDuplicateSearch'       => array( 'SpecialPage', 'FileDuplicateSearch' ),
                'Unwatchedpages'            => array( 'SpecialPage', 'Unwatchedpages', 'unwatchedpages' ),
                'Listredirects'             => array( 'SpecialPage', 'Listredirects' ),
                'Revisiondelete'            => array( 'UnlistedSpecialPage', 'Revisiondelete', 'deleterevision' ),
index 02da6d3..f8d9648 100644 (file)
@@ -411,6 +411,7 @@ $specialPageAliases = array(
        'Unlockdb'                  => array( 'UnlockDB' ),
        'Userrights'                => array( 'UserRights' ),
        'MIMEsearch'                => array( 'MIMESearch' ),
+       'FileDuplicateSearch'       => array( 'FileDuplicateSearch' ),
        'Unwatchedpages'            => array( 'UnwatchedPages' ),
        'Listredirects'             => array( 'ListRedirects' ),
        'Revisiondelete'            => array( 'RevisionDelete' ),
@@ -1546,6 +1547,7 @@ A click on a column header changes the sorting.',
 'noimage'                   => 'No file by this name exists, you can $1.',
 'noimage-linktext'          => 'upload it',
 'uploadnewversion-linktext' => 'Upload a new version of this file',
+'imagepage-searchdupe'      => 'Search for duplicate files',
 
 # File reversion
 'filerevert'                => 'Revert $1',
@@ -3129,4 +3131,16 @@ Images are shown in full resolution, other file types are started with their ass
 
 Enter the file name without the "{{ns:image}}:" prefix.',
 
+# Special:FileDuplicateSearch
+'fileduplicatesearch'          => 'Search for duplicate files',
+'fileduplicatesearch-summary'  => 'Search for duplicate files on base of its hash value.
+
+Enter the filename without the "{{ns:image}}:" prefix.',
+'fileduplicatesearch-legend'   => 'Search for a duplicate',
+'fileduplicatesearch-filename' => 'Filename:',
+'fileduplicatesearch-submit'   => 'Search',
+'fileduplicatesearch-info'     => '$1 × $2 pixel<br />File size: $3<br />MIME type: $4',
+'fileduplicatesearch-result-1' => 'The file "$1" has no identical duplication.',
+'fileduplicatesearch-result-n' => 'The file "$1" has {{PLURAL:$2|1 identical duplication|$2 identical duplications}}.',
+
 );
index 3000b4e..d653e17 100644 (file)
@@ -958,6 +958,7 @@ $wgMessageStructure = array(
                'noimage',
                'noimage-linktext',
                'uploadnewversion-linktext',
+               'imagepage-searchdupe',
        ),
        'filerevert' => array(
                'filerevert',
@@ -2396,7 +2397,18 @@ $wgMessageStructure = array(
                'filepath-submit',
                'filepath-summary',
        ),
+       'fileduplicatesearch' => array(
+               'fileduplicatesearch',
+               'fileduplicatesearch-summary',
+               'fileduplicatesearch-legend',
+               'fileduplicatesearch-filename',
+               'fileduplicatesearch-submit',
+               'fileduplicatesearch-info',
+               'fileduplicatesearch-result-1',
+               'fileduplicatesearch-result-n',
+       ),
 );
+
 /** Comments for each block */
 $wgBlockComments = array(
        'sidebar'             => "The sidebar for MonoBook is generated from this message, lines that do not
@@ -2574,6 +2586,7 @@ Variants for Chinese language",
        'CoreParserFunctions'   => 'Core parser functions',
        'version'               => 'Special:Version',
        'filepath'              => 'Special:Filepath',
+       'fileduplicate'         => 'Special:FileDuplicateSearch',
 );
 
 /** Short comments for standalone messages */