From fe0f192c673bba84ebdc8e4fcf782a8d0545f0d1 Mon Sep 17 00:00:00 2001 From: Bryan Tong Minh Date: Thu, 15 May 2008 19:17:21 +0000 Subject: [PATCH] Do some refactoring on ImagePage. Shuffled the links section; Use Title::getRedirectsHere; Move duplicate detection to its own function. --- includes/ImagePage.php | 78 +++++++++++++++++++------------------- includes/Title.php | 23 ++++++++--- includes/filerepo/File.php | 6 +++ 3 files changed, 64 insertions(+), 43 deletions(-) diff --git a/includes/ImagePage.php b/includes/ImagePage.php index 57cbf5bbc0..8dfcff5dec 100644 --- a/includes/ImagePage.php +++ b/includes/ImagePage.php @@ -18,6 +18,7 @@ class ImagePage extends Article { /* private */ var $img; // Image object this page is shown for /* private */ var $repo; var $mExtraDescription = false; + var $dupes; function __construct( $title, $time = false ) { parent::__construct( $title ); @@ -28,7 +29,8 @@ class ImagePage extends Article { } else { $this->current = $time ? wfLocalFile( $this->mTitle ) : $this->img; } - $this->repo = $this->img->repo; + $this->repo = $this->img->getRepo(); + $this->dupes = null; } /** @@ -104,11 +106,15 @@ class ImagePage extends Article { $this->closeShowImage(); $this->imageHistory(); // TODO: Cleanup the following - $this->imageLinks(); + + $wgOut->addHTML( Xml::element( 'h2', + array( 'id' => 'filelinks' ), + wfMsg( 'imagelinks' ) ) . "\n" ); $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 ( $this->img->isLocal() ) $this->imageRedirects(); + $this->imageLinks(); if ( $showmeta ) { global $wgStylePath, $wgStyleVersion; @@ -155,6 +161,28 @@ class ImagePage extends Article { public function getFile() { return $this->img; } + + public function getDuplicates() { + if ( !is_null($this->dupes) ) return $this->dupes; + + if ( !( $hash = $this->img->getSha1() ) ) + return $this->dupes = array(); + $dupes = RepoGroup::singleton()->findBySha1( $hash ); + + // Remove duplicates with self and non matching file sizes + $self = $this->img->getRepoName().':'.$this->img->getName(); + $size = $this->img->getSize(); + foreach ( $dupes as $index => $file ) { + $key = $file->getRepoName().':'.$file->getName(); + if ( $key == $self ) + unset( $dupes[$index] ); + if ( $file->getSize() != $size ) + unset( $dupes[$index] ); + } + return $this->dupes = $dupes; + + } + /** * Create the TOC @@ -438,6 +466,9 @@ EOT } } + /* + * Check for files with the same name on the foreign repos. + */ function checkSharedConflict() { global $wgOut, $wgUser; $repoGroup = RepoGroup::singleton(); @@ -561,8 +592,6 @@ EOT $limit = 100; - $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" ); - $dbr = wfGetDB( DB_SLAVE ); $res = $dbr->select( @@ -603,53 +632,26 @@ EOT { global $wgUser, $wgOut; - $dbr = wfGetDB( DB_SLAVE ); - $res = $dbr->select( - array( 'redirect', 'page' ), - array( 'page_title' ), - array( - 'rd_namespace' => NS_IMAGE, - 'rd_title' => $this->mTitle->getDBkey(), - 'page_namespace' => NS_IMAGE, - 'rd_from = page_id' - ), - __METHOD__ - ); + $redirects = $this->getTitle()->getRedirectsHere( NS_IMAGE ); + if ( count( $redirects ) == 0 ) return; - if ( 0 == $dbr->numRows( $res ) ) - return; - $wgOut->addWikiMsg( 'redirectstofile' ); $wgOut->addHTML( "\n" ); - - $res->free(); + } function imageDupes() { global $wgOut, $wgUser; - if ( !( $hash = $this->img->getSha1() ) ) return; - // Probably deprecates checkSharedConflict? - $dupes = RepoGroup::singleton()->findBySha1( $hash ); - //$dupes = RepoGroup::singleton()->getLocalRepo()->findBySha1( $hash ); - - // Don't dupe with self - $self = $this->img->getRepoName().':'.$this->img->getName(); - foreach ( $dupes as $index => $file ) { - $key = $file->getRepoName().':'.$file->getName(); - if ( $key == $self ) - unset( $dupes[$index] ); - } + $dupes = $this->getDuplicates(); if ( count( $dupes ) == 0 ) return; $wgOut->addWikiMsg( 'duplicatesoffile' ); diff --git a/includes/Title.php b/includes/Title.php index b5b2e555bc..0499d3fb52 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -3126,12 +3126,25 @@ class Title { return MWNamespace::isContent( $this->getNamespace() ); } - public function getRedirectsHere() { + public function getRedirectsHere( $ns = null ) { $redirs = array(); - $dbr = wfGetDB( DB_SLAVE ); - list($page,$redirect) = $dbr->tableNamesN( 'page', 'redirect' ); - $result = $dbr->query( "SELECT page_title, page_namespace FROM $page JOIN $redirect ON page_id = rd_from WHERE rd_title = " - . $dbr->addQuotes( $this->getDBKey() ) . " AND rd_namespace = " . $this->getNamespace(), __METHOD__ ); + + $dbr = wfGetDB( DB_SLAVE ); + $where = array( + 'rd_namespace' => $this->getNamespace(), + 'rd_title' => $this->getDBkey(), + 'rd_from = page_id' + ); + if ( !is_null($ns) ) $where['page_namespace'] = $ns; + + $result = $dbr->select( + array( 'redirect', 'page' ), + array( 'page_namespace', 'page_title' ), + $where, + __METHOD__ + ); + + while( $row = $dbr->fetchObject( $result ) ) { $redirs[] = self::newFromRow( $row ); } diff --git a/includes/filerepo/File.php b/includes/filerepo/File.php index 59ee21ee4f..6e5c72489a 100644 --- a/includes/filerepo/File.php +++ b/includes/filerepo/File.php @@ -904,6 +904,12 @@ abstract class File { function getRepoName() { return $this->repo ? $this->repo->getName() : 'unknown'; } + /* + * Returns the repository + */ + function getRepo() { + return $this->repo; + } /** * Returns true if the image is an old version -- 2.20.1