Removal of unused globals
[lhc/web/wiklou.git] / includes / filerepo / FileRepo.php
1 <?php
2
3 /**
4 * Base class for file repositories
5 * Do not instantiate, use a derived class.
6 * @ingroup FileRepo
7 */
8 abstract class FileRepo {
9 const FILES_ONLY = 1;
10 const DELETE_SOURCE = 1;
11 const OVERWRITE = 2;
12 const OVERWRITE_SAME = 4;
13
14 var $thumbScriptUrl, $transformVia404;
15 var $descBaseUrl, $scriptDirUrl, $scriptExtension, $articleUrl;
16 var $fetchDescription, $initialCapital;
17 var $pathDisclosureProtection = 'paranoid';
18 var $descriptionCacheExpiry, $hashLevels, $url, $thumbUrl;
19
20 /**
21 * Factory functions for creating new files
22 * Override these in the base class
23 */
24 var $fileFactory = false, $oldFileFactory = false;
25 var $fileFactoryKey = false, $oldFileFactoryKey = false;
26
27 function __construct( $info ) {
28 // Required settings
29 $this->name = $info['name'];
30
31 // Optional settings
32 $this->initialCapital = MWNamespace::isCapitalized( NS_FILE );
33 foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
34 'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection',
35 'descriptionCacheExpiry', 'hashLevels', 'url', 'thumbUrl', 'scriptExtension' )
36 as $var )
37 {
38 if ( isset( $info[$var] ) ) {
39 $this->$var = $info[$var];
40 }
41 }
42 $this->transformVia404 = !empty( $info['transformVia404'] );
43 }
44
45 /**
46 * Determine if a string is an mwrepo:// URL
47 */
48 static function isVirtualUrl( $url ) {
49 return substr( $url, 0, 9 ) == 'mwrepo://';
50 }
51
52 /**
53 * Create a new File object from the local repository
54 *
55 * @param $title Mixed: Title object or string
56 * @param $time Mixed: Time at which the image was uploaded.
57 * If this is specified, the returned object will be an
58 * instance of the repository's old file class instead of a
59 * current file. Repositories not supporting version control
60 * should return false if this parameter is set.
61 */
62 function newFile( $title, $time = false ) {
63 if ( !($title instanceof Title) ) {
64 $title = Title::makeTitleSafe( NS_FILE, $title );
65 if ( !is_object( $title ) ) {
66 return null;
67 }
68 }
69 if ( $time ) {
70 if ( $this->oldFileFactory ) {
71 return call_user_func( $this->oldFileFactory, $title, $this, $time );
72 } else {
73 return false;
74 }
75 } else {
76 return call_user_func( $this->fileFactory, $title, $this );
77 }
78 }
79
80 /**
81 * Find an instance of the named file created at the specified time
82 * Returns false if the file does not exist. Repositories not supporting
83 * version control should return false if the time is specified.
84 *
85 * @param $title Mixed: Title object or string
86 * @param $options Associative array of options:
87 * time: requested time for an archived image, or false for the
88 * current version. An image object will be returned which was
89 * created at the specified time.
90 *
91 * ignoreRedirect: If true, do not follow file redirects
92 *
93 * private: If true, return restricted (deleted) files if the current
94 * user is allowed to view them. Otherwise, such files will not
95 * be found.
96 */
97 function findFile( $title, $options = array() ) {
98 if ( !is_array( $options ) ) {
99 // MW 1.15 compat
100 $time = $options;
101 } else {
102 $time = isset( $options['time'] ) ? $options['time'] : false;
103 }
104 if ( !($title instanceof Title) ) {
105 $title = Title::makeTitleSafe( NS_FILE, $title );
106 if ( !is_object( $title ) ) {
107 return false;
108 }
109 }
110 # First try the current version of the file to see if it precedes the timestamp
111 $img = $this->newFile( $title );
112 if ( !$img ) {
113 return false;
114 }
115 if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
116 return $img;
117 }
118 # Now try an old version of the file
119 if ( $time !== false ) {
120 $img = $this->newFile( $title, $time );
121 if ( $img && $img->exists() ) {
122 if ( !$img->isDeleted(File::DELETED_FILE) ) {
123 return $img;
124 } else if ( !empty( $options['private'] ) && $img->userCan(File::DELETED_FILE) ) {
125 return $img;
126 }
127 }
128 }
129
130 # Now try redirects
131 if ( !empty( $options['ignoreRedirect'] ) ) {
132 return false;
133 }
134 $redir = $this->checkRedirect( $title );
135 if( $redir && $redir->getNamespace() == NS_FILE) {
136 $img = $this->newFile( $redir );
137 if( !$img ) {
138 return false;
139 }
140 if( $img->exists() ) {
141 $img->redirectedFrom( $title->getDBkey() );
142 return $img;
143 }
144 }
145 return false;
146 }
147
148 /*
149 * Find many files at once.
150 * @param $items An array of titles, or an array of findFile() options with
151 * the "title" option giving the title. Example:
152 *
153 * $findItem = array( 'title' => $title, 'private' => true );
154 * $findBatch = array( $findItem );
155 * $repo->findFiles( $findBatch );
156 */
157 function findFiles( $items ) {
158 $result = array();
159 foreach ( $items as $index => $item ) {
160 if ( is_array( $item ) ) {
161 $title = $item['title'];
162 $options = $item;
163 unset( $options['title'] );
164 } else {
165 $title = $item;
166 $options = array();
167 }
168 $file = $this->findFile( $title, $options );
169 if ( $file )
170 $result[$file->getTitle()->getDBkey()] = $file;
171 }
172 return $result;
173 }
174
175 /**
176 * Create a new File object from the local repository
177 * @param $sha1 Mixed: SHA-1 key
178 * @param $time Mixed: time at which the image was uploaded.
179 * If this is specified, the returned object will be an
180 * of the repository's old file class instead of a current
181 * file. Repositories not supporting version control should
182 * return false if this parameter is set.
183 */
184 function newFileFromKey( $sha1, $time = false ) {
185 if ( $time ) {
186 if ( $this->oldFileFactoryKey ) {
187 return call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
188 } else {
189 return false;
190 }
191 } else {
192 return call_user_func( $this->fileFactoryKey, $sha1, $this );
193 }
194 }
195
196 /**
197 * Find an instance of the file with this key, created at the specified time
198 * Returns false if the file does not exist. Repositories not supporting
199 * version control should return false if the time is specified.
200 *
201 * @param $sha1 String
202 * @param $options Option array, same as findFile().
203 */
204 function findFileFromKey( $sha1, $options = array() ) {
205 if ( !is_array( $options ) ) {
206 # MW 1.15 compat
207 $time = $options;
208 } else {
209 $time = isset( $options['time'] ) ? $options['time'] : false;
210 }
211
212 # First try the current version of the file to see if it precedes the timestamp
213 $img = $this->newFileFromKey( $sha1 );
214 if ( !$img ) {
215 return false;
216 }
217 if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
218 return $img;
219 }
220 # Now try an old version of the file
221 if ( $time !== false ) {
222 $img = $this->newFileFromKey( $sha1, $time );
223 if ( $img->exists() ) {
224 if ( !$img->isDeleted(File::DELETED_FILE) ) {
225 return $img;
226 } else if ( !empty( $options['private'] ) && $img->userCan(File::DELETED_FILE) ) {
227 return $img;
228 }
229 }
230 }
231 return false;
232 }
233
234 /**
235 * Get the URL of thumb.php
236 */
237 function getThumbScriptUrl() {
238 return $this->thumbScriptUrl;
239 }
240
241 /**
242 * Get the URL corresponding to one of the four basic zones
243 * @param $zone String: one of: public, deleted, temp, thumb
244 * @return String or false
245 */
246 function getZoneUrl( $zone ) {
247 return false;
248 }
249
250 /**
251 * Returns true if the repository can transform files via a 404 handler
252 */
253 function canTransformVia404() {
254 return $this->transformVia404;
255 }
256
257 /**
258 * Get the name of an image from its title object
259 */
260 function getNameFromTitle( $title ) {
261 if ( $this->initialCapital != MWNamespace::isCapitalized( NS_FILE ) ) {
262 global $wgContLang;
263 $name = $title->getUserCaseDBKey();
264 if ( $this->initialCapital ) {
265 $name = $wgContLang->ucfirst( $name );
266 }
267 } else {
268 $name = $title->getDBkey();
269 }
270 return $name;
271 }
272
273 static function getHashPathForLevel( $name, $levels ) {
274 if ( $levels == 0 ) {
275 return '';
276 } else {
277 $hash = md5( $name );
278 $path = '';
279 for ( $i = 1; $i <= $levels; $i++ ) {
280 $path .= substr( $hash, 0, $i ) . '/';
281 }
282 return $path;
283 }
284 }
285
286 /**
287 * Get a relative path including trailing slash, e.g. f/fa/
288 * If the repo is not hashed, returns an empty string
289 */
290 function getHashPath( $name ) {
291 return self::getHashPathForLevel( $name, $this->hashLevels );
292 }
293
294 /**
295 * Get the name of this repository, as specified by $info['name]' to the constructor
296 */
297 function getName() {
298 return $this->name;
299 }
300
301 /**
302 * Make an url to this repo
303 *
304 * @param $query mixed Query string to append
305 * @param $entry string Entry point; defaults to index
306 * @return string
307 */
308 function makeUrl( $query = '', $entry = 'index' ) {
309 $ext = isset( $this->scriptExtension ) ? $this->scriptExtension : '.php';
310 return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}{$ext}?", $query );
311 }
312
313 /**
314 * Get the URL of an image description page. May return false if it is
315 * unknown or not applicable. In general this should only be called by the
316 * File class, since it may return invalid results for certain kinds of
317 * repositories. Use File::getDescriptionUrl() in user code.
318 *
319 * In particular, it uses the article paths as specified to the repository
320 * constructor, whereas local repositories use the local Title functions.
321 */
322 function getDescriptionUrl( $name ) {
323 $encName = wfUrlencode( $name );
324 if ( !is_null( $this->descBaseUrl ) ) {
325 # "http://example.com/wiki/Image:"
326 return $this->descBaseUrl . $encName;
327 }
328 if ( !is_null( $this->articleUrl ) ) {
329 # "http://example.com/wiki/$1"
330 #
331 # We use "Image:" as the canonical namespace for
332 # compatibility across all MediaWiki versions.
333 return str_replace( '$1',
334 "Image:$encName", $this->articleUrl );
335 }
336 if ( !is_null( $this->scriptDirUrl ) ) {
337 # "http://example.com/w"
338 #
339 # We use "Image:" as the canonical namespace for
340 # compatibility across all MediaWiki versions,
341 # and just sort of hope index.php is right. ;)
342 return $this->makeUrl( "title=Image:$encName" );
343 }
344 return false;
345 }
346
347 /**
348 * Get the URL of the content-only fragment of the description page. For
349 * MediaWiki this means action=render. This should only be called by the
350 * repository's file class, since it may return invalid results. User code
351 * should use File::getDescriptionText().
352 * @param $name String: name of image to fetch
353 * @param $lang String: language to fetch it in, if any.
354 */
355 function getDescriptionRenderUrl( $name, $lang = null ) {
356 $query = 'action=render';
357 if ( !is_null( $lang ) ) {
358 $query .= '&uselang=' . $lang;
359 }
360 if ( isset( $this->scriptDirUrl ) ) {
361 return $this->makeUrl(
362 'title=' .
363 wfUrlencode( 'Image:' . $name ) .
364 "&$query" );
365 } else {
366 $descUrl = $this->getDescriptionUrl( $name );
367 if ( $descUrl ) {
368 return wfAppendQuery( $descUrl, $query );
369 } else {
370 return false;
371 }
372 }
373 }
374
375 /**
376 * Get the URL of the stylesheet to apply to description pages
377 * @return string
378 */
379 function getDescriptionStylesheetUrl() {
380 if ( $this->scriptDirUrl ) {
381 return self::makeUrl( 'title=MediaWiki:Filepage.css&' .
382 wfArrayToCGI( Skin::getDynamicStylesheetQuery() ) );
383 }
384 }
385
386 /**
387 * Store a file to a given destination.
388 *
389 * @param $srcPath String: source path or virtual URL
390 * @param $dstZone String: destination zone
391 * @param $dstRel String: destination relative path
392 * @param $flags Integer: bitwise combination of the following flags:
393 * self::DELETE_SOURCE Delete the source file after upload
394 * self::OVERWRITE Overwrite an existing destination file instead of failing
395 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
396 * same contents as the source
397 * @return FileRepoStatus
398 */
399 function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
400 $status = $this->storeBatch( array( array( $srcPath, $dstZone, $dstRel ) ), $flags );
401 if ( $status->successCount == 0 ) {
402 $status->ok = false;
403 }
404 return $status;
405 }
406
407 /**
408 * Store a batch of files
409 *
410 * @param $triplets Array: (src,zone,dest) triplets as per store()
411 * @param $flags Integer: flags as per store
412 */
413 abstract function storeBatch( $triplets, $flags = 0 );
414
415 /**
416 * Pick a random name in the temp zone and store a file to it.
417 * Returns a FileRepoStatus object with the URL in the value.
418 *
419 * @param $originalName String: the base name of the file as specified
420 * by the user. The file extension will be maintained.
421 * @param $srcPath String: the current location of the file.
422 */
423 abstract function storeTemp( $originalName, $srcPath );
424
425
426 /**
427 * Append the contents of the source path to the given file.
428 * @param $srcPath String: location of the source file
429 * @param $toAppendPath String: path to append to.
430 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
431 * that the source file should be deleted if possible
432 * @return mixed Status or false
433 */
434 abstract function append( $srcPath, $toAppendPath, $flags = 0 );
435
436 /**
437 * Remove a temporary file or mark it for garbage collection
438 * @param $virtualUrl String: the virtual URL returned by storeTemp
439 * @return Boolean: true on success, false on failure
440 * STUB
441 */
442 function freeTemp( $virtualUrl ) {
443 return true;
444 }
445
446 /**
447 * Copy or move a file either from the local filesystem or from an mwrepo://
448 * virtual URL, into this repository at the specified destination location.
449 *
450 * Returns a FileRepoStatus object. On success, the value contains "new" or
451 * "archived", to indicate whether the file was new with that name.
452 *
453 * @param $srcPath String: the source path or URL
454 * @param $dstRel String: the destination relative path
455 * @param $archiveRel String: rhe relative path where the existing file is to
456 * be archived, if there is one. Relative to the public zone root.
457 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
458 * that the source file should be deleted if possible
459 */
460 function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 ) {
461 $status = $this->publishBatch( array( array( $srcPath, $dstRel, $archiveRel ) ), $flags );
462 if ( $status->successCount == 0 ) {
463 $status->ok = false;
464 }
465 if ( isset( $status->value[0] ) ) {
466 $status->value = $status->value[0];
467 } else {
468 $status->value = false;
469 }
470 return $status;
471 }
472
473 /**
474 * Publish a batch of files
475 * @param $triplets Array: (source,dest,archive) triplets as per publish()
476 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
477 * that the source files should be deleted if possible
478 */
479 abstract function publishBatch( $triplets, $flags = 0 );
480
481 function fileExists( $file, $flags = 0 ) {
482 $result = $this->fileExistsBatch( array( $file ), $flags );
483 return $result[0];
484 }
485
486 /**
487 * Checks existence of an array of files.
488 *
489 * @param $files Array: URLs (or paths) of files to check
490 * @param $flags Integer: bitwise combination of the following flags:
491 * self::FILES_ONLY Mark file as existing only if it is a file (not directory)
492 * @return Either array of files and existence flags, or false
493 */
494 abstract function fileExistsBatch( $files, $flags = 0 );
495
496 /**
497 * Move a group of files to the deletion archive.
498 *
499 * If no valid deletion archive is configured, this may either delete the
500 * file or throw an exception, depending on the preference of the repository.
501 *
502 * The overwrite policy is determined by the repository -- currently FSRepo
503 * assumes a naming scheme in the deleted zone based on content hash, as
504 * opposed to the public zone which is assumed to be unique.
505 *
506 * @param $sourceDestPairs Array of source/destination pairs. Each element
507 * is a two-element array containing the source file path relative to the
508 * public root in the first element, and the archive file path relative
509 * to the deleted zone root in the second element.
510 * @return FileRepoStatus
511 */
512 abstract function deleteBatch( $sourceDestPairs );
513
514 /**
515 * Move a file to the deletion archive.
516 * If no valid deletion archive exists, this may either delete the file
517 * or throw an exception, depending on the preference of the repository
518 * @param $srcRel Mixed: relative path for the file to be deleted
519 * @param $archiveRel Mixed: relative path for the archive location.
520 * Relative to a private archive directory.
521 * @return FileRepoStatus object
522 */
523 function delete( $srcRel, $archiveRel ) {
524 return $this->deleteBatch( array( array( $srcRel, $archiveRel ) ) );
525 }
526
527 /**
528 * Get properties of a file with a given virtual URL
529 * The virtual URL must refer to this repo
530 * Properties should ultimately be obtained via File::getPropsFromPath()
531 */
532 abstract function getFileProps( $virtualUrl );
533
534 /**
535 * Call a callback function for every file in the repository
536 * May use either the database or the filesystem
537 * STUB
538 */
539 function enumFiles( $callback ) {
540 throw new MWException( 'enumFiles is not supported by ' . get_class( $this ) );
541 }
542
543 /**
544 * Determine if a relative path is valid, i.e. not blank or involving directory traveral
545 */
546 function validateFilename( $filename ) {
547 if ( strval( $filename ) == '' ) {
548 return false;
549 }
550 if ( wfIsWindows() ) {
551 $filename = strtr( $filename, '\\', '/' );
552 }
553 /**
554 * Use the same traversal protection as Title::secureAndSplit()
555 */
556 if ( strpos( $filename, '.' ) !== false &&
557 ( $filename === '.' || $filename === '..' ||
558 strpos( $filename, './' ) === 0 ||
559 strpos( $filename, '../' ) === 0 ||
560 strpos( $filename, '/./' ) !== false ||
561 strpos( $filename, '/../' ) !== false ) )
562 {
563 return false;
564 } else {
565 return true;
566 }
567 }
568
569 /**#@+
570 * Path disclosure protection functions
571 */
572 function paranoidClean( $param ) { return '[hidden]'; }
573 function passThrough( $param ) { return $param; }
574
575 /**
576 * Get a callback function to use for cleaning error message parameters
577 */
578 function getErrorCleanupFunction() {
579 switch ( $this->pathDisclosureProtection ) {
580 case 'none':
581 $callback = array( $this, 'passThrough' );
582 break;
583 default: // 'paranoid'
584 $callback = array( $this, 'paranoidClean' );
585 }
586 return $callback;
587 }
588 /**#@-*/
589
590 /**
591 * Create a new fatal error
592 */
593 function newFatal( $message /*, parameters...*/ ) {
594 $params = func_get_args();
595 array_unshift( $params, $this );
596 return call_user_func_array( array( 'FileRepoStatus', 'newFatal' ), $params );
597 }
598
599 /**
600 * Create a new good result
601 */
602 function newGood( $value = null ) {
603 return FileRepoStatus::newGood( $this, $value );
604 }
605
606 /**
607 * Delete files in the deleted directory if they are not referenced in the filearchive table
608 * STUB
609 */
610 function cleanupDeletedBatch( $storageKeys ) {}
611
612 /**
613 * Checks if there is a redirect named as $title. If there is, return the
614 * title object. If not, return false.
615 * STUB
616 *
617 * @param $title Title of image
618 */
619 function checkRedirect( $title ) {
620 return false;
621 }
622
623 /**
624 * Invalidates image redirect cache related to that image
625 * Doesn't do anything for repositories that don't support image redirects.
626 *
627 * STUB
628 * @param $title Title of image
629 */
630 function invalidateImageRedirect( $title ) {}
631
632 /**
633 * Get an array or iterator of file objects for files that have a given
634 * SHA-1 content hash.
635 *
636 * STUB
637 */
638 function findBySha1( $hash ) {
639 return array();
640 }
641
642 /**
643 * Get the human-readable name of the repo.
644 * @return string
645 */
646 public function getDisplayName() {
647 // We don't name our own repo, return nothing
648 if ( $this->isLocal() ) {
649 return null;
650 }
651 // 'shared-repo-name-wikimediacommons' is used when $wgUseInstantCommons = true
652 $repoName = wfMsg( 'shared-repo-name-' . $this->name );
653 if ( !wfEmptyMsg( 'shared-repo-name-' . $this->name, $repoName ) ) {
654 return $repoName;
655 }
656 return wfMsg( 'shared-repo' );
657 }
658
659 /**
660 * Returns true if this the local file repository.
661 *
662 * @return bool
663 */
664 function isLocal() {
665 return $this->getName() == 'local';
666 }
667
668
669 /**
670 * Get a key on the primary cache for this repository.
671 * Returns false if the repository's cache is not accessible at this site.
672 * The parameters are the parts of the key, as for wfMemcKey().
673 *
674 * STUB
675 */
676 function getSharedCacheKey( /*...*/ ) {
677 return false;
678 }
679
680 /**
681 * Get a key for this repo in the local cache domain. These cache keys are
682 * not shared with remote instances of the repo.
683 * The parameters are the parts of the key, as for wfMemcKey().
684 */
685 function getLocalCacheKey( /*...*/ ) {
686 $args = func_get_args();
687 array_unshift( $args, 'filerepo', $this->getName() );
688 return call_user_func_array( 'wfMemcKey', $args );
689 }
690 }