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