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