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