4 * Base class for file repositories
5 * Do not instantiate, use a derived class.
8 abstract class FileRepo
{
9 const DELETE_SOURCE
= 1;
10 const FIND_PRIVATE
= 1;
11 const FIND_IGNORE_REDIRECT
= 2;
13 const OVERWRITE_SAME
= 4;
15 var $thumbScriptUrl, $transformVia404;
16 var $descBaseUrl, $scriptDirUrl, $articleUrl, $fetchDescription, $initialCapital;
17 var $pathDisclosureProtection = 'paranoid';
18 var $descriptionCacheExpiry, $apiThumbCacheExpiry, $hashLevels;
21 * Factory functions for creating new files
22 * Override these in the base class
24 var $fileFactory = false, $oldFileFactory = false;
25 var $fileFactoryKey = false, $oldFileFactoryKey = false;
27 function __construct( $info ) {
29 $this->name
= $info['name'];
32 $this->initialCapital
= true; // by default
33 foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
34 'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection',
35 'descriptionCacheExpiry', 'apiThumbCacheExpiry', 'hashLevels' ) as $var )
37 if ( isset( $info[$var] ) ) {
38 $this->$var = $info[$var];
41 $this->transformVia404
= !empty( $info['transformVia404'] );
45 * Determine if a string is an mwrepo:// URL
47 static function isVirtualUrl( $url ) {
48 return substr( $url, 0, 9 ) == 'mwrepo://';
52 * Create a new File object from the local repository
53 * @param mixed $title Title object or string
54 * @param mixed $time Time at which the image was uploaded.
55 * If this is specified, the returned object will be an
56 * instance of the repository's old file class instead of
57 * a current file. Repositories not supporting version
58 * control should return false if this parameter is set.
60 function newFile( $title, $time = false ) {
61 if ( !($title instanceof Title
) ) {
62 $title = Title
::makeTitleSafe( NS_FILE
, $title );
63 if ( !is_object( $title ) ) {
68 if ( $this->oldFileFactory
) {
69 return call_user_func( $this->oldFileFactory
, $title, $this, $time );
74 return call_user_func( $this->fileFactory
, $title, $this );
79 * Find an instance of the named file created at the specified time
80 * Returns false if the file does not exist. Repositories not supporting
81 * version control should return false if the time is specified.
83 * @param mixed $title Title object or string
84 * @param mixed $time 14-character timestamp, or false for the current version
86 function findFile( $title, $time = false, $flags = 0 ) {
87 if ( !($title instanceof Title
) ) {
88 $title = Title
::makeTitleSafe( NS_FILE
, $title );
89 if ( !is_object( $title ) ) {
93 # First try the current version of the file to see if it precedes the timestamp
94 $img = $this->newFile( $title );
98 if ( $img->exists() && ( !$time ||
$img->getTimestamp() == $time ) ) {
101 # Now try an old version of the file
102 if ( $time !== false ) {
103 $img = $this->newFile( $title, $time );
104 if ( $img && $img->exists() ) {
105 if ( !$img->isDeleted(File
::DELETED_FILE
) ) {
107 } else if ( ($flags & FileRepo
::FIND_PRIVATE
) && $img->userCan(File
::DELETED_FILE
) ) {
114 if ( $flags & FileRepo
::FIND_IGNORE_REDIRECT
) {
117 $redir = $this->checkRedirect( $title );
118 if( $redir && $redir->getNamespace() == NS_FILE
) {
119 $img = $this->newFile( $redir );
123 if( $img->exists() ) {
124 $img->redirectedFrom( $title->getDBkey() );
132 * Find many files at once.
133 * @param array $titles, an array of titles
136 function findFiles( $titles, $flags ) {
138 foreach ( $titles as $index => $title ) {
139 $file = $this->findFile( $title, $flags );
141 $result[$file->getTitle()->getDBkey()] = $file;
147 * Create a new File object from the local repository
148 * @param mixed $sha1 SHA-1 key
149 * @param mixed $time Time at which the image was uploaded.
150 * If this is specified, the returned object will be an
151 * instance of the repository's old file class instead of
152 * a current file. Repositories not supporting version
153 * control should return false if this parameter is set.
155 function newFileFromKey( $sha1, $time = false ) {
157 if ( $this->oldFileFactoryKey
) {
158 return call_user_func( $this->oldFileFactoryKey
, $sha1, $this, $time );
163 return call_user_func( $this->fileFactoryKey
, $sha1, $this );
168 * Find an instance of the file with this key, created at the specified time
169 * Returns false if the file does not exist. Repositories not supporting
170 * version control should return false if the time is specified.
172 * @param string $sha1 string
173 * @param mixed $time 14-character timestamp, or false for the current version
175 function findFileFromKey( $sha1, $time = false, $flags = 0 ) {
176 # First try the current version of the file to see if it precedes the timestamp
177 $img = $this->newFileFromKey( $sha1 );
181 if ( $img->exists() && ( !$time ||
$img->getTimestamp() == $time ) ) {
184 # Now try an old version of the file
185 if ( $time !== false ) {
186 $img = $this->newFileFromKey( $sha1, $time );
187 if ( $img->exists() ) {
188 if ( !$img->isDeleted(File
::DELETED_FILE
) ) {
190 } else if ( ($flags & FileRepo
::FIND_PRIVATE
) && $img->userCan(File
::DELETED_FILE
) ) {
199 * Get the URL of thumb.php
201 function getThumbScriptUrl() {
202 return $this->thumbScriptUrl
;
206 * Returns true if the repository can transform files via a 404 handler
208 function canTransformVia404() {
209 return $this->transformVia404
;
213 * Get the name of an image from its title object
215 function getNameFromTitle( $title ) {
216 global $wgCapitalLinks;
217 if ( $this->initialCapital
!= $wgCapitalLinks ) {
219 $name = $title->getUserCaseDBKey();
220 if ( $this->initialCapital
) {
221 $name = $wgContLang->ucfirst( $name );
224 $name = $title->getDBkey();
229 static function getHashPathForLevel( $name, $levels ) {
230 if ( $levels == 0 ) {
233 $hash = md5( $name );
235 for ( $i = 1; $i <= $levels; $i++
) {
236 $path .= substr( $hash, 0, $i ) . '/';
243 * Get a relative path including trailing slash, e.g. f/fa/
244 * If the repo is not hashed, returns an empty string
246 function getHashPath( $name ) {
247 return self
::getHashPathForLevel( $name, $this->hashLevels
);
251 * Get the name of this repository, as specified by $info['name]' to the constructor
258 * Get the file description page base URL, or false if there isn't one.
261 function getDescBaseUrl() {
262 if ( is_null( $this->descBaseUrl
) ) {
263 if ( !is_null( $this->articleUrl
) ) {
264 $this->descBaseUrl
= str_replace( '$1',
265 wfUrlencode( 'Image' ) . ':', $this->articleUrl
);
266 } elseif ( !is_null( $this->scriptDirUrl
) ) {
267 $this->descBaseUrl
= $this->scriptDirUrl
. '/index.php?title=' .
268 wfUrlencode( 'Image' ) . ':';
270 $this->descBaseUrl
= false;
273 return $this->descBaseUrl
;
277 * Get the URL of an image description page. May return false if it is
278 * unknown or not applicable. In general this should only be called by the
279 * File class, since it may return invalid results for certain kinds of
280 * repositories. Use File::getDescriptionUrl() in user code.
282 * In particular, it uses the article paths as specified to the repository
283 * constructor, whereas local repositories use the local Title functions.
285 function getDescriptionUrl( $name ) {
286 $base = $this->getDescBaseUrl();
288 return $base . wfUrlencode( $name );
295 * Get the URL of the content-only fragment of the description page. For
296 * MediaWiki this means action=render. This should only be called by the
297 * repository's file class, since it may return invalid results. User code
298 * should use File::getDescriptionText().
300 function getDescriptionRenderUrl( $name ) {
301 if ( isset( $this->scriptDirUrl
) ) {
302 return $this->scriptDirUrl
. '/index.php?title=' .
303 wfUrlencode( 'Image:' . $name ) .
306 $descBase = $this->getDescBaseUrl();
308 return wfAppendQuery( $descBase . wfUrlencode( $name ), 'action=render' );
316 * Store a file to a given destination.
318 * @param string $srcPath Source path or virtual URL
319 * @param string $dstZone Destination zone
320 * @param string $dstRel Destination relative path
321 * @param integer $flags Bitwise combination of the following flags:
322 * self::DELETE_SOURCE Delete the source file after upload
323 * self::OVERWRITE Overwrite an existing destination file instead of failing
324 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
325 * same contents as the source
326 * @return FileRepoStatus
328 function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
329 $status = $this->storeBatch( array( array( $srcPath, $dstZone, $dstRel ) ), $flags );
330 if ( $status->successCount
== 0 ) {
337 * Store a batch of files
339 * @param array $triplets (src,zone,dest) triplets as per store()
340 * @param integer $flags Flags as per store
342 abstract function storeBatch( $triplets, $flags = 0 );
345 * Pick a random name in the temp zone and store a file to it.
346 * Returns a FileRepoStatus object with the URL in the value.
348 * @param string $originalName The base name of the file as specified
349 * by the user. The file extension will be maintained.
350 * @param string $srcPath The current location of the file.
352 abstract function storeTemp( $originalName, $srcPath );
355 * Remove a temporary file or mark it for garbage collection
356 * @param string $virtualUrl The virtual URL returned by storeTemp
357 * @return boolean True on success, false on failure
360 function freeTemp( $virtualUrl ) {
365 * Copy or move a file either from the local filesystem or from an mwrepo://
366 * virtual URL, into this repository at the specified destination location.
368 * Returns a FileRepoStatus object. On success, the value contains "new" or
369 * "archived", to indicate whether the file was new with that name.
371 * @param string $srcPath The source path or URL
372 * @param string $dstRel The destination relative path
373 * @param string $archiveRel The relative path where the existing file is to
374 * be archived, if there is one. Relative to the public zone root.
375 * @param integer $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
376 * that the source file should be deleted if possible
378 function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 ) {
379 $status = $this->publishBatch( array( array( $srcPath, $dstRel, $archiveRel ) ), $flags );
380 if ( $status->successCount
== 0 ) {
383 if ( isset( $status->value
[0] ) ) {
384 $status->value
= $status->value
[0];
386 $status->value
= false;
392 * Publish a batch of files
393 * @param array $triplets (source,dest,archive) triplets as per publish()
394 * @param integer $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
395 * that the source files should be deleted if possible
397 abstract function publishBatch( $triplets, $flags = 0 );
400 * Move a group of files to the deletion archive.
402 * If no valid deletion archive is configured, this may either delete the
403 * file or throw an exception, depending on the preference of the repository.
405 * The overwrite policy is determined by the repository -- currently FSRepo
406 * assumes a naming scheme in the deleted zone based on content hash, as
407 * opposed to the public zone which is assumed to be unique.
409 * @param array $sourceDestPairs Array of source/destination pairs. Each element
410 * is a two-element array containing the source file path relative to the
411 * public root in the first element, and the archive file path relative
412 * to the deleted zone root in the second element.
413 * @return FileRepoStatus
415 abstract function deleteBatch( $sourceDestPairs );
418 * Move a file to the deletion archive.
419 * If no valid deletion archive exists, this may either delete the file
420 * or throw an exception, depending on the preference of the repository
421 * @param mixed $srcRel Relative path for the file to be deleted
422 * @param mixed $archiveRel Relative path for the archive location.
423 * Relative to a private archive directory.
424 * @return WikiError object (wikitext-formatted), or true for success
426 function delete( $srcRel, $archiveRel ) {
427 return $this->deleteBatch( array( array( $srcRel, $archiveRel ) ) );
431 * Get properties of a file with a given virtual URL
432 * The virtual URL must refer to this repo
433 * Properties should ultimately be obtained via File::getPropsFromPath()
435 abstract function getFileProps( $virtualUrl );
438 * Call a callback function for every file in the repository
439 * May use either the database or the filesystem
442 function enumFiles( $callback ) {
443 throw new MWException( 'enumFiles is not supported by ' . get_class( $this ) );
447 * Determine if a relative path is valid, i.e. not blank or involving directory traveral
449 function validateFilename( $filename ) {
450 if ( strval( $filename ) == '' ) {
453 if ( wfIsWindows() ) {
454 $filename = strtr( $filename, '\\', '/' );
457 * Use the same traversal protection as Title::secureAndSplit()
459 if ( strpos( $filename, '.' ) !== false &&
460 ( $filename === '.' ||
$filename === '..' ||
461 strpos( $filename, './' ) === 0 ||
462 strpos( $filename, '../' ) === 0 ||
463 strpos( $filename, '/./' ) !== false ||
464 strpos( $filename, '/../' ) !== false ) )
473 * Path disclosure protection functions
475 function paranoidClean( $param ) { return '[hidden]'; }
476 function passThrough( $param ) { return $param; }
479 * Get a callback function to use for cleaning error message parameters
481 function getErrorCleanupFunction() {
482 switch ( $this->pathDisclosureProtection
) {
484 $callback = array( $this, 'passThrough' );
486 default: // 'paranoid'
487 $callback = array( $this, 'paranoidClean' );
494 * Create a new fatal error
496 function newFatal( $message /*, parameters...*/ ) {
497 $params = func_get_args();
498 array_unshift( $params, $this );
499 return call_user_func_array( array( 'FileRepoStatus', 'newFatal' ), $params );
503 * Create a new good result
505 function newGood( $value = null ) {
506 return FileRepoStatus
::newGood( $this, $value );
510 * Delete files in the deleted directory if they are not referenced in the filearchive table
513 function cleanupDeletedBatch( $storageKeys ) {}
516 * Checks if there is a redirect named as $title
519 * @param Title $title Title of image
521 function checkRedirect( $title ) {
526 * Invalidates image redirect cache related to that image
529 * @param Title $title Title of image
531 function invalidateImageRedirect( $title ) {
534 function findBySha1( $hash ) {