* Introduce LocalFile::selectFields similarly to Revision::SelectFields
authorBryan Tong Minh <btongminh@users.mediawiki.org>
Wed, 14 May 2008 15:11:48 +0000 (15:11 +0000)
committerBryan Tong Minh <btongminh@users.mediawiki.org>
Wed, 14 May 2008 15:11:48 +0000 (15:11 +0000)
* Introduce FileRepo::findBySha1
* Show duplicates on ImagePage

includes/ImagePage.php
includes/filerepo/FileRepo.php
includes/filerepo/LocalFile.php
includes/filerepo/LocalRepo.php
includes/filerepo/RepoGroup.php

index 399fc1c..d9243bc 100644 (file)
@@ -103,7 +103,11 @@ class ImagePage extends Article {
 
                $this->closeShowImage();
                $this->imageHistory();
+               // TODO: Cleanup the following
                $this->imageLinks();
+               $this->imageDupes();
+               // TODO: We may want to find local images redirecting to a foreign 
+               // file: "The following local files redirect to this file"
                if ( $this->img->isLocal() ) $this->imageRedirects();
 
                if ( $showmeta ) {
@@ -630,6 +634,37 @@ EOT
                
                $res->free();
        }
+       
+       function imageDupes() {
+               global $wgOut, $wgUser;         
+       
+               if ( !( $hash = $this->img->getSha1() ) ) return;
+               // Should find a proper way to link to foreign files
+               // Deprecates checkSharedConflict
+               //$dupes = RepoGroup::singleton()->findBySha1( $hash );
+               $dupes = RepoGroup::singleton()->getLocalRepo()->findBySha1( $hash );
+               
+               // Don't dupe with self
+               $index = 0;
+               $self = $this->img->getRepoName().':'.$this->img->getName();
+               foreach ( $dupes as $file ) {
+                       $key = $file->getRepoName().':'.$file->getName();
+                       if ( $key == $self )
+                               unset( $dupes[$index] );
+                       $index++;
+               }
+               if ( count( $dupes ) == 0 ) return;
+               
+               $wgOut->addWikiMsg( 'duplicatesoffile' );
+               $wgOut->addHTML( "<ul>\n" );
+               
+               $sk = $wgUser->getSkin();
+               foreach ( $dupes as $file ) {
+                       $link = $sk->makeKnownLinkObj( $file->getTitle(), "" );
+                       $wgOut->addHTML( "<li>{$link}</li>\n" );
+               }
+               $wgOut->addHTML( "</ul>\n" );
+       }
 
        /**
         * Delete the file, or an earlier version of it
index 5c847e1..af2fc94 100644 (file)
@@ -449,4 +449,8 @@ abstract class FileRepo {
         */
        function invalidateImageRedirect( $title ) {
        }
+       
+       function findBySha1( $hash ) {
+               return array();
+       }
 }
index 1f1c84d..4978fd3 100644 (file)
@@ -73,6 +73,28 @@ class LocalFile extends File
                $file->loadFromRow( $row );
                return $file;
        }
+       
+       /**
+        * Fields in the image table
+        */
+       static function selectFields() {
+               return array(
+                       'img_name',
+                       'img_size',
+                       'img_width',
+                       'img_height',
+                       'img_metadata',
+                       'img_bits',
+                       'img_media_type',
+                       'img_major_mime',
+                       'img_minor_mime',
+                       'img_description',
+                       'img_user',
+                       'img_user_text',
+                       'img_timestamp',
+                       'img_sha1',
+               );
+       }
 
        /**
         * Constructor.
index e123a52..41e70f1 100644 (file)
@@ -142,4 +142,19 @@ class LocalRepo extends FSRepo {
                $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
                $wgMemc->delete( $memcKey );
        }
+       
+       function findBySha1( $hash ) {
+               $dbr = $this->getSlaveDB();
+               $res = $dbr->select(
+                       'image',
+                       LocalFile::selectFields(),
+                       array( 'img_sha1' => $hash )
+               );
+               
+               $result = array();
+               while ( $row = $res->fetchObject() )
+                       $result[] = $this->newFileFromRow( $row );
+               $res->free();
+               return $result;
+       }
 }
index 477265e..00c1a29 100644 (file)
@@ -97,6 +97,17 @@ class RepoGroup {
                }
                return false;
        }
+       
+       function findBySha1( $hash ) {
+               if ( !$this->reposInitialised ) {
+                       $this->initialiseRepos();
+               }
+               
+               $result = $this->localRepo->findBySha1( $hash );
+               foreach ( $this->foreignRepos as $repo )
+                       $result = array_merge( $result, $repo->findBySha1( $hash ) );
+               return $result;         
+       }
 
        /**
         * Get the repo instance with a given key.