-<?php\r
-\r
-/**\r
- * Class to represent a file in the oldimage table\r
- *\r
- * @addtogroup FileRepo\r
- */\r
-class OldLocalFile extends LocalFile {\r
- var $requestedTime, $archive_name;\r
-\r
- const CACHE_VERSION = 1;\r
- const MAX_CACHE_ROWS = 20;\r
-\r
- function newFromTitle( $title, $repo, $time ) {\r
- return new self( $title, $repo, $time, null );\r
- }\r
-\r
- function newFromArchiveName( $title, $repo, $archiveName ) {\r
- return new self( $title, $repo, null, $archiveName );\r
- }\r
-\r
- function newFromRow( $row, $repo ) {\r
- $title = Title::makeTitle( NS_IMAGE, $row->oi_name );\r
- $file = new self( $title, $repo, null, $row->oi_archive_name );\r
- $file->loadFromRow( $row, 'oi_' );\r
- return $file;\r
- }\r
-\r
- /**\r
- * @param Title $title\r
- * @param FileRepo $repo\r
- * @param string $time Timestamp or null to load by archive name\r
- * @param string $archiveName Archive name or null to load by timestamp\r
- */\r
- function __construct( $title, $repo, $time, $archiveName ) {\r
- parent::__construct( $title, $repo );\r
- $this->requestedTime = $time;\r
- $this->archive_name = $archiveName;\r
- if ( is_null( $time ) && is_null( $archiveName ) ) {\r
- throw new MWException( __METHOD__.': must specify at least one of $time or $archiveName' );\r
- }\r
- }\r
-\r
- function getCacheKey() {\r
- $hashedName = md5($this->getName());\r
- return wfMemcKey( 'oldfile', $hashedName );\r
- }\r
-\r
- function getArchiveName() {\r
- if ( !isset( $this->archive_name ) ) {\r
- $this->load();\r
- }\r
- return $this->archive_name;\r
- }\r
-\r
- function isOld() {\r
- return true;\r
- }\r
-\r
- /**\r
- * Try to load file metadata from memcached. Returns true on success.\r
- */\r
- function loadFromCache() {\r
- global $wgMemc;\r
- wfProfileIn( __METHOD__ );\r
- $this->dataLoaded = false;\r
- $key = $this->getCacheKey();\r
- if ( !$key ) {\r
- return false;\r
- }\r
- $oldImages = $wgMemc->get( $key );\r
-\r
- if ( isset( $oldImages['version'] ) && $oldImages['version'] == MW_OLDFILE_VERSION ) {\r
- unset( $oldImages['version'] );\r
- $more = isset( $oldImages['more'] );\r
- unset( $oldImages['more'] );\r
- $found = false;\r
- if ( is_null( $this->requestedTime ) ) {\r
- foreach ( $oldImages as $timestamp => $info ) {\r
- if ( $info['archive_name'] == $this->archive_name ) {\r
- $found = true;\r
- break;\r
- }\r
- }\r
- } else {\r
- krsort( $oldImages );\r
- foreach ( $oldImages as $timestamp => $info ) {\r
- if ( $timestamp <= $this->requestedTime ) {\r
- $found = true;\r
- break;\r
- }\r
- }\r
- }\r
- if ( $found ) {\r
- wfDebug( "Pulling file metadata from cache key {$key}[{$timestamp}]\n" );\r
- $this->dataLoaded = true;\r
- foreach ( $cachedValues as $name => $value ) {\r
- $this->$name = $value;\r
- }\r
- } elseif ( $more ) {\r
- wfDebug( "Cache key was truncated, oldimage row might be found in the database\n" );\r
- } else {\r
- wfDebug( "Image did not exist at the specified time.\n" );\r
- $this->fileExists = false;\r
- $this->dataLoaded = true;\r
- }\r
- }\r
-\r
- if ( $this->dataLoaded ) {\r
- wfIncrStats( 'image_cache_hit' );\r
- } else {\r
- wfIncrStats( 'image_cache_miss' );\r
- }\r
-\r
- wfProfileOut( __METHOD__ );\r
- return $this->dataLoaded;\r
- }\r
-\r
- function saveToCache() {\r
- // Cache the entire history of the image (up to MAX_CACHE_ROWS).\r
- // This is expensive, so we only do it if $wgMemc is real\r
- global $wgMemc;\r
- if ( $wgMemc instanceof FakeMemcachedClient ) {\r
- return;\r
- }\r
- $key = $this->getCacheKey();\r
- if ( !$key ) { \r
- return;\r
- }\r
- wfProfileIn( __METHOD__ );\r
-\r
- $dbr = $this->repo->getSlaveDB();\r
- $res = $dbr->select( 'oldimage', $this->getCacheFields(),\r
- array( 'oi_name' => $this->getName() ), __METHOD__, \r
- array( \r
- 'LIMIT' => self::MAX_CACHE_ROWS + 1,\r
- 'ORDER BY' => 'oi_timestamp DESC',\r
- ));\r
- $cache = array( 'version' => self::CACHE_VERSION );\r
- $numRows = $dbr->numRows( $res );\r
- if ( $numRows > self::MAX_CACHE_ROWS ) {\r
- $cache['more'] = true;\r
- $numRows--;\r
- }\r
- for ( $i = 0; $i < $numRows; $i++ ) {\r
- $row = $dbr->fetchObject( $res );\r
- $this->decodeRow( $row, 'oi_' );\r
- $cache[$row->oi_timestamp] = $row;\r
- }\r
- $dbr->freeResult( $res );\r
- $wgMemc->set( $key, $cache, 7*86400 /* 1 week */ );\r
- wfProfileOut( __METHOD__ );\r
- }\r
-\r
- function loadFromDB() {\r
- wfProfileIn( __METHOD__ );\r
- $dbr = $this->repo->getSlaveDB();\r
- $conds = array( 'oi_name' => $this->getName() );\r
- if ( is_null( $this->requestedTime ) ) {\r
- $conds['oi_archive_name'] = $this->archive_name;\r
- } else {\r
- $conds[] = 'oi_timestamp <= ' . $dbr->addQuotes( $this->requestedTime );\r
- }\r
- $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),\r
- $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );\r
- if ( $row ) {\r
- $this->loadFromRow( $row, 'oi_' );\r
- } else {\r
- $this->fileExists = false;\r
- }\r
- $this->dataLoaded = true;\r
- }\r
-\r
- function getCacheFields( $prefix = 'img_' ) {\r
- $fields = parent::getCacheFields( $prefix );\r
- $fields[] = $prefix . 'archive_name';\r
-\r
- // XXX: Temporary hack before schema update\r
- $fields = array_diff( $fields, array( \r
- 'oi_media_type', 'oi_major_mime', 'oi_minor_mime', 'oi_metadata' ) );\r
- return $fields;\r
- }\r
-\r
- function getRel() {\r
- return 'archive/' . $this->getHashPath() . $this->getArchiveName();\r
- }\r
-\r
- function getUrlRel() {\r
- return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );\r
- }\r
- \r
- function upgradeRow() {\r
- wfProfileIn( __METHOD__ );\r
-\r
- $this->loadFromFile();\r
-\r
- $dbw = $this->repo->getMasterDB();\r
- list( $major, $minor ) = self::splitMime( $this->mime );\r
-\r
- wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n");\r
- $dbw->update( 'oldimage',\r
- array(\r
- 'oi_width' => $this->width,\r
- 'oi_height' => $this->height,\r
- 'oi_bits' => $this->bits,\r
- #'oi_media_type' => $this->media_type,\r
- #'oi_major_mime' => $major,\r
- #'oi_minor_mime' => $minor,\r
- #'oi_metadata' => $this->metadata,\r
- ), array( 'oi_name' => $this->getName(), 'oi_timestamp' => $this->requestedTime ),\r
- __METHOD__\r
- );\r
- wfProfileOut( __METHOD__ );\r
- }\r
-\r
- // XXX: Temporary hack before schema update\r
- function maybeUpgradeRow() {}\r
-\r
-}\r
-\r
-\r
-?>\r
+<?php
+
+/**
+ * Class to represent a file in the oldimage table
+ *
+ * @addtogroup FileRepo
+ */
+class OldLocalFile extends LocalFile {
+ var $requestedTime, $archive_name;
+
+ const CACHE_VERSION = 1;
+ const MAX_CACHE_ROWS = 20;
+
+ function newFromTitle( $title, $repo, $time ) {
+ return new self( $title, $repo, $time, null );
+ }
+
+ function newFromArchiveName( $title, $repo, $archiveName ) {
+ return new self( $title, $repo, null, $archiveName );
+ }
+
+ function newFromRow( $row, $repo ) {
+ $title = Title::makeTitle( NS_IMAGE, $row->oi_name );
+ $file = new self( $title, $repo, null, $row->oi_archive_name );
+ $file->loadFromRow( $row, 'oi_' );
+ return $file;
+ }
+
+ /**
+ * @param Title $title
+ * @param FileRepo $repo
+ * @param string $time Timestamp or null to load by archive name
+ * @param string $archiveName Archive name or null to load by timestamp
+ */
+ function __construct( $title, $repo, $time, $archiveName ) {
+ parent::__construct( $title, $repo );
+ $this->requestedTime = $time;
+ $this->archive_name = $archiveName;
+ if ( is_null( $time ) && is_null( $archiveName ) ) {
+ throw new MWException( __METHOD__.': must specify at least one of $time or $archiveName' );
+ }
+ }
+
+ function getCacheKey() {
+ $hashedName = md5($this->getName());
+ return wfMemcKey( 'oldfile', $hashedName );
+ }
+
+ function getArchiveName() {
+ if ( !isset( $this->archive_name ) ) {
+ $this->load();
+ }
+ return $this->archive_name;
+ }
+
+ function isOld() {
+ return true;
+ }
+
+ /**
+ * Try to load file metadata from memcached. Returns true on success.
+ */
+ function loadFromCache() {
+ global $wgMemc;
+ wfProfileIn( __METHOD__ );
+ $this->dataLoaded = false;
+ $key = $this->getCacheKey();
+ if ( !$key ) {
+ return false;
+ }
+ $oldImages = $wgMemc->get( $key );
+
+ if ( isset( $oldImages['version'] ) && $oldImages['version'] == MW_OLDFILE_VERSION ) {
+ unset( $oldImages['version'] );
+ $more = isset( $oldImages['more'] );
+ unset( $oldImages['more'] );
+ $found = false;
+ if ( is_null( $this->requestedTime ) ) {
+ foreach ( $oldImages as $timestamp => $info ) {
+ if ( $info['archive_name'] == $this->archive_name ) {
+ $found = true;
+ break;
+ }
+ }
+ } else {
+ krsort( $oldImages );
+ foreach ( $oldImages as $timestamp => $info ) {
+ if ( $timestamp <= $this->requestedTime ) {
+ $found = true;
+ break;
+ }
+ }
+ }
+ if ( $found ) {
+ wfDebug( "Pulling file metadata from cache key {$key}[{$timestamp}]\n" );
+ $this->dataLoaded = true;
+ foreach ( $cachedValues as $name => $value ) {
+ $this->$name = $value;
+ }
+ } elseif ( $more ) {
+ wfDebug( "Cache key was truncated, oldimage row might be found in the database\n" );
+ } else {
+ wfDebug( "Image did not exist at the specified time.\n" );
+ $this->fileExists = false;
+ $this->dataLoaded = true;
+ }
+ }
+
+ if ( $this->dataLoaded ) {
+ wfIncrStats( 'image_cache_hit' );
+ } else {
+ wfIncrStats( 'image_cache_miss' );
+ }
+
+ wfProfileOut( __METHOD__ );
+ return $this->dataLoaded;
+ }
+
+ function saveToCache() {
+ // Cache the entire history of the image (up to MAX_CACHE_ROWS).
+ // This is expensive, so we only do it if $wgMemc is real
+ global $wgMemc;
+ if ( $wgMemc instanceof FakeMemcachedClient ) {
+ return;
+ }
+ $key = $this->getCacheKey();
+ if ( !$key ) {
+ return;
+ }
+ wfProfileIn( __METHOD__ );
+
+ $dbr = $this->repo->getSlaveDB();
+ $res = $dbr->select( 'oldimage', $this->getCacheFields(),
+ array( 'oi_name' => $this->getName() ), __METHOD__,
+ array(
+ 'LIMIT' => self::MAX_CACHE_ROWS + 1,
+ 'ORDER BY' => 'oi_timestamp DESC',
+ ));
+ $cache = array( 'version' => self::CACHE_VERSION );
+ $numRows = $dbr->numRows( $res );
+ if ( $numRows > self::MAX_CACHE_ROWS ) {
+ $cache['more'] = true;
+ $numRows--;
+ }
+ for ( $i = 0; $i < $numRows; $i++ ) {
+ $row = $dbr->fetchObject( $res );
+ $this->decodeRow( $row, 'oi_' );
+ $cache[$row->oi_timestamp] = $row;
+ }
+ $dbr->freeResult( $res );
+ $wgMemc->set( $key, $cache, 7*86400 /* 1 week */ );
+ wfProfileOut( __METHOD__ );
+ }
+
+ function loadFromDB() {
+ wfProfileIn( __METHOD__ );
+ $dbr = $this->repo->getSlaveDB();
+ $conds = array( 'oi_name' => $this->getName() );
+ if ( is_null( $this->requestedTime ) ) {
+ $conds['oi_archive_name'] = $this->archive_name;
+ } else {
+ $conds[] = 'oi_timestamp <= ' . $dbr->addQuotes( $this->requestedTime );
+ }
+ $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
+ $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
+ if ( $row ) {
+ $this->loadFromRow( $row, 'oi_' );
+ } else {
+ $this->fileExists = false;
+ }
+ $this->dataLoaded = true;
+ }
+
+ function getCacheFields( $prefix = 'img_' ) {
+ $fields = parent::getCacheFields( $prefix );
+ $fields[] = $prefix . 'archive_name';
+
+ // XXX: Temporary hack before schema update
+ $fields = array_diff( $fields, array(
+ 'oi_media_type', 'oi_major_mime', 'oi_minor_mime', 'oi_metadata' ) );
+ return $fields;
+ }
+
+ function getRel() {
+ return 'archive/' . $this->getHashPath() . $this->getArchiveName();
+ }
+
+ function getUrlRel() {
+ return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );
+ }
+
+ function upgradeRow() {
+ wfProfileIn( __METHOD__ );
+
+ $this->loadFromFile();
+
+ $dbw = $this->repo->getMasterDB();
+ list( $major, $minor ) = self::splitMime( $this->mime );
+
+ wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n");
+ $dbw->update( 'oldimage',
+ array(
+ 'oi_width' => $this->width,
+ 'oi_height' => $this->height,
+ 'oi_bits' => $this->bits,
+ #'oi_media_type' => $this->media_type,
+ #'oi_major_mime' => $major,
+ #'oi_minor_mime' => $minor,
+ #'oi_metadata' => $this->metadata,
+ ), array( 'oi_name' => $this->getName(), 'oi_timestamp' => $this->requestedTime ),
+ __METHOD__
+ );
+ wfProfileOut( __METHOD__ );
+ }
+
+ // XXX: Temporary hack before schema update
+ function maybeUpgradeRow() {}
+
+}
+
+
+?>