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