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