diff options
Diffstat (limited to 'includes/filerepo/file')
-rw-r--r-- | includes/filerepo/file/ArchivedFile.php | 171 | ||||
-rw-r--r-- | includes/filerepo/file/File.php | 239 | ||||
-rw-r--r-- | includes/filerepo/file/ForeignAPIFile.php | 57 | ||||
-rw-r--r-- | includes/filerepo/file/ForeignDBFile.php | 20 | ||||
-rw-r--r-- | includes/filerepo/file/LocalFile.php | 505 | ||||
-rw-r--r-- | includes/filerepo/file/OldLocalFile.php | 68 | ||||
-rw-r--r-- | includes/filerepo/file/UnregisteredLocalFile.php | 22 |
7 files changed, 744 insertions, 338 deletions
diff --git a/includes/filerepo/file/ArchivedFile.php b/includes/filerepo/file/ArchivedFile.php index c5a0bd1b..749f11a5 100644 --- a/includes/filerepo/file/ArchivedFile.php +++ b/includes/filerepo/file/ArchivedFile.php @@ -47,6 +47,7 @@ class ArchivedFile { $timestamp, # time of upload $dataLoaded, # Whether or not all this has been loaded from the database (loadFromXxx) $deleted, # Bitfield akin to rev_deleted + $sha1, # sha1 hash of file content $pageCount, $archive_name; @@ -67,7 +68,7 @@ class ArchivedFile { * @param int $id * @param string $key */ - function __construct( $title, $id=0, $key='' ) { + function __construct( $title, $id = 0, $key = '' ) { $this->id = -1; $this->title = false; $this->name = false; @@ -87,17 +88,18 @@ class ArchivedFile { $this->deleted = 0; $this->dataLoaded = false; $this->exists = false; + $this->sha1 = ''; - if( $title instanceof Title ) { + if ( $title instanceof Title ) { $this->title = File::normalizeTitle( $title, 'exception' ); $this->name = $title->getDBkey(); } - if ($id) { + if ( $id ) { $this->id = $id; } - if ($key) { + if ( $key ) { $this->key = $key; } @@ -108,6 +110,7 @@ class ArchivedFile { /** * Loads a file object from the filearchive table + * @throws MWException * @return bool|null True on success or null */ public function load() { @@ -116,75 +119,41 @@ class ArchivedFile { } $conds = array(); - if( $this->id > 0 ) { + if ( $this->id > 0 ) { $conds['fa_id'] = $this->id; } - if( $this->key ) { + if ( $this->key ) { $conds['fa_storage_group'] = $this->group; $conds['fa_storage_key'] = $this->key; } - if( $this->title ) { + if ( $this->title ) { $conds['fa_name'] = $this->title->getDBkey(); } - if( !count($conds)) { + if ( !count( $conds ) ) { throw new MWException( "No specific information for retrieving archived file" ); } - if( !$this->title || $this->title->getNamespace() == NS_FILE ) { + if ( !$this->title || $this->title->getNamespace() == NS_FILE ) { + $this->dataLoaded = true; // set it here, to have also true on miss $dbr = wfGetDB( DB_SLAVE ); - $res = $dbr->select( 'filearchive', - array( - 'fa_id', - 'fa_name', - 'fa_archive_name', - 'fa_storage_key', - 'fa_storage_group', - 'fa_size', - 'fa_bits', - 'fa_width', - 'fa_height', - 'fa_metadata', - 'fa_media_type', - 'fa_major_mime', - 'fa_minor_mime', - 'fa_description', - 'fa_user', - 'fa_user_text', - 'fa_timestamp', - 'fa_deleted' ), + $row = $dbr->selectRow( + 'filearchive', + self::selectFields(), $conds, __METHOD__, - array( 'ORDER BY' => 'fa_timestamp DESC' ) ); - if ( $res == false || $dbr->numRows( $res ) == 0 ) { - // this revision does not exist? + array( 'ORDER BY' => 'fa_timestamp DESC' ) + ); + if ( !$row ) { + // this revision does not exist? return null; } - $ret = $dbr->resultObject( $res ); - $row = $ret->fetchObject(); // initialize fields for filestore image object - $this->id = intval($row->fa_id); - $this->name = $row->fa_name; - $this->archive_name = $row->fa_archive_name; - $this->group = $row->fa_storage_group; - $this->key = $row->fa_storage_key; - $this->size = $row->fa_size; - $this->bits = $row->fa_bits; - $this->width = $row->fa_width; - $this->height = $row->fa_height; - $this->metadata = $row->fa_metadata; - $this->mime = "$row->fa_major_mime/$row->fa_minor_mime"; - $this->media_type = $row->fa_media_type; - $this->description = $row->fa_description; - $this->user = $row->fa_user; - $this->user_text = $row->fa_user_text; - $this->timestamp = $row->fa_timestamp; - $this->deleted = $row->fa_deleted; + $this->loadFromRow( $row ); } else { throw new MWException( 'This title does not correspond to an image page.' ); } - $this->dataLoaded = true; $this->exists = true; return true; @@ -199,26 +168,69 @@ class ArchivedFile { */ public static function newFromRow( $row ) { $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) ); + $file->loadFromRow( $row ); + return $file; + } - $file->id = intval($row->fa_id); - $file->name = $row->fa_name; - $file->archive_name = $row->fa_archive_name; - $file->group = $row->fa_storage_group; - $file->key = $row->fa_storage_key; - $file->size = $row->fa_size; - $file->bits = $row->fa_bits; - $file->width = $row->fa_width; - $file->height = $row->fa_height; - $file->metadata = $row->fa_metadata; - $file->mime = "$row->fa_major_mime/$row->fa_minor_mime"; - $file->media_type = $row->fa_media_type; - $file->description = $row->fa_description; - $file->user = $row->fa_user; - $file->user_text = $row->fa_user_text; - $file->timestamp = $row->fa_timestamp; - $file->deleted = $row->fa_deleted; + /** + * Fields in the filearchive table + * @return array + */ + static function selectFields() { + return array( + 'fa_id', + 'fa_name', + 'fa_archive_name', + 'fa_storage_key', + 'fa_storage_group', + 'fa_size', + 'fa_bits', + 'fa_width', + 'fa_height', + 'fa_metadata', + 'fa_media_type', + 'fa_major_mime', + 'fa_minor_mime', + 'fa_description', + 'fa_user', + 'fa_user_text', + 'fa_timestamp', + 'fa_deleted', + 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */ + 'fa_sha1', + ); + } - return $file; + /** + * Load ArchivedFile object fields from a DB row. + * + * @param $row Object database row + * @since 1.21 + */ + public function loadFromRow( $row ) { + $this->id = intval( $row->fa_id ); + $this->name = $row->fa_name; + $this->archive_name = $row->fa_archive_name; + $this->group = $row->fa_storage_group; + $this->key = $row->fa_storage_key; + $this->size = $row->fa_size; + $this->bits = $row->fa_bits; + $this->width = $row->fa_width; + $this->height = $row->fa_height; + $this->metadata = $row->fa_metadata; + $this->mime = "$row->fa_major_mime/$row->fa_minor_mime"; + $this->media_type = $row->fa_media_type; + $this->description = $row->fa_description; + $this->user = $row->fa_user; + $this->user_text = $row->fa_user_text; + $this->timestamp = $row->fa_timestamp; + $this->deleted = $row->fa_deleted; + if ( isset( $row->fa_sha1 ) ) { + $this->sha1 = $row->fa_sha1; + } else { + // old row, populate from key + $this->sha1 = LocalRepo::getHashFromKey( $this->key ); + } } /** @@ -381,13 +393,24 @@ class ArchivedFile { } /** + * Get the SHA-1 base 36 hash of the file + * + * @return string + * @since 1.21 + */ + function getSha1() { + $this->load(); + return $this->sha1; + } + + /** * Return the user ID of the uploader. * * @return int */ public function getUser() { $this->load(); - if( $this->isDeleted( File::DELETED_USER ) ) { + if ( $this->isDeleted( File::DELETED_USER ) ) { return 0; } else { return $this->user; @@ -401,7 +424,7 @@ class ArchivedFile { */ public function getUserText() { $this->load(); - if( $this->isDeleted( File::DELETED_USER ) ) { + if ( $this->isDeleted( File::DELETED_USER ) ) { return 0; } else { return $this->user_text; @@ -415,7 +438,7 @@ class ArchivedFile { */ public function getDescription() { $this->load(); - if( $this->isDeleted( File::DELETED_COMMENT ) ) { + if ( $this->isDeleted( File::DELETED_COMMENT ) ) { return 0; } else { return $this->description; @@ -469,7 +492,7 @@ class ArchivedFile { */ public function isDeleted( $field ) { $this->load(); - return ($this->deleted & $field) == $field; + return ( $this->deleted & $field ) == $field; } /** diff --git a/includes/filerepo/file/File.php b/includes/filerepo/file/File.php index 557609d4..ec5f927b 100644 --- a/includes/filerepo/file/File.php +++ b/includes/filerepo/file/File.php @@ -40,7 +40,7 @@ * never name a file class explictly outside of the repo class. Instead use the * repo's factory functions to generate file objects, for example: * - * RepoGroup::singleton()->getLocalRepo()->newFile($title); + * RepoGroup::singleton()->getLocalRepo()->newFile( $title ); * * The convenience functions wfLocalFile() and wfFindFile() should be sufficient * in most cases. @@ -48,13 +48,14 @@ * @ingroup FileAbstraction */ abstract class File { + // Bitfield values akin to the Revision deletion constants const DELETED_FILE = 1; const DELETED_COMMENT = 2; const DELETED_USER = 4; const DELETED_RESTRICTED = 8; /** Force rendering in the current process */ - const RENDER_NOW = 1; + const RENDER_NOW = 1; /** * Force rendering even if thumbnail already exist and using RENDER_NOW * I.e. you have to pass both flags: File::RENDER_NOW | File::RENDER_FORCE @@ -152,7 +153,7 @@ abstract class File { * valid Title object with namespace NS_FILE or null * * @param $title Title|string - * @param $exception string|bool Use 'exception' to throw an error on bad titles + * @param string|bool $exception Use 'exception' to throw an error on bad titles * @throws MWException * @return Title|null */ @@ -190,7 +191,7 @@ abstract class File { * Normalize a file extension to the common form, and ensure it's clean. * Extensions with non-alphanumeric characters will be discarded. * - * @param $ext string (without the .) + * @param string $ext (without the .) * @return string */ static function normalizeExtension( $ext ) { @@ -201,9 +202,9 @@ abstract class File { 'mpeg' => 'mpg', 'tiff' => 'tif', 'ogv' => 'ogg' ); - if( isset( $squish[$lower] ) ) { + if ( isset( $squish[$lower] ) ) { return $squish[$lower]; - } elseif( preg_match( '/^[0-9a-z]+$/', $lower ) ) { + } elseif ( preg_match( '/^[0-9a-z]+$/', $lower ) ) { return $lower; } else { return ''; @@ -214,7 +215,7 @@ abstract class File { * Checks if file extensions are compatible * * @param $old File Old file - * @param $new string New name + * @param string $new New name * * @return bool|null */ @@ -241,7 +242,7 @@ abstract class File { * @return array ("text", "html") etc */ public static function splitMime( $mime ) { - if( strpos( $mime, '/' ) !== false ) { + if ( strpos( $mime, '/' ) !== false ) { return explode( '/', $mime, 2 ); } else { return array( $mime, 'unknown' ); @@ -316,7 +317,8 @@ abstract class File { public function getUrl() { if ( !isset( $this->url ) ) { $this->assertRepoDefined(); - $this->url = $this->repo->getZoneUrl( 'public' ) . '/' . $this->getUrlRel(); + $ext = $this->getExtension(); + $this->url = $this->repo->getZoneUrl( 'public', $ext ) . '/' . $this->getUrlRel(); } return $this->url; } @@ -347,7 +349,7 @@ abstract class File { if ( $this->canRender() ) { return $this->createThumb( $this->getWidth() ); } else { - wfDebug( __METHOD__.': supposed to render ' . $this->getName() . + wfDebug( __METHOD__ . ': supposed to render ' . $this->getName() . ' (' . $this->getMimeType() . "), but can't!\n" ); return $this->getURL(); #hm... return NULL? } @@ -368,7 +370,7 @@ abstract class File { * returns false. * * @return string|bool ForeignAPIFile::getPath can return false - */ + */ public function getPath() { if ( !isset( $this->path ) ) { $this->assertRepoDefined(); @@ -431,7 +433,7 @@ abstract class File { * Returns ID or name of user who uploaded the file * STUB * - * @param $type string 'text' or 'id' + * @param string $type 'text' or 'id' * * @return string|int */ @@ -511,13 +513,13 @@ abstract class File { } /** - * get versioned metadata - * - * @param $metadata Mixed Array or String of (serialized) metadata - * @param $version integer version number. - * @return Array containing metadata, or what was passed to it on fail (unserializing if not array) - */ - public function convertMetadataVersion($metadata, $version) { + * get versioned metadata + * + * @param $metadata Mixed Array or String of (serialized) metadata + * @param $version integer version number. + * @return Array containing metadata, or what was passed to it on fail (unserializing if not array) + */ + public function convertMetadataVersion( $metadata, $version ) { $handler = $this->getHandler(); if ( !is_array( $metadata ) ) { // Just to make the return type consistent @@ -662,13 +664,13 @@ abstract class File { if ( $this->allowInlineDisplay() ) { return true; } - if ($this->isTrustedFile()) { + if ( $this->isTrustedFile() ) { return true; } $type = $this->getMediaType(); $mime = $this->getMimeType(); - #wfDebug("LocalFile::isSafeFile: type= $type, mime= $mime\n"); + #wfDebug( "LocalFile::isSafeFile: type= $type, mime= $mime\n" ); if ( !$type || $type === MEDIATYPE_UNKNOWN ) { return false; #unknown type, not trusted @@ -680,7 +682,7 @@ abstract class File { if ( $mime === "unknown/unknown" ) { return false; #unknown type, not trusted } - if ( in_array( $mime, $wgTrustedMediaFormats) ) { + if ( in_array( $mime, $wgTrustedMediaFormats ) ) { return true; } @@ -736,7 +738,7 @@ abstract class File { if ( $this->repo ) { $script = $this->repo->getThumbScriptUrl(); if ( $script ) { - $this->transformScript = "$script?f=" . urlencode( $this->getName() ); + $this->transformScript = wfAppendQuery( $script, array( 'f' => $this->getName() ) ); } } } @@ -766,7 +768,7 @@ abstract class File { * Use File::THUMB_FULL_NAME to always get a name like "<params>-<source>". * Otherwise, the format may be "<params>-<source>" or "<params>-thumbnail.<ext>". * - * @param $params Array: handler-specific parameters + * @param array $params handler-specific parameters * @param $flags integer Bitfield that supports THUMB_* constants * @return string */ @@ -831,8 +833,8 @@ abstract class File { /** * Return either a MediaTransformError or placeholder thumbnail (if $wgIgnoreImageErrors) * - * @param $thumbPath string Thumbnail storage path - * @param $thumbUrl string Thumbnail URL + * @param string $thumbPath Thumbnail storage path + * @param string $thumbUrl Thumbnail URL * @param $params Array * @param $flags integer * @return MediaTransformOutput @@ -840,8 +842,9 @@ abstract class File { protected function transformErrorOutput( $thumbPath, $thumbUrl, $params, $flags ) { global $wgIgnoreImageErrors; - if ( $wgIgnoreImageErrors && !( $flags & self::RENDER_NOW ) ) { - return $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params ); + $handler = $this->getHandler(); + if ( $handler && $wgIgnoreImageErrors && !( $flags & self::RENDER_NOW ) ) { + return $handler->getTransform( $this, $thumbPath, $thumbUrl, $params ); } else { return new MediaTransformError( 'thumbnail_error', $params['width'], 0, wfMessage( 'thumbnail-dest-create' )->text() ); @@ -851,7 +854,7 @@ abstract class File { /** * Transform a media file * - * @param $params Array: an associative array of handler-specific parameters. + * @param array $params an associative array of handler-specific parameters. * Typical keys are width, height and page. * @param $flags Integer: a bitfield, may contain self::RENDER_NOW to force rendering * @return MediaTransformOutput|bool False on failure @@ -872,17 +875,18 @@ abstract class File { $params['descriptionUrl'] = wfExpandUrl( $descriptionUrl, PROTO_CANONICAL ); } + $handler = $this->getHandler(); $script = $this->getTransformScript(); if ( $script && !( $flags & self::RENDER_NOW ) ) { // Use a script to transform on client request, if possible - $thumb = $this->handler->getScriptedTransform( $this, $script, $params ); + $thumb = $handler->getScriptedTransform( $this, $script, $params ); if ( $thumb ) { break; } } $normalisedParams = $params; - $this->handler->normaliseParams( $this, $normalisedParams ); + $handler->normaliseParams( $this, $normalisedParams ); $thumbName = $this->thumbName( $normalisedParams ); $thumbUrl = $this->getThumbUrl( $thumbName ); @@ -895,20 +899,20 @@ abstract class File { // XXX: Pass in the storage path even though we are not rendering anything // and the path is supposed to be an FS path. This is due to getScalerType() // getting called on the path and clobbering $thumb->getUrl() if it's false. - $thumb = $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params ); + $thumb = $handler->getTransform( $this, $thumbPath, $thumbUrl, $params ); break; } // Clean up broken thumbnails as needed $this->migrateThumbFile( $thumbName ); // Check if an up-to-date thumbnail already exists... - wfDebug( __METHOD__.": Doing stat for $thumbPath\n" ); - if ( $this->repo->fileExists( $thumbPath ) && !( $flags & self::RENDER_FORCE ) ) { + wfDebug( __METHOD__ . ": Doing stat for $thumbPath\n" ); + if ( !( $flags & self::RENDER_FORCE ) && $this->repo->fileExists( $thumbPath ) ) { $timestamp = $this->repo->getFileTimestamp( $thumbPath ); if ( $timestamp !== false && $timestamp >= $wgThumbnailEpoch ) { // XXX: Pass in the storage path even though we are not rendering anything // and the path is supposed to be an FS path. This is due to getScalerType() // getting called on the path and clobbering $thumb->getUrl() if it's false. - $thumb = $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params ); + $thumb = $handler->getTransform( $this, $thumbPath, $thumbUrl, $params ); $thumb->setStoragePath( $thumbPath ); break; } @@ -935,7 +939,7 @@ abstract class File { // Actually render the thumbnail... wfProfileIn( __METHOD__ . '-doTransform' ); - $thumb = $this->handler->doTransform( $this, $tmpThumbPath, $thumbUrl, $params ); + $thumb = $handler->doTransform( $this, $tmpThumbPath, $thumbUrl, $params ); wfProfileOut( __METHOD__ . '-doTransform' ); $tmpFile->bind( $thumb ); // keep alive with $thumb @@ -945,7 +949,7 @@ abstract class File { $this->lastError = $thumb->toText(); // Ignore errors if requested if ( $wgIgnoreImageErrors && !( $flags & self::RENDER_NOW ) ) { - $thumb = $this->handler->getTransform( $this, $tmpThumbPath, $thumbUrl, $params ); + $thumb = $handler->getTransform( $this, $tmpThumbPath, $thumbUrl, $params ); } } elseif ( $this->repo && $thumb->hasFile() && !$thumb->fileIsSource() ) { // Copy the thumbnail from the file system into storage... @@ -975,7 +979,7 @@ abstract class File { } /** - * @param $thumbName string Thumbnail name + * @param string $thumbName Thumbnail name * @return string Content-Disposition header value */ function getThumbDisposition( $thumbName ) { @@ -997,7 +1001,7 @@ abstract class File { /** * Get a MediaHandler instance for this file * - * @return MediaHandler + * @return MediaHandler|boolean Registered MediaHandler for file's mime type or false if none found */ function getHandler() { if ( !isset( $this->handler ) ) { @@ -1048,7 +1052,7 @@ abstract class File { * Purge shared caches such as thumbnails and DB data caching * STUB * Overridden by LocalFile - * @param $options Array Options, which include: + * @param array $options Options, which include: * 'forThumbRefresh' : The purging is only to refresh thumbnails */ function purgeCache( $options = array() ) {} @@ -1088,13 +1092,13 @@ abstract class File { * * STUB * @param $limit integer Limit of rows to return - * @param $start string timestamp Only revisions older than $start will be returned - * @param $end string timestamp Only revisions newer than $end will be returned - * @param $inc bool Include the endpoints of the time range + * @param string $start timestamp Only revisions older than $start will be returned + * @param string $end timestamp Only revisions newer than $end will be returned + * @param bool $inc Include the endpoints of the time range * * @return array */ - function getHistory( $limit = null, $start = null, $end = null, $inc=true ) { + function getHistory( $limit = null, $start = null, $end = null, $inc = true ) { return array(); } @@ -1147,7 +1151,7 @@ abstract class File { /** * Get the path of an archived file relative to the public zone root * - * @param $suffix bool|string if not false, the name of an archived thumbnail file + * @param bool|string $suffix if not false, the name of an archived thumbnail file * * @return string */ @@ -1165,7 +1169,7 @@ abstract class File { * Get the path, relative to the thumbnail zone root, of the * thumbnail directory or a particular file if $suffix is specified * - * @param $suffix bool|string if not false, the name of a thumbnail file + * @param bool|string $suffix if not false, the name of a thumbnail file * * @return string */ @@ -1191,8 +1195,8 @@ abstract class File { * Get the path, relative to the thumbnail zone root, for an archived file's thumbs directory * or a specific thumb if the $suffix is given. * - * @param $archiveName string the timestamped name of an archived image - * @param $suffix bool|string if not false, the name of a thumbnail file + * @param string $archiveName the timestamped name of an archived image + * @param bool|string $suffix if not false, the name of a thumbnail file * * @return string */ @@ -1209,7 +1213,7 @@ abstract class File { /** * Get the path of the archived file. * - * @param $suffix bool|string if not false, the name of an archived file. + * @param bool|string $suffix if not false, the name of an archived file. * * @return string */ @@ -1221,8 +1225,8 @@ abstract class File { /** * Get the path of an archived file's thumbs, or a particular thumb if $suffix is specified * - * @param $archiveName string the timestamped name of an archived image - * @param $suffix bool|string if not false, the name of a thumbnail file + * @param string $archiveName the timestamped name of an archived image + * @param bool|string $suffix if not false, the name of a thumbnail file * * @return string */ @@ -1235,7 +1239,7 @@ abstract class File { /** * Get the path of the thumbnail directory, or a particular file if $suffix is specified * - * @param $suffix bool|string if not false, the name of a thumbnail file + * @param bool|string $suffix if not false, the name of a thumbnail file * * @return string */ @@ -1245,15 +1249,28 @@ abstract class File { } /** + * Get the path of the transcoded directory, or a particular file if $suffix is specified + * + * @param bool|string $suffix if not false, the name of a media file + * + * @return string + */ + function getTranscodedPath( $suffix = false ) { + $this->assertRepoDefined(); + return $this->repo->getZonePath( 'transcoded' ) . '/' . $this->getThumbRel( $suffix ); + } + + /** * Get the URL of the archive directory, or a particular file if $suffix is specified * - * @param $suffix bool|string if not false, the name of an archived file + * @param bool|string $suffix if not false, the name of an archived file * * @return string */ function getArchiveUrl( $suffix = false ) { $this->assertRepoDefined(); - $path = $this->repo->getZoneUrl( 'public' ) . '/archive/' . $this->getHashPath(); + $ext = $this->getExtension(); + $path = $this->repo->getZoneUrl( 'public', $ext ) . '/archive/' . $this->getHashPath(); if ( $suffix === false ) { $path = substr( $path, 0, -1 ); } else { @@ -1265,14 +1282,15 @@ abstract class File { /** * Get the URL of the archived file's thumbs, or a particular thumb if $suffix is specified * - * @param $archiveName string the timestamped name of an archived image - * @param $suffix bool|string if not false, the name of a thumbnail file + * @param string $archiveName the timestamped name of an archived image + * @param bool|string $suffix if not false, the name of a thumbnail file * * @return string */ function getArchiveThumbUrl( $archiveName, $suffix = false ) { $this->assertRepoDefined(); - $path = $this->repo->getZoneUrl( 'thumb' ) . '/archive/' . + $ext = $this->getExtension(); + $path = $this->repo->getZoneUrl( 'thumb', $ext ) . '/archive/' . $this->getHashPath() . rawurlencode( $archiveName ) . "/"; if ( $suffix === false ) { $path = substr( $path, 0, -1 ); @@ -1283,15 +1301,17 @@ abstract class File { } /** - * Get the URL of the thumbnail directory, or a particular file if $suffix is specified + * Get the URL of the zone directory, or a particular file if $suffix is specified * - * @param $suffix bool|string if not false, the name of a thumbnail file + * @param string $zone name of requested zone + * @param bool|string $suffix if not false, the name of a file in zone * * @return string path */ - function getThumbUrl( $suffix = false ) { + function getZoneUrl( $zone, $suffix = false ) { $this->assertRepoDefined(); - $path = $this->repo->getZoneUrl( 'thumb' ) . '/' . $this->getUrlRel(); + $ext = $this->getExtension(); + $path = $this->repo->getZoneUrl( $zone, $ext ) . '/' . $this->getUrlRel(); if ( $suffix !== false ) { $path .= '/' . rawurlencode( $suffix ); } @@ -1299,9 +1319,31 @@ abstract class File { } /** + * Get the URL of the thumbnail directory, or a particular file if $suffix is specified + * + * @param bool|string $suffix if not false, the name of a thumbnail file + * + * @return string path + */ + function getThumbUrl( $suffix = false ) { + return $this->getZoneUrl( 'thumb', $suffix ); + } + + /** + * Get the URL of the transcoded directory, or a particular file if $suffix is specified + * + * @param bool|string $suffix if not false, the name of a media file + * + * @return string path + */ + function getTranscodedUrl( $suffix = false ) { + return $this->getZoneUrl( 'transcoded', $suffix ); + } + + /** * Get the public zone virtual URL for a current version source file * - * @param $suffix bool|string if not false, the name of a thumbnail file + * @param bool|string $suffix if not false, the name of a thumbnail file * * @return string */ @@ -1317,7 +1359,7 @@ abstract class File { /** * Get the public zone virtual URL for an archived version source file * - * @param $suffix bool|string if not false, the name of a thumbnail file + * @param bool|string $suffix if not false, the name of a thumbnail file * * @return string */ @@ -1335,7 +1377,7 @@ abstract class File { /** * Get the virtual URL for a thumbnail file or directory * - * @param $suffix bool|string if not false, the name of a thumbnail file + * @param bool|string $suffix if not false, the name of a thumbnail file * * @return string */ @@ -1360,7 +1402,7 @@ abstract class File { * @throws MWException */ function readOnlyError() { - throw new MWException( get_class($this) . ': write operations are not supported' ); + throw new MWException( get_class( $this ) . ': write operations are not supported' ); } /** @@ -1373,8 +1415,12 @@ abstract class File { * @param $copyStatus string * @param $source string * @param $watch bool + * @param $timestamp string|bool + * @param $user User object or null to use $wgUser + * @return bool + * @throws MWException */ - function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '', $watch = false ) { + function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '', $watch = false, $timestamp = false, User $user = null ) { $this->readOnlyError(); } @@ -1386,17 +1432,21 @@ abstract class File { * The archive name should be passed through to recordUpload for database * registration. * - * @param $srcPath String: local filesystem path to the source image + * Options to $options include: + * - headers : name/value map of HTTP headers to use in response to GET/HEAD requests + * + * @param string $srcPath local filesystem path to the source image * @param $flags Integer: a bitwise combination of: * File::DELETE_SOURCE Delete the source file, i.e. move * rather than copy + * @param array $options Optional additional parameters * @return FileRepoStatus object. On success, the value member contains the * archive name, or an empty string if it was a new file. * * STUB * Overridden by LocalFile */ - function publish( $srcPath, $flags = 0 ) { + function publish( $srcPath, $flags = 0, array $options = array() ) { $this->readOnlyError(); } @@ -1451,7 +1501,7 @@ abstract class File { * Is this file a "deleted" file in a private archive? * STUB * - * @param $field + * @param integer $field one of DELETED_* bitfield constants * * @return bool */ @@ -1490,9 +1540,9 @@ abstract class File { * @param $target Title New file name * @return FileRepoStatus object. */ - function move( $target ) { + function move( $target ) { $this->readOnlyError(); - } + } /** * Delete all versions of the file. @@ -1518,9 +1568,9 @@ abstract class File { * * May throw database exceptions on error. * - * @param $versions array set of record ids of deleted items to restore, + * @param array $versions set of record ids of deleted items to restore, * or empty to restore all revisions. - * @param $unsuppress bool remove restrictions on content upon restoration? + * @param bool $unsuppress remove restrictions on content upon restoration? * @return int|bool the number of file revisions restored if successful, * or false on failure * STUB @@ -1580,7 +1630,7 @@ abstract class File { * Get an image size array like that returned by getImageSize(), or false if it * can't be determined. * - * @param $fileName String: The filename + * @param string $fileName The filename * @return Array */ function getImageSize( $fileName ) { @@ -1607,25 +1657,29 @@ abstract class File { /** * Get the HTML text of the description page, if available * + * @param $lang Language Optional language to fetch description in * @return string */ - function getDescriptionText() { + function getDescriptionText( $lang = false ) { global $wgMemc, $wgLang; if ( !$this->repo || !$this->repo->fetchDescription ) { return false; } - $renderUrl = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgLang->getCode() ); + if ( !$lang ) { + $lang = $wgLang; + } + $renderUrl = $this->repo->getDescriptionRenderUrl( $this->getName(), $lang->getCode() ); if ( $renderUrl ) { if ( $this->repo->descriptionCacheExpiry > 0 ) { - wfDebug("Attempting to get the description from cache..."); - $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', $wgLang->getCode(), + wfDebug( "Attempting to get the description from cache..." ); + $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', $lang->getCode(), $this->getName() ); - $obj = $wgMemc->get($key); - if ($obj) { - wfDebug("success!\n"); + $obj = $wgMemc->get( $key ); + if ( $obj ) { + wfDebug( "success!\n" ); return $obj; } - wfDebug("miss\n"); + wfDebug( "miss\n" ); } wfDebug( "Fetching shared description from $renderUrl\n" ); $res = Http::get( $renderUrl ); @@ -1704,14 +1758,15 @@ abstract class File { /** * Get an associative array containing information about a file in the local filesystem. * - * @param $path String: absolute local filesystem path + * @param string $path absolute local filesystem path * @param $ext Mixed: the file extension, or true to extract it from the filename. * Set it to false to ignore the extension. * * @return array + * @deprecated since 1.19 */ static function getPropsFromPath( $path, $ext = true ) { - wfDebug( __METHOD__.": Getting file info for $path\n" ); + wfDebug( __METHOD__ . ": Getting file info for $path\n" ); wfDeprecated( __METHOD__, '1.19' ); $fsFile = new FSFile( $path ); @@ -1728,6 +1783,7 @@ abstract class File { * @param $path string * * @return bool|string False on failure + * @deprecated since 1.19 */ static function sha1Base36( $path ) { wfDeprecated( __METHOD__, '1.19' ); @@ -1737,6 +1793,18 @@ abstract class File { } /** + * @return Array HTTP header name/value map to use for HEAD/GET request responses + */ + function getStreamHeaders() { + $handler = $this->getHandler(); + if ( $handler ) { + return $handler->getStreamHeaders( $this->getMetadata() ); + } else { + return array(); + } + } + + /** * @return string */ function getLongDesc() { @@ -1780,7 +1848,7 @@ abstract class File { } /** - * @return Title + * @return Title|null */ function getRedirectedTitle() { if ( $this->redirected ) { @@ -1789,6 +1857,7 @@ abstract class File { } return $this->redirectTitle; } + return null; } /** diff --git a/includes/filerepo/file/ForeignAPIFile.php b/includes/filerepo/file/ForeignAPIFile.php index 56482611..ed96d446 100644 --- a/includes/filerepo/file/ForeignAPIFile.php +++ b/includes/filerepo/file/ForeignAPIFile.php @@ -54,22 +54,22 @@ class ForeignAPIFile extends File { */ static function newFromTitle( Title $title, $repo ) { $data = $repo->fetchImageQuery( array( - 'titles' => 'File:' . $title->getDBKey(), - 'iiprop' => self::getProps(), - 'prop' => 'imageinfo', + 'titles' => 'File:' . $title->getDBkey(), + 'iiprop' => self::getProps(), + 'prop' => 'imageinfo', 'iimetadataversion' => MediaHandler::getMetadataVersion() ) ); $info = $repo->getImageInfo( $data ); - if( $info ) { + if ( $info ) { $lastRedirect = isset( $data['query']['redirects'] ) ? count( $data['query']['redirects'] ) - 1 : -1; - if( $lastRedirect >= 0 ) { - $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to']); + if ( $lastRedirect >= 0 ) { + $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to'] ); $img = new self( $newtitle, $repo, $info, true ); - if( $img ) { + if ( $img ) { $img->redirectedFrom( $title->getDBkey() ); } } else { @@ -86,7 +86,7 @@ class ForeignAPIFile extends File { * @return string */ static function getProps() { - return 'timestamp|user|comment|url|size|sha1|metadata|mime'; + return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype'; } // Dummy functions... @@ -106,12 +106,12 @@ class ForeignAPIFile extends File { } /** - * @param Array $params + * @param array $params * @param int $flags * @return bool|MediaTransformOutput */ function transform( $params, $flags = 0 ) { - if( !$this->canRender() ) { + if ( !$this->canRender() ) { // show icon return parent::transform( $params, $flags ); } @@ -119,12 +119,25 @@ class ForeignAPIFile extends File { // Note, the this->canRender() check above implies // that we have a handler, and it can do makeParamString. $otherParams = $this->handler->makeParamString( $params ); + $width = isset( $params['width'] ) ? $params['width'] : -1; + $height = isset( $params['height'] ) ? $params['height'] : -1; $thumbUrl = $this->repo->getThumbUrlFromCache( $this->getName(), - isset( $params['width'] ) ? $params['width'] : -1, - isset( $params['height'] ) ? $params['height'] : -1, - $otherParams ); + $width, + $height, + $otherParams + ); + if ( $thumbUrl === false ) { + global $wgLang; + return $this->repo->getThumbError( + $this->getName(), + $width, + $height, + $otherParams, + $wgLang->getCode() + ); + } return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params ); } @@ -161,12 +174,12 @@ class ForeignAPIFile extends File { * @return array */ public static function parseMetadata( $metadata ) { - if( !is_array( $metadata ) ) { + if ( !is_array( $metadata ) ) { return $metadata; } $ret = array(); - foreach( $metadata as $meta ) { - $ret[ $meta['name'] ] = self::parseMetadata( $meta['value'] ); + foreach ( $metadata as $meta ) { + $ret[$meta['name']] = self::parseMetadata( $meta['value'] ); } return $ret; } @@ -189,7 +202,7 @@ class ForeignAPIFile extends File { * @param string $method * @return int|null|string */ - public function getUser( $method='text' ) { + public function getUser( $method = 'text' ) { return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null; } @@ -224,7 +237,7 @@ class ForeignAPIFile extends File { * @return string */ function getMimeType() { - if( !isset( $this->mInfo['mime'] ) ) { + if ( !isset( $this->mInfo['mime'] ) ) { $magic = MimeMagic::singleton(); $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() ); } @@ -232,10 +245,12 @@ class ForeignAPIFile extends File { } /** - * @todo FIXME: May guess wrong on file types that can be eg audio or video * @return int|string */ function getMediaType() { + if ( isset( $this->mInfo['mediatype'] ) ) { + return $this->mInfo['mediatype']; + } $magic = MimeMagic::singleton(); return $magic->getMediaType( null, $this->getMimeType() ); } @@ -256,7 +271,7 @@ class ForeignAPIFile extends File { */ function getThumbPath( $suffix = '' ) { if ( $this->repo->canCacheThumbs() ) { - $path = $this->repo->getZonePath('thumb') . '/' . $this->getHashPath( $this->getName() ); + $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath( $this->getName() ); if ( $suffix ) { $path = $path . $suffix . '/'; } @@ -293,7 +308,7 @@ class ForeignAPIFile extends File { global $wgMemc, $wgContLang; $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() ); - $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) ); + $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) ); $wgMemc->delete( $key ); } diff --git a/includes/filerepo/file/ForeignDBFile.php b/includes/filerepo/file/ForeignDBFile.php index 91f6cb62..01d6b0f5 100644 --- a/includes/filerepo/file/ForeignDBFile.php +++ b/includes/filerepo/file/ForeignDBFile.php @@ -57,9 +57,11 @@ class ForeignDBFile extends LocalFile { /** * @param $srcPath String * @param $flags int + * @param $options Array + * @return \FileRepoStatus * @throws MWException */ - function publish( $srcPath, $flags = 0 ) { + function publish( $srcPath, $flags = 0, array $options = array() ) { $this->readOnlyError(); } @@ -71,16 +73,19 @@ class ForeignDBFile extends LocalFile { * @param $source string * @param $watch bool * @param $timestamp bool|string + * @param $user User object or null to use $wgUser + * @return bool * @throws MWException */ function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '', - $watch = false, $timestamp = false ) { + $watch = false, $timestamp = false, User $user = null ) { $this->readOnlyError(); } /** * @param $versions array * @param $unsuppress bool + * @return \FileRepoStatus * @throws MWException */ function restore( $versions = array(), $unsuppress = false ) { @@ -90,6 +95,7 @@ class ForeignDBFile extends LocalFile { /** * @param $reason string * @param $suppress bool + * @return \FileRepoStatus * @throws MWException */ function delete( $reason, $suppress = false ) { @@ -98,6 +104,7 @@ class ForeignDBFile extends LocalFile { /** * @param $target Title + * @return \FileRepoStatus * @throws MWException */ function move( $target ) { @@ -108,15 +115,16 @@ class ForeignDBFile extends LocalFile { * @return string */ function getDescriptionUrl() { - // Restore remote behaviour + // Restore remote behavior return File::getDescriptionUrl(); } /** + * @param $lang Language Optional language to fetch description in. * @return string */ - function getDescriptionText() { - // Restore remote behaviour - return File::getDescriptionText(); + function getDescriptionText( $lang = false ) { + // Restore remote behavior + return File::getDescriptionText( $lang ); } } diff --git a/includes/filerepo/file/LocalFile.php b/includes/filerepo/file/LocalFile.php index 695c4e9e..fe769be2 100644 --- a/includes/filerepo/file/LocalFile.php +++ b/includes/filerepo/file/LocalFile.php @@ -24,7 +24,7 @@ /** * Bump this number when serialized cache records may be incompatible. */ -define( 'MW_FILE_VERSION', 8 ); +define( 'MW_FILE_VERSION', 9 ); /** * Class to represent a local file in the wiki's own database @@ -36,7 +36,7 @@ define( 'MW_FILE_VERSION', 8 ); * never name a file class explictly outside of the repo class. Instead use the * repo's factory functions to generate file objects, for example: * - * RepoGroup::singleton()->getLocalRepo()->newFile($title); + * RepoGroup::singleton()->getLocalRepo()->newFile( $title ); * * The convenience functions wfLocalFile() and wfFindFile() should be sufficient * in most cases. @@ -67,9 +67,11 @@ class LocalFile extends File { $sha1, # SHA-1 base 36 content hash $user, $user_text, # User, who uploaded the file $description, # Description of current revision of the file - $dataLoaded, # Whether or not all this has been loaded from the database (loadFromXxx) + $dataLoaded, # Whether or not core data has been loaded from the database (loadFromXxx) + $extraDataLoaded, # Whether or not lazy-loaded data has been loaded from the database $upgraded, # Whether the row was upgraded on load $locked, # True if the image row is locked + $lockedOwnTrx, # True if the image row is locked with a lock initiated transaction $missing, # True if file is not present in file system. Not to be cached in memcached $deleted; # Bitfield akin to rev_deleted @@ -82,6 +84,8 @@ class LocalFile extends File { protected $repoClass = 'LocalRepo'; + const LOAD_ALL = 1; // integer; load all the lazy fields too (like metadata) + /** * Create a LocalFile from a title * Do not call this except from inside a repo class. @@ -119,7 +123,7 @@ class LocalFile extends File { * Create a LocalFile from a SHA-1 key * Do not call this except from inside a repo class. * - * @param $sha1 string base-36 SHA-1 + * @param string $sha1 base-36 SHA-1 * @param $repo LocalRepo * @param string|bool $timestamp MW_timestamp (optional) * @@ -175,6 +179,7 @@ class LocalFile extends File { $this->historyLine = 0; $this->historyRes = null; $this->dataLoaded = false; + $this->extraDataLoaded = false; $this->assertRepoDefined(); $this->assertTitleDefined(); @@ -200,6 +205,7 @@ class LocalFile extends File { wfProfileIn( __METHOD__ ); $this->dataLoaded = false; + $this->extraDataLoaded = false; $key = $this->getCacheKey(); if ( !$key ) { @@ -210,13 +216,17 @@ class LocalFile extends File { $cachedValues = $wgMemc->get( $key ); // Check if the key existed and belongs to this version of MediaWiki - if ( isset( $cachedValues['version'] ) && ( $cachedValues['version'] == MW_FILE_VERSION ) ) { + if ( isset( $cachedValues['version'] ) && $cachedValues['version'] == MW_FILE_VERSION ) { wfDebug( "Pulling file metadata from cache key $key\n" ); $this->fileExists = $cachedValues['fileExists']; if ( $this->fileExists ) { $this->setProps( $cachedValues ); } $this->dataLoaded = true; + $this->extraDataLoaded = true; + foreach ( $this->getLazyCacheFields( '' ) as $field ) { + $this->extraDataLoaded = $this->extraDataLoaded && isset( $cachedValues[$field] ); + } } if ( $this->dataLoaded ) { @@ -252,7 +262,17 @@ class LocalFile extends File { } } - $wgMemc->set( $key, $cache, 60 * 60 * 24 * 7 ); // A week + // Strip off excessive entries from the subset of fields that can become large. + // If the cache value gets to large it will not fit in memcached and nothing will + // get cached at all, causing master queries for any file access. + foreach ( $this->getLazyCacheFields( '' ) as $field ) { + if ( isset( $cache[$field] ) && strlen( $cache[$field] ) > 100 * 1024 ) { + unset( $cache[$field] ); // don't let the value get too big + } + } + + // Cache presence for 1 week and negatives for 1 day + $wgMemc->set( $key, $cache, $this->fileExists ? 86400 * 7 : 86400 ); } /** @@ -288,6 +308,28 @@ class LocalFile extends File { } /** + * @return array + */ + function getLazyCacheFields( $prefix = 'img_' ) { + static $fields = array( 'metadata' ); + static $results = array(); + + if ( $prefix == '' ) { + return $fields; + } + + if ( !isset( $results[$prefix] ) ) { + $prefixedFields = array(); + foreach ( $fields as $field ) { + $prefixedFields[] = $prefix . $field; + } + $results[$prefix] = $prefixedFields; + } + + return $results[$prefix]; + } + + /** * Load file metadata from the DB */ function loadFromDB() { @@ -297,9 +339,9 @@ class LocalFile extends File { # Unconditionally set loaded=true, we don't want the accessors constantly rechecking $this->dataLoaded = true; + $this->extraDataLoaded = true; $dbr = $this->repo->getMasterDB(); - $row = $dbr->selectRow( 'image', $this->getCacheFields( 'img_' ), array( 'img_name' => $this->getName() ), $fname ); @@ -313,27 +355,71 @@ class LocalFile extends File { } /** - * Decode a row from the database (either object or array) to an array - * with timestamps and MIME types decoded, and the field prefix removed. - * @param $row + * Load lazy file metadata from the DB. + * This covers fields that are sometimes not cached. + */ + protected function loadExtraFromDB() { + # Polymorphic function name to distinguish foreign and local fetches + $fname = get_class( $this ) . '::' . __FUNCTION__; + wfProfileIn( $fname ); + + # Unconditionally set loaded=true, we don't want the accessors constantly rechecking + $this->extraDataLoaded = true; + + $dbr = $this->repo->getSlaveDB(); + // In theory the file could have just been renamed/deleted...oh well + $row = $dbr->selectRow( 'image', $this->getLazyCacheFields( 'img_' ), + array( 'img_name' => $this->getName() ), $fname ); + + if ( !$row ) { // fallback to master + $dbr = $this->repo->getMasterDB(); + $row = $dbr->selectRow( 'image', $this->getLazyCacheFields( 'img_' ), + array( 'img_name' => $this->getName() ), $fname ); + } + + if ( $row ) { + foreach ( $this->unprefixRow( $row, 'img_' ) as $name => $value ) { + $this->$name = $value; + } + } else { + wfProfileOut( $fname ); + throw new MWException( "Could not find data for image '{$this->getName()}'." ); + } + + wfProfileOut( $fname ); + } + + /** + * @param Row $row * @param $prefix string - * @throws MWException - * @return array + * @return Array */ - function decodeRow( $row, $prefix = 'img_' ) { + protected function unprefixRow( $row, $prefix = 'img_' ) { $array = (array)$row; $prefixLength = strlen( $prefix ); // Sanity check prefix once if ( substr( key( $array ), 0, $prefixLength ) !== $prefix ) { - throw new MWException( __METHOD__ . ': incorrect $prefix parameter' ); + throw new MWException( __METHOD__ . ': incorrect $prefix parameter' ); } $decoded = array(); - foreach ( $array as $name => $value ) { $decoded[substr( $name, $prefixLength )] = $value; } + return $decoded; + } + + /** + * Decode a row from the database (either object or array) to an array + * with timestamps and MIME types decoded, and the field prefix removed. + * @param $row + * @param $prefix string + * @throws MWException + * @return array + */ + function decodeRow( $row, $prefix = 'img_' ) { + $decoded = $this->unprefixRow( $row, $prefix ); $decoded['timestamp'] = wfTimestamp( TS_MW, $decoded['timestamp'] ); @@ -357,6 +443,8 @@ class LocalFile extends File { */ function loadFromRow( $row, $prefix = 'img_' ) { $this->dataLoaded = true; + $this->extraDataLoaded = true; + $array = $this->decodeRow( $row, $prefix ); foreach ( $array as $name => $value ) { @@ -369,8 +457,9 @@ class LocalFile extends File { /** * Load file metadata from cache or DB, unless already loaded + * @param integer $flags */ - function load() { + function load( $flags = 0 ) { if ( !$this->dataLoaded ) { if ( !$this->loadFromCache() ) { $this->loadFromDB(); @@ -378,6 +467,9 @@ class LocalFile extends File { } $this->dataLoaded = true; } + if ( ( $flags & self::LOAD_ALL ) && !$this->extraDataLoaded ) { + $this->loadExtraFromDB(); + } } /** @@ -397,7 +489,7 @@ class LocalFile extends File { } else { $handler = $this->getHandler(); if ( $handler ) { - $validity = $handler->isMetadataValid( $this, $this->metadata ); + $validity = $handler->isMetadataValid( $this, $this->getMetadata() ); if ( $validity === MediaHandler::METADATA_BAD || ( $validity === MediaHandler::METADATA_COMPATIBLE && $wgUpdateCompatibleMetadata ) ) { @@ -440,15 +532,15 @@ class LocalFile extends File { $dbw->update( 'image', array( - 'img_size' => $this->size, // sanity - 'img_width' => $this->width, - 'img_height' => $this->height, - 'img_bits' => $this->bits, + 'img_size' => $this->size, // sanity + 'img_width' => $this->width, + 'img_height' => $this->height, + 'img_bits' => $this->bits, 'img_media_type' => $this->media_type, 'img_major_mime' => $major, 'img_minor_mime' => $minor, - 'img_metadata' => $this->metadata, - 'img_sha1' => $this->sha1, + 'img_metadata' => $dbw->encodeBlob($this->metadata), + 'img_sha1' => $this->sha1, ), array( 'img_name' => $this->getName() ), __METHOD__ @@ -464,6 +556,7 @@ class LocalFile extends File { /** * Set properties in this object to be equal to those given in the * associative array $info. Only cacheable fields can be set. + * All fields *must* be set in $info except for getLazyCacheFields(). * * If 'mime' is given, it will be split into major_mime/minor_mime. * If major_mime/minor_mime are given, $this->mime will also be set. @@ -511,17 +604,23 @@ class LocalFile extends File { * Return the width of the image * * @param $page int - * @return bool|int Returns false on error + * @return int */ public function getWidth( $page = 1 ) { $this->load(); if ( $this->isMultipage() ) { - $dim = $this->getHandler()->getPageDimensions( $this, $page ); + $handler = $this->getHandler(); + if ( !$handler ) { + return 0; + } + $dim = $handler->getPageDimensions( $this, $page ); if ( $dim ) { return $dim['width']; } else { - return false; + // For non-paged media, the false goes through an + // intval, turning failure into 0, so do same here. + return 0; } } else { return $this->width; @@ -532,17 +631,23 @@ class LocalFile extends File { * Return the height of the image * * @param $page int - * @return bool|int Returns false on error + * @return int */ public function getHeight( $page = 1 ) { $this->load(); if ( $this->isMultipage() ) { - $dim = $this->getHandler()->getPageDimensions( $this, $page ); + $handler = $this->getHandler(); + if ( !$handler ) { + return 0; + } + $dim = $handler->getPageDimensions( $this, $page ); if ( $dim ) { return $dim['height']; } else { - return false; + // For non-paged media, the false goes through an + // intval, turning failure into 0, so do same here. + return 0; } } else { return $this->height; @@ -552,7 +657,7 @@ class LocalFile extends File { /** * Returns ID or name of user who uploaded the file * - * @param $type string 'text' or 'id' + * @param string $type 'text' or 'id' * @return int|string */ function getUser( $type = 'text' ) { @@ -570,7 +675,7 @@ class LocalFile extends File { * @return string */ function getMetadata() { - $this->load(); + $this->load( self::LOAD_ALL ); // large metadata is loaded in another step return $this->metadata; } @@ -638,15 +743,14 @@ class LocalFile extends File { * RTT regression for wikis without 404 handling. */ function migrateThumbFile( $thumbName ) { - $thumbDir = $this->getThumbPath(); - /* Old code for bug 2532 + $thumbDir = $this->getThumbPath(); $thumbPath = "$thumbDir/$thumbName"; if ( is_dir( $thumbPath ) ) { // Directory where file should be // This happened occasionally due to broken migration code in 1.5 // Rename to broken-* - for ( $i = 0; $i < 100 ; $i++ ) { + for ( $i = 0; $i < 100; $i++ ) { $broken = $this->repo->getZonePath( 'public' ) . "/broken-$i-$thumbName"; if ( !file_exists( $broken ) ) { rename( $thumbPath, $broken ); @@ -672,7 +776,7 @@ class LocalFile extends File { /** * Get all thumbnail names previously generated for this file - * @param $archiveName string|bool Name of an archive file, default false + * @param string|bool $archiveName Name of an archive file, default false * @return array first element is the base dir, then files in that base dir. */ function getThumbnails( $archiveName = false ) { @@ -684,10 +788,12 @@ class LocalFile extends File { $backend = $this->repo->getBackend(); $files = array( $dir ); - $iterator = $backend->getFileList( array( 'dir' => $dir ) ); - foreach ( $iterator as $file ) { - $files[] = $file; - } + try { + $iterator = $backend->getFileList( array( 'dir' => $dir ) ); + foreach ( $iterator as $file ) { + $files[] = $file; + } + } catch ( FileBackendError $e ) {} // suppress (bug 54674) return $files; } @@ -702,7 +808,9 @@ class LocalFile extends File { } /** - * Purge the shared history (OldLocalFile) cache + * Purge the shared history (OldLocalFile) cache. + * + * @note This used to purge old thumbnails as well. */ function purgeHistory() { global $wgMemc; @@ -710,20 +818,20 @@ class LocalFile extends File { $hashedName = md5( $this->getName() ); $oldKey = $this->repo->getSharedCacheKey( 'oldfile', $hashedName ); - // Must purge thumbnails for old versions too! bug 30192 - foreach( $this->getHistory() as $oldFile ) { - $oldFile->purgeThumbnails(); - } - if ( $oldKey ) { $wgMemc->delete( $oldKey ); } } /** - * Delete all previously generated thumbnails, refresh metadata in memcached and purge the squid + * Delete all previously generated thumbnails, refresh metadata in memcached and purge the squid. + * + * @param Array $options An array potentially with the key forThumbRefresh. + * + * @note This used to purge old thumbnails by default as well, but doesn't anymore. */ function purgeCache( $options = array() ) { + wfProfileIn( __METHOD__ ); // Refresh metadata cache $this->purgeMetadataCache(); @@ -732,11 +840,12 @@ class LocalFile extends File { // Purge squid cache for this file SquidUpdate::purge( array( $this->getURL() ) ); + wfProfileOut( __METHOD__ ); } /** * Delete cached transformed files for an archived version only. - * @param $archiveName string name of the archived file + * @param string $archiveName name of the archived file */ function purgeOldThumbnails( $archiveName ) { global $wgUseSquid; @@ -753,7 +862,7 @@ class LocalFile extends File { // Purge the squid if ( $wgUseSquid ) { $urls = array(); - foreach( $files as $file ) { + foreach ( $files as $file ) { $urls[] = $this->getArchiveThumbUrl( $archiveName, $file ); } SquidUpdate::purge( $urls ); @@ -771,8 +880,16 @@ class LocalFile extends File { // Delete thumbnails $files = $this->getThumbnails(); + // Always purge all files from squid regardless of handler filters + if ( $wgUseSquid ) { + $urls = array(); + foreach ( $files as $file ) { + $urls[] = $this->getThumbUrl( $file ); + } + array_shift( $urls ); // don't purge directory + } - // Give media handler a chance to filter the purge list + // Give media handler a chance to filter the file purge list if ( !empty( $options['forThumbRefresh'] ) ) { $handler = $this->getHandler(); if ( $handler ) { @@ -788,10 +905,6 @@ class LocalFile extends File { // Purge the squid if ( $wgUseSquid ) { - $urls = array(); - foreach( $files as $file ) { - $urls[] = $this->getThumbUrl( $file ); - } SquidUpdate::purge( $urls ); } @@ -800,13 +913,13 @@ class LocalFile extends File { /** * Delete a list of thumbnails visible at urls - * @param $dir string base dir of the files. - * @param $files array of strings: relative filenames (to $dir) + * @param string $dir base dir of the files. + * @param array $files of strings: relative filenames (to $dir) */ protected function purgeThumbList( $dir, $files ) { $fileListDebug = strtr( var_export( $files, true ), - array("\n"=>'') + array( "\n" => '' ) ); wfDebug( __METHOD__ . ": $fileListDebug\n" ); @@ -814,7 +927,9 @@ class LocalFile extends File { foreach ( $files as $file ) { # Check that the base file name is part of the thumb name # This is a basic sanity check to avoid erasing unrelated directories - if ( strpos( $file, $this->getName() ) !== false ) { + if ( strpos( $file, $this->getName() ) !== false + || strpos( $file, "-thumbnail" ) !== false // "short" thumb name + ) { $purgeList[] = "{$dir}/{$file}"; } } @@ -949,15 +1064,15 @@ class LocalFile extends File { /** * Upload a file and record it in the DB - * @param $srcPath String: source storage path or virtual URL - * @param $comment String: upload description - * @param $pageText String: text to use for the new description page, + * @param string $srcPath source storage path, virtual URL, or filesystem path + * @param string $comment upload description + * @param string $pageText text to use for the new description page, * if a new description page is created * @param $flags Integer|bool: flags for publish() - * @param $props Array|bool: File properties, if known. This can be used to reduce the + * @param array|bool $props File properties, if known. This can be used to reduce the * upload time when uploading virtual URLs for which the file info * is already known - * @param $timestamp String|bool: timestamp for img_timestamp, or false to use the current time + * @param string|bool $timestamp timestamp for img_timestamp, or false to use the current time * @param $user User|null: User object or null to use $wgUser * * @return FileRepoStatus object. On success, the value member contains the @@ -970,11 +1085,34 @@ class LocalFile extends File { return $this->readOnlyFatalStatus(); } + if ( !$props ) { + wfProfileIn( __METHOD__ . '-getProps' ); + if ( $this->repo->isVirtualUrl( $srcPath ) + || FileBackend::isStoragePath( $srcPath ) ) + { + $props = $this->repo->getFileProps( $srcPath ); + } else { + $props = FSFile::getPropsFromPath( $srcPath ); + } + wfProfileOut( __METHOD__ . '-getProps' ); + } + + $options = array(); + $handler = MediaHandler::getHandler( $props['mime'] ); + if ( $handler ) { + $options['headers'] = $handler->getStreamHeaders( $props['metadata'] ); + } else { + $options['headers'] = array(); + } + + // Trim spaces on user supplied text + $comment = trim( $comment ); + // truncate nicely or the DB will do it for us // non-nicely (dangling multi-byte chars, non-truncated version in cache). $comment = $wgContLang->truncate( $comment, 255 ); $this->lock(); // begin - $status = $this->publish( $srcPath, $flags ); + $status = $this->publish( $srcPath, $flags, $options ); if ( $status->successCount > 0 ) { # Essentially we are displacing any existing current file and saving @@ -999,20 +1137,25 @@ class LocalFile extends File { * @param $source string * @param $watch bool * @param $timestamp string|bool + * @param $user User object or null to use $wgUser * @return bool */ function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '', - $watch = false, $timestamp = false ) + $watch = false, $timestamp = false, User $user = null ) { + if ( !$user ) { + global $wgUser; + $user = $wgUser; + } + $pageText = SpecialUpload::getInitialPageText( $desc, $license, $copyStatus, $source ); - if ( !$this->recordUpload2( $oldver, $desc, $pageText ) ) { + if ( !$this->recordUpload2( $oldver, $desc, $pageText, false, $timestamp, $user ) ) { return false; } if ( $watch ) { - global $wgUser; - $wgUser->addWatch( $this->getTitle() ); + $user->addWatch( $this->getTitle() ); } return true; } @@ -1070,20 +1213,20 @@ class LocalFile extends File { # doesn't deadlock. SELECT FOR UPDATE causes a deadlock for every race condition. $dbw->insert( 'image', array( - 'img_name' => $this->getName(), - 'img_size' => $this->size, - 'img_width' => intval( $this->width ), - 'img_height' => intval( $this->height ), - 'img_bits' => $this->bits, - 'img_media_type' => $this->media_type, - 'img_major_mime' => $this->major_mime, - 'img_minor_mime' => $this->minor_mime, - 'img_timestamp' => $timestamp, + 'img_name' => $this->getName(), + 'img_size' => $this->size, + 'img_width' => intval( $this->width ), + 'img_height' => intval( $this->height ), + 'img_bits' => $this->bits, + 'img_media_type' => $this->media_type, + 'img_major_mime' => $this->major_mime, + 'img_minor_mime' => $this->minor_mime, + 'img_timestamp' => $timestamp, 'img_description' => $comment, - 'img_user' => $user->getId(), - 'img_user_text' => $user->getName(), - 'img_metadata' => $this->metadata, - 'img_sha1' => $this->sha1 + 'img_user' => $user->getId(), + 'img_user_text' => $user->getName(), + 'img_metadata' => $dbw->encodeBlob($this->metadata), + 'img_sha1' => $this->sha1 ), __METHOD__, 'IGNORE' @@ -1133,7 +1276,7 @@ class LocalFile extends File { 'img_description' => $comment, 'img_user' => $user->getId(), 'img_user_text' => $user->getName(), - 'img_metadata' => $this->metadata, + 'img_metadata' => $dbw->encodeBlob($this->metadata), 'img_sha1' => $this->sha1 ), array( 'img_name' => $this->getName() ), @@ -1149,23 +1292,48 @@ class LocalFile extends File { $wikiPage->setFile( $this ); # Add the log entry - $log = new LogPage( 'upload' ); $action = $reupload ? 'overwrite' : 'upload'; - $logId = $log->addEntry( $action, $descTitle, $comment, array(), $user ); - wfProfileIn( __METHOD__ . '-edit' ); + $logEntry = new ManualLogEntry( 'upload', $action ); + $logEntry->setPerformer( $user ); + $logEntry->setComment( $comment ); + $logEntry->setTarget( $descTitle ); + + // Allow people using the api to associate log entries with the upload. + // Log has a timestamp, but sometimes different from upload timestamp. + $logEntry->setParameters( + array( + 'img_sha1' => $this->sha1, + 'img_timestamp' => $timestamp, + ) + ); + // Note we keep $logId around since during new image + // creation, page doesn't exist yet, so log_page = 0 + // but we want it to point to the page we're making, + // so we later modify the log entry. + // For a similar reason, we avoid making an RC entry + // now and wait until the page exists. + $logId = $logEntry->insert(); + $exists = $descTitle->exists(); + if ( $exists ) { + // Page exists, do RC entry now (otherwise we wait for later). + $logEntry->publish( $logId ); + } + wfProfileIn( __METHOD__ . '-edit' ); if ( $exists ) { # Create a null revision $latest = $descTitle->getLatestRevID(); + $editSummary = LogFormatter::newFromEntry( $logEntry )->getPlainActionText(); + $nullRevision = Revision::newNullRevision( $dbw, $descTitle->getArticleID(), - $log->getRcComment(), + $editSummary, false ); - if (!is_null($nullRevision)) { + if ( !is_null( $nullRevision ) ) { $nullRevision->insertOn( $dbw ); wfRunHooks( 'NewRevisionFromEditComplete', array( $wikiPage, $nullRevision, $latest, $user ) ); @@ -1186,19 +1354,24 @@ class LocalFile extends File { } else { # New file; create the description page. # There's already a log entry, so don't make a second RC entry - # Squid and file cache for the description page are purged by doEdit. - $status = $wikiPage->doEdit( $pageText, $comment, EDIT_NEW | EDIT_SUPPRESS_RC, false, $user ); - - if ( isset( $status->value['revision'] ) ) { // XXX; doEdit() uses a transaction - $dbw->begin(); + # Squid and file cache for the description page are purged by doEditContent. + $content = ContentHandler::makeContent( $pageText, $descTitle ); + $status = $wikiPage->doEditContent( $content, $comment, EDIT_NEW | EDIT_SUPPRESS_RC, false, $user ); + + $dbw->begin( __METHOD__ ); // XXX; doEdit() uses a transaction + // Now that the page exists, make an RC entry. + $logEntry->publish( $logId ); + if ( isset( $status->value['revision'] ) ) { $dbw->update( 'logging', array( 'log_page' => $status->value['revision']->getPage() ), array( 'log_id' => $logId ), __METHOD__ ); - $dbw->commit(); // commit before anything bad can happen } + $dbw->commit( __METHOD__ ); // commit before anything bad can happen } + + wfProfileOut( __METHOD__ . '-edit' ); # Save to cache and purge the squid @@ -1225,11 +1398,17 @@ class LocalFile extends File { # Invalidate cache for all pages using this file $update = new HTMLCacheUpdate( $this->getTitle(), 'imagelinks' ); $update->doUpdate(); + if ( !$reupload ) { + LinksUpdate::queueRecursiveJobsForTable( $this->getTitle(), 'imagelinks' ); + } # Invalidate cache for all pages that redirects on this page $redirs = $this->getTitle()->getRedirectsHere(); foreach ( $redirs as $redir ) { + if ( !$reupload && $redir->getNamespace() === NS_FILE ) { + LinksUpdate::queueRecursiveJobsForTable( $redir, 'imagelinks' ); + } $update = new HTMLCacheUpdate( $redir, 'imagelinks' ); $update->doUpdate(); } @@ -1246,14 +1425,15 @@ class LocalFile extends File { * The archive name should be passed through to recordUpload for database * registration. * - * @param $srcPath String: local filesystem path to the source image + * @param string $srcPath local filesystem path to the source image * @param $flags Integer: a bitwise combination of: * File::DELETE_SOURCE Delete the source file, i.e. move rather than copy + * @param array $options Optional additional parameters * @return FileRepoStatus object. On success, the value member contains the * archive name, or an empty string if it was a new file. */ - function publish( $srcPath, $flags = 0 ) { - return $this->publishTo( $srcPath, $this->getRel(), $flags ); + function publish( $srcPath, $flags = 0, array $options = array() ) { + return $this->publishTo( $srcPath, $this->getRel(), $flags, $options ); } /** @@ -1263,24 +1443,25 @@ class LocalFile extends File { * The archive name should be passed through to recordUpload for database * registration. * - * @param $srcPath String: local filesystem path to the source image - * @param $dstRel String: target relative path + * @param string $srcPath local filesystem path to the source image + * @param string $dstRel target relative path * @param $flags Integer: a bitwise combination of: * File::DELETE_SOURCE Delete the source file, i.e. move rather than copy + * @param array $options Optional additional parameters * @return FileRepoStatus object. On success, the value member contains the * archive name, or an empty string if it was a new file. */ - function publishTo( $srcPath, $dstRel, $flags = 0 ) { + function publishTo( $srcPath, $dstRel, $flags = 0, array $options = array() ) { if ( $this->getRepo()->getReadOnlyReason() !== false ) { return $this->readOnlyFatalStatus(); } $this->lock(); // begin - $archiveName = wfTimestamp( TS_MW ) . '!'. $this->getName(); + $archiveName = wfTimestamp( TS_MW ) . '!' . $this->getName(); $archiveRel = 'archive/' . $this->getHashPath() . $archiveName; $flags = $flags & File::DELETE_SOURCE ? LocalRepo::DELETE_SOURCE : 0; - $status = $this->repo->publish( $srcPath, $dstRel, $archiveRel, $flags ); + $status = $this->repo->publish( $srcPath, $dstRel, $archiveRel, $flags, $options ); if ( $status->value == 'new' ) { $status->value = ''; @@ -1326,18 +1507,27 @@ class LocalFile extends File { wfDebugLog( 'imagemove', "Finished moving {$this->name}" ); - $this->purgeEverything(); - foreach ( $archiveNames as $archiveName ) { - $this->purgeOldThumbnails( $archiveName ); - } + // Purge the source and target files... + $oldTitleFile = wfLocalFile( $this->title ); + $newTitleFile = wfLocalFile( $target ); + // Hack: the lock()/unlock() pair is nested in a transaction so the locking is not + // tied to BEGIN/COMMIT. To avoid slow purges in the transaction, move them outside. + $this->getRepo()->getMasterDB()->onTransactionIdle( + function() use ( $oldTitleFile, $newTitleFile, $archiveNames ) { + $oldTitleFile->purgeEverything(); + foreach ( $archiveNames as $archiveName ) { + $oldTitleFile->purgeOldThumbnails( $archiveName ); + } + $newTitleFile->purgeEverything(); + } + ); + if ( $status->isOK() ) { // Now switch the object $this->title = $target; // Force regeneration of the name and hashpath unset( $this->name ); unset( $this->hashPath ); - // Purge the new image - $this->purgeEverything(); } return $status; @@ -1373,10 +1563,28 @@ class LocalFile extends File { DeferredUpdates::addUpdate( SiteStatsUpdate::factory( array( 'images' => -1 ) ) ); } - $this->purgeEverything(); - foreach ( $archiveNames as $archiveName ) { - $this->purgeOldThumbnails( $archiveName ); - } + // Hack: the lock()/unlock() pair is nested in a transaction so the locking is not + // tied to BEGIN/COMMIT. To avoid slow purges in the transaction, move them outside. + $file = $this; + $this->getRepo()->getMasterDB()->onTransactionIdle( + function() use ( $file, $archiveNames ) { + global $wgUseSquid; + + $file->purgeEverything(); + foreach ( $archiveNames as $archiveName ) { + $file->purgeOldThumbnails( $archiveName ); + } + + if ( $wgUseSquid ) { + // Purge the squid + $purgeUrls = array(); + foreach ( $archiveNames as $archiveName ) { + $purgeUrls[] = $file->getArchiveUrl( $archiveName ); + } + SquidUpdate::purge( $purgeUrls ); + } + } + ); return $status; } @@ -1396,6 +1604,7 @@ class LocalFile extends File { * @return FileRepoStatus object. */ function deleteOld( $archiveName, $reason, $suppress = false ) { + global $wgUseSquid; if ( $this->getRepo()->getReadOnlyReason() !== false ) { return $this->readOnlyFatalStatus(); } @@ -1413,6 +1622,11 @@ class LocalFile extends File { $this->purgeHistory(); } + if ( $wgUseSquid ) { + // Purge the squid + SquidUpdate::purge( array( $this->getArchiveUrl( $archiveName ) ) ); + } + return $status; } @@ -1422,7 +1636,7 @@ class LocalFile extends File { * * May throw database exceptions on error. * - * @param $versions array set of record ids of deleted items to restore, + * @param array $versions set of record ids of deleted items to restore, * or empty to restore all revisions. * @param $unsuppress Boolean * @return FileRepoStatus @@ -1462,22 +1676,27 @@ class LocalFile extends File { * @return String */ function getDescriptionUrl() { - return $this->title->getLocalUrl(); + return $this->title->getLocalURL(); } /** * Get the HTML text of the description page * This is not used by ImagePage for local files, since (among other things) * it skips the parser cache. + * + * @param $lang Language What language to get description in (Optional) * @return bool|mixed */ - function getDescriptionText() { - global $wgParser; + function getDescriptionText( $lang = null ) { $revision = Revision::newFromTitle( $this->title, false, Revision::READ_NORMAL ); - if ( !$revision ) return false; - $text = $revision->getText(); - if ( !$text ) return false; - $pout = $wgParser->parse( $text, $this->title, new ParserOptions() ); + if ( !$revision ) { + return false; + } + $content = $revision->getContent(); + if ( !$content ) { + return false; + } + $pout = $content->getParserOutput( $this->title, null, new ParserOptions( null, $lang ) ); return $pout->getText(); } @@ -1531,11 +1750,13 @@ class LocalFile extends File { } /** - * @return bool + * @return bool Whether to cache in RepoGroup (this avoids OOMs) */ function isCacheable() { $this->load(); - return strlen( $this->metadata ) <= self::CACHE_FIELD_MAX_LEN; // avoid OOMs + // If extra data (metadata) was not loaded then it must have been large + return $this->extraDataLoaded + && strlen( serialize( $this->metadata ) ) <= self::CACHE_FIELD_MAX_LEN; } /** @@ -1547,8 +1768,21 @@ class LocalFile extends File { $dbw = $this->repo->getMasterDB(); if ( !$this->locked ) { - $dbw->begin( __METHOD__ ); + if ( !$dbw->trxLevel() ) { + $dbw->begin( __METHOD__ ); + $this->lockedOwnTrx = true; + } $this->locked++; + // Bug 54736: use simple lock to handle when the file does not exist. + // SELECT FOR UPDATE only locks records not the gaps where there are none. + $cache = wfGetMainCache(); + $key = $this->getCacheKey(); + if ( !$cache->lock( $key, 60 ) ) { + throw new MWException( "Could not acquire lock for '{$this->getName()}.'" ); + } + $dbw->onTransactionIdle( function() use ( $cache, $key ) { + $cache->unlock( $key ); // release on commit + } ); } return $dbw->selectField( 'image', '1', @@ -1562,9 +1796,10 @@ class LocalFile extends File { function unlock() { if ( $this->locked ) { --$this->locked; - if ( !$this->locked ) { + if ( !$this->locked && $this->lockedOwnTrx ) { $dbw = $this->repo->getMasterDB(); $dbw->commit( __METHOD__ ); + $this->lockedOwnTrx = false; } } } @@ -1576,6 +1811,7 @@ class LocalFile extends File { $this->locked = false; $dbw = $this->repo->getMasterDB(); $dbw->rollback( __METHOD__ ); + $this->lockedOwnTrx = false; } /** @@ -1758,7 +1994,7 @@ class LocalFileDeleteBatch { 'fa_deleted_user' => $encUserId, 'fa_deleted_timestamp' => $encTimestamp, 'fa_deleted_reason' => $encReason, - 'fa_deleted' => $this->suppress ? $bitfield : 0, + 'fa_deleted' => $this->suppress ? $bitfield : 0, 'fa_name' => 'img_name', 'fa_archive_name' => 'NULL', @@ -1773,7 +2009,8 @@ class LocalFileDeleteBatch { 'fa_description' => 'img_description', 'fa_user' => 'img_user', 'fa_user_text' => 'img_user_text', - 'fa_timestamp' => 'img_timestamp' + 'fa_timestamp' => 'img_timestamp', + 'fa_sha1' => 'img_sha1', ), $where, __METHOD__ ); } @@ -1805,6 +2042,7 @@ class LocalFileDeleteBatch { 'fa_user' => 'oi_user', 'fa_user_text' => 'oi_user_text', 'fa_timestamp' => 'oi_timestamp', + 'fa_sha1' => 'oi_sha1', ), $where, __METHOD__ ); } } @@ -1836,7 +2074,7 @@ class LocalFileDeleteBatch { $this->file->lock(); // Leave private files alone $privateFiles = array(); - list( $oldRels, $deleteCurrent ) = $this->getOldRels(); + list( $oldRels, ) = $this->getOldRels(); $dbw = $this->file->repo->getMasterDB(); if ( !empty( $oldRels ) ) { @@ -1914,7 +2152,7 @@ class LocalFileDeleteBatch { $files = $newBatch = array(); foreach ( $batch as $batchItem ) { - list( $src, $dest ) = $batchItem; + list( $src, ) = $batchItem; $files[$src] = $this->file->repo->getVirtualUrl( 'public' ) . '/' . rawurlencode( $src ); } @@ -2004,7 +2242,9 @@ class LocalFileRestoreBatch { $conditions[] = 'fa_id IN (' . $dbw->makeList( $this->ids ) . ')'; } - $result = $dbw->select( 'filearchive', '*', + $result = $dbw->select( + 'filearchive', + ArchivedFile::selectFields(), $conditions, __METHOD__, array( 'ORDER BY' => 'fa_timestamp DESC' ) @@ -2037,7 +2277,12 @@ class LocalFileRestoreBatch { $deletedRel = $this->file->repo->getDeletedHashPath( $row->fa_storage_key ) . $row->fa_storage_key; $deletedUrl = $this->file->repo->getVirtualUrl() . '/deleted/' . $deletedRel; - $sha1 = substr( $row->fa_storage_key, 0, strcspn( $row->fa_storage_key, '.' ) ); + if ( isset( $row->fa_sha1 ) ) { + $sha1 = $row->fa_sha1; + } else { + // old row, populate from key + $sha1 = LocalRepo::getHashFromKey( $row->fa_storage_key ); + } # Fix leading zero if ( strlen( $sha1 ) == 32 && $sha1[0] == '0' ) { @@ -2251,7 +2496,7 @@ class LocalFileRestoreBatch { /** * Delete unused files in the deleted zone. * This should be called from outside the transaction in which execute() was called. - * @return FileRepoStatus|void + * @return FileRepoStatus */ function cleanup() { if ( !$this->cleanupBatch ) { @@ -2492,7 +2737,7 @@ class LocalFileMoveBatch { */ function getMoveTriplets() { $moves = array_merge( array( $this->cur ), $this->olds ); - $triplets = array(); // The format is: (srcUrl, destZone, destUrl) + $triplets = array(); // The format is: (srcUrl, destZone, destUrl) foreach ( $moves as $move ) { // $move: (oldRelativePath, newRelativePath) diff --git a/includes/filerepo/file/OldLocalFile.php b/includes/filerepo/file/OldLocalFile.php index 40d7dca7..2c545963 100644 --- a/includes/filerepo/file/OldLocalFile.php +++ b/includes/filerepo/file/OldLocalFile.php @@ -42,7 +42,7 @@ class OldLocalFile extends LocalFile { static function newFromTitle( $title, $repo, $time = null ) { # The null default value is only here to avoid an E_STRICT if ( $time === null ) { - throw new MWException( __METHOD__.' got null for $time parameter' ); + throw new MWException( __METHOD__ . ' got null for $time parameter' ); } return new self( $title, $repo, $time, null ); } @@ -73,7 +73,7 @@ class OldLocalFile extends LocalFile { * Create a OldLocalFile from a SHA-1 key * Do not call this except from inside a repo class. * - * @param $sha1 string base-36 SHA-1 + * @param string $sha1 base-36 SHA-1 * @param $repo LocalRepo * @param string|bool $timestamp MW_timestamp (optional) * @@ -123,8 +123,8 @@ class OldLocalFile extends LocalFile { /** * @param $title Title * @param $repo FileRepo - * @param $time String: timestamp or null to load by archive name - * @param $archiveName String: archive name or null to load by timestamp + * @param string $time timestamp or null to load by archive name + * @param string $archiveName archive name or null to load by timestamp * @throws MWException */ function __construct( $title, $repo, $time, $archiveName ) { @@ -132,7 +132,7 @@ class OldLocalFile extends LocalFile { $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' ); + throw new MWException( __METHOD__ . ': must specify at least one of $time or $archiveName' ); } } @@ -164,18 +164,19 @@ class OldLocalFile extends LocalFile { * @return bool */ function isVisible() { - return $this->exists() && !$this->isDeleted(File::DELETED_FILE); + return $this->exists() && !$this->isDeleted( File::DELETED_FILE ); } function loadFromDB() { wfProfileIn( __METHOD__ ); + $this->dataLoaded = true; $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( $dbr->timestamp( $this->requestedTime ) ); + $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime ); } $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ), $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) ); @@ -184,6 +185,43 @@ class OldLocalFile extends LocalFile { } else { $this->fileExists = false; } + + wfProfileOut( __METHOD__ ); + } + + /** + * Load lazy file metadata from the DB + */ + protected function loadExtraFromDB() { + wfProfileIn( __METHOD__ ); + + $this->extraDataLoaded = true; + $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->timestamp( $this->requestedTime ); + } + // In theory the file could have just been renamed/deleted...oh well + $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ), + $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) ); + + if ( !$row ) { // fallback to master + $dbr = $this->repo->getMasterDB(); + $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ), + $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) ); + } + + if ( $row ) { + foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) { + $this->$name = $value; + } + } else { + wfProfileOut( __METHOD__ ); + throw new MWException( "Could not find data for image '{$this->archive_name}'." ); + } + wfProfileOut( __METHOD__ ); } @@ -218,7 +256,7 @@ class OldLocalFile extends LocalFile { # Don't destroy file info of missing files if ( !$this->fileExists ) { - wfDebug( __METHOD__.": file does not exist, aborting\n" ); + wfDebug( __METHOD__ . ": file does not exist, aborting\n" ); wfProfileOut( __METHOD__ ); return; } @@ -226,7 +264,7 @@ class OldLocalFile extends LocalFile { $dbw = $this->repo->getMasterDB(); list( $major, $minor ) = self::splitMime( $this->mime ); - wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n"); + wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema\n" ); $dbw->update( 'oldimage', array( 'oi_size' => $this->size, // sanity @@ -253,7 +291,7 @@ class OldLocalFile extends LocalFile { */ function isDeleted( $field ) { $this->load(); - return ($this->deleted & $field) == $field; + return ( $this->deleted & $field ) == $field; } /** @@ -281,8 +319,8 @@ class OldLocalFile extends LocalFile { /** * Upload a file directly into archive. Generally for Special:Import. * - * @param $srcPath string File system path of the source file - * @param $archiveName string Full archive name of the file, in the form + * @param string $srcPath File system path of the source file + * @param string $archiveName Full archive name of the file, in the form * $timestamp!$filename, where $filename must match $this->getName() * * @param $timestamp string @@ -313,10 +351,10 @@ class OldLocalFile extends LocalFile { /** * Record a file upload in the oldimage table, without adding log entries. * - * @param $srcPath string File system path to the source file - * @param $archiveName string The archive name of the file + * @param string $srcPath File system path to the source file + * @param string $archiveName The archive name of the file * @param $timestamp string - * @param $comment string Upload comment + * @param string $comment Upload comment * @param $user User User who did this upload * @return bool */ diff --git a/includes/filerepo/file/UnregisteredLocalFile.php b/includes/filerepo/file/UnregisteredLocalFile.php index 8d4a3f88..47ba6d6b 100644 --- a/includes/filerepo/file/UnregisteredLocalFile.php +++ b/includes/filerepo/file/UnregisteredLocalFile.php @@ -42,7 +42,7 @@ class UnregisteredLocalFile extends File { var $handler; /** - * @param $path string Storage path + * @param string $path Storage path * @param $mime string * @return UnregisteredLocalFile */ @@ -71,7 +71,7 @@ class UnregisteredLocalFile extends File { */ function __construct( $title = false, $repo = false, $path = false, $mime = false ) { if ( !( $title && $repo ) && !$path ) { - throw new MWException( __METHOD__.': not enough parameters, must specify title and repo, or a full path' ); + throw new MWException( __METHOD__ . ': not enough parameters, must specify title and repo, or a full path' ); } if ( $title instanceof Title ) { $this->title = File::normalizeTitle( $title, 'exception' ); @@ -179,10 +179,18 @@ class UnregisteredLocalFile extends File { */ function getSize() { $this->assertRepoDefined(); - $props = $this->repo->getFileProps( $this->path ); - if ( isset( $props['size'] ) ) { - return $props['size']; - } - return false; // doesn't exist + return $this->repo->getFileSize( $this->path ); + } + + /** + * Optimize getLocalRefPath() by using an existing local reference. + * The file at the path of $fsFile should not be deleted (or at least + * not until the end of the request). This is mostly a performance hack. + * + * @param $fsFile FSFile + * @return void + */ + public function setLocalReference( FSFile $fsFile ) { + $this->fsFile = $fsFile; } } |