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