From 08aa4418c30cfc18ccc69a0f0f9cb9e17be6c196 Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Mon, 12 Aug 2013 09:28:15 +0200 Subject: Update to MediaWiki 1.21.1 --- includes/filerepo/file/ArchivedFile.php | 152 ++++++------ includes/filerepo/file/File.php | 208 +++++++++++------ includes/filerepo/file/ForeignAPIFile.php | 8 +- includes/filerepo/file/ForeignDBFile.php | 15 +- includes/filerepo/file/LocalFile.php | 281 +++++++++++++++++------ includes/filerepo/file/OldLocalFile.php | 65 ++++-- includes/filerepo/file/UnregisteredLocalFile.php | 22 +- 7 files changed, 515 insertions(+), 236 deletions(-) (limited to 'includes/filerepo/file') diff --git a/includes/filerepo/file/ArchivedFile.php b/includes/filerepo/file/ArchivedFile.php index c5a0bd1b..3f786197 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 ) { $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() { @@ -127,64 +130,30 @@ class ArchivedFile { $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 ) { + $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,68 @@ 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_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 ); + } } /** @@ -380,6 +391,17 @@ class ArchivedFile { return wfTimestamp( TS_MW, $this->timestamp ); } + /** + * 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. * diff --git a/includes/filerepo/file/File.php b/includes/filerepo/file/File.php index 557609d4..cecd0aee 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. @@ -54,7 +54,7 @@ abstract class File { 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 +152,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 +190,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 ) { @@ -214,7 +214,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 */ @@ -316,7 +316,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 +348,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 +369,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 +432,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,12 +512,12 @@ 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) - */ + * 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 ) ) { @@ -662,13 +663,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 @@ -766,7 +767,7 @@ abstract class File { * Use File::THUMB_FULL_NAME to always get a name like "-". * Otherwise, the format may be "-" or "-thumbnail.". * - * @param $params Array: handler-specific parameters + * @param array $params handler-specific parameters * @param $flags integer Bitfield that supports THUMB_* constants * @return string */ @@ -831,8 +832,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 @@ -841,7 +842,7 @@ abstract class File { global $wgIgnoreImageErrors; if ( $wgIgnoreImageErrors && !( $flags & self::RENDER_NOW ) ) { - return $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params ); + return $this->getHandler()->getTransform( $this, $thumbPath, $thumbUrl, $params ); } else { return new MediaTransformError( 'thumbnail_error', $params['width'], 0, wfMessage( 'thumbnail-dest-create' )->text() ); @@ -851,7 +852,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 +873,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 +897,21 @@ 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 +938,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 +948,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 +978,7 @@ abstract class File { } /** - * @param $thumbName string Thumbnail name + * @param string $thumbName Thumbnail name * @return string Content-Disposition header value */ function getThumbDisposition( $thumbName ) { @@ -1048,7 +1051,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,9 +1091,9 @@ 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 */ @@ -1147,7 +1150,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 +1168,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 +1194,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 +1212,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 +1224,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 +1238,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 */ @@ -1244,16 +1247,29 @@ abstract class File { return $this->repo->getZonePath( 'thumb' ) . '/' . $this->getThumbRel( $suffix ); } + /** + * 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 +1281,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,25 +1300,49 @@ 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 ); } return $path; } + /** + * 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 +1358,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 +1376,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 +1401,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 +1414,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 +1431,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(); } @@ -1490,9 +1539,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 +1567,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 +1629,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 ) { @@ -1617,15 +1666,15 @@ abstract class File { $renderUrl = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgLang->getCode() ); if ( $renderUrl ) { if ( $this->repo->descriptionCacheExpiry > 0 ) { - wfDebug("Attempting to get the description from cache..."); + wfDebug( "Attempting to get the description from cache..." ); $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', $wgLang->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 +1753,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 +1778,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' ); @@ -1736,6 +1787,18 @@ abstract class File { return $fsFile->getSha1Base36(); } + /** + * @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 */ @@ -1780,7 +1843,7 @@ abstract class File { } /** - * @return Title + * @return Title|null */ function getRedirectedTitle() { if ( $this->redirected ) { @@ -1789,6 +1852,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..a96c1f3f 100644 --- a/includes/filerepo/file/ForeignAPIFile.php +++ b/includes/filerepo/file/ForeignAPIFile.php @@ -106,7 +106,7 @@ class ForeignAPIFile extends File { } /** - * @param Array $params + * @param array $params * @param int $flags * @return bool|MediaTransformOutput */ @@ -189,7 +189,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; } @@ -256,7 +256,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 +293,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..ee5883c4 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,7 +115,7 @@ class ForeignDBFile extends LocalFile { * @return string */ function getDescriptionUrl() { - // Restore remote behaviour + // Restore remote behavior return File::getDescriptionUrl(); } @@ -116,7 +123,7 @@ class ForeignDBFile extends LocalFile { * @return string */ function getDescriptionText() { - // Restore remote behaviour + // Restore remote behavior return File::getDescriptionText(); } } diff --git a/includes/filerepo/file/LocalFile.php b/includes/filerepo/file/LocalFile.php index 695c4e9e..639228b9 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 ); } /** @@ -287,6 +307,28 @@ class LocalFile extends File { return $results[$prefix]; } + /** + * @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 */ @@ -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,70 @@ 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 { + 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 +442,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 +456,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 +466,9 @@ class LocalFile extends File { } $this->dataLoaded = true; } + if ( ( $flags & self::LOAD_ALL ) && !$this->extraDataLoaded ) { + $this->loadExtraFromDB(); + } } /** @@ -397,7 +488,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 ) ) { @@ -464,6 +555,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. @@ -552,7 +644,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 +662,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 +730,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 +763,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 ) { @@ -736,7 +827,7 @@ class LocalFile extends File { /** * 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; @@ -771,8 +862,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 +887,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 +895,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 +909,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 +1046,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 +1067,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 +1119,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; } @@ -1165,7 +1290,7 @@ class LocalFile extends File { $log->getRcComment(), false ); - if (!is_null($nullRevision)) { + if ( !is_null( $nullRevision ) ) { $nullRevision->insertOn( $dbw ); wfRunHooks( 'NewRevisionFromEditComplete', array( $wikiPage, $nullRevision, $latest, $user ) ); @@ -1186,17 +1311,18 @@ 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 ); + # 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 ); if ( isset( $status->value['revision'] ) ) { // XXX; doEdit() uses a transaction - $dbw->begin(); + $dbw->begin( __METHOD__ ); $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' ); @@ -1246,14 +1372,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,14 +1390,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 $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(); } @@ -1280,7 +1408,7 @@ class LocalFile extends File { $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 = ''; @@ -1422,7 +1550,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 @@ -1472,12 +1600,11 @@ class LocalFile extends File { * @return bool|mixed */ function getDescriptionText() { - global $wgParser; $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() ); + $content = $revision->getContent(); + if ( !$content ) return false; + $pout = $content->getParserOutput( $this->title, null, new ParserOptions() ); return $pout->getText(); } @@ -1547,7 +1674,10 @@ 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++; } @@ -1562,9 +1692,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 +1707,7 @@ class LocalFile extends File { $this->locked = false; $dbw = $this->repo->getMasterDB(); $dbw->rollback( __METHOD__ ); + $this->lockedOwnTrx = false; } /** @@ -1758,7 +1890,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 +1905,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 +1938,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 +1970,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 +2048,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 +2138,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 +2173,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 +2392,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 +2633,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..5c505928 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,42 @@ 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 { + throw new MWException( "Could not find data for image '{$this->archive_name}'." ); + } + wfProfileOut( __METHOD__ ); } @@ -218,7 +255,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 +263,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 @@ -281,8 +318,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 +350,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; } } -- cgit v1.2.2