From 5ddebb1b8512d1f2faf4e30a8faf1cfb5552fbb4 Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Thu, 3 Jul 2008 07:49:35 +0000 Subject: [PATCH] Cache foreign image descriptions in the transcache, saves an Http::get() on cache hit. --- RELEASE-NOTES | 2 ++ includes/filerepo/File.php | 42 +++++++++++++++++++++++++++++++++- includes/filerepo/FileRepo.php | 2 +- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 5cec8db2c9..bc60b96218 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -179,6 +179,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * Added blank special page Special:BlankPage for benchmarking, etc. * (bug 13862) Specialpages now has a horizontal TOC if there's three or more groups. +* Foreign repo file descriptions via fetchDescription are now cached in the + transcache. === Bug fixes in 1.13 === diff --git a/includes/filerepo/File.php b/includes/filerepo/File.php index 7a1c68e3dc..af5519c1e9 100644 --- a/includes/filerepo/File.php +++ b/includes/filerepo/File.php @@ -1067,13 +1067,53 @@ abstract class File { } $renderUrl = $this->repo->getDescriptionRenderUrl( $this->getName() ); if ( $renderUrl ) { + if ( $this->repo->useTransCache ) { + wfDebug("Attempting to get the description from the transwiki cache..."); + $this->purgeTransCacheEntries(); + $dbr = wfGetDB(DB_SLAVE); + $obj = $dbr->selectRow('transcache', array('tc_contents'), + array('tc_url' => $renderUrl)); + if ($obj) { + wfDebug("success!\n"); + return $obj->tc_contents; + } + wfDebug("miss\n"); + } wfDebug( "Fetching shared description from $renderUrl\n" ); - return Http::get( $renderUrl ); + $res = Http::get( $renderUrl ); + if ( $res && $this->repo->useTransCache ) $this->addToTransCache( $res, $renderUrl ); + return $res; } else { return false; } } + /** + * Purge expired transcache entries + */ + function purgeTranscacheEntries() { + global $wgTranscludeCacheExpiry; + $dbw = wfGetDB( DB_MASTER ); + $table = $dbw->tableName('transcache'); + $expiry = $dbw->addQuotes(time() - $wgTranscludeCacheExpiry); + $res = $dbw->delete( $table, array("tc_time < $expiry"), __METHOD__ ); + if ($res) wfDebug("purging old transcache entries..."); + } + + /** + * Add new transcache entry + * + * @param string $text Text to add to the cache + * @param string $url Url we're caching + */ + function addToTransCache( $text, $url ) { + $dbw = wfGetDB( DB_MASTER ); + $dbw->replace('transcache', array('tc_url'), array( + 'tc_url' => $url, + 'tc_time' => time(), + 'tc_contents' => $text)); + } + /** * Get discription of file revision * STUB diff --git a/includes/filerepo/FileRepo.php b/includes/filerepo/FileRepo.php index 3606292f5e..93089a8553 100644 --- a/includes/filerepo/FileRepo.php +++ b/includes/filerepo/FileRepo.php @@ -30,7 +30,7 @@ abstract class FileRepo { // Optional settings $this->initialCapital = true; // by default foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription', - 'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection' ) as $var ) + 'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection', 'useTransCache' ) as $var ) { if ( isset( $info[$var] ) ) { $this->$var = $info[$var]; -- 2.20.1