dbda0d1bfc690422b44dbdda3d0b5db4f7d50fe7
[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 * @param $url string
57 *
58 * @return bool
59 */
60 static function isVirtualUrl( $url ) {
61 return substr( $url, 0, 9 ) == 'mwrepo://';
62 }
63
64 /**
65 * Create a new File object from the local repository
66 *
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.
73 *
74 * @return File|null A File, or null if passed an invalid Title
75 */
76 function newFile( $title, $time = false ) {
77 $title = File::normalizeTitle( $title );
78 if ( !$title ) {
79 return null;
80 }
81 if ( $time ) {
82 if ( $this->oldFileFactory ) {
83 return call_user_func( $this->oldFileFactory, $title, $this, $time );
84 } else {
85 return false;
86 }
87 } else {
88 return call_user_func( $this->fileFactory, $title, $this );
89 }
90 }
91
92 /**
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.
96 *
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.
102 *
103 * ignoreRedirect: If true, do not follow file redirects
104 *
105 * private: If true, return restricted (deleted) files if the current
106 * user is allowed to view them. Otherwise, such files will not
107 * be found.
108 *
109 * @return File|false
110 */
111 function findFile( $title, $options = array() ) {
112 $title = File::normalizeTitle( $title );
113 if ( !$title ) {
114 return false;
115 }
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 );
119 if ( !$img ) {
120 return false;
121 }
122 if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
123 return $img;
124 }
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 ) ) {
132 return $img;
133 }
134 }
135 }
136
137 # Now try redirects
138 if ( !empty( $options['ignoreRedirect'] ) ) {
139 return false;
140 }
141 $redir = $this->checkRedirect( $title );
142 if( $redir && $title->getNamespace() == NS_FILE) {
143 $img = $this->newFile( $redir );
144 if( !$img ) {
145 return false;
146 }
147 if( $img->exists() ) {
148 $img->redirectedFrom( $title->getDBkey() );
149 return $img;
150 }
151 }
152 return false;
153 }
154
155 /**
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:
159 *
160 * $findItem = array( 'title' => $title, 'private' => true );
161 * $findBatch = array( $findItem );
162 * $repo->findFiles( $findBatch );
163 *
164 * @return array
165 */
166 function findFiles( $items ) {
167 $result = array();
168 foreach ( $items as $item ) {
169 if ( is_array( $item ) ) {
170 $title = $item['title'];
171 $options = $item;
172 unset( $options['title'] );
173 } else {
174 $title = $item;
175 $options = array();
176 }
177 $file = $this->findFile( $title, $options );
178 if ( $file ) {
179 $result[$file->getTitle()->getDBkey()] = $file;
180 }
181 }
182 return $result;
183 }
184
185 /**
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.
189 *
190 * @param $sha1 String base 36 SHA-1 hash
191 * @param $options Option array, same as findFile().
192 */
193 function findFileFromKey( $sha1, $options = array() ) {
194 $time = isset( $options['time'] ) ? $options['time'] : false;
195
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 );
199 } else {
200 return false; // find-by-sha1 not supported
201 }
202 if ( $img && $img->exists() ) {
203 return $img;
204 }
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 ) ) {
212 return $img;
213 }
214 }
215 }
216 return false;
217 }
218
219 /**
220 * Get the URL of thumb.php
221 */
222 function getThumbScriptUrl() {
223 return $this->thumbScriptUrl;
224 }
225
226 /**
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
230 */
231 function getZoneUrl( $zone ) {
232 return false;
233 }
234
235 /**
236 * Returns true if the repository can transform files via a 404 handler
237 *
238 * @return bool
239 */
240 function canTransformVia404() {
241 return $this->transformVia404;
242 }
243
244 /**
245 * Get the name of an image from its title object
246 * @param $title Title
247 */
248 function getNameFromTitle( Title $title ) {
249 if ( $this->initialCapital != MWNamespace::isCapitalized( NS_FILE ) ) {
250 global $wgContLang;
251 $name = $title->getUserCaseDBKey();
252 if ( $this->initialCapital ) {
253 $name = $wgContLang->ucfirst( $name );
254 }
255 } else {
256 $name = $title->getDBkey();
257 }
258 return $name;
259 }
260
261 /**
262 * @param $name
263 * @param $levels
264 * @return string
265 */
266 static function getHashPathForLevel( $name, $levels ) {
267 if ( $levels == 0 ) {
268 return '';
269 } else {
270 $hash = md5( $name );
271 $path = '';
272 for ( $i = 1; $i <= $levels; $i++ ) {
273 $path .= substr( $hash, 0, $i ) . '/';
274 }
275 return $path;
276 }
277 }
278
279 /**
280 * Get a relative path including trailing slash, e.g. f/fa/
281 * If the repo is not hashed, returns an empty string
282 *
283 * @param $name string
284 *
285 * @return string
286 */
287 function getHashPath( $name ) {
288 return self::getHashPathForLevel( $name, $this->hashLevels );
289 }
290
291 /**
292 * Get the name of this repository, as specified by $info['name]' to the constructor
293 */
294 function getName() {
295 return $this->name;
296 }
297
298 /**
299 * Make an url to this repo
300 *
301 * @param $query mixed Query string to append
302 * @param $entry string Entry point; defaults to index
303 * @return string
304 */
305 function makeUrl( $query = '', $entry = 'index' ) {
306 $ext = isset( $this->scriptExtension ) ? $this->scriptExtension : '.php';
307 return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}{$ext}", $query );
308 }
309
310 /**
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.
315 *
316 * In particular, it uses the article paths as specified to the repository
317 * constructor, whereas local repositories use the local Title functions.
318 */
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;
324 }
325 if ( !is_null( $this->articleUrl ) ) {
326 # "http://example.com/wiki/$1"
327 #
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 );
332 }
333 if ( !is_null( $this->scriptDirUrl ) ) {
334 # "http://example.com/w"
335 #
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" );
340 }
341 return false;
342 }
343
344 /**
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.
351 */
352 function getDescriptionRenderUrl( $name, $lang = null ) {
353 $query = 'action=render';
354 if ( !is_null( $lang ) ) {
355 $query .= '&uselang=' . $lang;
356 }
357 if ( isset( $this->scriptDirUrl ) ) {
358 return $this->makeUrl(
359 'title=' .
360 wfUrlencode( 'Image:' . $name ) .
361 "&$query" );
362 } else {
363 $descUrl = $this->getDescriptionUrl( $name );
364 if ( $descUrl ) {
365 return wfAppendQuery( $descUrl, $query );
366 } else {
367 return false;
368 }
369 }
370 }
371
372 /**
373 * Get the URL of the stylesheet to apply to description pages
374 * @return string
375 */
376 function getDescriptionStylesheetUrl() {
377 if ( $this->scriptDirUrl ) {
378 return $this->makeUrl( 'title=MediaWiki:Filepage.css&' .
379 wfArrayToCGI( Skin::getDynamicStylesheetQuery() ) );
380 }
381 }
382
383 /**
384 * Store a file to a given destination.
385 *
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
395 */
396 function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
397 $status = $this->storeBatch( array( array( $srcPath, $dstZone, $dstRel ) ), $flags );
398 if ( $status->successCount == 0 ) {
399 $status->ok = false;
400 }
401 return $status;
402 }
403
404 /**
405 * Store a batch of files
406 *
407 * @param $triplets Array: (src,zone,dest) triplets as per store()
408 * @param $flags Integer: flags as per store
409 */
410 abstract function storeBatch( $triplets, $flags = 0 );
411
412 /**
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.
415 *
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.
419 */
420 abstract function storeTemp( $originalName, $srcPath );
421
422
423 /**
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
428 */
429 abstract function concatenate( $fileList, $targetPath, $flags = 0 );
430
431 /**
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
439 */
440 abstract function append( $srcPath, $toAppendPath, $flags = 0 );
441
442 /**
443 * Finish the append operation.
444 * @param $toAppendPath String: path to append to.
445 * @return mixed Status or false
446 */
447 abstract function appendFinish( $toAppendPath );
448
449 /**
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
453 * STUB
454 */
455 function freeTemp( $virtualUrl ) {
456 return true;
457 }
458
459 /**
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.
462 *
463 * Returns a FileRepoStatus object. On success, the value contains "new" or
464 * "archived", to indicate whether the file was new with that name.
465 *
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
472 */
473 function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 ) {
474 $status = $this->publishBatch( array( array( $srcPath, $dstRel, $archiveRel ) ), $flags );
475 if ( $status->successCount == 0 ) {
476 $status->ok = false;
477 }
478 if ( isset( $status->value[0] ) ) {
479 $status->value = $status->value[0];
480 } else {
481 $status->value = false;
482 }
483 return $status;
484 }
485
486 /**
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
491 */
492 abstract function publishBatch( $triplets, $flags = 0 );
493
494 /**
495 * @param $file
496 * @param int $flags
497 * @return bool
498 */
499 function fileExists( $file, $flags = 0 ) {
500 $result = $this->fileExistsBatch( array( $file ), $flags );
501 return $result[0];
502 }
503
504 /**
505 * Checks existence of an array of files.
506 *
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
511 */
512 abstract function fileExistsBatch( $files, $flags = 0 );
513
514 /**
515 * Move a group of files to the deletion archive.
516 *
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.
519 *
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.
523 *
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
529 */
530 abstract function deleteBatch( $sourceDestPairs );
531
532 /**
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
540 */
541 function delete( $srcRel, $archiveRel ) {
542 return $this->deleteBatch( array( array( $srcRel, $archiveRel ) ) );
543 }
544
545 /**
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()
549 *
550 * @param $virtualUrl string
551 */
552 abstract function getFileProps( $virtualUrl );
553
554 /**
555 * Call a callback function for every file in the repository
556 * May use either the database or the filesystem
557 * STUB
558 */
559 function enumFiles( $callback ) {
560 throw new MWException( 'enumFiles is not supported by ' . get_class( $this ) );
561 }
562
563 /**
564 * Determine if a relative path is valid, i.e. not blank or involving directory traveral
565 *
566 * @param $filename string
567 *
568 * @return bool
569 */
570 function validateFilename( $filename ) {
571 if ( strval( $filename ) == '' ) {
572 return false;
573 }
574 if ( wfIsWindows() ) {
575 $filename = strtr( $filename, '\\', '/' );
576 }
577 /**
578 * Use the same traversal protection as Title::secureAndSplit()
579 */
580 if ( strpos( $filename, '.' ) !== false &&
581 ( $filename === '.' || $filename === '..' ||
582 strpos( $filename, './' ) === 0 ||
583 strpos( $filename, '../' ) === 0 ||
584 strpos( $filename, '/./' ) !== false ||
585 strpos( $filename, '/../' ) !== false ) )
586 {
587 return false;
588 } else {
589 return true;
590 }
591 }
592
593 /**#@+
594 * Path disclosure protection functions
595 */
596 function paranoidClean( $param ) { return '[hidden]'; }
597
598 /**
599 * @param $param
600 * @return
601 */
602 function passThrough( $param ) { return $param; }
603
604 /**
605 * Get a callback function to use for cleaning error message parameters
606 */
607 function getErrorCleanupFunction() {
608 switch ( $this->pathDisclosureProtection ) {
609 case 'none':
610 $callback = array( $this, 'passThrough' );
611 break;
612 default: // 'paranoid'
613 $callback = array( $this, 'paranoidClean' );
614 }
615 return $callback;
616 }
617 /**#@-*/
618
619 /**
620 * Create a new fatal error
621 */
622 function newFatal( $message /*, parameters...*/ ) {
623 $params = func_get_args();
624 array_unshift( $params, $this );
625 return MWInit::callStaticMethod( 'FileRepoStatus', 'newFatal', $params );
626 }
627
628 /**
629 * Create a new good result
630 *
631 * @return FileRepoStatus
632 */
633 function newGood( $value = null ) {
634 return FileRepoStatus::newGood( $this, $value );
635 }
636
637 /**
638 * Delete files in the deleted directory if they are not referenced in the filearchive table
639 * STUB
640 */
641 function cleanupDeletedBatch( $storageKeys ) {}
642
643 /**
644 * Checks if there is a redirect named as $title. If there is, return the
645 * title object. If not, return false.
646 * STUB
647 *
648 * @param $title Title of image
649 * @return Bool
650 */
651 function checkRedirect( Title $title ) {
652 return false;
653 }
654
655 /**
656 * Invalidates image redirect cache related to that image
657 * Doesn't do anything for repositories that don't support image redirects.
658 *
659 * STUB
660 * @param $title Title of image
661 */
662 function invalidateImageRedirect( Title $title ) {}
663
664 /**
665 * Get an array or iterator of file objects for files that have a given
666 * SHA-1 content hash.
667 *
668 * STUB
669 */
670 function findBySha1( $hash ) {
671 return array();
672 }
673
674 /**
675 * Get the human-readable name of the repo.
676 * @return string
677 */
678 public function getDisplayName() {
679 // We don't name our own repo, return nothing
680 if ( $this->isLocal() ) {
681 return null;
682 }
683 // 'shared-repo-name-wikimediacommons' is used when $wgUseInstantCommons = true
684 return wfMessageFallback( 'shared-repo-name-' . $this->name, 'shared-repo' )->text();
685 }
686
687 /**
688 * Returns true if this the local file repository.
689 *
690 * @return bool
691 */
692 function isLocal() {
693 return $this->getName() == 'local';
694 }
695
696 /**
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().
700 *
701 * STUB
702 */
703 function getSharedCacheKey( /*...*/ ) {
704 return false;
705 }
706
707 /**
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().
711 */
712 function getLocalCacheKey( /*...*/ ) {
713 $args = func_get_args();
714 array_unshift( $args, 'filerepo', $this->getName() );
715 return call_user_func_array( 'wfMemcKey', $args );
716 }
717
718 /**
719 * Get an UploadStash associated with this repo.
720 *
721 * @return UploadStash
722 */
723 function getUploadStash() {
724 return new UploadStash( $this );
725 }
726 }