diff options
author | Pierre Schmitz <pierre@archlinux.de> | 2013-01-18 16:46:04 +0100 |
---|---|---|
committer | Pierre Schmitz <pierre@archlinux.de> | 2013-01-18 16:46:04 +0100 |
commit | 63601400e476c6cf43d985f3e7b9864681695ed4 (patch) | |
tree | f7846203a952e38aaf66989d0a4702779f549962 /includes/filerepo/file | |
parent | 8ff01378c9e0207f9169b81966a51def645b6a51 (diff) |
Update to MediaWiki 1.20.2
this update includes:
* adjusted Arch Linux skin
* updated FluxBBAuthPlugin
* patch for https://bugzilla.wikimedia.org/show_bug.cgi?id=44024
Diffstat (limited to 'includes/filerepo/file')
-rw-r--r-- | includes/filerepo/file/ArchivedFile.php | 21 | ||||
-rw-r--r-- | includes/filerepo/file/File.php | 219 | ||||
-rw-r--r-- | includes/filerepo/file/ForeignAPIFile.php | 90 | ||||
-rw-r--r-- | includes/filerepo/file/ForeignDBFile.php | 46 | ||||
-rw-r--r-- | includes/filerepo/file/LocalFile.php | 491 | ||||
-rw-r--r-- | includes/filerepo/file/OldLocalFile.php | 103 | ||||
-rw-r--r-- | includes/filerepo/file/UnregisteredLocalFile.php | 57 |
7 files changed, 801 insertions, 226 deletions
diff --git a/includes/filerepo/file/ArchivedFile.php b/includes/filerepo/file/ArchivedFile.php index 3b9bd7f0..c5a0bd1b 100644 --- a/includes/filerepo/file/ArchivedFile.php +++ b/includes/filerepo/file/ArchivedFile.php @@ -1,6 +1,21 @@ <?php /** - * Deleted file in the 'filearchive' table + * Deleted file in the 'filearchive' table. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html * * @file * @ingroup FileAbstraction @@ -93,7 +108,7 @@ class ArchivedFile { /** * Loads a file object from the filearchive table - * @return true on success or null + * @return bool|null True on success or null */ public function load() { if ( $this->dataLoaded ) { @@ -143,7 +158,7 @@ class ArchivedFile { array( 'ORDER BY' => 'fa_timestamp DESC' ) ); if ( $res == false || $dbr->numRows( $res ) == 0 ) { // this revision does not exist? - return; + return null; } $ret = $dbr->resultObject( $res ); $row = $ret->fetchObject(); diff --git a/includes/filerepo/file/File.php b/includes/filerepo/file/File.php index f74fb678..557609d4 100644 --- a/includes/filerepo/file/File.php +++ b/includes/filerepo/file/File.php @@ -9,6 +9,21 @@ /** * Base code for files. * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * * @file * @ingroup FileAbstraction */ @@ -48,6 +63,14 @@ abstract class File { const DELETE_SOURCE = 1; + // Audience options for File::getDescription() + const FOR_PUBLIC = 1; + const FOR_THIS_USER = 2; + const RAW = 3; + + // Options for File::thumbName() + const THUMB_FULL_NAME = 1; + /** * Some member variables can be lazy-initialised using __get(). The * initialisation function for these variables is always a function named @@ -68,19 +91,19 @@ abstract class File { */ /** - * @var FileRepo|false + * @var FileRepo|bool */ var $repo; /** - * @var Title|false + * @var Title */ var $title; var $lastError, $redirected, $redirectedTitle; /** - * @var FSFile|false + * @var FSFile|bool False if undefined */ protected $fsFile; @@ -94,6 +117,8 @@ abstract class File { */ protected $url, $extension, $name, $path, $hashPath, $pageCount, $transformScript; + protected $redirectTitle; + /** * @var bool */ @@ -111,8 +136,8 @@ abstract class File { * may return false or throw exceptions if they are not set. * Most subclasses will want to call assertRepoDefined() here. * - * @param $title Title|string|false - * @param $repo FileRepo|false + * @param $title Title|string|bool + * @param $repo FileRepo|bool */ function __construct( $title, $repo ) { if ( $title !== false ) { // subclasses may not use MW titles @@ -127,7 +152,8 @@ abstract class File { * valid Title object with namespace NS_FILE or null * * @param $title Title|string - * @param $exception string|false Use 'exception' to throw an error on bad titles + * @param $exception string|bool Use 'exception' to throw an error on bad titles + * @throws MWException * @return Title|null */ static function normalizeTitle( $title, $exception = false ) { @@ -223,6 +249,18 @@ abstract class File { } /** + * Callback for usort() to do file sorts by name + * + * @param $a File + * @param $b File + * + * @return Integer: result of name comparison + */ + public static function compare( File $a, File $b ) { + return strcmp( $a->getName(), $b->getName() ); + } + + /** * Return the name of this file * * @return string @@ -252,7 +290,7 @@ abstract class File { /** * Return the associated title object * - * @return Title|false + * @return Title */ public function getTitle() { return $this->title; @@ -319,17 +357,17 @@ abstract class File { } /** - * Return the storage path to the file. Note that this does - * not mean that a file actually exists under that location. - * - * This path depends on whether directory hashing is active or not, - * i.e. whether the files are all found in the same directory, - * or in hashed paths like /images/3/3c. - * - * Most callers don't check the return value, but ForeignAPIFile::getPath - * returns false. + * Return the storage path to the file. Note that this does + * not mean that a file actually exists under that location. + * + * This path depends on whether directory hashing is active or not, + * i.e. whether the files are all found in the same directory, + * or in hashed paths like /images/3/3c. + * + * Most callers don't check the return value, but ForeignAPIFile::getPath + * returns false. * - * @return string|false + * @return string|bool ForeignAPIFile::getPath can return false */ public function getPath() { if ( !isset( $this->path ) ) { @@ -344,7 +382,7 @@ abstract class File { * Returns false on failure. Callers must not alter the file. * Temporary files are cleared automatically. * - * @return string|false + * @return string|bool False on failure */ public function getLocalRefPath() { $this->assertRepoDefined(); @@ -383,7 +421,7 @@ abstract class File { * * @param $page int * - * @return false|number + * @return bool|number False on failure */ public function getHeight( $page = 1 ) { return false; @@ -430,9 +468,43 @@ abstract class File { } /** + * Will the thumbnail be animated if one would expect it to be. + * + * Currently used to add a warning to the image description page + * + * @return bool false if the main image is both animated + * and the thumbnail is not. In all other cases must return + * true. If image is not renderable whatsoever, should + * return true. + */ + public function canAnimateThumbIfAppropriate() { + $handler = $this->getHandler(); + if ( !$handler ) { + // We cannot handle image whatsoever, thus + // one would not expect it to be animated + // so true. + return true; + } else { + if ( $this->allowInlineDisplay() + && $handler->isAnimatedImage( $this ) + && !$handler->canAnimateThumbnail( $this ) + ) { + // Image is animated, but thumbnail isn't. + // This is unexpected to the user. + return false; + } else { + // Image is not animated, so one would + // not expect thumb to be + return true; + } + } + } + + /** * Get handler-specific metadata * Overridden by LocalFile, UnregisteredLocalFile * STUB + * @return bool */ public function getMetadata() { return false; @@ -462,6 +534,7 @@ abstract class File { * Return the bit depth of the file * Overridden by LocalFile * STUB + * @return int */ public function getBitDepth() { return 0; @@ -471,6 +544,7 @@ abstract class File { * Return the size of the image file, in bytes * Overridden by LocalFile, UnregisteredLocalFile * STUB + * @return bool */ public function getSize() { return false; @@ -492,6 +566,7 @@ abstract class File { * Use the value returned by this function with the MEDIATYPE_xxx constants. * Overridden by LocalFile, * STUB + * @return string */ function getMediaType() { return MEDIATYPE_UNKNOWN; @@ -518,6 +593,7 @@ abstract class File { /** * Accessor for __get() + * @return bool */ protected function getCanRender() { return $this->canRender(); @@ -686,15 +762,19 @@ abstract class File { } /** - * Return the file name of a thumbnail with the specified parameters + * Return the file name of a thumbnail with the specified parameters. + * 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 - * @private -ish - * + * @param $flags integer Bitfield that supports THUMB_* constants * @return string */ - function thumbName( $params ) { - return $this->generateThumbName( $this->getName(), $params ); + public function thumbName( $params, $flags = 0 ) { + $name = ( $this->repo && !( $flags & self::THUMB_FULL_NAME ) ) + ? $this->repo->nameForThumb( $this->getName() ) + : $this->getName(); + return $this->generateThumbName( $name, $params ); } /** @@ -705,7 +785,7 @@ abstract class File { * * @return string */ - function generateThumbName( $name, $params ) { + public function generateThumbName( $name, $params ) { if ( !$this->getHandler() ) { return null; } @@ -750,7 +830,7 @@ abstract class File { /** * Return either a MediaTransformError or placeholder thumbnail (if $wgIgnoreImageErrors) - * + * * @param $thumbPath string Thumbnail storage path * @param $thumbUrl string Thumbnail URL * @param $params Array @@ -764,7 +844,7 @@ abstract class File { return $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params ); } else { return new MediaTransformError( 'thumbnail_error', - $params['width'], 0, wfMsg( 'thumbnail-dest-create' ) ); + $params['width'], 0, wfMessage( 'thumbnail-dest-create' )->text() ); } } @@ -774,7 +854,7 @@ abstract class File { * @param $params Array: 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|false + * @return MediaTransformOutput|bool False on failure */ function transform( $params, $flags = 0 ) { global $wgUseSquid, $wgIgnoreImageErrors, $wgThumbnailEpoch; @@ -837,6 +917,13 @@ abstract class File { } } + // If the backend is ready-only, don't keep generating thumbnails + // only to return transformation errors, just return the error now. + if ( $this->repo->getReadOnlyReason() !== false ) { + $thumb = $this->transformErrorOutput( $thumbPath, $thumbUrl, $params, $flags ); + break; + } + // Create a temp FS file with the same extension and the thumbnail $thumbExt = FileBackend::extensionFromPath( $thumbPath ); $tmpFile = TempFSFile::factory( 'transform_', $thumbExt ); @@ -847,7 +934,9 @@ abstract class File { $tmpThumbPath = $tmpFile->getPath(); // path of 0-byte temp file // Actually render the thumbnail... + wfProfileIn( __METHOD__ . '-doTransform' ); $thumb = $this->handler->doTransform( $this, $tmpThumbPath, $thumbUrl, $params ); + wfProfileOut( __METHOD__ . '-doTransform' ); $tmpFile->bind( $thumb ); // keep alive with $thumb if ( !$thumb ) { // bad params? @@ -859,19 +948,16 @@ abstract class File { $thumb = $this->handler->getTransform( $this, $tmpThumbPath, $thumbUrl, $params ); } } elseif ( $this->repo && $thumb->hasFile() && !$thumb->fileIsSource() ) { - $backend = $this->repo->getBackend(); - // Copy the thumbnail from the file system into storage. This avoids using - // FileRepo::store(); getThumbPath() uses a different zone in some subclasses. - $backend->prepare( array( 'dir' => dirname( $thumbPath ) ) ); - $status = $backend->store( - array( 'src' => $tmpThumbPath, 'dst' => $thumbPath, 'overwrite' => 1 ), - array( 'force' => 1, 'nonLocking' => 1, 'allowStale' => 1 ) - ); + // Copy the thumbnail from the file system into storage... + $disposition = $this->getThumbDisposition( $thumbName ); + $status = $this->repo->quickImport( $tmpThumbPath, $thumbPath, $disposition ); if ( $status->isOK() ) { $thumb->setStoragePath( $thumbPath ); } else { $thumb = $this->transformErrorOutput( $thumbPath, $thumbUrl, $params, $flags ); } + // Give extensions a chance to do something with this thumbnail... + wfRunHooks( 'FileTransformed', array( $this, $thumb, $tmpThumbPath, $thumbPath ) ); } // Purge. Useful in the event of Core -> Squid connection failure or squid @@ -889,6 +975,19 @@ abstract class File { } /** + * @param $thumbName string Thumbnail name + * @return string Content-Disposition header value + */ + function getThumbDisposition( $thumbName ) { + $fileName = $this->name; // file name to suggest + $thumbExt = FileBackend::extensionFromPath( $thumbName ); + if ( $thumbExt != '' && $thumbExt !== $this->getExtension() ) { + $fileName .= ".$thumbExt"; + } + return FileBackend::makeContentDisposition( 'inline', $fileName ); + } + + /** * Hook into transform() to allow migration of thumbnail files * STUB * Overridden by LocalFile @@ -920,7 +1019,8 @@ abstract class File { $path = '/common/images/icons/' . $icon; $filepath = $wgStyleDirectory . $path; if ( file_exists( $filepath ) ) { // always FS - return new ThumbnailImage( $this, $wgStylePath . $path, 120, 120 ); + $params = array( 'width' => 120, 'height' => 120 ); + return new ThumbnailImage( $this, $wgStylePath . $path, false, $params ); } } return null; @@ -938,6 +1038,7 @@ abstract class File { * Get all thumbnail names previously generated for this file * STUB * Overridden by LocalFile + * @return array */ function getThumbnails() { return array(); @@ -987,13 +1088,13 @@ abstract class File { * * STUB * @param $limit integer Limit of rows to return - * @param $start timestamp Only revisions older than $start will be returned - * @param $end timestamp Only revisions newer than $end will be returned + * @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 * * @return array */ - function getHistory($limit = null, $start = null, $end = null, $inc=true) { + function getHistory( $limit = null, $start = null, $end = null, $inc=true ) { return array(); } @@ -1004,6 +1105,7 @@ abstract class File { * * STUB * Overridden in LocalFile + * @return bool */ public function nextHistoryLine() { return false; @@ -1185,7 +1287,7 @@ abstract class File { * * @param $suffix bool|string if not false, the name of a thumbnail file * - * @return path + * @return string path */ function getThumbUrl( $suffix = false ) { $this->assertRepoDefined(); @@ -1251,7 +1353,7 @@ abstract class File { */ function isHashed() { $this->assertRepoDefined(); - return $this->repo->isHashed(); + return (bool)$this->repo->getHashLevels(); } /** @@ -1329,7 +1431,7 @@ abstract class File { /** * Returns the repository * - * @return FileRepo|false + * @return FileRepo|bool */ function getRepo() { return $this->repo; @@ -1360,6 +1462,7 @@ abstract class File { /** * Return the deletion bitfield * STUB + * @return int */ function getVisibility() { return 0; @@ -1401,7 +1504,7 @@ abstract class File { * * @param $reason String * @param $suppress Boolean: hide content from sysops? - * @return true on success, false on some kind of failure + * @return bool on success, false on some kind of failure * STUB * Overridden by LocalFile */ @@ -1418,7 +1521,7 @@ abstract class File { * @param $versions array set of record ids of deleted items to restore, * or empty to restore all revisions. * @param $unsuppress bool remove restrictions on content upon restoration? - * @return int|false the number of file revisions restored if successful, + * @return int|bool the number of file revisions restored if successful, * or false on failure * STUB * Overridden by LocalFile @@ -1442,7 +1545,7 @@ abstract class File { * Returns the number of pages of a multipage document, or false for * documents which aren't multipage documents * - * @return false|int + * @return bool|int */ function pageCount() { if ( !isset( $this->pageCount ) ) { @@ -1536,19 +1639,25 @@ abstract class File { } /** - * Get discription of file revision + * Get description of file revision * STUB * + * @param $audience Integer: one of: + * File::FOR_PUBLIC to be displayed to all users + * File::FOR_THIS_USER to be displayed to the given user + * File::RAW get the description regardless of permissions + * @param $user User object to check for, only if FOR_THIS_USER is passed + * to the $audience parameter * @return string */ - function getDescription() { + function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) { return null; } /** * Get the 14-character timestamp of the file upload * - * @return string|false TS_MW timestamp or false on failure + * @return string|bool TS_MW timestamp or false on failure */ function getTimestamp() { $this->assertRepoDefined(); @@ -1566,7 +1675,7 @@ abstract class File { } /** - * Get the deletion archive key, <sha1>.<ext> + * Get the deletion archive key, "<sha1>.<ext>" * * @return string */ @@ -1618,7 +1727,7 @@ abstract class File { * * @param $path string * - * @return false|string False on failure + * @return bool|string False on failure */ static function sha1Base36( $path ) { wfDeprecated( __METHOD__, '1.19' ); @@ -1698,6 +1807,14 @@ abstract class File { } /** + * Check if this file object is small and can be cached + * @return boolean + */ + public function isCacheable() { + return true; + } + + /** * Assert that $this->repo is set to a valid FileRepo instance * @throws MWException */ diff --git a/includes/filerepo/file/ForeignAPIFile.php b/includes/filerepo/file/ForeignAPIFile.php index 681544fd..56482611 100644 --- a/includes/filerepo/file/ForeignAPIFile.php +++ b/includes/filerepo/file/ForeignAPIFile.php @@ -2,6 +2,21 @@ /** * Foreign file accessible through api.php requests. * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * * @file * @ingroup FileAbstraction */ @@ -39,9 +54,9 @@ 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() ) ); @@ -68,20 +83,33 @@ class ForeignAPIFile extends File { /** * Get the property string for iiprop and aiprop + * @return string */ static function getProps() { return 'timestamp|user|comment|url|size|sha1|metadata|mime'; } // Dummy functions... + + /** + * @return bool + */ public function exists() { return $this->mExists; } + /** + * @return bool + */ public function getPath() { return false; } + /** + * @param Array $params + * @param int $flags + * @return bool|MediaTransformOutput + */ function transform( $params, $flags = 0 ) { if( !$this->canRender() ) { // show icon @@ -101,6 +129,11 @@ class ForeignAPIFile extends File { } // Info we can get from API... + + /** + * @param $page int + * @return int|number + */ public function getWidth( $page = 1 ) { return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0; } @@ -113,6 +146,9 @@ class ForeignAPIFile extends File { return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0; } + /** + * @return bool|null|string + */ public function getMetadata() { if ( isset( $this->mInfo['metadata'] ) ) { return serialize( self::parseMetadata( $this->mInfo['metadata'] ) ); @@ -120,6 +156,10 @@ class ForeignAPIFile extends File { return null; } + /** + * @param $metadata array + * @return array + */ public static function parseMetadata( $metadata ) { if( !is_array( $metadata ) ) { return $metadata; @@ -131,28 +171,47 @@ class ForeignAPIFile extends File { return $ret; } + /** + * @return bool|int|null + */ public function getSize() { return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null; } + /** + * @return null|string + */ public function getUrl() { return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null; } + /** + * @param string $method + * @return int|null|string + */ public function getUser( $method='text' ) { return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null; } - public function getDescription() { + /** + * @return null|string + */ + public function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) { return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null; } + /** + * @return null|String + */ function getSha1() { return isset( $this->mInfo['sha1'] ) ? wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 ) : null; } + /** + * @return bool|Mixed|string + */ function getTimestamp() { return wfTimestamp( TS_MW, isset( $this->mInfo['timestamp'] ) @@ -161,6 +220,9 @@ class ForeignAPIFile extends File { ); } + /** + * @return string + */ function getMimeType() { if( !isset( $this->mInfo['mime'] ) ) { $magic = MimeMagic::singleton(); @@ -169,12 +231,18 @@ class ForeignAPIFile extends File { return $this->mInfo['mime']; } - /// @todo FIXME: May guess wrong on file types that can be eg audio or video + /** + * @todo FIXME: May guess wrong on file types that can be eg audio or video + * @return int|string + */ function getMediaType() { $magic = MimeMagic::singleton(); return $magic->getMediaType( null, $this->getMimeType() ); } + /** + * @return bool|string + */ function getDescriptionUrl() { return isset( $this->mInfo['descriptionurl'] ) ? $this->mInfo['descriptionurl'] @@ -183,6 +251,8 @@ class ForeignAPIFile extends File { /** * Only useful if we're locally caching thumbs anyway... + * @param $suffix string + * @return null|string */ function getThumbPath( $suffix = '' ) { if ( $this->repo->canCacheThumbs() ) { @@ -196,6 +266,9 @@ class ForeignAPIFile extends File { } } + /** + * @return array + */ function getThumbnails() { $dir = $this->getThumbPath( $this->getName() ); $iter = $this->repo->getBackend()->getFileList( array( 'dir' => $dir ) ); @@ -225,6 +298,9 @@ class ForeignAPIFile extends File { $wgMemc->delete( $key ); } + /** + * @param $options array + */ function purgeThumbnails( $options = array() ) { global $wgMemc; @@ -245,8 +321,8 @@ class ForeignAPIFile extends File { } # Delete the thumbnails - $this->repo->cleanupBatch( $purgeList, FileRepo::SKIP_LOCKING ); + $this->repo->quickPurgeBatch( $purgeList ); # Clear out the thumbnail directory if empty - $this->repo->getBackend()->clean( array( 'dir' => $dir ) ); + $this->repo->quickCleanDir( $dir ); } } diff --git a/includes/filerepo/file/ForeignDBFile.php b/includes/filerepo/file/ForeignDBFile.php index 191a712d..91f6cb62 100644 --- a/includes/filerepo/file/ForeignDBFile.php +++ b/includes/filerepo/file/ForeignDBFile.php @@ -1,6 +1,21 @@ <?php /** - * Foreign file with an accessible MediaWiki database + * Foreign file with an accessible MediaWiki database. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html * * @file * @ingroup FileAbstraction @@ -39,23 +54,52 @@ class ForeignDBFile extends LocalFile { return $file; } + /** + * @param $srcPath String + * @param $flags int + * @throws MWException + */ function publish( $srcPath, $flags = 0 ) { $this->readOnlyError(); } + /** + * @param $oldver + * @param $desc string + * @param $license string + * @param $copyStatus string + * @param $source string + * @param $watch bool + * @param $timestamp bool|string + * @throws MWException + */ function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '', $watch = false, $timestamp = false ) { $this->readOnlyError(); } + /** + * @param $versions array + * @param $unsuppress bool + * @throws MWException + */ function restore( $versions = array(), $unsuppress = false ) { $this->readOnlyError(); } + /** + * @param $reason string + * @param $suppress bool + * @throws MWException + */ function delete( $reason, $suppress = false ) { $this->readOnlyError(); } + /** + * @param $target Title + * @throws MWException + */ function move( $target ) { $this->readOnlyError(); } diff --git a/includes/filerepo/file/LocalFile.php b/includes/filerepo/file/LocalFile.php index 0f8b4754..695c4e9e 100644 --- a/includes/filerepo/file/LocalFile.php +++ b/includes/filerepo/file/LocalFile.php @@ -1,6 +1,21 @@ <?php /** - * Local file in the wiki's own database + * Local file in the wiki's own database. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html * * @file * @ingroup FileAbstraction @@ -29,6 +44,8 @@ define( 'MW_FILE_VERSION', 8 ); * @ingroup FileAbstraction */ class LocalFile extends File { + const CACHE_FIELD_MAX_LEN = 1000; + /**#@+ * @private */ @@ -58,6 +75,11 @@ class LocalFile extends File { /**#@-*/ + /** + * @var LocalRepo + */ + var $repo; + protected $repoClass = 'LocalRepo'; /** @@ -121,6 +143,7 @@ class LocalFile extends File { /** * Fields in the image table + * @return array */ static function selectFields() { return array( @@ -160,6 +183,7 @@ class LocalFile extends File { /** * Get the memcached key for the main data for this file, or false if * there is no access to the shared cache. + * @return bool */ function getCacheKey() { $hashedName = md5( $this->getName() ); @@ -169,6 +193,7 @@ class LocalFile extends File { /** * Try to load file metadata from memcached. Returns true on success. + * @return bool */ function loadFromCache() { global $wgMemc; @@ -238,6 +263,10 @@ class LocalFile extends File { $this->setProps( $props ); } + /** + * @param $prefix string + * @return array + */ function getCacheFields( $prefix = 'img_' ) { static $fields = array( 'size', 'width', 'height', 'bits', 'media_type', 'major_mime', 'minor_mime', 'metadata', 'timestamp', 'sha1', 'user', 'user_text', 'description' ); @@ -286,6 +315,10 @@ 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 + * @param $prefix string + * @throws MWException + * @return array */ function decodeRow( $row, $prefix = 'img_' ) { $array = (array)$row; @@ -407,6 +440,7 @@ 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, @@ -462,9 +496,12 @@ class LocalFile extends File { /** getPath inherited */ /** isVisible inhereted */ + /** + * @return bool + */ function isMissing() { if ( $this->missing === null ) { - list( $fileExists ) = $this->repo->fileExists( $this->getVirtualUrl(), FileRepo::FILES_ONLY ); + list( $fileExists ) = $this->repo->fileExists( $this->getVirtualUrl() ); $this->missing = !$fileExists; } return $this->missing; @@ -473,7 +510,8 @@ class LocalFile extends File { /** * Return the width of the image * - * Returns false on error + * @param $page int + * @return bool|int Returns false on error */ public function getWidth( $page = 1 ) { $this->load(); @@ -493,7 +531,8 @@ class LocalFile extends File { /** * Return the height of the image * - * Returns false on error + * @param $page int + * @return bool|int Returns false on error */ public function getHeight( $page = 1 ) { $this->load(); @@ -514,6 +553,7 @@ class LocalFile extends File { * Returns ID or name of user who uploaded the file * * @param $type string 'text' or 'id' + * @return int|string */ function getUser( $type = 'text' ) { $this->load(); @@ -527,12 +567,16 @@ class LocalFile extends File { /** * Get handler-specific metadata + * @return string */ function getMetadata() { $this->load(); return $this->metadata; } + /** + * @return int + */ function getBitDepth() { $this->load(); return $this->bits; @@ -540,6 +584,7 @@ class LocalFile extends File { /** * Return the size of the image file, in bytes + * @return int */ public function getSize() { $this->load(); @@ -548,6 +593,7 @@ class LocalFile extends File { /** * Returns the mime type of the file. + * @return string */ function getMimeType() { $this->load(); @@ -557,6 +603,7 @@ class LocalFile extends File { /** * Return the type of the media in the file. * Use the value returned by this function with the MEDIATYPE_xxx constants. + * @return string */ function getMediaType() { $this->load(); @@ -586,6 +633,9 @@ class LocalFile extends File { /** * Fix thumbnail files from 1.4 or before, with extreme prejudice + * @todo : do we still care about this? Perhaps a maintenance script + * can be made instead. Enabling this code results in a serious + * RTT regression for wikis without 404 handling. */ function migrateThumbFile( $thumbName ) { $thumbDir = $this->getThumbPath(); @@ -608,10 +658,12 @@ class LocalFile extends File { } */ - if ( $this->repo->fileExists( $thumbDir, FileRepo::FILES_ONLY ) ) { + /* + if ( $this->repo->fileExists( $thumbDir ) ) { // Delete file where directory should be $this->repo->cleanupBatch( array( $thumbDir ) ); } + */ } /** getHandler inherited */ @@ -620,12 +672,10 @@ class LocalFile extends File { /** * Get all thumbnail names previously generated for this file - * @param $archiveName string|false Name of an archive file + * @param $archiveName string|bool 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 ) { - $this->load(); - if ( $archiveName ) { $dir = $this->getArchiveThumbPath( $archiveName ); } else { @@ -690,6 +740,8 @@ class LocalFile extends File { */ function purgeOldThumbnails( $archiveName ) { global $wgUseSquid; + wfProfileIn( __METHOD__ ); + // Get a list of old thumbnails and URLs $files = $this->getThumbnails( $archiveName ); $dir = array_shift( $files ); @@ -706,6 +758,8 @@ class LocalFile extends File { } SquidUpdate::purge( $urls ); } + + wfProfileOut( __METHOD__ ); } /** @@ -713,6 +767,7 @@ class LocalFile extends File { */ function purgeThumbnails( $options = array() ) { global $wgUseSquid; + wfProfileIn( __METHOD__ ); // Delete thumbnails $files = $this->getThumbnails(); @@ -739,6 +794,8 @@ class LocalFile extends File { } SquidUpdate::purge( $urls ); } + + wfProfileOut( __METHOD__ ); } /** @@ -763,14 +820,21 @@ class LocalFile extends File { } # Delete the thumbnails - $this->repo->cleanupBatch( $purgeList, FileRepo::SKIP_LOCKING ); + $this->repo->quickPurgeBatch( $purgeList ); # Clear out the thumbnail directory if empty - $this->repo->getBackend()->clean( array( 'dir' => $dir ) ); + $this->repo->quickCleanDir( $dir ); } /** purgeDescription inherited */ /** purgeEverything inherited */ + /** + * @param $limit null + * @param $start null + * @param $end null + * @param $inc bool + * @return array + */ function getHistory( $limit = null, $start = null, $end = null, $inc = true ) { $dbr = $this->repo->getSlaveDB(); $tables = array( 'oldimage' ); @@ -824,6 +888,7 @@ class LocalFile extends File { * 0 return line for current version * 1 query for old versions, return first one * 2, ... return next old version from above query + * @return bool */ public function nextHistoryLine() { # Polymorphic function name to distinguish foreign and local fetches @@ -888,21 +953,25 @@ class LocalFile extends File { * @param $comment String: upload description * @param $pageText String: text to use for the new description page, * if a new description page is created - * @param $flags Integer: flags for publish() - * @param $props Array: File properties, if known. This can be used to reduce the + * @param $flags Integer|bool: flags for publish() + * @param $props Array|bool: 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: timestamp for img_timestamp, or false to use the current time - * @param $user Mixed: User object or null to use $wgUser + * @param $timestamp String|bool: 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 * archive name, or an empty string if it was a new file. */ function upload( $srcPath, $comment, $pageText, $flags = 0, $props = false, $timestamp = false, $user = null ) { global $wgContLang; + + if ( $this->getRepo()->getReadOnlyReason() !== false ) { + return $this->readOnlyFatalStatus(); + } + // truncate nicely or the DB will do it for us - // non-nicely (dangling multi-byte chars, non-truncated - // version in cache). + // non-nicely (dangling multi-byte chars, non-truncated version in cache). $comment = $wgContLang->truncate( $comment, 255 ); $this->lock(); // begin $status = $this->publish( $srcPath, $flags ); @@ -923,6 +992,14 @@ class LocalFile extends File { /** * Record a file upload in the upload log and the image table + * @param $oldver + * @param $desc string + * @param $license string + * @param $copyStatus string + * @param $source string + * @param $watch bool + * @param $timestamp string|bool + * @return bool */ function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '', $watch = false, $timestamp = false ) @@ -942,20 +1019,31 @@ class LocalFile extends File { /** * Record a file upload in the upload log and the image table + * @param $oldver + * @param $comment string + * @param $pageText string + * @param $props bool|array + * @param $timestamp bool|string + * @param $user null|User + * @return bool */ function recordUpload2( $oldver, $comment, $pageText, $props = false, $timestamp = false, $user = null ) { + wfProfileIn( __METHOD__ ); + if ( is_null( $user ) ) { global $wgUser; $user = $wgUser; } $dbw = $this->repo->getMasterDB(); - $dbw->begin(); + $dbw->begin( __METHOD__ ); if ( !$props ) { + wfProfileIn( __METHOD__ . '-getProps' ); $props = $this->repo->getFileProps( $this->getVirtualUrl() ); + wfProfileOut( __METHOD__ . '-getProps' ); } if ( $timestamp === false ) { @@ -968,15 +1056,10 @@ class LocalFile extends File { $props['timestamp'] = wfTimestamp( TS_MW, $timestamp ); // DB -> TS_MW $this->setProps( $props ); - # Delete thumbnails - $this->purgeThumbnails(); - - # The file is already on its final location, remove it from the squid cache - SquidUpdate::purge( array( $this->getURL() ) ); - # Fail now if the file isn't there if ( !$this->fileExists ) { wfDebug( __METHOD__ . ": File " . $this->getRel() . " went missing!\n" ); + wfProfileOut( __METHOD__ ); return false; } @@ -1005,15 +1088,12 @@ class LocalFile extends File { __METHOD__, 'IGNORE' ); - if ( $dbw->affectedRows() == 0 ) { - if ( $oldver == '' ) { // XXX - # (bug 34993) publish() can displace the current file and yet fail to save - # a new one. The next publish attempt will treat the file as a brand new file - # and pass an empty $oldver. Allow this bogus value so we can displace the - # `image` row to `oldimage`, leaving room for the new current file `image` row. - #throw new MWException( "Empty oi_archive_name. Database and storage out of sync?" ); - } + # (bug 34993) Note: $oldver can be empty here, if the previous + # version of the file was broken. Allow registration of the new + # version to continue anyway, because that's better than having + # an image that's not fixable by user operations. + $reupload = true; # Collision, this is an update of a file # Insert previous contents into oldimage @@ -1060,16 +1140,8 @@ class LocalFile extends File { __METHOD__ ); } else { - # This is a new file - # Update the image count - $dbw->begin( __METHOD__ ); - $dbw->update( - 'site_stats', - array( 'ss_images = ss_images+1' ), - '*', - __METHOD__ - ); - $dbw->commit( __METHOD__ ); + # This is a new file, so update the image count + DeferredUpdates::addUpdate( SiteStatsUpdate::factory( array( 'images' => 1 ) ) ); } $descTitle = $this->getTitle(); @@ -1079,14 +1151,17 @@ class LocalFile extends File { # Add the log entry $log = new LogPage( 'upload' ); $action = $reupload ? 'overwrite' : 'upload'; - $log->addEntry( $action, $descTitle, $comment, array(), $user ); + $logId = $log->addEntry( $action, $descTitle, $comment, array(), $user ); + + wfProfileIn( __METHOD__ . '-edit' ); + $exists = $descTitle->exists(); - if ( $descTitle->exists() ) { + if ( $exists ) { # Create a null revision $latest = $descTitle->getLatestRevID(); $nullRevision = Revision::newNullRevision( $dbw, - $descTitle->getArticleId(), + $descTitle->getArticleID(), $log->getRcComment(), false ); @@ -1096,6 +1171,15 @@ class LocalFile extends File { wfRunHooks( 'NewRevisionFromEditComplete', array( $wikiPage, $nullRevision, $latest, $user ) ); $wikiPage->updateRevisionOn( $dbw, $nullRevision ); } + } + + # Commit the transaction now, in case something goes wrong later + # The most important thing is that files don't get lost, especially archives + # NOTE: once we have support for nested transactions, the commit may be moved + # to after $wikiPage->doEdit has been called. + $dbw->commit( __METHOD__ ); + + if ( $exists ) { # Invalidate the cache for the description page $descTitle->invalidateCache(); $descTitle->purgeSquid(); @@ -1103,12 +1187,19 @@ class LocalFile extends File { # 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. - $wikiPage->doEdit( $pageText, $comment, EDIT_NEW | EDIT_SUPPRESS_RC, false, $user ); + $status = $wikiPage->doEdit( $pageText, $comment, EDIT_NEW | EDIT_SUPPRESS_RC, false, $user ); + + if ( isset( $status->value['revision'] ) ) { // XXX; doEdit() uses a transaction + $dbw->begin(); + $dbw->update( 'logging', + array( 'log_page' => $status->value['revision']->getPage() ), + array( 'log_id' => $logId ), + __METHOD__ + ); + $dbw->commit(); // commit before anything bad can happen + } } - - # Commit the transaction now, in case something goes wrong later - # The most important thing is that files don't get lost, especially archives - $dbw->commit(); + wfProfileOut( __METHOD__ . '-edit' ); # Save to cache and purge the squid # We shall not saveToCache before the commit since otherwise @@ -1116,8 +1207,20 @@ class LocalFile extends File { # which in fact doesn't really exist (bug 24978) $this->saveToCache(); + if ( $reupload ) { + # Delete old thumbnails + wfProfileIn( __METHOD__ . '-purge' ); + $this->purgeThumbnails(); + wfProfileOut( __METHOD__ . '-purge' ); + + # Remove the old file from the squid cache + SquidUpdate::purge( array( $this->getURL() ) ); + } + # Hooks, hooks, the magic of hooks... + wfProfileIn( __METHOD__ . '-hooks' ); wfRunHooks( 'FileUpload', array( $this, $reupload, $descTitle->exists() ) ); + wfProfileOut( __METHOD__ . '-hooks' ); # Invalidate cache for all pages using this file $update = new HTMLCacheUpdate( $this->getTitle(), 'imagelinks' ); @@ -1131,6 +1234,7 @@ class LocalFile extends File { $update->doUpdate(); } + wfProfileOut( __METHOD__ ); return true; } @@ -1167,6 +1271,10 @@ class LocalFile extends File { * archive name, or an empty string if it was a new file. */ function publishTo( $srcPath, $dstRel, $flags = 0 ) { + if ( $this->getRepo()->getReadOnlyReason() !== false ) { + return $this->readOnlyFatalStatus(); + } + $this->lock(); // begin $archiveName = wfTimestamp( TS_MW ) . '!'. $this->getName(); @@ -1203,20 +1311,26 @@ class LocalFile extends File { * @return FileRepoStatus object. */ function move( $target ) { - wfDebugLog( 'imagemove', "Got request to move {$this->name} to " . $target->getText() ); - $this->lock(); // begin + if ( $this->getRepo()->getReadOnlyReason() !== false ) { + return $this->readOnlyFatalStatus(); + } + wfDebugLog( 'imagemove', "Got request to move {$this->name} to " . $target->getText() ); $batch = new LocalFileMoveBatch( $this, $target ); - $batch->addCurrent(); - $batch->addOlds(); + $this->lock(); // begin + $batch->addCurrent(); + $archiveNames = $batch->addOlds(); $status = $batch->execute(); + $this->unlock(); // done + wfDebugLog( 'imagemove', "Finished moving {$this->name}" ); $this->purgeEverything(); - $this->unlock(); // done - - if ( $status->isOk() ) { + foreach ( $archiveNames as $archiveName ) { + $this->purgeOldThumbnails( $archiveName ); + } + if ( $status->isOK() ) { // Now switch the object $this->title = $target; // Force regeneration of the name and hashpath @@ -1242,30 +1356,27 @@ class LocalFile extends File { * @return FileRepoStatus object. */ function delete( $reason, $suppress = false ) { - $this->lock(); // begin + if ( $this->getRepo()->getReadOnlyReason() !== false ) { + return $this->readOnlyFatalStatus(); + } $batch = new LocalFileDeleteBatch( $this, $reason, $suppress ); - $batch->addCurrent(); + $this->lock(); // begin + $batch->addCurrent(); # Get old version relative paths - $dbw = $this->repo->getMasterDB(); - $result = $dbw->select( 'oldimage', - array( 'oi_archive_name' ), - array( 'oi_name' => $this->getName() ) ); - foreach ( $result as $row ) { - $batch->addOld( $row->oi_archive_name ); - $this->purgeOldThumbnails( $row->oi_archive_name ); - } + $archiveNames = $batch->addOlds(); $status = $batch->execute(); + $this->unlock(); // done - if ( $status->ok ) { - // Update site_stats - $site_stats = $dbw->tableName( 'site_stats' ); - $dbw->query( "UPDATE $site_stats SET ss_images=ss_images-1", __METHOD__ ); - $this->purgeEverything(); + if ( $status->isOK() ) { + DeferredUpdates::addUpdate( SiteStatsUpdate::factory( array( 'images' => -1 ) ) ); } - $this->unlock(); // done + $this->purgeEverything(); + foreach ( $archiveNames as $archiveName ) { + $this->purgeOldThumbnails( $archiveName ); + } return $status; } @@ -1285,16 +1396,19 @@ class LocalFile extends File { * @return FileRepoStatus object. */ function deleteOld( $archiveName, $reason, $suppress = false ) { - $this->lock(); // begin + if ( $this->getRepo()->getReadOnlyReason() !== false ) { + return $this->readOnlyFatalStatus(); + } $batch = new LocalFileDeleteBatch( $this, $reason, $suppress ); + + $this->lock(); // begin $batch->addOld( $archiveName ); - $this->purgeOldThumbnails( $archiveName ); $status = $batch->execute(); - $this->unlock(); // done - if ( $status->ok ) { + $this->purgeOldThumbnails( $archiveName ); + if ( $status->isOK() ) { $this->purgeDescription(); $this->purgeHistory(); } @@ -1308,30 +1422,32 @@ class LocalFile extends File { * * May throw database exceptions on error. * - * @param $versions set of record ids of deleted items to restore, + * @param $versions array set of record ids of deleted items to restore, * or empty to restore all revisions. * @param $unsuppress Boolean * @return FileRepoStatus */ function restore( $versions = array(), $unsuppress = false ) { + if ( $this->getRepo()->getReadOnlyReason() !== false ) { + return $this->readOnlyFatalStatus(); + } + $batch = new LocalFileRestoreBatch( $this, $unsuppress ); + $this->lock(); // begin if ( !$versions ) { $batch->addAll(); } else { $batch->addIds( $versions ); } - $status = $batch->execute(); - - if ( !$status->isGood() ) { - return $status; + if ( $status->isGood() ) { + $cleanupStatus = $batch->cleanup(); + $cleanupStatus->successCount = 0; + $cleanupStatus->failCount = 0; + $status->merge( $cleanupStatus ); } - - $cleanupStatus = $batch->cleanup(); - $cleanupStatus->successCount = 0; - $cleanupStatus->failCount = 0; - $status->merge( $cleanupStatus ); + $this->unlock(); // done return $status; } @@ -1343,6 +1459,7 @@ class LocalFile extends File { /** * Get the URL of the file description page. + * @return String */ function getDescriptionUrl() { return $this->title->getLocalUrl(); @@ -1352,10 +1469,11 @@ class LocalFile extends File { * 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. + * @return bool|mixed */ function getDescriptionText() { global $wgParser; - $revision = Revision::newFromTitle( $this->title ); + $revision = Revision::newFromTitle( $this->title, false, Revision::READ_NORMAL ); if ( !$revision ) return false; $text = $revision->getText(); if ( !$text ) return false; @@ -1363,16 +1481,33 @@ class LocalFile extends File { return $pout->getText(); } - function getDescription() { + /** + * @return string + */ + function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) { $this->load(); - return $this->description; + if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) { + return ''; + } elseif ( $audience == self::FOR_THIS_USER + && !$this->userCan( self::DELETED_COMMENT, $user ) ) + { + return ''; + } else { + return $this->description; + } } + /** + * @return bool|string + */ function getTimestamp() { $this->load(); return $this->timestamp; } + /** + * @return string + */ function getSha1() { $this->load(); // Initialise now if necessary @@ -1396,6 +1531,14 @@ class LocalFile extends File { } /** + * @return bool + */ + function isCacheable() { + $this->load(); + return strlen( $this->metadata ) <= self::CACHE_FIELD_MAX_LEN; // avoid OOMs + } + + /** * Start a transaction and lock the image for update * Increments a reference counter if the lock is already held * @return boolean True if the image exists, false otherwise @@ -1404,11 +1547,12 @@ class LocalFile extends File { $dbw = $this->repo->getMasterDB(); if ( !$this->locked ) { - $dbw->begin(); + $dbw->begin( __METHOD__ ); $this->locked++; } - return $dbw->selectField( 'image', '1', array( 'img_name' => $this->getName() ), __METHOD__ ); + return $dbw->selectField( 'image', '1', + array( 'img_name' => $this->getName() ), __METHOD__, array( 'FOR UPDATE' ) ); } /** @@ -1420,7 +1564,7 @@ class LocalFile extends File { --$this->locked; if ( !$this->locked ) { $dbw = $this->repo->getMasterDB(); - $dbw->commit(); + $dbw->commit( __METHOD__ ); } } } @@ -1431,7 +1575,15 @@ class LocalFile extends File { function unlockAndRollback() { $this->locked = false; $dbw = $this->repo->getMasterDB(); - $dbw->rollback(); + $dbw->rollback( __METHOD__ ); + } + + /** + * @return Status + */ + protected function readOnlyFatalStatus() { + return $this->getRepo()->newFatal( 'filereadonlyerror', $this->getName(), + $this->getRepo()->getName(), $this->getRepo()->getReadOnlyReason() ); } } // LocalFile class @@ -1451,6 +1603,11 @@ class LocalFileDeleteBatch { var $reason, $srcRels = array(), $archiveUrls = array(), $deletionBatch, $suppress; var $status; + /** + * @param $file File + * @param $reason string + * @param $suppress bool + */ function __construct( File $file, $reason = '', $suppress = false ) { $this->file = $file; $this->reason = $reason; @@ -1462,11 +1619,39 @@ class LocalFileDeleteBatch { $this->srcRels['.'] = $this->file->getRel(); } + /** + * @param $oldName string + */ function addOld( $oldName ) { $this->srcRels[$oldName] = $this->file->getArchiveRel( $oldName ); $this->archiveUrls[] = $this->file->getArchiveUrl( $oldName ); } + /** + * Add the old versions of the image to the batch + * @return Array List of archive names from old versions + */ + function addOlds() { + $archiveNames = array(); + + $dbw = $this->file->repo->getMasterDB(); + $result = $dbw->select( 'oldimage', + array( 'oi_archive_name' ), + array( 'oi_name' => $this->file->getName() ), + __METHOD__ + ); + + foreach ( $result as $row ) { + $this->addOld( $row->oi_archive_name ); + $archiveNames[] = $row->oi_archive_name; + } + + return $archiveNames; + } + + /** + * @return array + */ function getOldRels() { if ( !isset( $this->srcRels['.'] ) ) { $oldRels =& $this->srcRels; @@ -1480,6 +1665,9 @@ class LocalFileDeleteBatch { return array( $oldRels, $deleteCurrent ); } + /** + * @return array + */ protected function getHashes() { $hashes = array(); list( $oldRels, $deleteCurrent ) = $this->getOldRels(); @@ -1601,7 +1789,7 @@ class LocalFileDeleteBatch { 'fa_deleted_user' => $encUserId, 'fa_deleted_timestamp' => $encTimestamp, 'fa_deleted_reason' => $encReason, - 'fa_deleted' => $this->suppress ? $bitfield : 'oi_deleted', + 'fa_deleted' => $this->suppress ? $bitfield : 'oi_deleted', 'fa_name' => 'oi_name', 'fa_archive_name' => 'oi_archive_name', @@ -1617,7 +1805,6 @@ class LocalFileDeleteBatch { 'fa_user' => 'oi_user', 'fa_user_text' => 'oi_user_text', 'fa_timestamp' => 'oi_timestamp', - 'fa_deleted' => $bitfield ), $where, __METHOD__ ); } } @@ -1641,9 +1828,9 @@ class LocalFileDeleteBatch { /** * Run the transaction + * @return FileRepoStatus */ function execute() { - global $wgUseSquid; wfProfileIn( __METHOD__ ); $this->file->lock(); @@ -1699,7 +1886,7 @@ class LocalFileDeleteBatch { $this->status->merge( $status ); } - if ( !$this->status->ok ) { + if ( !$this->status->isOK() ) { // Critical file deletion error // Roll back inserts, release lock and abort // TODO: delete the defunct filearchive rows if we are using a non-transactional DB @@ -1708,17 +1895,6 @@ class LocalFileDeleteBatch { return $this->status; } - // Purge squid - if ( $wgUseSquid ) { - $urls = array(); - - foreach ( $this->srcRels as $srcRel ) { - $urlRel = str_replace( '%2F', '/', rawurlencode( $srcRel ) ); - $urls[] = $this->file->repo->getZoneUrl( 'public' ) . '/' . $urlRel; - } - SquidUpdate::purge( $urls ); - } - // Delete image/oldimage rows $this->doDBDeletes(); @@ -1731,6 +1907,8 @@ class LocalFileDeleteBatch { /** * Removes non-existent files from a deletion batch. + * @param $batch array + * @return array */ function removeNonexistentFiles( $batch ) { $files = $newBatch = array(); @@ -1740,7 +1918,7 @@ class LocalFileDeleteBatch { $files[$src] = $this->file->repo->getVirtualUrl( 'public' ) . '/' . rawurlencode( $src ); } - $result = $this->file->repo->fileExistsBatch( $files, FileRepo::FILES_ONLY ); + $result = $this->file->repo->fileExistsBatch( $files ); foreach ( $batch as $batchItem ) { if ( $result[$batchItem[0]] ) { @@ -1766,6 +1944,10 @@ class LocalFileRestoreBatch { var $cleanupBatch, $ids, $all, $unsuppress = false; + /** + * @param $file File + * @param $unsuppress bool + */ function __construct( File $file, $unsuppress = false ) { $this->file = $file; $this->cleanupBatch = $this->ids = array(); @@ -1800,6 +1982,7 @@ class LocalFileRestoreBatch { * rows and there's no need to keep the image row locked while it's acquiring those locks * The caller may have its own transaction open. * So we save the batch and let the caller call cleanup() + * @return FileRepoStatus */ function execute() { global $wgLang; @@ -2003,9 +2186,7 @@ class LocalFileRestoreBatch { if ( !$exists ) { wfDebug( __METHOD__ . " restored {$status->successCount} items, creating a new current\n" ); - // Update site_stats - $site_stats = $dbw->tableName( 'site_stats' ); - $dbw->query( "UPDATE $site_stats SET ss_images=ss_images+1", __METHOD__ ); + DeferredUpdates::addUpdate( SiteStatsUpdate::factory( array( 'images' => 1 ) ) ); $this->file->purgeEverything(); } else { @@ -2022,13 +2203,16 @@ class LocalFileRestoreBatch { /** * Removes non-existent files from a store batch. + * @param $triplets array + * @return array */ function removeNonexistentFiles( $triplets ) { $files = $filteredTriplets = array(); - foreach ( $triplets as $file ) + foreach ( $triplets as $file ) { $files[$file[0]] = $file[0]; + } - $result = $this->file->repo->fileExistsBatch( $files, FileRepo::FILES_ONLY ); + $result = $this->file->repo->fileExistsBatch( $files ); foreach ( $triplets as $file ) { if ( $result[$file[0]] ) { @@ -2041,6 +2225,8 @@ class LocalFileRestoreBatch { /** * Removes non-existent files from a cleanup batch. + * @param $batch array + * @return array */ function removeNonexistentFromCleanup( $batch ) { $files = $newBatch = array(); @@ -2051,7 +2237,7 @@ class LocalFileRestoreBatch { rawurlencode( $repo->getDeletedHashPath( $file ) . $file ); } - $result = $repo->fileExistsBatch( $files, FileRepo::FILES_ONLY ); + $result = $repo->fileExistsBatch( $files ); foreach ( $batch as $file ) { if ( $result[$file] ) { @@ -2065,6 +2251,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 */ function cleanup() { if ( !$this->cleanupBatch ) { @@ -2109,7 +2296,7 @@ class LocalFileRestoreBatch { class LocalFileMoveBatch { /** - * @var File + * @var LocalFile */ var $file; @@ -2118,8 +2305,17 @@ class LocalFileMoveBatch { */ var $target; - var $cur, $olds, $oldCount, $archive, $db; + var $cur, $olds, $oldCount, $archive; + /** + * @var DatabaseBase + */ + var $db; + + /** + * @param File $file + * @param Title $target + */ function __construct( File $file, Title $target ) { $this->file = $file; $this->target = $target; @@ -2129,7 +2325,7 @@ class LocalFileMoveBatch { $this->newName = $this->file->repo->getNameFromTitle( $this->target ); $this->oldRel = $this->oldHash . $this->oldName; $this->newRel = $this->newHash . $this->newName; - $this->db = $file->repo->getMasterDb(); + $this->db = $file->getRepo()->getMasterDb(); } /** @@ -2141,11 +2337,13 @@ class LocalFileMoveBatch { /** * Add the old versions of the image to the batch + * @return Array List of archive names from old versions */ function addOlds() { $archiveBase = 'archive'; $this->olds = array(); $this->oldCount = 0; + $archiveNames = array(); $result = $this->db->select( 'oldimage', array( 'oi_archive_name', 'oi_deleted' ), @@ -2154,6 +2352,7 @@ class LocalFileMoveBatch { ); foreach ( $result as $row ) { + $archiveNames[] = $row->oi_archive_name; $oldName = $row->oi_archive_name; $bits = explode( '!', $oldName, 2 ); @@ -2181,39 +2380,49 @@ class LocalFileMoveBatch { "{$archiveBase}/{$this->newHash}{$timestamp}!{$this->newName}" ); } + + return $archiveNames; } /** * Perform the move. + * @return FileRepoStatus */ function execute() { $repo = $this->file->repo; $status = $repo->newGood(); - $triplets = $this->getMoveTriplets(); + $triplets = $this->getMoveTriplets(); $triplets = $this->removeNonexistentFiles( $triplets ); - // Copy the files into their new location - $statusMove = $repo->storeBatch( $triplets ); + $this->file->lock(); // begin + // Rename the file versions metadata in the DB. + // This implicitly locks the destination file, which avoids race conditions. + // If we moved the files from A -> C before DB updates, another process could + // move files from B -> C at this point, causing storeBatch() to fail and thus + // cleanupTarget() to trigger. It would delete the C files and cause data loss. + $statusDb = $this->doDBUpdates(); + if ( !$statusDb->isGood() ) { + $this->file->unlockAndRollback(); + $statusDb->ok = false; + return $statusDb; + } + wfDebugLog( 'imagemove', "Renamed {$this->file->getName()} in database: {$statusDb->successCount} successes, {$statusDb->failCount} failures" ); + + // Copy the files into their new location. + // If a prior process fataled copying or cleaning up files we tolerate any + // of the existing files if they are identical to the ones being stored. + $statusMove = $repo->storeBatch( $triplets, FileRepo::OVERWRITE_SAME ); wfDebugLog( 'imagemove', "Moved files for {$this->file->getName()}: {$statusMove->successCount} successes, {$statusMove->failCount} failures" ); if ( !$statusMove->isGood() ) { - wfDebugLog( 'imagemove', "Error in moving files: " . $statusMove->getWikiText() ); + // Delete any files copied over (while the destination is still locked) $this->cleanupTarget( $triplets ); + $this->file->unlockAndRollback(); // unlocks the destination + wfDebugLog( 'imagemove', "Error in moving files: " . $statusMove->getWikiText() ); $statusMove->ok = false; return $statusMove; } - - $this->db->begin(); - $statusDb = $this->doDBUpdates(); - wfDebugLog( 'imagemove', "Renamed {$this->file->getName()} in database: {$statusDb->successCount} successes, {$statusDb->failCount} failures" ); - if ( !$statusDb->isGood() ) { - $this->db->rollback(); - // Something went wrong with the DB updates, so remove the target files - $this->cleanupTarget( $triplets ); - $statusDb->ok = false; - return $statusDb; - } - $this->db->commit(); + $this->file->unlock(); // done // Everything went ok, remove the source files $this->cleanupSource( $triplets ); @@ -2256,7 +2465,8 @@ class LocalFileMoveBatch { 'oldimage', array( 'oi_name' => $this->newName, - 'oi_archive_name = ' . $dbw->strreplace( 'oi_archive_name', $dbw->addQuotes( $this->oldName ), $dbw->addQuotes( $this->newName ) ), + 'oi_archive_name = ' . $dbw->strreplace( 'oi_archive_name', + $dbw->addQuotes( $this->oldName ), $dbw->addQuotes( $this->newName ) ), ), array( 'oi_name' => $this->oldName ), __METHOD__ @@ -2265,7 +2475,10 @@ class LocalFileMoveBatch { $affected = $dbw->affectedRows(); $total = $this->oldCount; $status->successCount += $affected; - $status->failCount += $total - $affected; + // Bug 34934: $total is based on files that actually exist. + // There may be more DB rows than such files, in which case $affected + // can be greater than $total. We use max() to avoid negatives here. + $status->failCount += max( 0, $total - $affected ); if ( $status->failCount ) { $status->error( 'imageinvalidfilename' ); } @@ -2275,6 +2488,7 @@ class LocalFileMoveBatch { /** * Generate triplets for FileRepo::storeBatch(). + * @return array */ function getMoveTriplets() { $moves = array_merge( array( $this->cur ), $this->olds ); @@ -2292,6 +2506,8 @@ class LocalFileMoveBatch { /** * Removes non-existent files from move batch. + * @param $triplets array + * @return array */ function removeNonexistentFiles( $triplets ) { $files = array(); @@ -2300,7 +2516,7 @@ class LocalFileMoveBatch { $files[$file[0]] = $file[0]; } - $result = $this->file->repo->fileExistsBatch( $files, FileRepo::FILES_ONLY ); + $result = $this->file->repo->fileExistsBatch( $files ); $filteredTriplets = array(); foreach ( $triplets as $file ) { @@ -2322,6 +2538,7 @@ class LocalFileMoveBatch { // Create dest pairs from the triplets $pairs = array(); foreach ( $triplets as $triplet ) { + // $triplet: (old source virtual URL, dst zone, dest rel) $pairs[] = array( $triplet[1], $triplet[2] ); } diff --git a/includes/filerepo/file/OldLocalFile.php b/includes/filerepo/file/OldLocalFile.php index ebd83c4d..40d7dca7 100644 --- a/includes/filerepo/file/OldLocalFile.php +++ b/includes/filerepo/file/OldLocalFile.php @@ -1,6 +1,21 @@ <?php /** - * Old file in the oldimage table + * Old file in the oldimage table. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html * * @file * @ingroup FileAbstraction @@ -17,6 +32,13 @@ class OldLocalFile extends LocalFile { const CACHE_VERSION = 1; const MAX_CACHE_ROWS = 20; + /** + * @param $title Title + * @param $repo FileRepo + * @param $time null + * @return OldLocalFile + * @throws MWException + */ static function newFromTitle( $title, $repo, $time = null ) { # The null default value is only here to avoid an E_STRICT if ( $time === null ) { @@ -25,10 +47,21 @@ class OldLocalFile extends LocalFile { return new self( $title, $repo, $time, null ); } + /** + * @param $title Title + * @param $repo FileRepo + * @param $archiveName + * @return OldLocalFile + */ static function newFromArchiveName( $title, $repo, $archiveName ) { return new self( $title, $repo, null, $archiveName ); } + /** + * @param $row + * @param $repo FileRepo + * @return OldLocalFile + */ static function newFromRow( $row, $repo ) { $title = Title::makeTitle( NS_FILE, $row->oi_name ); $file = new self( $title, $repo, null, $row->oi_archive_name ); @@ -61,9 +94,10 @@ class OldLocalFile extends LocalFile { return false; } } - + /** * Fields in the oldimage table + * @return array */ static function selectFields() { return array( @@ -91,6 +125,7 @@ class OldLocalFile extends LocalFile { * @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 + * @throws MWException */ function __construct( $title, $repo, $time, $archiveName ) { parent::__construct( $title, $repo ); @@ -101,10 +136,16 @@ class OldLocalFile extends LocalFile { } } + /** + * @return bool + */ function getCacheKey() { return false; } + /** + * @return String + */ function getArchiveName() { if ( !isset( $this->archive_name ) ) { $this->load(); @@ -112,10 +153,16 @@ class OldLocalFile extends LocalFile { return $this->archive_name; } + /** + * @return bool + */ function isOld() { return true; } + /** + * @return bool + */ function isVisible() { return $this->exists() && !$this->isDeleted(File::DELETED_FILE); } @@ -140,6 +187,10 @@ class OldLocalFile extends LocalFile { wfProfileOut( __METHOD__ ); } + /** + * @param $prefix string + * @return array + */ function getCacheFields( $prefix = 'img_' ) { $fields = parent::getCacheFields( $prefix ); $fields[] = $prefix . 'archive_name'; @@ -147,10 +198,16 @@ class OldLocalFile extends LocalFile { return $fields; } + /** + * @return string + */ function getRel() { return 'archive/' . $this->getHashPath() . $this->getArchiveName(); } + /** + * @return string + */ function getUrlRel() { return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() ); } @@ -172,14 +229,15 @@ class OldLocalFile extends LocalFile { wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n"); $dbw->update( 'oldimage', array( - 'oi_width' => $this->width, - 'oi_height' => $this->height, - 'oi_bits' => $this->bits, + 'oi_size' => $this->size, // sanity + 'oi_width' => $this->width, + 'oi_height' => $this->height, + 'oi_bits' => $this->bits, 'oi_media_type' => $this->media_type, 'oi_major_mime' => $major, 'oi_minor_mime' => $minor, - 'oi_metadata' => $this->metadata, - 'oi_sha1' => $this->sha1, + 'oi_metadata' => $this->metadata, + 'oi_sha1' => $this->sha1, ), array( 'oi_name' => $this->getName(), 'oi_archive_name' => $this->archive_name ), @@ -219,47 +277,52 @@ class OldLocalFile extends LocalFile { $this->load(); return Revision::userCanBitfield( $this->deleted, $field, $user ); } - + /** * 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 - * $timestamp!$filename, where $filename must match $this->getName() + * @param $archiveName string Full archive name of the file, in the form + * $timestamp!$filename, where $filename must match $this->getName() * + * @param $timestamp string + * @param $comment string + * @param $user + * @param $flags int * @return FileRepoStatus */ function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) { $this->lock(); - + $dstRel = 'archive/' . $this->getHashPath() . $archiveName; $status = $this->publishTo( $srcPath, $dstRel, $flags & File::DELETE_SOURCE ? FileRepo::DELETE_SOURCE : 0 ); - + if ( $status->isGood() ) { if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) { $status->fatal( 'filenotfound', $srcPath ); } } - + $this->unlock(); - + return $status; } - + /** * 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 $timestamp string * @param $comment string Upload comment * @param $user User User who did this upload * @return bool */ function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) { $dbw = $this->repo->getMasterDB(); - $dbw->begin(); + $dbw->begin( __METHOD__ ); $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel(); $props = $this->repo->getFileProps( $dstPath ); @@ -287,9 +350,9 @@ class OldLocalFile extends LocalFile { ), __METHOD__ ); - $dbw->commit(); + $dbw->commit( __METHOD__ ); return true; } - + } diff --git a/includes/filerepo/file/UnregisteredLocalFile.php b/includes/filerepo/file/UnregisteredLocalFile.php index cd9d3d02..8d4a3f88 100644 --- a/includes/filerepo/file/UnregisteredLocalFile.php +++ b/includes/filerepo/file/UnregisteredLocalFile.php @@ -1,6 +1,21 @@ <?php /** - * File without associated database record + * File without associated database record. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html * * @file * @ingroup FileAbstraction @@ -19,7 +34,7 @@ * @ingroup FileAbstraction */ class UnregisteredLocalFile extends File { - var $title, $path, $mime, $dims; + var $title, $path, $mime, $dims, $metadata; /** * @var MediaHandler @@ -47,12 +62,12 @@ class UnregisteredLocalFile extends File { /** * Create an UnregisteredLocalFile based on a path or a (title,repo) pair. * A FileRepo object is not required here, unlike most other File classes. - * + * * @throws MWException - * @param $title Title|false - * @param $repo FileRepo - * @param $path string - * @param $mime string + * @param $title Title|bool + * @param $repo FileRepo|bool + * @param $path string|bool + * @param $mime string|bool */ function __construct( $title = false, $repo = false, $path = false, $mime = false ) { if ( !( $title && $repo ) && !$path ) { @@ -79,6 +94,10 @@ class UnregisteredLocalFile extends File { $this->dims = array(); } + /** + * @param $page int + * @return bool + */ private function cachePageDimensions( $page = 1 ) { if ( !isset( $this->dims[$page] ) ) { if ( !$this->getHandler() ) { @@ -89,16 +108,27 @@ class UnregisteredLocalFile extends File { return $this->dims[$page]; } + /** + * @param $page int + * @return number + */ function getWidth( $page = 1 ) { $dim = $this->cachePageDimensions( $page ); return $dim['width']; } + /** + * @param $page int + * @return number + */ function getHeight( $page = 1 ) { $dim = $this->cachePageDimensions( $page ); return $dim['height']; } + /** + * @return bool|string + */ function getMimeType() { if ( !isset( $this->mime ) ) { $magic = MimeMagic::singleton(); @@ -107,6 +137,10 @@ class UnregisteredLocalFile extends File { return $this->mime; } + /** + * @param $filename String + * @return Array|bool + */ function getImageSize( $filename ) { if ( !$this->getHandler() ) { return false; @@ -114,6 +148,9 @@ class UnregisteredLocalFile extends File { return $this->handler->getImageSize( $this, $this->getLocalRefPath() ); } + /** + * @return bool + */ function getMetadata() { if ( !isset( $this->metadata ) ) { if ( !$this->getHandler() ) { @@ -125,6 +162,9 @@ class UnregisteredLocalFile extends File { return $this->metadata; } + /** + * @return bool|string + */ function getURL() { if ( $this->repo ) { return $this->repo->getZoneUrl( 'public' ) . '/' . @@ -134,6 +174,9 @@ class UnregisteredLocalFile extends File { } } + /** + * @return bool|int + */ function getSize() { $this->assertRepoDefined(); $props = $this->repo->getFileProps( $this->path ); |