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