From 5510874e984d89e013d664f5dfdb0eb80793c359 Mon Sep 17 00:00:00 2001 From: Bryan Tong Minh Date: Wed, 14 May 2008 15:11:48 +0000 Subject: [PATCH] * Introduce LocalFile::selectFields similarly to Revision::SelectFields * Introduce FileRepo::findBySha1 * Show duplicates on ImagePage --- includes/ImagePage.php | 35 +++++++++++++++++++++++++++++++++ includes/filerepo/FileRepo.php | 4 ++++ includes/filerepo/LocalFile.php | 22 +++++++++++++++++++++ includes/filerepo/LocalRepo.php | 15 ++++++++++++++ includes/filerepo/RepoGroup.php | 11 +++++++++++ 5 files changed, 87 insertions(+) diff --git a/includes/ImagePage.php b/includes/ImagePage.php index 399fc1cad0..d9243bc2c2 100644 --- a/includes/ImagePage.php +++ b/includes/ImagePage.php @@ -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( "\n" ); + } /** * Delete the file, or an earlier version of it diff --git a/includes/filerepo/FileRepo.php b/includes/filerepo/FileRepo.php index 5c847e1600..af2fc941d3 100644 --- a/includes/filerepo/FileRepo.php +++ b/includes/filerepo/FileRepo.php @@ -449,4 +449,8 @@ abstract class FileRepo { */ function invalidateImageRedirect( $title ) { } + + function findBySha1( $hash ) { + return array(); + } } diff --git a/includes/filerepo/LocalFile.php b/includes/filerepo/LocalFile.php index 1f1c84d3df..4978fd3af5 100644 --- a/includes/filerepo/LocalFile.php +++ b/includes/filerepo/LocalFile.php @@ -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. diff --git a/includes/filerepo/LocalRepo.php b/includes/filerepo/LocalRepo.php index e123a52f3a..41e70f1fc0 100644 --- a/includes/filerepo/LocalRepo.php +++ b/includes/filerepo/LocalRepo.php @@ -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; + } } diff --git a/includes/filerepo/RepoGroup.php b/includes/filerepo/RepoGroup.php index 477265e242..00c1a29f95 100644 --- a/includes/filerepo/RepoGroup.php +++ b/includes/filerepo/RepoGroup.php @@ -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. -- 2.20.1