Merge "Re-adding dbdataobject stuff which got pulled from core about 2 weeks back...
[lhc/web/wiklou.git] / includes / filerepo / FileRepo.php
1 <?php
2 /**
3 * @defgroup FileRepo File Repository
4 *
5 * @brief This module handles how MediaWiki interacts with filesystems.
6 *
7 * @details
8 */
9
10 /**
11 * Base code for file repositories.
12 *
13 * @file
14 * @ingroup FileRepo
15 */
16
17 /**
18 * Base class for file repositories
19 *
20 * @ingroup FileRepo
21 */
22 class FileRepo {
23 const DELETE_SOURCE = 1;
24 const OVERWRITE = 2;
25 const OVERWRITE_SAME = 4;
26 const SKIP_LOCKING = 8;
27
28 /** @var FileBackend */
29 protected $backend;
30 /** @var Array Map of zones to config */
31 protected $zones = array();
32
33 var $thumbScriptUrl, $transformVia404;
34 var $descBaseUrl, $scriptDirUrl, $scriptExtension, $articleUrl;
35 var $fetchDescription, $initialCapital;
36 var $pathDisclosureProtection = 'simple'; // 'paranoid'
37 var $descriptionCacheExpiry, $url, $thumbUrl;
38 var $hashLevels, $deletedHashLevels;
39
40 /**
41 * Factory functions for creating new files
42 * Override these in the base class
43 */
44 var $fileFactory = array( 'UnregisteredLocalFile', 'newFromTitle' );
45 var $oldFileFactory = false;
46 var $fileFactoryKey = false, $oldFileFactoryKey = false;
47
48 function __construct( array $info = null ) {
49 // Verify required settings presence
50 if(
51 $info === null
52 || !array_key_exists( 'name', $info )
53 || !array_key_exists( 'backend', $info )
54 ) {
55 throw new MWException( __CLASS__ . " requires an array of options having both 'name' and 'backend' keys.\n" );
56 }
57
58 // Required settings
59 $this->name = $info['name'];
60 if ( $info['backend'] instanceof FileBackend ) {
61 $this->backend = $info['backend']; // useful for testing
62 } else {
63 $this->backend = FileBackendGroup::singleton()->get( $info['backend'] );
64 }
65
66 // Optional settings that can have no value
67 $optionalSettings = array(
68 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
69 'thumbScriptUrl', 'pathDisclosureProtection', 'descriptionCacheExpiry',
70 'scriptExtension'
71 );
72 foreach ( $optionalSettings as $var ) {
73 if ( isset( $info[$var] ) ) {
74 $this->$var = $info[$var];
75 }
76 }
77
78 // Optional settings that have a default
79 $this->initialCapital = isset( $info['initialCapital'] )
80 ? $info['initialCapital']
81 : MWNamespace::isCapitalized( NS_FILE );
82 $this->url = isset( $info['url'] )
83 ? $info['url']
84 : false; // a subclass may set the URL (e.g. ForeignAPIRepo)
85 if ( isset( $info['thumbUrl'] ) ) {
86 $this->thumbUrl = $info['thumbUrl'];
87 } else {
88 $this->thumbUrl = $this->url ? "{$this->url}/thumb" : false;
89 }
90 $this->hashLevels = isset( $info['hashLevels'] )
91 ? $info['hashLevels']
92 : 2;
93 $this->deletedHashLevels = isset( $info['deletedHashLevels'] )
94 ? $info['deletedHashLevels']
95 : $this->hashLevels;
96 $this->transformVia404 = !empty( $info['transformVia404'] );
97 $this->zones = isset( $info['zones'] )
98 ? $info['zones']
99 : array();
100 // Give defaults for the basic zones...
101 foreach ( array( 'public', 'thumb', 'temp', 'deleted' ) as $zone ) {
102 if ( !isset( $this->zones[$zone] ) ) {
103 $this->zones[$zone] = array(
104 'container' => "{$this->name}-{$zone}",
105 'directory' => '' // container root
106 );
107 }
108 }
109 }
110
111 /**
112 * Get the file backend instance. Use this function wisely.
113 *
114 * @return FileBackend
115 */
116 public function getBackend() {
117 return $this->backend;
118 }
119
120 /**
121 * Get an explanatory message if this repo is read-only.
122 * This checks if an administrator disabled writes to the backend.
123 *
124 * @return string|bool Returns false if the repo is not read-only
125 */
126 public function getReadOnlyReason() {
127 return $this->backend->getReadOnlyReason();
128 }
129
130 /**
131 * Check if a single zone or list of zones is defined for usage
132 *
133 * @param $doZones Array Only do a particular zones
134 * @return Status
135 */
136 protected function initZones( $doZones = array() ) {
137 $status = $this->newGood();
138 foreach ( (array)$doZones as $zone ) {
139 $root = $this->getZonePath( $zone );
140 if ( $root === null ) {
141 throw new MWException( "No '$zone' zone defined in the {$this->name} repo." );
142 }
143 }
144 return $status;
145 }
146
147 /**
148 * Take all available measures to prevent web accessibility of new deleted
149 * directories, in case the user has not configured offline storage
150 *
151 * @param $dir string
152 * @return void
153 */
154 protected function initDeletedDir( $dir ) {
155 $this->backend->secure( // prevent web access & dir listings
156 array( 'dir' => $dir, 'noAccess' => true, 'noListing' => true ) );
157 }
158
159 /**
160 * Determine if a string is an mwrepo:// URL
161 *
162 * @param $url string
163 * @return bool
164 */
165 public static function isVirtualUrl( $url ) {
166 return substr( $url, 0, 9 ) == 'mwrepo://';
167 }
168
169 /**
170 * Get a URL referring to this repository, with the private mwrepo protocol.
171 * The suffix, if supplied, is considered to be unencoded, and will be
172 * URL-encoded before being returned.
173 *
174 * @param $suffix string
175 * @return string
176 */
177 public function getVirtualUrl( $suffix = false ) {
178 $path = 'mwrepo://' . $this->name;
179 if ( $suffix !== false ) {
180 $path .= '/' . rawurlencode( $suffix );
181 }
182 return $path;
183 }
184
185 /**
186 * Get the URL corresponding to one of the four basic zones
187 *
188 * @param $zone String: one of: public, deleted, temp, thumb
189 * @return String or false
190 */
191 public function getZoneUrl( $zone ) {
192 switch ( $zone ) {
193 case 'public':
194 return $this->url;
195 case 'temp':
196 return "{$this->url}/temp";
197 case 'deleted':
198 return false; // no public URL
199 case 'thumb':
200 return $this->thumbUrl;
201 default:
202 return false;
203 }
204 }
205
206 /**
207 * Get the backend storage path corresponding to a virtual URL.
208 * Use this function wisely.
209 *
210 * @param $url string
211 * @return string
212 */
213 public function resolveVirtualUrl( $url ) {
214 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
215 throw new MWException( __METHOD__.': unknown protocol' );
216 }
217 $bits = explode( '/', substr( $url, 9 ), 3 );
218 if ( count( $bits ) != 3 ) {
219 throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
220 }
221 list( $repo, $zone, $rel ) = $bits;
222 if ( $repo !== $this->name ) {
223 throw new MWException( __METHOD__.": fetching from a foreign repo is not supported" );
224 }
225 $base = $this->getZonePath( $zone );
226 if ( !$base ) {
227 throw new MWException( __METHOD__.": invalid zone: $zone" );
228 }
229 return $base . '/' . rawurldecode( $rel );
230 }
231
232 /**
233 * The the storage container and base path of a zone
234 *
235 * @param $zone string
236 * @return Array (container, base path) or (null, null)
237 */
238 protected function getZoneLocation( $zone ) {
239 if ( !isset( $this->zones[$zone] ) ) {
240 return array( null, null ); // bogus
241 }
242 return array( $this->zones[$zone]['container'], $this->zones[$zone]['directory'] );
243 }
244
245 /**
246 * Get the storage path corresponding to one of the zones
247 *
248 * @param $zone string
249 * @return string|null Returns null if the zone is not defined
250 */
251 public function getZonePath( $zone ) {
252 list( $container, $base ) = $this->getZoneLocation( $zone );
253 if ( $container === null || $base === null ) {
254 return null;
255 }
256 $backendName = $this->backend->getName();
257 if ( $base != '' ) { // may not be set
258 $base = "/{$base}";
259 }
260 return "mwstore://$backendName/{$container}{$base}";
261 }
262
263 /**
264 * Create a new File object from the local repository
265 *
266 * @param $title Mixed: Title object or string
267 * @param $time Mixed: Time at which the image was uploaded.
268 * If this is specified, the returned object will be an
269 * instance of the repository's old file class instead of a
270 * current file. Repositories not supporting version control
271 * should return false if this parameter is set.
272 * @return File|null A File, or null if passed an invalid Title
273 */
274 public function newFile( $title, $time = false ) {
275 $title = File::normalizeTitle( $title );
276 if ( !$title ) {
277 return null;
278 }
279 if ( $time ) {
280 if ( $this->oldFileFactory ) {
281 return call_user_func( $this->oldFileFactory, $title, $this, $time );
282 } else {
283 return false;
284 }
285 } else {
286 return call_user_func( $this->fileFactory, $title, $this );
287 }
288 }
289
290 /**
291 * Find an instance of the named file created at the specified time
292 * Returns false if the file does not exist. Repositories not supporting
293 * version control should return false if the time is specified.
294 *
295 * @param $title Mixed: Title object or string
296 * @param $options array Associative array of options:
297 * time: requested time for a specific file version, or false for the
298 * current version. An image object will be returned which was
299 * created at the specified time (which may be archived or current).
300 *
301 * ignoreRedirect: If true, do not follow file redirects
302 *
303 * private: If true, return restricted (deleted) files if the current
304 * user is allowed to view them. Otherwise, such files will not
305 * be found.
306 * @return File|bool False on failure
307 */
308 public function findFile( $title, $options = array() ) {
309 $title = File::normalizeTitle( $title );
310 if ( !$title ) {
311 return false;
312 }
313 $time = isset( $options['time'] ) ? $options['time'] : false;
314 # First try the current version of the file to see if it precedes the timestamp
315 $img = $this->newFile( $title );
316 if ( !$img ) {
317 return false;
318 }
319 if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
320 return $img;
321 }
322 # Now try an old version of the file
323 if ( $time !== false ) {
324 $img = $this->newFile( $title, $time );
325 if ( $img && $img->exists() ) {
326 if ( !$img->isDeleted( File::DELETED_FILE ) ) {
327 return $img; // always OK
328 } elseif ( !empty( $options['private'] ) && $img->userCan( File::DELETED_FILE ) ) {
329 return $img;
330 }
331 }
332 }
333
334 # Now try redirects
335 if ( !empty( $options['ignoreRedirect'] ) ) {
336 return false;
337 }
338 $redir = $this->checkRedirect( $title );
339 if ( $redir && $title->getNamespace() == NS_FILE) {
340 $img = $this->newFile( $redir );
341 if ( !$img ) {
342 return false;
343 }
344 if ( $img->exists() ) {
345 $img->redirectedFrom( $title->getDBkey() );
346 return $img;
347 }
348 }
349 return false;
350 }
351
352 /**
353 * Find many files at once.
354 *
355 * @param $items array An array of titles, or an array of findFile() options with
356 * the "title" option giving the title. Example:
357 *
358 * $findItem = array( 'title' => $title, 'private' => true );
359 * $findBatch = array( $findItem );
360 * $repo->findFiles( $findBatch );
361 * @return array
362 */
363 public function findFiles( array $items ) {
364 $result = array();
365 foreach ( $items as $item ) {
366 if ( is_array( $item ) ) {
367 $title = $item['title'];
368 $options = $item;
369 unset( $options['title'] );
370 } else {
371 $title = $item;
372 $options = array();
373 }
374 $file = $this->findFile( $title, $options );
375 if ( $file ) {
376 $result[$file->getTitle()->getDBkey()] = $file;
377 }
378 }
379 return $result;
380 }
381
382 /**
383 * Find an instance of the file with this key, created at the specified time
384 * Returns false if the file does not exist. Repositories not supporting
385 * version control should return false if the time is specified.
386 *
387 * @param $sha1 String base 36 SHA-1 hash
388 * @param $options array Option array, same as findFile().
389 * @return File|bool False on failure
390 */
391 public function findFileFromKey( $sha1, $options = array() ) {
392 $time = isset( $options['time'] ) ? $options['time'] : false;
393 # First try to find a matching current version of a file...
394 if ( $this->fileFactoryKey ) {
395 $img = call_user_func( $this->fileFactoryKey, $sha1, $this, $time );
396 } else {
397 return false; // find-by-sha1 not supported
398 }
399 if ( $img && $img->exists() ) {
400 return $img;
401 }
402 # Now try to find a matching old version of a file...
403 if ( $time !== false && $this->oldFileFactoryKey ) { // find-by-sha1 supported?
404 $img = call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
405 if ( $img && $img->exists() ) {
406 if ( !$img->isDeleted( File::DELETED_FILE ) ) {
407 return $img; // always OK
408 } elseif ( !empty( $options['private'] ) && $img->userCan( File::DELETED_FILE ) ) {
409 return $img;
410 }
411 }
412 }
413 return false;
414 }
415
416 /**
417 * Get an array or iterator of file objects for files that have a given
418 * SHA-1 content hash.
419 *
420 * STUB
421 * @return array
422 */
423 public function findBySha1( $hash ) {
424 return array();
425 }
426
427 /**
428 * Get the public root URL of the repository
429 *
430 * @return string
431 */
432 public function getRootUrl() {
433 return $this->url;
434 }
435
436 /**
437 * Get the URL of thumb.php
438 *
439 * @return string
440 */
441 public function getThumbScriptUrl() {
442 return $this->thumbScriptUrl;
443 }
444
445 /**
446 * Returns true if the repository can transform files via a 404 handler
447 *
448 * @return bool
449 */
450 public function canTransformVia404() {
451 return $this->transformVia404;
452 }
453
454 /**
455 * Get the name of an image from its title object
456 *
457 * @param $title Title
458 * @return String
459 */
460 public function getNameFromTitle( Title $title ) {
461 global $wgContLang;
462 if ( $this->initialCapital != MWNamespace::isCapitalized( NS_FILE ) ) {
463 $name = $title->getUserCaseDBKey();
464 if ( $this->initialCapital ) {
465 $name = $wgContLang->ucfirst( $name );
466 }
467 } else {
468 $name = $title->getDBkey();
469 }
470 return $name;
471 }
472
473 /**
474 * Get the public zone root storage directory of the repository
475 *
476 * @return string
477 */
478 public function getRootDirectory() {
479 return $this->getZonePath( 'public' );
480 }
481
482 /**
483 * Get a relative path including trailing slash, e.g. f/fa/
484 * If the repo is not hashed, returns an empty string
485 *
486 * @param $name string Name of file
487 * @return string
488 */
489 public function getHashPath( $name ) {
490 return self::getHashPathForLevel( $name, $this->hashLevels );
491 }
492
493 /**
494 * Get a relative path including trailing slash, e.g. f/fa/
495 * If the repo is not hashed, returns an empty string
496 *
497 * @param $suffix string Basename of file from FileRepo::storeTemp()
498 * @return string
499 */
500 public function getTempHashPath( $suffix ) {
501 $parts = explode( '!', $suffix, 2 ); // format is <timestamp>!<name> or just <name>
502 $name = isset( $parts[1] ) ? $parts[1] : $suffix; // hash path is not based on timestamp
503 return self::getHashPathForLevel( $name, $this->hashLevels );
504 }
505
506 /**
507 * @param $name
508 * @param $levels
509 * @return string
510 */
511 protected static function getHashPathForLevel( $name, $levels ) {
512 if ( $levels == 0 ) {
513 return '';
514 } else {
515 $hash = md5( $name );
516 $path = '';
517 for ( $i = 1; $i <= $levels; $i++ ) {
518 $path .= substr( $hash, 0, $i ) . '/';
519 }
520 return $path;
521 }
522 }
523
524 /**
525 * Get the number of hash directory levels
526 *
527 * @return integer
528 */
529 public function getHashLevels() {
530 return $this->hashLevels;
531 }
532
533 /**
534 * Get the name of this repository, as specified by $info['name]' to the constructor
535 *
536 * @return string
537 */
538 public function getName() {
539 return $this->name;
540 }
541
542 /**
543 * Make an url to this repo
544 *
545 * @param $query mixed Query string to append
546 * @param $entry string Entry point; defaults to index
547 * @return string|bool False on failure
548 */
549 public function makeUrl( $query = '', $entry = 'index' ) {
550 if ( isset( $this->scriptDirUrl ) ) {
551 $ext = isset( $this->scriptExtension ) ? $this->scriptExtension : '.php';
552 return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}{$ext}", $query );
553 }
554 return false;
555 }
556
557 /**
558 * Get the URL of an image description page. May return false if it is
559 * unknown or not applicable. In general this should only be called by the
560 * File class, since it may return invalid results for certain kinds of
561 * repositories. Use File::getDescriptionUrl() in user code.
562 *
563 * In particular, it uses the article paths as specified to the repository
564 * constructor, whereas local repositories use the local Title functions.
565 *
566 * @param $name string
567 * @return string
568 */
569 public function getDescriptionUrl( $name ) {
570 $encName = wfUrlencode( $name );
571 if ( !is_null( $this->descBaseUrl ) ) {
572 # "http://example.com/wiki/Image:"
573 return $this->descBaseUrl . $encName;
574 }
575 if ( !is_null( $this->articleUrl ) ) {
576 # "http://example.com/wiki/$1"
577 #
578 # We use "Image:" as the canonical namespace for
579 # compatibility across all MediaWiki versions.
580 return str_replace( '$1',
581 "Image:$encName", $this->articleUrl );
582 }
583 if ( !is_null( $this->scriptDirUrl ) ) {
584 # "http://example.com/w"
585 #
586 # We use "Image:" as the canonical namespace for
587 # compatibility across all MediaWiki versions,
588 # and just sort of hope index.php is right. ;)
589 return $this->makeUrl( "title=Image:$encName" );
590 }
591 return false;
592 }
593
594 /**
595 * Get the URL of the content-only fragment of the description page. For
596 * MediaWiki this means action=render. This should only be called by the
597 * repository's file class, since it may return invalid results. User code
598 * should use File::getDescriptionText().
599 *
600 * @param $name String: name of image to fetch
601 * @param $lang String: language to fetch it in, if any.
602 * @return string
603 */
604 public function getDescriptionRenderUrl( $name, $lang = null ) {
605 $query = 'action=render';
606 if ( !is_null( $lang ) ) {
607 $query .= '&uselang=' . $lang;
608 }
609 if ( isset( $this->scriptDirUrl ) ) {
610 return $this->makeUrl(
611 'title=' .
612 wfUrlencode( 'Image:' . $name ) .
613 "&$query" );
614 } else {
615 $descUrl = $this->getDescriptionUrl( $name );
616 if ( $descUrl ) {
617 return wfAppendQuery( $descUrl, $query );
618 } else {
619 return false;
620 }
621 }
622 }
623
624 /**
625 * Get the URL of the stylesheet to apply to description pages
626 *
627 * @return string|bool False on failure
628 */
629 public function getDescriptionStylesheetUrl() {
630 if ( isset( $this->scriptDirUrl ) ) {
631 return $this->makeUrl( 'title=MediaWiki:Filepage.css&' .
632 wfArrayToCGI( Skin::getDynamicStylesheetQuery() ) );
633 }
634 return false;
635 }
636
637 /**
638 * Store a file to a given destination.
639 *
640 * @param $srcPath String: source FS path, storage path, or virtual URL
641 * @param $dstZone String: destination zone
642 * @param $dstRel String: destination relative path
643 * @param $flags Integer: bitwise combination of the following flags:
644 * self::DELETE_SOURCE Delete the source file after upload
645 * self::OVERWRITE Overwrite an existing destination file instead of failing
646 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
647 * same contents as the source
648 * self::SKIP_LOCKING Skip any file locking when doing the store
649 * @return FileRepoStatus
650 */
651 public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
652 $this->assertWritableRepo(); // fail out if read-only
653
654 $status = $this->storeBatch( array( array( $srcPath, $dstZone, $dstRel ) ), $flags );
655 if ( $status->successCount == 0 ) {
656 $status->ok = false;
657 }
658
659 return $status;
660 }
661
662 /**
663 * Store a batch of files
664 *
665 * @param $triplets Array: (src, dest zone, dest rel) triplets as per store()
666 * @param $flags Integer: bitwise combination of the following flags:
667 * self::DELETE_SOURCE Delete the source file after upload
668 * self::OVERWRITE Overwrite an existing destination file instead of failing
669 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
670 * same contents as the source
671 * self::SKIP_LOCKING Skip any file locking when doing the store
672 * @return FileRepoStatus
673 */
674 public function storeBatch( array $triplets, $flags = 0 ) {
675 $this->assertWritableRepo(); // fail out if read-only
676
677 $status = $this->newGood();
678 $backend = $this->backend; // convenience
679
680 $operations = array();
681 $sourceFSFilesToDelete = array(); // cleanup for disk source files
682 // Validate each triplet and get the store operation...
683 foreach ( $triplets as $triplet ) {
684 list( $srcPath, $dstZone, $dstRel ) = $triplet;
685 wfDebug( __METHOD__
686 . "( \$src='$srcPath', \$dstZone='$dstZone', \$dstRel='$dstRel' )\n"
687 );
688
689 // Resolve destination path
690 $root = $this->getZonePath( $dstZone );
691 if ( !$root ) {
692 throw new MWException( "Invalid zone: $dstZone" );
693 }
694 if ( !$this->validateFilename( $dstRel ) ) {
695 throw new MWException( 'Validation error in $dstRel' );
696 }
697 $dstPath = "$root/$dstRel";
698 $dstDir = dirname( $dstPath );
699 // Create destination directories for this triplet
700 if ( !$backend->prepare( array( 'dir' => $dstDir ) )->isOK() ) {
701 return $this->newFatal( 'directorycreateerror', $dstDir );
702 }
703
704 if ( $dstZone == 'deleted' ) {
705 $this->initDeletedDir( $dstDir );
706 }
707
708 // Resolve source to a storage path if virtual
709 if ( self::isVirtualUrl( $srcPath ) ) {
710 $srcPath = $this->resolveVirtualUrl( $srcPath );
711 }
712
713 // Get the appropriate file operation
714 if ( FileBackend::isStoragePath( $srcPath ) ) {
715 $opName = ( $flags & self::DELETE_SOURCE ) ? 'move' : 'copy';
716 } else {
717 $opName = 'store';
718 if ( $flags & self::DELETE_SOURCE ) {
719 $sourceFSFilesToDelete[] = $srcPath;
720 }
721 }
722 $operations[] = array(
723 'op' => $opName,
724 'src' => $srcPath,
725 'dst' => $dstPath,
726 'overwrite' => $flags & self::OVERWRITE,
727 'overwriteSame' => $flags & self::OVERWRITE_SAME,
728 );
729 }
730
731 // Execute the store operation for each triplet
732 $opts = array( 'force' => true );
733 if ( $flags & self::SKIP_LOCKING ) {
734 $opts['nonLocking'] = true;
735 }
736 $status->merge( $backend->doOperations( $operations, $opts ) );
737 // Cleanup for disk source files...
738 foreach ( $sourceFSFilesToDelete as $file ) {
739 wfSuppressWarnings();
740 unlink( $file ); // FS cleanup
741 wfRestoreWarnings();
742 }
743
744 return $status;
745 }
746
747 /**
748 * Deletes a batch of files.
749 * Each file can be a (zone, rel) pair, virtual url, storage path.
750 * It will try to delete each file, but ignores any errors that may occur.
751 *
752 * @param $pairs array List of files to delete
753 * @param $flags Integer: bitwise combination of the following flags:
754 * self::SKIP_LOCKING Skip any file locking when doing the deletions
755 * @return FileRepoStatus
756 */
757 public function cleanupBatch( array $files, $flags = 0 ) {
758 $this->assertWritableRepo(); // fail out if read-only
759
760 $status = $this->newGood();
761
762 $operations = array();
763 foreach ( $files as $path ) {
764 if ( is_array( $path ) ) {
765 // This is a pair, extract it
766 list( $zone, $rel ) = $path;
767 $path = $this->getZonePath( $zone ) . "/$rel";
768 } else {
769 // Resolve source to a storage path if virtual
770 if ( self::isVirtualUrl( $path ) ) {
771 $path = $this->resolveVirtualUrl( $path );
772 }
773 }
774 $operations[] = array( 'op' => 'delete', 'src' => $path );
775 }
776 // Actually delete files from storage...
777 $opts = array( 'force' => true );
778 if ( $flags & self::SKIP_LOCKING ) {
779 $opts['nonLocking'] = true;
780 }
781 $status->merge( $this->backend->doOperations( $operations, $opts ) );
782
783 return $status;
784 }
785
786 /**
787 * Import a file from the local file system into the repo.
788 * This does no locking nor journaling and overrides existing files.
789 * This is intended for copying generated thumbnails into the repo.
790 *
791 * @param $src string File system path
792 * @param $dst string Virtual URL or storage path
793 * @return FileRepoStatus
794 */
795 final public function quickImport( $src, $dst ) {
796 return $this->quickImportBatch( array( array( $src, $dst ) ) );
797 }
798
799 /**
800 * Purge a file from the repo. This does no locking nor journaling.
801 * This is intended for purging thumbnail.
802 *
803 * @param $path string Virtual URL or storage path
804 * @return FileRepoStatus
805 */
806 final public function quickPurge( $path ) {
807 return $this->quickPurgeBatch( array( $path ) );
808 }
809
810 /**
811 * Import a batch of files from the local file system into the repo.
812 * This does no locking nor journaling and overrides existing files.
813 * This is intended for copying generated thumbnails into the repo.
814 *
815 * @param $src Array List of tuples (file system path, virtual URL or storage path)
816 * @return FileRepoStatus
817 */
818 public function quickImportBatch( array $pairs ) {
819 $this->assertWritableRepo(); // fail out if read-only
820
821 $status = $this->newGood();
822 $operations = array();
823 foreach ( $pairs as $pair ) {
824 list ( $src, $dst ) = $pair;
825 $operations[] = array(
826 'op' => 'store',
827 'src' => $src,
828 'dst' => $this->resolveToStoragePath( $dst ),
829 'overwrite' => true
830 );
831 $this->backend->prepare( array( 'dir' => dirname( $dst ) ) );
832 }
833 $status->merge( $this->backend->doOperations( $operations,
834 array( 'force' => 1, 'nonLocking' => 1, 'allowStale' => 1, 'nonJournaled' => 1 )
835 ) );
836
837 return $status;
838 }
839
840 /**
841 * Purge a batch of files from the repo. This does no locking nor journaling.
842 * This is intended for purging thumbnails.
843 *
844 * @param $path Array List of virtual URLs or storage paths
845 * @return FileRepoStatus
846 */
847 public function quickPurgeBatch( array $paths ) {
848 $this->assertWritableRepo(); // fail out if read-only
849
850 $status = $this->newGood();
851 $operations = array();
852 foreach ( $paths as $path ) {
853 $operations[] = array(
854 'op' => 'delete',
855 'src' => $this->resolveToStoragePath( $path ),
856 'ignoreMissingSource' => true
857 );
858 }
859 $status->merge( $this->backend->doOperations( $operations,
860 array( 'force' => 1, 'nonLocking' => 1, 'allowStale' => 1, 'nonJournaled' => 1 )
861 ) );
862
863 return $status;
864 }
865
866 /**
867 * Pick a random name in the temp zone and store a file to it.
868 * Returns a FileRepoStatus object with the file Virtual URL in the value,
869 * file can later be disposed using FileRepo::freeTemp().
870 *
871 * @param $originalName String: the base name of the file as specified
872 * by the user. The file extension will be maintained.
873 * @param $srcPath String: the current location of the file.
874 * @return FileRepoStatus object with the URL in the value.
875 */
876 public function storeTemp( $originalName, $srcPath ) {
877 $this->assertWritableRepo(); // fail out if read-only
878
879 $date = gmdate( "YmdHis" );
880 $hashPath = $this->getHashPath( $originalName );
881 $dstRel = "{$hashPath}{$date}!{$originalName}";
882 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
883
884 $result = $this->store( $srcPath, 'temp', $dstRel, self::SKIP_LOCKING );
885 $result->value = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
886
887 return $result;
888 }
889
890 /**
891 * Concatenate a list of files into a target file location.
892 *
893 * @param $srcPaths Array Ordered list of source virtual URLs/storage paths
894 * @param $dstPath String Target file system path
895 * @param $flags Integer: bitwise combination of the following flags:
896 * self::DELETE_SOURCE Delete the source files
897 * @return FileRepoStatus
898 */
899 public function concatenate( array $srcPaths, $dstPath, $flags = 0 ) {
900 $this->assertWritableRepo(); // fail out if read-only
901
902 $status = $this->newGood();
903
904 $sources = array();
905 $deleteOperations = array(); // post-concatenate ops
906 foreach ( $srcPaths as $srcPath ) {
907 // Resolve source to a storage path if virtual
908 $source = $this->resolveToStoragePath( $srcPath );
909 $sources[] = $source; // chunk to merge
910 if ( $flags & self::DELETE_SOURCE ) {
911 $deleteOperations[] = array( 'op' => 'delete', 'src' => $source );
912 }
913 }
914
915 // Concatenate the chunks into one FS file
916 $params = array( 'srcs' => $sources, 'dst' => $dstPath );
917 $status->merge( $this->backend->concatenate( $params ) );
918 if ( !$status->isOK() ) {
919 return $status;
920 }
921
922 // Delete the sources if required
923 if ( $deleteOperations ) {
924 $opts = array( 'force' => true );
925 $status->merge( $this->backend->doOperations( $deleteOperations, $opts ) );
926 }
927
928 // Make sure status is OK, despite any $deleteOperations fatals
929 $status->setResult( true );
930
931 return $status;
932 }
933
934 /**
935 * Remove a temporary file or mark it for garbage collection
936 *
937 * @param $virtualUrl String: the virtual URL returned by FileRepo::storeTemp()
938 * @return Boolean: true on success, false on failure
939 */
940 public function freeTemp( $virtualUrl ) {
941 $this->assertWritableRepo(); // fail out if read-only
942
943 $temp = "mwrepo://{$this->name}/temp";
944 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
945 wfDebug( __METHOD__.": Invalid temp virtual URL\n" );
946 return false;
947 }
948 $path = $this->resolveVirtualUrl( $virtualUrl );
949
950 return $this->cleanupBatch( array( $path ), self::SKIP_LOCKING )->isOK();
951 }
952
953 /**
954 * Copy or move a file either from a storage path, virtual URL,
955 * or FS path, into this repository at the specified destination location.
956 *
957 * Returns a FileRepoStatus object. On success, the value contains "new" or
958 * "archived", to indicate whether the file was new with that name.
959 *
960 * @param $srcPath String: the source FS path, storage path, or URL
961 * @param $dstRel String: the destination relative path
962 * @param $archiveRel String: the relative path where the existing file is to
963 * be archived, if there is one. Relative to the public zone root.
964 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
965 * that the source file should be deleted if possible
966 * @return FileRepoStatus
967 */
968 public function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 ) {
969 $this->assertWritableRepo(); // fail out if read-only
970
971 $status = $this->publishBatch( array( array( $srcPath, $dstRel, $archiveRel ) ), $flags );
972 if ( $status->successCount == 0 ) {
973 $status->ok = false;
974 }
975 if ( isset( $status->value[0] ) ) {
976 $status->value = $status->value[0];
977 } else {
978 $status->value = false;
979 }
980
981 return $status;
982 }
983
984 /**
985 * Publish a batch of files
986 *
987 * @param $triplets Array: (source, dest, archive) triplets as per publish()
988 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
989 * that the source files should be deleted if possible
990 * @return FileRepoStatus
991 */
992 public function publishBatch( array $triplets, $flags = 0 ) {
993 $this->assertWritableRepo(); // fail out if read-only
994
995 $backend = $this->backend; // convenience
996 // Try creating directories
997 $status = $this->initZones( 'public' );
998 if ( !$status->isOK() ) {
999 return $status;
1000 }
1001
1002 $status = $this->newGood( array() );
1003
1004 $operations = array();
1005 $sourceFSFilesToDelete = array(); // cleanup for disk source files
1006 // Validate each triplet and get the store operation...
1007 foreach ( $triplets as $i => $triplet ) {
1008 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
1009 // Resolve source to a storage path if virtual
1010 if ( $this->isVirtualUrl( $srcPath ) ) {
1011 $srcPath = $this->resolveVirtualUrl( $srcPath );
1012 }
1013 if ( !$this->validateFilename( $dstRel ) ) {
1014 throw new MWException( 'Validation error in $dstRel' );
1015 }
1016 if ( !$this->validateFilename( $archiveRel ) ) {
1017 throw new MWException( 'Validation error in $archiveRel' );
1018 }
1019
1020 $publicRoot = $this->getZonePath( 'public' );
1021 $dstPath = "$publicRoot/$dstRel";
1022 $archivePath = "$publicRoot/$archiveRel";
1023
1024 $dstDir = dirname( $dstPath );
1025 $archiveDir = dirname( $archivePath );
1026 // Abort immediately on directory creation errors since they're likely to be repetitive
1027 if ( !$backend->prepare( array( 'dir' => $dstDir ) )->isOK() ) {
1028 return $this->newFatal( 'directorycreateerror', $dstDir );
1029 }
1030 if ( !$backend->prepare( array( 'dir' => $archiveDir ) )->isOK() ) {
1031 return $this->newFatal( 'directorycreateerror', $archiveDir );
1032 }
1033
1034 // Archive destination file if it exists
1035 if ( $backend->fileExists( array( 'src' => $dstPath ) ) ) {
1036 // Check if the archive file exists
1037 // This is a sanity check to avoid data loss. In UNIX, the rename primitive
1038 // unlinks the destination file if it exists. DB-based synchronisation in
1039 // publishBatch's caller should prevent races. In Windows there's no
1040 // problem because the rename primitive fails if the destination exists.
1041 if ( $backend->fileExists( array( 'src' => $archivePath ) ) ) {
1042 $operations[] = array( 'op' => 'null' );
1043 continue;
1044 } else {
1045 $operations[] = array(
1046 'op' => 'move',
1047 'src' => $dstPath,
1048 'dst' => $archivePath
1049 );
1050 }
1051 $status->value[$i] = 'archived';
1052 } else {
1053 $status->value[$i] = 'new';
1054 }
1055 // Copy (or move) the source file to the destination
1056 if ( FileBackend::isStoragePath( $srcPath ) ) {
1057 if ( $flags & self::DELETE_SOURCE ) {
1058 $operations[] = array(
1059 'op' => 'move',
1060 'src' => $srcPath,
1061 'dst' => $dstPath
1062 );
1063 } else {
1064 $operations[] = array(
1065 'op' => 'copy',
1066 'src' => $srcPath,
1067 'dst' => $dstPath
1068 );
1069 }
1070 } else { // FS source path
1071 $operations[] = array(
1072 'op' => 'store',
1073 'src' => $srcPath,
1074 'dst' => $dstPath
1075 );
1076 if ( $flags & self::DELETE_SOURCE ) {
1077 $sourceFSFilesToDelete[] = $srcPath;
1078 }
1079 }
1080 }
1081
1082 // Execute the operations for each triplet
1083 $opts = array( 'force' => true );
1084 $status->merge( $backend->doOperations( $operations, $opts ) );
1085 // Cleanup for disk source files...
1086 foreach ( $sourceFSFilesToDelete as $file ) {
1087 wfSuppressWarnings();
1088 unlink( $file ); // FS cleanup
1089 wfRestoreWarnings();
1090 }
1091
1092 return $status;
1093 }
1094
1095 /**
1096 * Deletes a directory if empty
1097 *
1098 * @param $dir string Virtual URL (or storage path) of directory to clean
1099 * @return Status
1100 */
1101 public function cleanDir( $dir ) {
1102 $this->assertWritableRepo(); // fail out if read-only
1103
1104 $status = $this->newGood();
1105 $status->merge( $this->backend->clean(
1106 array( 'dir' => $this->resolveToStoragePath( $dir ) ) ) );
1107
1108 return $status;
1109 }
1110
1111 /**
1112 * Checks existence of a a file
1113 *
1114 * @param $file string Virtual URL (or storage path) of file to check
1115 * @return bool
1116 */
1117 public function fileExists( $file ) {
1118 $result = $this->fileExistsBatch( array( $file ) );
1119 return $result[0];
1120 }
1121
1122 /**
1123 * Checks existence of an array of files.
1124 *
1125 * @param $files Array: Virtual URLs (or storage paths) of files to check
1126 * @return array|bool Either array of files and existence flags, or false
1127 */
1128 public function fileExistsBatch( array $files ) {
1129 $result = array();
1130 foreach ( $files as $key => $file ) {
1131 if ( self::isVirtualUrl( $file ) ) {
1132 $file = $this->resolveVirtualUrl( $file );
1133 }
1134 $result[$key] = $this->backend->fileExists( array( 'src' => $file ) );
1135 }
1136 return $result;
1137 }
1138
1139 /**
1140 * Move a file to the deletion archive.
1141 * If no valid deletion archive exists, this may either delete the file
1142 * or throw an exception, depending on the preference of the repository
1143 *
1144 * @param $srcRel Mixed: relative path for the file to be deleted
1145 * @param $archiveRel Mixed: relative path for the archive location.
1146 * Relative to a private archive directory.
1147 * @return FileRepoStatus object
1148 */
1149 public function delete( $srcRel, $archiveRel ) {
1150 $this->assertWritableRepo(); // fail out if read-only
1151
1152 return $this->deleteBatch( array( array( $srcRel, $archiveRel ) ) );
1153 }
1154
1155 /**
1156 * Move a group of files to the deletion archive.
1157 *
1158 * If no valid deletion archive is configured, this may either delete the
1159 * file or throw an exception, depending on the preference of the repository.
1160 *
1161 * The overwrite policy is determined by the repository -- currently LocalRepo
1162 * assumes a naming scheme in the deleted zone based on content hash, as
1163 * opposed to the public zone which is assumed to be unique.
1164 *
1165 * @param $sourceDestPairs Array of source/destination pairs. Each element
1166 * is a two-element array containing the source file path relative to the
1167 * public root in the first element, and the archive file path relative
1168 * to the deleted zone root in the second element.
1169 * @return FileRepoStatus
1170 */
1171 public function deleteBatch( array $sourceDestPairs ) {
1172 $this->assertWritableRepo(); // fail out if read-only
1173
1174 // Try creating directories
1175 $status = $this->initZones( array( 'public', 'deleted' ) );
1176 if ( !$status->isOK() ) {
1177 return $status;
1178 }
1179
1180 $status = $this->newGood();
1181
1182 $backend = $this->backend; // convenience
1183 $operations = array();
1184 // Validate filenames and create archive directories
1185 foreach ( $sourceDestPairs as $pair ) {
1186 list( $srcRel, $archiveRel ) = $pair;
1187 if ( !$this->validateFilename( $srcRel ) ) {
1188 throw new MWException( __METHOD__.':Validation error in $srcRel' );
1189 } elseif ( !$this->validateFilename( $archiveRel ) ) {
1190 throw new MWException( __METHOD__.':Validation error in $archiveRel' );
1191 }
1192
1193 $publicRoot = $this->getZonePath( 'public' );
1194 $srcPath = "{$publicRoot}/$srcRel";
1195
1196 $deletedRoot = $this->getZonePath( 'deleted' );
1197 $archivePath = "{$deletedRoot}/{$archiveRel}";
1198 $archiveDir = dirname( $archivePath ); // does not touch FS
1199
1200 // Create destination directories
1201 if ( !$backend->prepare( array( 'dir' => $archiveDir ) )->isOK() ) {
1202 return $this->newFatal( 'directorycreateerror', $archiveDir );
1203 }
1204 $this->initDeletedDir( $archiveDir );
1205
1206 $operations[] = array(
1207 'op' => 'move',
1208 'src' => $srcPath,
1209 'dst' => $archivePath,
1210 // We may have 2+ identical files being deleted,
1211 // all of which will map to the same destination file
1212 'overwriteSame' => true // also see bug 31792
1213 );
1214 }
1215
1216 // Move the files by execute the operations for each pair.
1217 // We're now committed to returning an OK result, which will
1218 // lead to the files being moved in the DB also.
1219 $opts = array( 'force' => true );
1220 $status->merge( $backend->doOperations( $operations, $opts ) );
1221
1222 return $status;
1223 }
1224
1225 /**
1226 * Delete files in the deleted directory if they are not referenced in the filearchive table
1227 *
1228 * STUB
1229 */
1230 public function cleanupDeletedBatch( array $storageKeys ) {
1231 $this->assertWritableRepo();
1232 }
1233
1234 /**
1235 * Get a relative path for a deletion archive key,
1236 * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
1237 *
1238 * @return string
1239 */
1240 public function getDeletedHashPath( $key ) {
1241 $path = '';
1242 for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) {
1243 $path .= $key[$i] . '/';
1244 }
1245 return $path;
1246 }
1247
1248 /**
1249 * If a path is a virtual URL, resolve it to a storage path.
1250 * Otherwise, just return the path as it is.
1251 *
1252 * @param $path string
1253 * @return string
1254 * @throws MWException
1255 */
1256 protected function resolveToStoragePath( $path ) {
1257 if ( $this->isVirtualUrl( $path ) ) {
1258 return $this->resolveVirtualUrl( $path );
1259 }
1260 return $path;
1261 }
1262
1263 /**
1264 * Get a local FS copy of a file with a given virtual URL/storage path.
1265 * Temporary files may be purged when the file object falls out of scope.
1266 *
1267 * @param $virtualUrl string
1268 * @return TempFSFile|null Returns null on failure
1269 */
1270 public function getLocalCopy( $virtualUrl ) {
1271 $path = $this->resolveToStoragePath( $virtualUrl );
1272 return $this->backend->getLocalCopy( array( 'src' => $path ) );
1273 }
1274
1275 /**
1276 * Get a local FS file with a given virtual URL/storage path.
1277 * The file is either an original or a copy. It should not be changed.
1278 * Temporary files may be purged when the file object falls out of scope.
1279 *
1280 * @param $virtualUrl string
1281 * @return FSFile|null Returns null on failure.
1282 */
1283 public function getLocalReference( $virtualUrl ) {
1284 $path = $this->resolveToStoragePath( $virtualUrl );
1285 return $this->backend->getLocalReference( array( 'src' => $path ) );
1286 }
1287
1288 /**
1289 * Get properties of a file with a given virtual URL/storage path.
1290 * Properties should ultimately be obtained via FSFile::getProps().
1291 *
1292 * @param $virtualUrl string
1293 * @return Array
1294 */
1295 public function getFileProps( $virtualUrl ) {
1296 $path = $this->resolveToStoragePath( $virtualUrl );
1297 return $this->backend->getFileProps( array( 'src' => $path ) );
1298 }
1299
1300 /**
1301 * Get the timestamp of a file with a given virtual URL/storage path
1302 *
1303 * @param $virtualUrl string
1304 * @return string|bool False on failure
1305 */
1306 public function getFileTimestamp( $virtualUrl ) {
1307 $path = $this->resolveToStoragePath( $virtualUrl );
1308 return $this->backend->getFileTimestamp( array( 'src' => $path ) );
1309 }
1310
1311 /**
1312 * Get the sha1 of a file with a given virtual URL/storage path
1313 *
1314 * @param $virtualUrl string
1315 * @return string|bool
1316 */
1317 public function getFileSha1( $virtualUrl ) {
1318 $path = $this->resolveToStoragePath( $virtualUrl );
1319 $tmpFile = $this->backend->getLocalReference( array( 'src' => $path ) );
1320 if ( !$tmpFile ) {
1321 return false;
1322 }
1323 return $tmpFile->getSha1Base36();
1324 }
1325
1326 /**
1327 * Attempt to stream a file with the given virtual URL/storage path
1328 *
1329 * @param $virtualUrl string
1330 * @param $headers Array Additional HTTP headers to send on success
1331 * @return bool Success
1332 */
1333 public function streamFile( $virtualUrl, $headers = array() ) {
1334 $path = $this->resolveToStoragePath( $virtualUrl );
1335 $params = array( 'src' => $path, 'headers' => $headers );
1336 return $this->backend->streamFile( $params )->isOK();
1337 }
1338
1339 /**
1340 * Call a callback function for every public regular file in the repository.
1341 * This only acts on the current version of files, not any old versions.
1342 * May use either the database or the filesystem.
1343 *
1344 * @param $callback Array|string
1345 * @return void
1346 */
1347 public function enumFiles( $callback ) {
1348 $this->enumFilesInStorage( $callback );
1349 }
1350
1351 /**
1352 * Call a callback function for every public file in the repository.
1353 * May use either the database or the filesystem.
1354 *
1355 * @param $callback Array|string
1356 * @return void
1357 */
1358 protected function enumFilesInStorage( $callback ) {
1359 $publicRoot = $this->getZonePath( 'public' );
1360 $numDirs = 1 << ( $this->hashLevels * 4 );
1361 // Use a priori assumptions about directory structure
1362 // to reduce the tree height of the scanning process.
1363 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++ ) {
1364 $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
1365 $path = $publicRoot;
1366 for ( $hexPos = 0; $hexPos < $this->hashLevels; $hexPos++ ) {
1367 $path .= '/' . substr( $hexString, 0, $hexPos + 1 );
1368 }
1369 $iterator = $this->backend->getFileList( array( 'dir' => $path ) );
1370 foreach ( $iterator as $name ) {
1371 // Each item returned is a public file
1372 call_user_func( $callback, "{$path}/{$name}" );
1373 }
1374 }
1375 }
1376
1377 /**
1378 * Determine if a relative path is valid, i.e. not blank or involving directory traveral
1379 *
1380 * @param $filename string
1381 * @return bool
1382 */
1383 public function validateFilename( $filename ) {
1384 if ( strval( $filename ) == '' ) {
1385 return false;
1386 }
1387 return FileBackend::isPathTraversalFree( $filename );
1388 }
1389
1390 /**
1391 * Get a callback function to use for cleaning error message parameters
1392 *
1393 * @return Array
1394 */
1395 function getErrorCleanupFunction() {
1396 switch ( $this->pathDisclosureProtection ) {
1397 case 'none':
1398 case 'simple': // b/c
1399 $callback = array( $this, 'passThrough' );
1400 break;
1401 default: // 'paranoid'
1402 $callback = array( $this, 'paranoidClean' );
1403 }
1404 return $callback;
1405 }
1406
1407 /**
1408 * Path disclosure protection function
1409 *
1410 * @param $param string
1411 * @return string
1412 */
1413 function paranoidClean( $param ) {
1414 return '[hidden]';
1415 }
1416
1417 /**
1418 * Path disclosure protection function
1419 *
1420 * @param $param string
1421 * @return string
1422 */
1423 function passThrough( $param ) {
1424 return $param;
1425 }
1426
1427 /**
1428 * Create a new fatal error
1429 *
1430 * @return FileRepoStatus
1431 */
1432 public function newFatal( $message /*, parameters...*/ ) {
1433 $params = func_get_args();
1434 array_unshift( $params, $this );
1435 return MWInit::callStaticMethod( 'FileRepoStatus', 'newFatal', $params );
1436 }
1437
1438 /**
1439 * Create a new good result
1440 *
1441 * @return FileRepoStatus
1442 */
1443 public function newGood( $value = null ) {
1444 return FileRepoStatus::newGood( $this, $value );
1445 }
1446
1447 /**
1448 * Checks if there is a redirect named as $title. If there is, return the
1449 * title object. If not, return false.
1450 * STUB
1451 *
1452 * @param $title Title of image
1453 * @return Bool
1454 */
1455 public function checkRedirect( Title $title ) {
1456 return false;
1457 }
1458
1459 /**
1460 * Invalidates image redirect cache related to that image
1461 * Doesn't do anything for repositories that don't support image redirects.
1462 *
1463 * STUB
1464 * @param $title Title of image
1465 */
1466 public function invalidateImageRedirect( Title $title ) {}
1467
1468 /**
1469 * Get the human-readable name of the repo
1470 *
1471 * @return string
1472 */
1473 public function getDisplayName() {
1474 // We don't name our own repo, return nothing
1475 if ( $this->isLocal() ) {
1476 return null;
1477 }
1478 // 'shared-repo-name-wikimediacommons' is used when $wgUseInstantCommons = true
1479 return wfMessageFallback( 'shared-repo-name-' . $this->name, 'shared-repo' )->text();
1480 }
1481
1482 /**
1483 * Returns true if this the local file repository.
1484 *
1485 * @return bool
1486 */
1487 public function isLocal() {
1488 return $this->getName() == 'local';
1489 }
1490
1491 /**
1492 * Get a key on the primary cache for this repository.
1493 * Returns false if the repository's cache is not accessible at this site.
1494 * The parameters are the parts of the key, as for wfMemcKey().
1495 *
1496 * STUB
1497 * @return bool
1498 */
1499 public function getSharedCacheKey( /*...*/ ) {
1500 return false;
1501 }
1502
1503 /**
1504 * Get a key for this repo in the local cache domain. These cache keys are
1505 * not shared with remote instances of the repo.
1506 * The parameters are the parts of the key, as for wfMemcKey().
1507 *
1508 * @return string
1509 */
1510 public function getLocalCacheKey( /*...*/ ) {
1511 $args = func_get_args();
1512 array_unshift( $args, 'filerepo', $this->getName() );
1513 return call_user_func_array( 'wfMemcKey', $args );
1514 }
1515
1516 /**
1517 * Get an temporary FileRepo associated with this repo.
1518 * Files will be created in the temp zone of this repo and
1519 * thumbnails in a /temp subdirectory in thumb zone of this repo.
1520 * It will have the same backend as this repo.
1521 *
1522 * @return TempFileRepo
1523 */
1524 public function getTempRepo() {
1525 return new TempFileRepo( array(
1526 'name' => "{$this->name}-temp",
1527 'backend' => $this->backend,
1528 'zones' => array(
1529 'public' => array(
1530 'container' => $this->zones['temp']['container'],
1531 'directory' => $this->zones['temp']['directory']
1532 ),
1533 'thumb' => array(
1534 'container' => $this->zones['thumb']['container'],
1535 'directory' => ( $this->zones['thumb']['directory'] == '' )
1536 ? 'temp'
1537 : $this->zones['thumb']['directory'] . '/temp'
1538 )
1539 ),
1540 'url' => $this->getZoneUrl( 'temp' ),
1541 'thumbUrl' => $this->getZoneUrl( 'thumb' ) . '/temp',
1542 'hashLevels' => $this->hashLevels // performance
1543 ) );
1544 }
1545
1546 /**
1547 * Get an UploadStash associated with this repo.
1548 *
1549 * @return UploadStash
1550 */
1551 public function getUploadStash() {
1552 return new UploadStash( $this );
1553 }
1554
1555 /**
1556 * Throw an exception if this repo is read-only by design.
1557 * This does not and should not check getReadOnlyReason().
1558 *
1559 * @return void
1560 * @throws MWException
1561 */
1562 protected function assertWritableRepo() {}
1563 }
1564
1565 /**
1566 * FileRepo for temporary files created via FileRepo::getTempRepo()
1567 */
1568 class TempFileRepo extends FileRepo {
1569 public function getTempRepo() {
1570 throw new MWException( "Cannot get a temp repo from a temp repo." );
1571 }
1572 }