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