dbda0d1bfc690422b44dbdda3d0b5db4f7d50fe7
3 * Base code for file repositories.
10 * Base class for file repositories.
11 * Do not instantiate, use a derived class.
15 abstract class FileRepo
{
17 const DELETE_SOURCE
= 1;
19 const OVERWRITE_SAME
= 4;
20 const SKIP_VALIDATION
= 8;
22 var $thumbScriptUrl, $transformVia404;
23 var $descBaseUrl, $scriptDirUrl, $scriptExtension, $articleUrl;
24 var $fetchDescription, $initialCapital;
25 var $pathDisclosureProtection = 'paranoid';
26 var $descriptionCacheExpiry, $hashLevels, $url, $thumbUrl;
29 * Factory functions for creating new files
30 * Override these in the base class
32 var $fileFactory = false, $oldFileFactory = false;
33 var $fileFactoryKey = false, $oldFileFactoryKey = false;
35 function __construct( $info ) {
37 $this->name
= $info['name'];
40 $this->initialCapital
= MWNamespace
::isCapitalized( NS_FILE
);
41 foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
42 'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection',
43 'descriptionCacheExpiry', 'hashLevels', 'url', 'thumbUrl', 'scriptExtension' )
46 if ( isset( $info[$var] ) ) {
47 $this->$var = $info[$var];
50 $this->transformVia404
= !empty( $info['transformVia404'] );
54 * Determine if a string is an mwrepo:// URL
60 static function isVirtualUrl( $url ) {
61 return substr( $url, 0, 9 ) == 'mwrepo://';
65 * Create a new File object from the local repository
67 * @param $title Mixed: Title object or string
68 * @param $time Mixed: Time at which the image was uploaded.
69 * If this is specified, the returned object will be an
70 * instance of the repository's old file class instead of a
71 * current file. Repositories not supporting version control
72 * should return false if this parameter is set.
74 * @return File|null A File, or null if passed an invalid Title
76 function newFile( $title, $time = false ) {
77 $title = File
::normalizeTitle( $title );
82 if ( $this->oldFileFactory
) {
83 return call_user_func( $this->oldFileFactory
, $title, $this, $time );
88 return call_user_func( $this->fileFactory
, $title, $this );
93 * Find an instance of the named file created at the specified time
94 * Returns false if the file does not exist. Repositories not supporting
95 * version control should return false if the time is specified.
97 * @param $title Mixed: Title object or string
98 * @param $options array Associative array of options:
99 * time: requested time for an archived image, or false for the
100 * current version. An image object will be returned which was
101 * created at the specified time.
103 * ignoreRedirect: If true, do not follow file redirects
105 * private: If true, return restricted (deleted) files if the current
106 * user is allowed to view them. Otherwise, such files will not
111 function findFile( $title, $options = array() ) {
112 $title = File
::normalizeTitle( $title );
116 $time = isset( $options['time'] ) ?
$options['time'] : false;
117 # First try the current version of the file to see if it precedes the timestamp
118 $img = $this->newFile( $title );
122 if ( $img->exists() && ( !$time ||
$img->getTimestamp() == $time ) ) {
125 # Now try an old version of the file
126 if ( $time !== false ) {
127 $img = $this->newFile( $title, $time );
128 if ( $img && $img->exists() ) {
129 if ( !$img->isDeleted( File
::DELETED_FILE
) ) {
130 return $img; // always OK
131 } elseif ( !empty( $options['private'] ) && $img->userCan( File
::DELETED_FILE
) ) {
138 if ( !empty( $options['ignoreRedirect'] ) ) {
141 $redir = $this->checkRedirect( $title );
142 if( $redir && $title->getNamespace() == NS_FILE
) {
143 $img = $this->newFile( $redir );
147 if( $img->exists() ) {
148 $img->redirectedFrom( $title->getDBkey() );
156 * Find many files at once.
157 * @param $items An array of titles, or an array of findFile() options with
158 * the "title" option giving the title. Example:
160 * $findItem = array( 'title' => $title, 'private' => true );
161 * $findBatch = array( $findItem );
162 * $repo->findFiles( $findBatch );
166 function findFiles( $items ) {
168 foreach ( $items as $item ) {
169 if ( is_array( $item ) ) {
170 $title = $item['title'];
172 unset( $options['title'] );
177 $file = $this->findFile( $title, $options );
179 $result[$file->getTitle()->getDBkey()] = $file;
186 * Find an instance of the file with this key, created at the specified time
187 * Returns false if the file does not exist. Repositories not supporting
188 * version control should return false if the time is specified.
190 * @param $sha1 String base 36 SHA-1 hash
191 * @param $options Option array, same as findFile().
193 function findFileFromKey( $sha1, $options = array() ) {
194 $time = isset( $options['time'] ) ?
$options['time'] : false;
196 # First try to find a matching current version of a file...
197 if ( $this->fileFactoryKey
) {
198 $img = call_user_func( $this->fileFactoryKey
, $sha1, $this, $time );
200 return false; // find-by-sha1 not supported
202 if ( $img && $img->exists() ) {
205 # Now try to find a matching old version of a file...
206 if ( $time !== false && $this->oldFileFactoryKey
) { // find-by-sha1 supported?
207 $img = call_user_func( $this->oldFileFactoryKey
, $sha1, $this, $time );
208 if ( $img && $img->exists() ) {
209 if ( !$img->isDeleted( File
::DELETED_FILE
) ) {
210 return $img; // always OK
211 } elseif ( !empty( $options['private'] ) && $img->userCan( File
::DELETED_FILE
) ) {
220 * Get the URL of thumb.php
222 function getThumbScriptUrl() {
223 return $this->thumbScriptUrl
;
227 * Get the URL corresponding to one of the four basic zones
228 * @param $zone String: one of: public, deleted, temp, thumb
229 * @return String or false
231 function getZoneUrl( $zone ) {
236 * Returns true if the repository can transform files via a 404 handler
240 function canTransformVia404() {
241 return $this->transformVia404
;
245 * Get the name of an image from its title object
246 * @param $title Title
248 function getNameFromTitle( Title
$title ) {
249 if ( $this->initialCapital
!= MWNamespace
::isCapitalized( NS_FILE
) ) {
251 $name = $title->getUserCaseDBKey();
252 if ( $this->initialCapital
) {
253 $name = $wgContLang->ucfirst( $name );
256 $name = $title->getDBkey();
266 static function getHashPathForLevel( $name, $levels ) {
267 if ( $levels == 0 ) {
270 $hash = md5( $name );
272 for ( $i = 1; $i <= $levels; $i++
) {
273 $path .= substr( $hash, 0, $i ) . '/';
280 * Get a relative path including trailing slash, e.g. f/fa/
281 * If the repo is not hashed, returns an empty string
283 * @param $name string
287 function getHashPath( $name ) {
288 return self
::getHashPathForLevel( $name, $this->hashLevels
);
292 * Get the name of this repository, as specified by $info['name]' to the constructor
299 * Make an url to this repo
301 * @param $query mixed Query string to append
302 * @param $entry string Entry point; defaults to index
305 function makeUrl( $query = '', $entry = 'index' ) {
306 $ext = isset( $this->scriptExtension
) ?
$this->scriptExtension
: '.php';
307 return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}{$ext}", $query );
311 * Get the URL of an image description page. May return false if it is
312 * unknown or not applicable. In general this should only be called by the
313 * File class, since it may return invalid results for certain kinds of
314 * repositories. Use File::getDescriptionUrl() in user code.
316 * In particular, it uses the article paths as specified to the repository
317 * constructor, whereas local repositories use the local Title functions.
319 function getDescriptionUrl( $name ) {
320 $encName = wfUrlencode( $name );
321 if ( !is_null( $this->descBaseUrl
) ) {
322 # "http://example.com/wiki/Image:"
323 return $this->descBaseUrl
. $encName;
325 if ( !is_null( $this->articleUrl
) ) {
326 # "http://example.com/wiki/$1"
328 # We use "Image:" as the canonical namespace for
329 # compatibility across all MediaWiki versions.
330 return str_replace( '$1',
331 "Image:$encName", $this->articleUrl
);
333 if ( !is_null( $this->scriptDirUrl
) ) {
334 # "http://example.com/w"
336 # We use "Image:" as the canonical namespace for
337 # compatibility across all MediaWiki versions,
338 # and just sort of hope index.php is right. ;)
339 return $this->makeUrl( "title=Image:$encName" );
345 * Get the URL of the content-only fragment of the description page. For
346 * MediaWiki this means action=render. This should only be called by the
347 * repository's file class, since it may return invalid results. User code
348 * should use File::getDescriptionText().
349 * @param $name String: name of image to fetch
350 * @param $lang String: language to fetch it in, if any.
352 function getDescriptionRenderUrl( $name, $lang = null ) {
353 $query = 'action=render';
354 if ( !is_null( $lang ) ) {
355 $query .= '&uselang=' . $lang;
357 if ( isset( $this->scriptDirUrl
) ) {
358 return $this->makeUrl(
360 wfUrlencode( 'Image:' . $name ) .
363 $descUrl = $this->getDescriptionUrl( $name );
365 return wfAppendQuery( $descUrl, $query );
373 * Get the URL of the stylesheet to apply to description pages
376 function getDescriptionStylesheetUrl() {
377 if ( $this->scriptDirUrl
) {
378 return $this->makeUrl( 'title=MediaWiki:Filepage.css&' .
379 wfArrayToCGI( Skin
::getDynamicStylesheetQuery() ) );
384 * Store a file to a given destination.
386 * @param $srcPath String: source path or virtual URL
387 * @param $dstZone String: destination zone
388 * @param $dstRel String: destination relative path
389 * @param $flags Integer: bitwise combination of the following flags:
390 * self::DELETE_SOURCE Delete the source file after upload
391 * self::OVERWRITE Overwrite an existing destination file instead of failing
392 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
393 * same contents as the source
394 * @return FileRepoStatus
396 function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
397 $status = $this->storeBatch( array( array( $srcPath, $dstZone, $dstRel ) ), $flags );
398 if ( $status->successCount
== 0 ) {
405 * Store a batch of files
407 * @param $triplets Array: (src,zone,dest) triplets as per store()
408 * @param $flags Integer: flags as per store
410 abstract function storeBatch( $triplets, $flags = 0 );
413 * Pick a random name in the temp zone and store a file to it.
414 * Returns a FileRepoStatus object with the URL in the value.
416 * @param $originalName String: the base name of the file as specified
417 * by the user. The file extension will be maintained.
418 * @param $srcPath String: the current location of the file.
420 abstract function storeTemp( $originalName, $srcPath );
424 * Concatenate and array of file sources.
425 * @param $fileList Array of file sources
426 * @param $targetPath String target destination for file.
427 * @throws MWException
429 abstract function concatenate( $fileList, $targetPath, $flags = 0 );
432 * Append the contents of the source path to the given file, OR queue
433 * the appending operation in anticipation of a later appendFinish() call.
434 * @param $srcPath String: location of the source file
435 * @param $toAppendPath String: path to append to.
436 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
437 * that the source file should be deleted if possible
438 * @return mixed Status or false
440 abstract function append( $srcPath, $toAppendPath, $flags = 0 );
443 * Finish the append operation.
444 * @param $toAppendPath String: path to append to.
445 * @return mixed Status or false
447 abstract function appendFinish( $toAppendPath );
450 * Remove a temporary file or mark it for garbage collection
451 * @param $virtualUrl String: the virtual URL returned by storeTemp
452 * @return Boolean: true on success, false on failure
455 function freeTemp( $virtualUrl ) {
460 * Copy or move a file either from the local filesystem or from an mwrepo://
461 * virtual URL, into this repository at the specified destination location.
463 * Returns a FileRepoStatus object. On success, the value contains "new" or
464 * "archived", to indicate whether the file was new with that name.
466 * @param $srcPath String: the source path or URL
467 * @param $dstRel String: the destination relative path
468 * @param $archiveRel String: the relative path where the existing file is to
469 * be archived, if there is one. Relative to the public zone root.
470 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
471 * that the source file should be deleted if possible
473 function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 ) {
474 $status = $this->publishBatch( array( array( $srcPath, $dstRel, $archiveRel ) ), $flags );
475 if ( $status->successCount
== 0 ) {
478 if ( isset( $status->value
[0] ) ) {
479 $status->value
= $status->value
[0];
481 $status->value
= false;
487 * Publish a batch of files
488 * @param $triplets Array: (source,dest,archive) triplets as per publish()
489 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
490 * that the source files should be deleted if possible
492 abstract function publishBatch( $triplets, $flags = 0 );
499 function fileExists( $file, $flags = 0 ) {
500 $result = $this->fileExistsBatch( array( $file ), $flags );
505 * Checks existence of an array of files.
507 * @param $files Array: URLs (or paths) of files to check
508 * @param $flags Integer: bitwise combination of the following flags:
509 * self::FILES_ONLY Mark file as existing only if it is a file (not directory)
510 * @return Either array of files and existence flags, or false
512 abstract function fileExistsBatch( $files, $flags = 0 );
515 * Move a group of files to the deletion archive.
517 * If no valid deletion archive is configured, this may either delete the
518 * file or throw an exception, depending on the preference of the repository.
520 * The overwrite policy is determined by the repository -- currently FSRepo
521 * assumes a naming scheme in the deleted zone based on content hash, as
522 * opposed to the public zone which is assumed to be unique.
524 * @param $sourceDestPairs Array of source/destination pairs. Each element
525 * is a two-element array containing the source file path relative to the
526 * public root in the first element, and the archive file path relative
527 * to the deleted zone root in the second element.
528 * @return FileRepoStatus
530 abstract function deleteBatch( $sourceDestPairs );
533 * Move a file to the deletion archive.
534 * If no valid deletion archive exists, this may either delete the file
535 * or throw an exception, depending on the preference of the repository
536 * @param $srcRel Mixed: relative path for the file to be deleted
537 * @param $archiveRel Mixed: relative path for the archive location.
538 * Relative to a private archive directory.
539 * @return FileRepoStatus object
541 function delete( $srcRel, $archiveRel ) {
542 return $this->deleteBatch( array( array( $srcRel, $archiveRel ) ) );
546 * Get properties of a file with a given virtual URL
547 * The virtual URL must refer to this repo
548 * Properties should ultimately be obtained via File::getPropsFromPath()
550 * @param $virtualUrl string
552 abstract function getFileProps( $virtualUrl );
555 * Call a callback function for every file in the repository
556 * May use either the database or the filesystem
559 function enumFiles( $callback ) {
560 throw new MWException( 'enumFiles is not supported by ' . get_class( $this ) );
564 * Determine if a relative path is valid, i.e. not blank or involving directory traveral
566 * @param $filename string
570 function validateFilename( $filename ) {
571 if ( strval( $filename ) == '' ) {
574 if ( wfIsWindows() ) {
575 $filename = strtr( $filename, '\\', '/' );
578 * Use the same traversal protection as Title::secureAndSplit()
580 if ( strpos( $filename, '.' ) !== false &&
581 ( $filename === '.' ||
$filename === '..' ||
582 strpos( $filename, './' ) === 0 ||
583 strpos( $filename, '../' ) === 0 ||
584 strpos( $filename, '/./' ) !== false ||
585 strpos( $filename, '/../' ) !== false ) )
594 * Path disclosure protection functions
596 function paranoidClean( $param ) { return '[hidden]'; }
602 function passThrough( $param ) { return $param; }
605 * Get a callback function to use for cleaning error message parameters
607 function getErrorCleanupFunction() {
608 switch ( $this->pathDisclosureProtection
) {
610 $callback = array( $this, 'passThrough' );
612 default: // 'paranoid'
613 $callback = array( $this, 'paranoidClean' );
620 * Create a new fatal error
622 function newFatal( $message /*, parameters...*/ ) {
623 $params = func_get_args();
624 array_unshift( $params, $this );
625 return MWInit
::callStaticMethod( 'FileRepoStatus', 'newFatal', $params );
629 * Create a new good result
631 * @return FileRepoStatus
633 function newGood( $value = null ) {
634 return FileRepoStatus
::newGood( $this, $value );
638 * Delete files in the deleted directory if they are not referenced in the filearchive table
641 function cleanupDeletedBatch( $storageKeys ) {}
644 * Checks if there is a redirect named as $title. If there is, return the
645 * title object. If not, return false.
648 * @param $title Title of image
651 function checkRedirect( Title
$title ) {
656 * Invalidates image redirect cache related to that image
657 * Doesn't do anything for repositories that don't support image redirects.
660 * @param $title Title of image
662 function invalidateImageRedirect( Title
$title ) {}
665 * Get an array or iterator of file objects for files that have a given
666 * SHA-1 content hash.
670 function findBySha1( $hash ) {
675 * Get the human-readable name of the repo.
678 public function getDisplayName() {
679 // We don't name our own repo, return nothing
680 if ( $this->isLocal() ) {
683 // 'shared-repo-name-wikimediacommons' is used when $wgUseInstantCommons = true
684 return wfMessageFallback( 'shared-repo-name-' . $this->name
, 'shared-repo' )->text();
688 * Returns true if this the local file repository.
693 return $this->getName() == 'local';
697 * Get a key on the primary cache for this repository.
698 * Returns false if the repository's cache is not accessible at this site.
699 * The parameters are the parts of the key, as for wfMemcKey().
703 function getSharedCacheKey( /*...*/ ) {
708 * Get a key for this repo in the local cache domain. These cache keys are
709 * not shared with remote instances of the repo.
710 * The parameters are the parts of the key, as for wfMemcKey().
712 function getLocalCacheKey( /*...*/ ) {
713 $args = func_get_args();
714 array_unshift( $args, 'filerepo', $this->getName() );
715 return call_user_func_array( 'wfMemcKey', $args );
719 * Get an UploadStash associated with this repo.
721 * @return UploadStash
723 function getUploadStash() {
724 return new UploadStash( $this );