4 * Base class for file repositories
5 * Do not instantiate, use a derived class.
7 abstract class FileRepo
{
8 const DELETE_SOURCE
= 1;
10 const OVERWRITE_SAME
= 4;
12 var $thumbScriptUrl, $transformVia404;
13 var $descBaseUrl, $scriptDirUrl, $articleUrl, $fetchDescription, $initialCapital;
14 var $pathDisclosureProtection = 'paranoid';
17 * Factory functions for creating new files
18 * Override these in the base class
20 var $fileFactory = false, $oldFileFactory = false;
22 function __construct( $info ) {
24 $this->name
= $info['name'];
27 $this->initialCapital
= true; // by default
28 foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
29 'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection' ) as $var )
31 if ( isset( $info[$var] ) ) {
32 $this->$var = $info[$var];
35 $this->transformVia404
= !empty( $info['transformVia404'] );
39 * Determine if a string is an mwrepo:// URL
41 static function isVirtualUrl( $url ) {
42 return substr( $url, 0, 9 ) == 'mwrepo://';
46 * Create a new File object from the local repository
47 * @param mixed $title Title object or string
48 * @param mixed $time Time at which the image is supposed to have existed.
49 * If this is specified, the returned object will be an
50 * instance of the repository's old file class instead of
51 * a current file. Repositories not supporting version
52 * control should return false if this parameter is set.
54 function newFile( $title, $time = false ) {
55 if ( !($title instanceof Title
) ) {
56 $title = Title
::makeTitleSafe( NS_IMAGE
, $title );
57 if ( !is_object( $title ) ) {
62 if ( $this->oldFileFactory
) {
63 return call_user_func( $this->oldFileFactory
, $title, $this, $time );
68 return call_user_func( $this->fileFactory
, $title, $this );
73 * Find an instance of the named file that existed at the specified time
74 * Returns false if the file did not exist. Repositories not supporting
75 * version control should return false if the time is specified.
77 * @param mixed $time 14-character timestamp, or false for the current version
79 function findFile( $title, $time = false, $redirected = false ) {
80 if ( !($title instanceof Title
) ) {
81 $title = Title
::makeTitleSafe( NS_IMAGE
, $title );
82 if ( !is_object( $title ) ) {
87 # First try the current version of the file to see if it precedes the timestamp
88 $img = $this->newFile( $title );
92 if ( $img->exists() && ( !$time ||
$img->getTimestamp() <= $time ) ) {
95 # Now try an old version of the file
96 $img = $this->newFile( $title, $time );
97 if ( $img->exists() ) {
102 if( !$redirected ) { // Prevent redirect loops
103 $redir = $this->checkRedirects( $title->getDBkey() );
105 return $this->findFile( $redir, $time, $title );
110 * Get the URL of thumb.php
112 function getThumbScriptUrl() {
113 return $this->thumbScriptUrl
;
117 * Returns true if the repository can transform files via a 404 handler
119 function canTransformVia404() {
120 return $this->transformVia404
;
124 * Get the name of an image from its title object
126 function getNameFromTitle( $title ) {
127 global $wgCapitalLinks;
128 if ( $this->initialCapital
!= $wgCapitalLinks ) {
130 $name = $title->getUserCaseDBKey();
131 if ( $this->initialCapital
) {
132 $name = $wgContLang->ucfirst( $name );
135 $name = $title->getDBkey();
140 static function getHashPathForLevel( $name, $levels ) {
141 if ( $levels == 0 ) {
144 $hash = md5( $name );
146 for ( $i = 1; $i <= $levels; $i++
) {
147 $path .= substr( $hash, 0, $i ) . '/';
154 * Get the name of this repository, as specified by $info['name]' to the constructor
161 * Get the file description page base URL, or false if there isn't one.
164 function getDescBaseUrl() {
165 if ( is_null( $this->descBaseUrl
) ) {
166 if ( !is_null( $this->articleUrl
) ) {
167 $this->descBaseUrl
= str_replace( '$1',
168 wfUrlencode( Namespace::getCanonicalName( NS_IMAGE
) ) . ':', $this->articleUrl
);
169 } elseif ( !is_null( $this->scriptDirUrl
) ) {
170 $this->descBaseUrl
= $this->scriptDirUrl
. '/index.php?title=' .
171 wfUrlencode( Namespace::getCanonicalName( NS_IMAGE
) ) . ':';
173 $this->descBaseUrl
= false;
176 return $this->descBaseUrl
;
180 * Get the URL of an image description page. May return false if it is
181 * unknown or not applicable. In general this should only be called by the
182 * File class, since it may return invalid results for certain kinds of
183 * repositories. Use File::getDescriptionUrl() in user code.
185 * In particular, it uses the article paths as specified to the repository
186 * constructor, whereas local repositories use the local Title functions.
188 function getDescriptionUrl( $name ) {
189 $base = $this->getDescBaseUrl();
191 return $base . wfUrlencode( $name );
198 * Get the URL of the content-only fragment of the description page. For
199 * MediaWiki this means action=render. This should only be called by the
200 * repository's file class, since it may return invalid results. User code
201 * should use File::getDescriptionText().
203 function getDescriptionRenderUrl( $name ) {
204 if ( isset( $this->scriptDirUrl
) ) {
205 return $this->scriptDirUrl
. '/index.php?title=' .
206 wfUrlencode( Namespace::getCanonicalName( NS_IMAGE
) . ':' . $name ) .
209 $descBase = $this->getDescBaseUrl();
211 return wfAppendQuery( $descBase . wfUrlencode( $name ), 'action=render' );
219 * Store a file to a given destination.
221 * @param string $srcPath Source path or virtual URL
222 * @param string $dstZone Destination zone
223 * @param string $dstRel Destination relative path
224 * @param integer $flags Bitwise combination of the following flags:
225 * self::DELETE_SOURCE Delete the source file after upload
226 * self::OVERWRITE Overwrite an existing destination file instead of failing
227 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
228 * same contents as the source
229 * @return FileRepoStatus
231 function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
232 $status = $this->storeBatch( array( array( $srcPath, $dstZone, $dstRel ) ), $flags );
233 if ( $status->successCount
== 0 ) {
240 * Store a batch of files
242 * @param array $triplets (src,zone,dest) triplets as per store()
243 * @param integer $flags Flags as per store
245 abstract function storeBatch( $triplets, $flags = 0 );
248 * Pick a random name in the temp zone and store a file to it.
249 * Returns a FileRepoStatus object with the URL in the value.
251 * @param string $originalName The base name of the file as specified
252 * by the user. The file extension will be maintained.
253 * @param string $srcPath The current location of the file.
255 abstract function storeTemp( $originalName, $srcPath );
258 * Remove a temporary file or mark it for garbage collection
259 * @param string $virtualUrl The virtual URL returned by storeTemp
260 * @return boolean True on success, false on failure
263 function freeTemp( $virtualUrl ) {
268 * Copy or move a file either from the local filesystem or from an mwrepo://
269 * virtual URL, into this repository at the specified destination location.
271 * Returns a FileRepoStatus object. On success, the value contains "new" or
272 * "archived", to indicate whether the file was new with that name.
274 * @param string $srcPath The source path or URL
275 * @param string $dstRel The destination relative path
276 * @param string $archiveRel The relative path where the existing file is to
277 * be archived, if there is one. Relative to the public zone root.
278 * @param integer $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
279 * that the source file should be deleted if possible
281 function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 ) {
282 $status = $this->publishBatch( array( array( $srcPath, $dstRel, $archiveRel ) ), $flags );
283 if ( $status->successCount
== 0 ) {
286 if ( isset( $status->value
[0] ) ) {
287 $status->value
= $status->value
[0];
289 $status->value
= false;
295 * Publish a batch of files
296 * @param array $triplets (source,dest,archive) triplets as per publish()
297 * @param integer $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
298 * that the source files should be deleted if possible
300 abstract function publishBatch( $triplets, $flags = 0 );
303 * Move a group of files to the deletion archive.
305 * If no valid deletion archive is configured, this may either delete the
306 * file or throw an exception, depending on the preference of the repository.
308 * The overwrite policy is determined by the repository -- currently FSRepo
309 * assumes a naming scheme in the deleted zone based on content hash, as
310 * opposed to the public zone which is assumed to be unique.
312 * @param array $sourceDestPairs Array of source/destination pairs. Each element
313 * is a two-element array containing the source file path relative to the
314 * public root in the first element, and the archive file path relative
315 * to the deleted zone root in the second element.
316 * @return FileRepoStatus
318 abstract function deleteBatch( $sourceDestPairs );
321 * Move a file to the deletion archive.
322 * If no valid deletion archive exists, this may either delete the file
323 * or throw an exception, depending on the preference of the repository
324 * @param mixed $srcRel Relative path for the file to be deleted
325 * @param mixed $archiveRel Relative path for the archive location.
326 * Relative to a private archive directory.
327 * @return WikiError object (wikitext-formatted), or true for success
329 function delete( $srcRel, $archiveRel ) {
330 return $this->deleteBatch( array( array( $srcRel, $archiveRel ) ) );
334 * Get properties of a file with a given virtual URL
335 * The virtual URL must refer to this repo
336 * Properties should ultimately be obtained via File::getPropsFromPath()
338 abstract function getFileProps( $virtualUrl );
341 * Call a callback function for every file in the repository
342 * May use either the database or the filesystem
345 function enumFiles( $callback ) {
346 throw new MWException( 'enumFiles is not supported by ' . get_class( $this ) );
350 * Determine if a relative path is valid, i.e. not blank or involving directory traveral
352 function validateFilename( $filename ) {
353 if ( strval( $filename ) == '' ) {
356 if ( wfIsWindows() ) {
357 $filename = strtr( $filename, '\\', '/' );
360 * Use the same traversal protection as Title::secureAndSplit()
362 if ( strpos( $filename, '.' ) !== false &&
363 ( $filename === '.' ||
$filename === '..' ||
364 strpos( $filename, './' ) === 0 ||
365 strpos( $filename, '../' ) === 0 ||
366 strpos( $filename, '/./' ) !== false ||
367 strpos( $filename, '/../' ) !== false ) )
376 * Path disclosure protection functions
378 function paranoidClean( $param ) { return '[hidden]'; }
379 function passThrough( $param ) { return $param; }
382 * Get a callback function to use for cleaning error message parameters
384 function getErrorCleanupFunction() {
385 switch ( $this->pathDisclosureProtection
) {
387 $callback = array( $this, 'passThrough' );
389 default: // 'paranoid'
390 $callback = array( $this, 'paranoidClean' );
397 * Create a new fatal error
399 function newFatal( $message /*, parameters...*/ ) {
400 $params = func_get_args();
401 array_unshift( $params, $this );
402 return call_user_func_array( array( 'FileRepoStatus', 'newFatal' ), $params );
406 * Create a new good result
408 function newGood( $value = null ) {
409 return FileRepoStatus
::newGood( $this, $value );
413 * Delete files in the deleted directory if they are not referenced in the filearchive table
416 function cleanupDeletedBatch( $storageKeys ) {}
419 * Check for redirects.
421 function checkRedirects( $filename ) {
422 $dbr = $this->getSlaveDB();
423 $res = $dbr->selectRow(
425 array( 'ir_from', 'ir_to' ),
426 array( 'ir_from' => $filename ),
429 if( !$res ) return false;