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