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