From f1850a9238924c0b11b89ca387c905446e809a9e Mon Sep 17 00:00:00 2001 From: Victor Vasiliev Date: Wed, 16 Jan 2008 18:27:43 +0000 Subject: [PATCH] * Support redirects in image namespace Now doesn't require schema change --- RELEASE-NOTES | 1 + includes/Linker.php | 4 ++++ includes/Wiki.php | 4 ++++ includes/filerepo/FileRepo.php | 23 ++++++++++++++++++ includes/filerepo/LocalRepo.php | 42 +++++++++++++++++++++++++++++++++ includes/filerepo/RepoGroup.php | 21 +++++++++++++++++ 6 files changed, 95 insertions(+) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 1aa8f28f2d..0d50a7cad5 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -132,6 +132,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * Add APCOND_INGROUPS * Add DBA caching to installer * (bug 18585) Added a bunch of parameters to the revertpage message +* Support redirects in image namespace === Bug fixes in 1.12 === diff --git a/includes/Linker.php b/includes/Linker.php index e0dc1a7dd3..678be8d799 100644 --- a/includes/Linker.php +++ b/includes/Linker.php @@ -723,6 +723,10 @@ class Linker { $upload = SpecialPage::getTitleFor( 'Upload' ); if( $text == '' ) $text = htmlspecialchars( $title->getPrefixedText() ); + $redir = RepoGroup::getLocalRepo()->checkRedirect( $title ); + if( $redir ) { + return $this->makeKnownLinkObj( $title, $text, $query, $trail, $prefix ); + } $q = 'wpDestFile=' . $title->getPartialUrl(); if( $query != '' ) $q .= '&' . $query; diff --git a/includes/Wiki.php b/includes/Wiki.php index e1a3c5ff9c..2a60262c83 100644 --- a/includes/Wiki.php +++ b/includes/Wiki.php @@ -220,6 +220,10 @@ class MediaWiki { switch( $title->getNamespace() ) { case NS_IMAGE: + $file = wfFindFile( $title ); + if( $file && $file->getRedirected() ) { + return new Article( $title ); + } return new ImagePage( $title ); case NS_CATEGORY: return new CategoryPage( $title ); diff --git a/includes/filerepo/FileRepo.php b/includes/filerepo/FileRepo.php index cf6d65c2ce..96bcd19572 100644 --- a/includes/filerepo/FileRepo.php +++ b/includes/filerepo/FileRepo.php @@ -90,6 +90,19 @@ abstract class FileRepo { if ( $img->exists() ) { return $img; } + + # Now try redirects + $redir = $this->checkRedirect( $title ); + if( $redir ) { + $img = $this->newFile( $redir ); + if( !$img ) { + return false; + } + if( $img->exists() ) { + $img->redirectedFrom( $title->getText() ); + return $img; + } + } } /** @@ -400,5 +413,15 @@ abstract class FileRepo { * STUB */ function cleanupDeletedBatch( $storageKeys ) {} + + /** + * Checks if there is a redirect named as $title + * STUB + * + * @param Title $title Title of image + */ + function checkRedirect( $title ) { + return false; + } } diff --git a/includes/filerepo/LocalRepo.php b/includes/filerepo/LocalRepo.php index 72f9e9a6c3..021e67942f 100644 --- a/includes/filerepo/LocalRepo.php +++ b/includes/filerepo/LocalRepo.php @@ -62,4 +62,46 @@ class LocalRepo extends FSRepo { } return $status; } + + /** + * Function link Title::getArticleID(). + * We can't say Title object, what database it should use, so we duplicate that function here. + */ + function getArticleID( $title ) { + if( !$title instanceof Title ) { + return 0; + } + $dbr = $this->getSlaveDB(); + $id = $dbr->selectField( + 'page', // Table + 'page_id', //Field + array( //Conditions + 'page_namespace' => $title->getNamespace(), + 'page_title' => $title->getDbKey(), + ), + __METHOD__ //Function name + ); + return $id; + } + + function checkRedirect( $title ) { + $id = $this->getArticleID( $title ); + if( !$id ) { + return false; + } + $dbr = $this->getSlaveDB(); + $row = $dbr->selectRow( + 'redirect', + array( 'rd_title', 'rd_namespace' ), + array( 'rd_from' => $id ), + __METHOD__ + ); + if( !$row ) { + return false; + } + if( $row->rd_namespace != NS_IMAGE ) { + return false; + } + return Title::makeTitle( $row->rd_namespace, $row->rd_title ); + } } diff --git a/includes/filerepo/RepoGroup.php b/includes/filerepo/RepoGroup.php index 4e9a8a6dc6..e5e49d0422 100644 --- a/includes/filerepo/RepoGroup.php +++ b/includes/filerepo/RepoGroup.php @@ -76,6 +76,27 @@ class RepoGroup { return false; } + /** + * Interface for FileRepo::checkRedirect() + */ + function checkRedirect( $title ) { + if ( !$this->reposInitialised ) { + $this->initialiseRepos(); + } + + $redir = $this->localRepo->checkRedirect( $title ); + if( $redir ) { + return $redir; + } + foreach ( $this->foreignRepos as $repo ) { + $redir = $repo->checkRedirect( $title ); + if ( $redir ) { + return $redir; + } + } + return false; + } + /** * Get the repo instance with a given key. */ -- 2.20.1