(bug 18533) Add readonly reason to readonly exception
[mediawiki.git] / includes / filerepo / FileRepo.php
blob79fd87e3ab1ed7b9eb481c3e2cd55116529b71d9
1 <?php
3 /**
4 * Base class for file repositories
5 * Do not instantiate, use a derived class.
6 * @ingroup FileRepo
7 */
8 abstract class FileRepo {
9 const FILES_ONLY = 1;
10 const DELETE_SOURCE = 1;
11 const FIND_PRIVATE = 1;
12 const FIND_IGNORE_REDIRECT = 2;
13 const OVERWRITE = 2;
14 const OVERWRITE_SAME = 4;
16 var $thumbScriptUrl, $transformVia404;
17 var $descBaseUrl, $scriptDirUrl, $articleUrl, $fetchDescription, $initialCapital;
18 var $pathDisclosureProtection = 'paranoid';
19 var $descriptionCacheExpiry, $apiThumbCacheExpiry, $hashLevels;
21 /**
22 * Factory functions for creating new files
23 * Override these in the base class
25 var $fileFactory = false, $oldFileFactory = false;
26 var $fileFactoryKey = false, $oldFileFactoryKey = false;
28 function __construct( $info ) {
29 // Required settings
30 $this->name = $info['name'];
32 // Optional settings
33 $this->initialCapital = true; // by default
34 foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
35 'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection',
36 'descriptionCacheExpiry', 'apiThumbCacheExpiry', 'hashLevels' ) as $var )
38 if ( isset( $info[$var] ) ) {
39 $this->$var = $info[$var];
42 $this->transformVia404 = !empty( $info['transformVia404'] );
45 /**
46 * Determine if a string is an mwrepo:// URL
48 static function isVirtualUrl( $url ) {
49 return substr( $url, 0, 9 ) == 'mwrepo://';
52 /**
53 * Create a new File object from the local repository
54 * @param mixed $title Title object or string
55 * @param mixed $time Time at which the image was uploaded.
56 * If this is specified, the returned object will be an
57 * instance of the repository's old file class instead of
58 * a current file. Repositories not supporting version
59 * control should return false if this parameter is set.
61 function newFile( $title, $time = false ) {
62 if ( !($title instanceof Title) ) {
63 $title = Title::makeTitleSafe( NS_FILE, $title );
64 if ( !is_object( $title ) ) {
65 return null;
68 if ( $time ) {
69 if ( $this->oldFileFactory ) {
70 return call_user_func( $this->oldFileFactory, $title, $this, $time );
71 } else {
72 return false;
74 } else {
75 return call_user_func( $this->fileFactory, $title, $this );
79 /**
80 * Find an instance of the named file created at the specified time
81 * Returns false if the file does not exist. Repositories not supporting
82 * version control should return false if the time is specified.
84 * @param mixed $title Title object or string
85 * @param mixed $time 14-character timestamp, or false for the current version
87 function findFile( $title, $time = false, $flags = 0 ) {
88 if ( !($title instanceof Title) ) {
89 $title = Title::makeTitleSafe( NS_FILE, $title );
90 if ( !is_object( $title ) ) {
91 return false;
94 # First try the current version of the file to see if it precedes the timestamp
95 $img = $this->newFile( $title );
96 if ( !$img ) {
97 return false;
99 if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
100 return $img;
102 # Now try an old version of the file
103 if ( $time !== false ) {
104 $img = $this->newFile( $title, $time );
105 if ( $img && $img->exists() ) {
106 if ( !$img->isDeleted(File::DELETED_FILE) ) {
107 return $img;
108 } else if ( ($flags & FileRepo::FIND_PRIVATE) && $img->userCan(File::DELETED_FILE) ) {
109 return $img;
114 # Now try redirects
115 if ( $flags & FileRepo::FIND_IGNORE_REDIRECT ) {
116 return false;
118 $redir = $this->checkRedirect( $title );
119 if( $redir && $redir->getNamespace() == NS_FILE) {
120 $img = $this->newFile( $redir );
121 if( !$img ) {
122 return false;
124 if( $img->exists() ) {
125 $img->redirectedFrom( $title->getDBkey() );
126 return $img;
129 return false;
133 * Find many files at once.
134 * @param array $titles, an array of titles
135 * @todo Think of a good way to optionally pass timestamps to this function.
137 function findFiles( $titles ) {
138 $result = array();
139 foreach ( $titles as $index => $title ) {
140 $file = $this->findFile( $title );
141 if ( $file )
142 $result[$file->getTitle()->getDBkey()] = $file;
144 return $result;
148 * Create a new File object from the local repository
149 * @param mixed $sha1 SHA-1 key
150 * @param mixed $time Time at which the image was uploaded.
151 * If this is specified, the returned object will be an
152 * instance of the repository's old file class instead of
153 * a current file. Repositories not supporting version
154 * control should return false if this parameter is set.
156 function newFileFromKey( $sha1, $time = false ) {
157 if ( $time ) {
158 if ( $this->oldFileFactoryKey ) {
159 return call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
160 } else {
161 return false;
163 } else {
164 return call_user_func( $this->fileFactoryKey, $sha1, $this );
169 * Find an instance of the file with this key, created at the specified time
170 * Returns false if the file does not exist. Repositories not supporting
171 * version control should return false if the time is specified.
173 * @param string $sha1 string
174 * @param mixed $time 14-character timestamp, or false for the current version
176 function findFileFromKey( $sha1, $time = false, $flags = 0 ) {
177 # First try the current version of the file to see if it precedes the timestamp
178 $img = $this->newFileFromKey( $sha1 );
179 if ( !$img ) {
180 return false;
182 if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
183 return $img;
185 # Now try an old version of the file
186 if ( $time !== false ) {
187 $img = $this->newFileFromKey( $sha1, $time );
188 if ( $img->exists() ) {
189 if ( !$img->isDeleted(File::DELETED_FILE) ) {
190 return $img;
191 } else if ( ($flags & FileRepo::FIND_PRIVATE) && $img->userCan(File::DELETED_FILE) ) {
192 return $img;
196 return false;
200 * Get the URL of thumb.php
202 function getThumbScriptUrl() {
203 return $this->thumbScriptUrl;
207 * Returns true if the repository can transform files via a 404 handler
209 function canTransformVia404() {
210 return $this->transformVia404;
214 * Get the name of an image from its title object
216 function getNameFromTitle( $title ) {
217 global $wgCapitalLinks;
218 if ( $this->initialCapital != $wgCapitalLinks ) {
219 global $wgContLang;
220 $name = $title->getUserCaseDBKey();
221 if ( $this->initialCapital ) {
222 $name = $wgContLang->ucfirst( $name );
224 } else {
225 $name = $title->getDBkey();
227 return $name;
230 static function getHashPathForLevel( $name, $levels ) {
231 if ( $levels == 0 ) {
232 return '';
233 } else {
234 $hash = md5( $name );
235 $path = '';
236 for ( $i = 1; $i <= $levels; $i++ ) {
237 $path .= substr( $hash, 0, $i ) . '/';
239 return $path;
244 * Get a relative path including trailing slash, e.g. f/fa/
245 * If the repo is not hashed, returns an empty string
247 function getHashPath( $name ) {
248 return self::getHashPathForLevel( $name, $this->hashLevels );
252 * Get the name of this repository, as specified by $info['name]' to the constructor
254 function getName() {
255 return $this->name;
259 * Get the URL of an image description page. May return false if it is
260 * unknown or not applicable. In general this should only be called by the
261 * File class, since it may return invalid results for certain kinds of
262 * repositories. Use File::getDescriptionUrl() in user code.
264 * In particular, it uses the article paths as specified to the repository
265 * constructor, whereas local repositories use the local Title functions.
267 function getDescriptionUrl( $name ) {
268 $encName = wfUrlencode( $name );
269 if ( !is_null( $this->descBaseUrl ) ) {
270 # "http://example.com/wiki/Image:"
271 return $this->descBaseUrl . $encName;
273 if ( !is_null( $this->articleUrl ) ) {
274 # "http://example.com/wiki/$1"
276 # We use "Image:" as the canonical namespace for
277 # compatibility across all MediaWiki versions.
278 return str_replace( '$1',
279 "Image:$encName", $this->articleUrl );
281 if ( !is_null( $this->scriptDirUrl ) ) {
282 # "http://example.com/w"
284 # We use "Image:" as the canonical namespace for
285 # compatibility across all MediaWiki versions,
286 # and just sort of hope index.php is right. ;)
287 return $this->scriptDirUrl .
288 "/index.php?title=Image:$encName";
290 return false;
294 * Get the URL of the content-only fragment of the description page. For
295 * MediaWiki this means action=render. This should only be called by the
296 * repository's file class, since it may return invalid results. User code
297 * should use File::getDescriptionText().
298 * @param string $name Name of image to fetch
299 * @param string $lang Language to fetch it in, if any.
301 function getDescriptionRenderUrl( $name, $lang = null ) {
302 $query = 'action=render';
303 if ( !is_null( $lang ) ) {
304 $query .= '&uselang=' . $lang;
306 if ( isset( $this->scriptDirUrl ) ) {
307 return $this->scriptDirUrl . '/index.php?title=' .
308 wfUrlencode( 'Image:' . $name ) .
309 "&$query";
310 } else {
311 $descUrl = $this->getDescriptionUrl( $name );
312 if ( $descUrl ) {
313 return wfAppendQuery( $descUrl, $query );
314 } else {
315 return false;
321 * Store a file to a given destination.
323 * @param string $srcPath Source path or virtual URL
324 * @param string $dstZone Destination zone
325 * @param string $dstRel Destination relative path
326 * @param integer $flags Bitwise combination of the following flags:
327 * self::DELETE_SOURCE Delete the source file after upload
328 * self::OVERWRITE Overwrite an existing destination file instead of failing
329 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
330 * same contents as the source
331 * @return FileRepoStatus
333 function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
334 $status = $this->storeBatch( array( array( $srcPath, $dstZone, $dstRel ) ), $flags );
335 if ( $status->successCount == 0 ) {
336 $status->ok = false;
338 return $status;
342 * Store a batch of files
344 * @param array $triplets (src,zone,dest) triplets as per store()
345 * @param integer $flags Flags as per store
347 abstract function storeBatch( $triplets, $flags = 0 );
350 * Pick a random name in the temp zone and store a file to it.
351 * Returns a FileRepoStatus object with the URL in the value.
353 * @param string $originalName The base name of the file as specified
354 * by the user. The file extension will be maintained.
355 * @param string $srcPath The current location of the file.
357 abstract function storeTemp( $originalName, $srcPath );
360 * Remove a temporary file or mark it for garbage collection
361 * @param string $virtualUrl The virtual URL returned by storeTemp
362 * @return boolean True on success, false on failure
363 * STUB
365 function freeTemp( $virtualUrl ) {
366 return true;
370 * Copy or move a file either from the local filesystem or from an mwrepo://
371 * virtual URL, into this repository at the specified destination location.
373 * Returns a FileRepoStatus object. On success, the value contains "new" or
374 * "archived", to indicate whether the file was new with that name.
376 * @param string $srcPath The source path or URL
377 * @param string $dstRel The destination relative path
378 * @param string $archiveRel The relative path where the existing file is to
379 * be archived, if there is one. Relative to the public zone root.
380 * @param integer $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
381 * that the source file should be deleted if possible
383 function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 ) {
384 $status = $this->publishBatch( array( array( $srcPath, $dstRel, $archiveRel ) ), $flags );
385 if ( $status->successCount == 0 ) {
386 $status->ok = false;
388 if ( isset( $status->value[0] ) ) {
389 $status->value = $status->value[0];
390 } else {
391 $status->value = false;
393 return $status;
397 * Publish a batch of files
398 * @param array $triplets (source,dest,archive) triplets as per publish()
399 * @param integer $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
400 * that the source files should be deleted if possible
402 abstract function publishBatch( $triplets, $flags = 0 );
404 function fileExists( $file, $flags = 0 ) {
405 $result = $this->fileExistsBatch( array( $file ), $flags );
406 return $result[0];
410 * Checks existence of an array of files.
412 * @param array $files URLs (or paths) of files to check
413 * @param integer $flags Bitwise combination of the following flags:
414 * self::FILES_ONLY Mark file as existing only if it is a file (not directory)
415 * @return Either array of files and existence flags, or false
417 abstract function fileExistsBatch( $files, $flags = 0 );
420 * Move a group of files to the deletion archive.
422 * If no valid deletion archive is configured, this may either delete the
423 * file or throw an exception, depending on the preference of the repository.
425 * The overwrite policy is determined by the repository -- currently FSRepo
426 * assumes a naming scheme in the deleted zone based on content hash, as
427 * opposed to the public zone which is assumed to be unique.
429 * @param array $sourceDestPairs Array of source/destination pairs. Each element
430 * is a two-element array containing the source file path relative to the
431 * public root in the first element, and the archive file path relative
432 * to the deleted zone root in the second element.
433 * @return FileRepoStatus
435 abstract function deleteBatch( $sourceDestPairs );
438 * Move a file to the deletion archive.
439 * If no valid deletion archive exists, this may either delete the file
440 * or throw an exception, depending on the preference of the repository
441 * @param mixed $srcRel Relative path for the file to be deleted
442 * @param mixed $archiveRel Relative path for the archive location.
443 * Relative to a private archive directory.
444 * @return WikiError object (wikitext-formatted), or true for success
446 function delete( $srcRel, $archiveRel ) {
447 return $this->deleteBatch( array( array( $srcRel, $archiveRel ) ) );
451 * Get properties of a file with a given virtual URL
452 * The virtual URL must refer to this repo
453 * Properties should ultimately be obtained via File::getPropsFromPath()
455 abstract function getFileProps( $virtualUrl );
458 * Call a callback function for every file in the repository
459 * May use either the database or the filesystem
460 * STUB
462 function enumFiles( $callback ) {
463 throw new MWException( 'enumFiles is not supported by ' . get_class( $this ) );
467 * Determine if a relative path is valid, i.e. not blank or involving directory traveral
469 function validateFilename( $filename ) {
470 if ( strval( $filename ) == '' ) {
471 return false;
473 if ( wfIsWindows() ) {
474 $filename = strtr( $filename, '\\', '/' );
477 * Use the same traversal protection as Title::secureAndSplit()
479 if ( strpos( $filename, '.' ) !== false &&
480 ( $filename === '.' || $filename === '..' ||
481 strpos( $filename, './' ) === 0 ||
482 strpos( $filename, '../' ) === 0 ||
483 strpos( $filename, '/./' ) !== false ||
484 strpos( $filename, '/../' ) !== false ) )
486 return false;
487 } else {
488 return true;
492 /**#@+
493 * Path disclosure protection functions
495 function paranoidClean( $param ) { return '[hidden]'; }
496 function passThrough( $param ) { return $param; }
499 * Get a callback function to use for cleaning error message parameters
501 function getErrorCleanupFunction() {
502 switch ( $this->pathDisclosureProtection ) {
503 case 'none':
504 $callback = array( $this, 'passThrough' );
505 break;
506 default: // 'paranoid'
507 $callback = array( $this, 'paranoidClean' );
509 return $callback;
511 /**#@-*/
514 * Create a new fatal error
516 function newFatal( $message /*, parameters...*/ ) {
517 $params = func_get_args();
518 array_unshift( $params, $this );
519 return call_user_func_array( array( 'FileRepoStatus', 'newFatal' ), $params );
523 * Create a new good result
525 function newGood( $value = null ) {
526 return FileRepoStatus::newGood( $this, $value );
530 * Delete files in the deleted directory if they are not referenced in the filearchive table
531 * STUB
533 function cleanupDeletedBatch( $storageKeys ) {}
536 * Checks if there is a redirect named as $title. If there is, return the
537 * title object. If not, return false.
538 * STUB
540 * @param Title $title Title of image
542 function checkRedirect( $title ) {
543 return false;
547 * Invalidates image redirect cache related to that image
548 * Doesn't do anything for repositories that don't support image redirects.
550 * STUB
551 * @param Title $title Title of image
553 function invalidateImageRedirect( $title ) {}
556 * Get an array or iterator of file objects for files that have a given
557 * SHA-1 content hash.
559 * STUB
561 function findBySha1( $hash ) {
562 return array();
566 * Get the human-readable name of the repo.
567 * @return string
569 public function getDisplayName() {
570 // We don't name our own repo, return nothing
571 if ( $this->name == 'local' ) {
572 return null;
574 $repoName = wfMsg( 'shared-repo-name-' . $this->name );
575 if ( !wfEmptyMsg( 'shared-repo-name-' . $this->name, $repoName ) ) {
576 return $repoName;
578 return wfMsg( 'shared-repo' );
582 * Get a key on the primary cache for this repository.
583 * Returns false if the repository's cache is not accessible at this site.
584 * The parameters are the parts of the key, as for wfMemcKey().
586 * STUB
588 function getSharedCacheKey( /*...*/ ) {
589 return false;
593 * Get a key for this repo in the local cache domain. These cache keys are
594 * not shared with remote instances of the repo.
595 * The parameters are the parts of the key, as for wfMemcKey().
597 function getLocalCacheKey( /*...*/ ) {
598 $args = func_get_args();
599 array_unshift( $args, 'filerepo', $this->getName() );
600 return call_user_func_array( 'wfMemcKey', $args );