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