(bug 19751) Filesystem is now checked during image undeletion
[lhc/web/wiklou.git] / includes / filerepo / FileRepo.php
1 <?php
2 /**
3 * Base code for file repositories.
4 *
5 * @file
6 * @ingroup FileRepo
7 */
8
9 /**
10 * Base class for file repositories.
11 * Do not instantiate, use a derived class.
12 *
13 * @ingroup FileRepo
14 */
15 abstract class FileRepo {
16 const FILES_ONLY = 1;
17 const DELETE_SOURCE = 1;
18 const OVERWRITE = 2;
19 const OVERWRITE_SAME = 4;
20 const SKIP_VALIDATION = 8;
21
22 var $thumbScriptUrl, $transformVia404;
23 var $descBaseUrl, $scriptDirUrl, $scriptExtension, $articleUrl;
24 var $fetchDescription, $initialCapital;
25 var $pathDisclosureProtection = 'paranoid';
26 var $descriptionCacheExpiry, $hashLevels, $url, $thumbUrl;
27
28 /**
29 * Factory functions for creating new files
30 * Override these in the base class
31 */
32 var $fileFactory = false, $oldFileFactory = false;
33 var $fileFactoryKey = false, $oldFileFactoryKey = false;
34
35 function __construct( $info ) {
36 // Required settings
37 $this->name = $info['name'];
38
39 // Optional settings
40 $this->initialCapital = MWNamespace::isCapitalized( NS_FILE );
41 foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
42 'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection',
43 'descriptionCacheExpiry', 'hashLevels', 'url', 'thumbUrl', 'scriptExtension' )
44 as $var )
45 {
46 if ( isset( $info[$var] ) ) {
47 $this->$var = $info[$var];
48 }
49 }
50 $this->transformVia404 = !empty( $info['transformVia404'] );
51 }
52
53 /**
54 * Determine if a string is an mwrepo:// URL
55 */
56 static function isVirtualUrl( $url ) {
57 return substr( $url, 0, 9 ) == 'mwrepo://';
58 }
59
60 /**
61 * Create a new File object from the local repository
62 *
63 * @param $title Mixed: Title object or string
64 * @param $time Mixed: Time at which the image was uploaded.
65 * If this is specified, the returned object will be an
66 * instance of the repository's old file class instead of a
67 * current file. Repositories not supporting version control
68 * should return false if this parameter is set.
69 */
70 function newFile( $title, $time = false ) {
71 if ( !($title instanceof Title) ) {
72 $title = Title::makeTitleSafe( NS_FILE, $title );
73 if ( !is_object( $title ) ) {
74 return null;
75 }
76 }
77 if ( $time ) {
78 if ( $this->oldFileFactory ) {
79 return call_user_func( $this->oldFileFactory, $title, $this, $time );
80 } else {
81 return false;
82 }
83 } else {
84 return call_user_func( $this->fileFactory, $title, $this );
85 }
86 }
87
88 /**
89 * Find an instance of the named file created at the specified time
90 * Returns false if the file does not exist. Repositories not supporting
91 * version control should return false if the time is specified.
92 *
93 * @param $title Mixed: Title object or string
94 * @param $options Associative array of options:
95 * time: requested time for an archived image, or false for the
96 * current version. An image object will be returned which was
97 * created at the specified time.
98 *
99 * ignoreRedirect: If true, do not follow file redirects
100 *
101 * private: If true, return restricted (deleted) files if the current
102 * user is allowed to view them. Otherwise, such files will not
103 * be found.
104 */
105 function findFile( $title, $options = array() ) {
106 if ( !is_array( $options ) ) {
107 // MW 1.15 compat
108 $time = $options;
109 } else {
110 $time = isset( $options['time'] ) ? $options['time'] : false;
111 }
112 if ( !($title instanceof Title) ) {
113 $title = Title::makeTitleSafe( NS_FILE, $title );
114 if ( !is_object( $title ) ) {
115 return false;
116 }
117 }
118 # First try the current version of the file to see if it precedes the timestamp
119 $img = $this->newFile( $title );
120 if ( !$img ) {
121 return false;
122 }
123 if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
124 return $img;
125 }
126 # Now try an old version of the file
127 if ( $time !== false ) {
128 $img = $this->newFile( $title, $time );
129 if ( $img && $img->exists() ) {
130 if ( !$img->isDeleted(File::DELETED_FILE) ) {
131 return $img;
132 } else if ( !empty( $options['private'] ) && $img->userCan(File::DELETED_FILE) ) {
133 return $img;
134 }
135 }
136 }
137
138 # Now try redirects
139 if ( !empty( $options['ignoreRedirect'] ) ) {
140 return false;
141 }
142 $redir = $this->checkRedirect( $title );
143 if( $redir && $redir->getNamespace() == NS_FILE) {
144 $img = $this->newFile( $redir );
145 if( !$img ) {
146 return false;
147 }
148 if( $img->exists() ) {
149 $img->redirectedFrom( $title->getDBkey() );
150 return $img;
151 }
152 }
153 return false;
154 }
155
156 /*
157 * Find many files at once.
158 * @param $items An array of titles, or an array of findFile() options with
159 * the "title" option giving the title. Example:
160 *
161 * $findItem = array( 'title' => $title, 'private' => true );
162 * $findBatch = array( $findItem );
163 * $repo->findFiles( $findBatch );
164 */
165 function findFiles( $items ) {
166 $result = array();
167 foreach ( $items as $item ) {
168 if ( is_array( $item ) ) {
169 $title = $item['title'];
170 $options = $item;
171 unset( $options['title'] );
172 } else {
173 $title = $item;
174 $options = array();
175 }
176 $file = $this->findFile( $title, $options );
177 if ( $file ) {
178 $result[$file->getTitle()->getDBkey()] = $file;
179 }
180 }
181 return $result;
182 }
183
184 /**
185 * Create a new File object from the local repository
186 * @param $sha1 Mixed: SHA-1 key
187 * @param $time Mixed: time at which the image was uploaded.
188 * If this is specified, the returned object will be an
189 * of the repository's old file class instead of a current
190 * file. Repositories not supporting version control should
191 * return false if this parameter is set.
192 */
193 function newFileFromKey( $sha1, $time = false ) {
194 if ( $time ) {
195 if ( $this->oldFileFactoryKey ) {
196 return call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
197 } else {
198 return false;
199 }
200 } else {
201 return call_user_func( $this->fileFactoryKey, $sha1, $this );
202 }
203 }
204
205 /**
206 * Find an instance of the file with this key, created at the specified time
207 * Returns false if the file does not exist. Repositories not supporting
208 * version control should return false if the time is specified.
209 *
210 * @param $sha1 String
211 * @param $options Option array, same as findFile().
212 */
213 function findFileFromKey( $sha1, $options = array() ) {
214 if ( !is_array( $options ) ) {
215 # MW 1.15 compat
216 $time = $options;
217 } else {
218 $time = isset( $options['time'] ) ? $options['time'] : false;
219 }
220
221 # First try the current version of the file to see if it precedes the timestamp
222 $img = $this->newFileFromKey( $sha1 );
223 if ( !$img ) {
224 return false;
225 }
226 if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
227 return $img;
228 }
229 # Now try an old version of the file
230 if ( $time !== false ) {
231 $img = $this->newFileFromKey( $sha1, $time );
232 if ( $img->exists() ) {
233 if ( !$img->isDeleted(File::DELETED_FILE) ) {
234 return $img;
235 } else if ( !empty( $options['private'] ) && $img->userCan(File::DELETED_FILE) ) {
236 return $img;
237 }
238 }
239 }
240 return false;
241 }
242
243 /**
244 * Get the URL of thumb.php
245 */
246 function getThumbScriptUrl() {
247 return $this->thumbScriptUrl;
248 }
249
250 /**
251 * Get the URL corresponding to one of the four basic zones
252 * @param $zone String: one of: public, deleted, temp, thumb
253 * @return String or false
254 */
255 function getZoneUrl( $zone ) {
256 return false;
257 }
258
259 /**
260 * Returns true if the repository can transform files via a 404 handler
261 */
262 function canTransformVia404() {
263 return $this->transformVia404;
264 }
265
266 /**
267 * Get the name of an image from its title object
268 */
269 function getNameFromTitle( $title ) {
270 if ( $this->initialCapital != MWNamespace::isCapitalized( NS_FILE ) ) {
271 global $wgContLang;
272 $name = $title->getUserCaseDBKey();
273 if ( $this->initialCapital ) {
274 $name = $wgContLang->ucfirst( $name );
275 }
276 } else {
277 $name = $title->getDBkey();
278 }
279 return $name;
280 }
281
282 static function getHashPathForLevel( $name, $levels ) {
283 if ( $levels == 0 ) {
284 return '';
285 } else {
286 $hash = md5( $name );
287 $path = '';
288 for ( $i = 1; $i <= $levels; $i++ ) {
289 $path .= substr( $hash, 0, $i ) . '/';
290 }
291 return $path;
292 }
293 }
294
295 /**
296 * Get a relative path including trailing slash, e.g. f/fa/
297 * If the repo is not hashed, returns an empty string
298 */
299 function getHashPath( $name ) {
300 return self::getHashPathForLevel( $name, $this->hashLevels );
301 }
302
303 /**
304 * Get the name of this repository, as specified by $info['name]' to the constructor
305 */
306 function getName() {
307 return $this->name;
308 }
309
310 /**
311 * Make an url to this repo
312 *
313 * @param $query mixed Query string to append
314 * @param $entry string Entry point; defaults to index
315 * @return string
316 */
317 function makeUrl( $query = '', $entry = 'index' ) {
318 $ext = isset( $this->scriptExtension ) ? $this->scriptExtension : '.php';
319 return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}{$ext}", $query );
320 }
321
322 /**
323 * Get the URL of an image description page. May return false if it is
324 * unknown or not applicable. In general this should only be called by the
325 * File class, since it may return invalid results for certain kinds of
326 * repositories. Use File::getDescriptionUrl() in user code.
327 *
328 * In particular, it uses the article paths as specified to the repository
329 * constructor, whereas local repositories use the local Title functions.
330 */
331 function getDescriptionUrl( $name ) {
332 $encName = wfUrlencode( $name );
333 if ( !is_null( $this->descBaseUrl ) ) {
334 # "http://example.com/wiki/Image:"
335 return $this->descBaseUrl . $encName;
336 }
337 if ( !is_null( $this->articleUrl ) ) {
338 # "http://example.com/wiki/$1"
339 #
340 # We use "Image:" as the canonical namespace for
341 # compatibility across all MediaWiki versions.
342 return str_replace( '$1',
343 "Image:$encName", $this->articleUrl );
344 }
345 if ( !is_null( $this->scriptDirUrl ) ) {
346 # "http://example.com/w"
347 #
348 # We use "Image:" as the canonical namespace for
349 # compatibility across all MediaWiki versions,
350 # and just sort of hope index.php is right. ;)
351 return $this->makeUrl( "title=Image:$encName" );
352 }
353 return false;
354 }
355
356 /**
357 * Get the URL of the content-only fragment of the description page. For
358 * MediaWiki this means action=render. This should only be called by the
359 * repository's file class, since it may return invalid results. User code
360 * should use File::getDescriptionText().
361 * @param $name String: name of image to fetch
362 * @param $lang String: language to fetch it in, if any.
363 */
364 function getDescriptionRenderUrl( $name, $lang = null ) {
365 $query = 'action=render';
366 if ( !is_null( $lang ) ) {
367 $query .= '&uselang=' . $lang;
368 }
369 if ( isset( $this->scriptDirUrl ) ) {
370 return $this->makeUrl(
371 'title=' .
372 wfUrlencode( 'Image:' . $name ) .
373 "&$query" );
374 } else {
375 $descUrl = $this->getDescriptionUrl( $name );
376 if ( $descUrl ) {
377 return wfAppendQuery( $descUrl, $query );
378 } else {
379 return false;
380 }
381 }
382 }
383
384 /**
385 * Get the URL of the stylesheet to apply to description pages
386 * @return string
387 */
388 function getDescriptionStylesheetUrl() {
389 if ( $this->scriptDirUrl ) {
390 return $this->makeUrl( 'title=MediaWiki:Filepage.css&' .
391 wfArrayToCGI( Skin::getDynamicStylesheetQuery() ) );
392 }
393 }
394
395 /**
396 * Store a file to a given destination.
397 *
398 * @param $srcPath String: source path or virtual URL
399 * @param $dstZone String: destination zone
400 * @param $dstRel String: destination relative path
401 * @param $flags Integer: bitwise combination of the following flags:
402 * self::DELETE_SOURCE Delete the source file after upload
403 * self::OVERWRITE Overwrite an existing destination file instead of failing
404 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
405 * same contents as the source
406 * @return FileRepoStatus
407 */
408 function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
409 $status = $this->storeBatch( array( array( $srcPath, $dstZone, $dstRel ) ), $flags );
410 if ( $status->successCount == 0 ) {
411 $status->ok = false;
412 }
413 return $status;
414 }
415
416 /**
417 * Store a batch of files
418 *
419 * @param $triplets Array: (src,zone,dest) triplets as per store()
420 * @param $flags Integer: flags as per store
421 */
422 abstract function storeBatch( $triplets, $flags = 0 );
423
424 /**
425 * Pick a random name in the temp zone and store a file to it.
426 * Returns a FileRepoStatus object with the URL in the value.
427 *
428 * @param $originalName String: the base name of the file as specified
429 * by the user. The file extension will be maintained.
430 * @param $srcPath String: the current location of the file.
431 */
432 abstract function storeTemp( $originalName, $srcPath );
433
434
435 /**
436 * Append the contents of the source path to the given file.
437 * @param $srcPath String: location of the source file
438 * @param $toAppendPath String: path to append to.
439 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
440 * that the source file should be deleted if possible
441 * @return mixed Status or false
442 */
443 abstract function append( $srcPath, $toAppendPath, $flags = 0 );
444
445 /**
446 * Remove a temporary file or mark it for garbage collection
447 * @param $virtualUrl String: the virtual URL returned by storeTemp
448 * @return Boolean: true on success, false on failure
449 * STUB
450 */
451 function freeTemp( $virtualUrl ) {
452 return true;
453 }
454
455 /**
456 * Copy or move a file either from the local filesystem or from an mwrepo://
457 * virtual URL, into this repository at the specified destination location.
458 *
459 * Returns a FileRepoStatus object. On success, the value contains "new" or
460 * "archived", to indicate whether the file was new with that name.
461 *
462 * @param $srcPath String: the source path or URL
463 * @param $dstRel String: the destination relative path
464 * @param $archiveRel String: rhe relative path where the existing file is to
465 * be archived, if there is one. Relative to the public zone root.
466 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
467 * that the source file should be deleted if possible
468 */
469 function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 ) {
470 $status = $this->publishBatch( array( array( $srcPath, $dstRel, $archiveRel ) ), $flags );
471 if ( $status->successCount == 0 ) {
472 $status->ok = false;
473 }
474 if ( isset( $status->value[0] ) ) {
475 $status->value = $status->value[0];
476 } else {
477 $status->value = false;
478 }
479 return $status;
480 }
481
482 /**
483 * Publish a batch of files
484 * @param $triplets Array: (source,dest,archive) triplets as per publish()
485 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
486 * that the source files should be deleted if possible
487 */
488 abstract function publishBatch( $triplets, $flags = 0 );
489
490 function fileExists( $file, $flags = 0 ) {
491 $result = $this->fileExistsBatch( array( $file ), $flags );
492 return $result[0];
493 }
494
495 /**
496 * Checks existence of an array of files.
497 *
498 * @param $files Array: URLs (or paths) of files to check
499 * @param $flags Integer: bitwise combination of the following flags:
500 * self::FILES_ONLY Mark file as existing only if it is a file (not directory)
501 * @return Either array of files and existence flags, or false
502 */
503 abstract function fileExistsBatch( $files, $flags = 0 );
504
505 /**
506 * Move a group of files to the deletion archive.
507 *
508 * If no valid deletion archive is configured, this may either delete the
509 * file or throw an exception, depending on the preference of the repository.
510 *
511 * The overwrite policy is determined by the repository -- currently FSRepo
512 * assumes a naming scheme in the deleted zone based on content hash, as
513 * opposed to the public zone which is assumed to be unique.
514 *
515 * @param $sourceDestPairs Array of source/destination pairs. Each element
516 * is a two-element array containing the source file path relative to the
517 * public root in the first element, and the archive file path relative
518 * to the deleted zone root in the second element.
519 * @return FileRepoStatus
520 */
521 abstract function deleteBatch( $sourceDestPairs );
522
523 /**
524 * Move a file to the deletion archive.
525 * If no valid deletion archive exists, this may either delete the file
526 * or throw an exception, depending on the preference of the repository
527 * @param $srcRel Mixed: relative path for the file to be deleted
528 * @param $archiveRel Mixed: relative path for the archive location.
529 * Relative to a private archive directory.
530 * @return FileRepoStatus object
531 */
532 function delete( $srcRel, $archiveRel ) {
533 return $this->deleteBatch( array( array( $srcRel, $archiveRel ) ) );
534 }
535
536 /**
537 * Get properties of a file with a given virtual URL
538 * The virtual URL must refer to this repo
539 * Properties should ultimately be obtained via File::getPropsFromPath()
540 */
541 abstract function getFileProps( $virtualUrl );
542
543 /**
544 * Call a callback function for every file in the repository
545 * May use either the database or the filesystem
546 * STUB
547 */
548 function enumFiles( $callback ) {
549 throw new MWException( 'enumFiles is not supported by ' . get_class( $this ) );
550 }
551
552 /**
553 * Determine if a relative path is valid, i.e. not blank or involving directory traveral
554 */
555 function validateFilename( $filename ) {
556 if ( strval( $filename ) == '' ) {
557 return false;
558 }
559 if ( wfIsWindows() ) {
560 $filename = strtr( $filename, '\\', '/' );
561 }
562 /**
563 * Use the same traversal protection as Title::secureAndSplit()
564 */
565 if ( strpos( $filename, '.' ) !== false &&
566 ( $filename === '.' || $filename === '..' ||
567 strpos( $filename, './' ) === 0 ||
568 strpos( $filename, '../' ) === 0 ||
569 strpos( $filename, '/./' ) !== false ||
570 strpos( $filename, '/../' ) !== false ) )
571 {
572 return false;
573 } else {
574 return true;
575 }
576 }
577
578 /**#@+
579 * Path disclosure protection functions
580 */
581 function paranoidClean( $param ) { return '[hidden]'; }
582 function passThrough( $param ) { return $param; }
583
584 /**
585 * Get a callback function to use for cleaning error message parameters
586 */
587 function getErrorCleanupFunction() {
588 switch ( $this->pathDisclosureProtection ) {
589 case 'none':
590 $callback = array( $this, 'passThrough' );
591 break;
592 default: // 'paranoid'
593 $callback = array( $this, 'paranoidClean' );
594 }
595 return $callback;
596 }
597 /**#@-*/
598
599 /**
600 * Create a new fatal error
601 */
602 function newFatal( $message /*, parameters...*/ ) {
603 $params = func_get_args();
604 array_unshift( $params, $this );
605 return call_user_func_array( array( 'FileRepoStatus', 'newFatal' ), $params );
606 }
607
608 /**
609 * Create a new good result
610 */
611 function newGood( $value = null ) {
612 return FileRepoStatus::newGood( $this, $value );
613 }
614
615 /**
616 * Delete files in the deleted directory if they are not referenced in the filearchive table
617 * STUB
618 */
619 function cleanupDeletedBatch( $storageKeys ) {}
620
621 /**
622 * Checks if there is a redirect named as $title. If there is, return the
623 * title object. If not, return false.
624 * STUB
625 *
626 * @param $title Title of image
627 */
628 function checkRedirect( $title ) {
629 return false;
630 }
631
632 /**
633 * Invalidates image redirect cache related to that image
634 * Doesn't do anything for repositories that don't support image redirects.
635 *
636 * STUB
637 * @param $title Title of image
638 */
639 function invalidateImageRedirect( $title ) {}
640
641 /**
642 * Get an array or iterator of file objects for files that have a given
643 * SHA-1 content hash.
644 *
645 * STUB
646 */
647 function findBySha1( $hash ) {
648 return array();
649 }
650
651 /**
652 * Get the human-readable name of the repo.
653 * @return string
654 */
655 public function getDisplayName() {
656 // We don't name our own repo, return nothing
657 if ( $this->isLocal() ) {
658 return null;
659 }
660 // 'shared-repo-name-wikimediacommons' is used when $wgUseInstantCommons = true
661 return wfMessageFallback( 'shared-repo-name-' . $this->name, 'shared-repo' )->text();
662 }
663
664 /**
665 * Returns true if this the local file repository.
666 *
667 * @return bool
668 */
669 function isLocal() {
670 return $this->getName() == 'local';
671 }
672
673
674 /**
675 * Get a key on the primary cache for this repository.
676 * Returns false if the repository's cache is not accessible at this site.
677 * The parameters are the parts of the key, as for wfMemcKey().
678 *
679 * STUB
680 */
681 function getSharedCacheKey( /*...*/ ) {
682 return false;
683 }
684
685 /**
686 * Get a key for this repo in the local cache domain. These cache keys are
687 * not shared with remote instances of the repo.
688 * The parameters are the parts of the key, as for wfMemcKey().
689 */
690 function getLocalCacheKey( /*...*/ ) {
691 $args = func_get_args();
692 array_unshift( $args, 'filerepo', $this->getName() );
693 return call_user_func_array( 'wfMemcKey', $args );
694 }
695
696 /**
697 * Get an UploadStash associated with this repo.
698 */
699 function getUploadStash() {
700 return new UploadStash( $this );
701 }
702 }