enhance filerepo doc structure
[lhc/web/wiklou.git] / includes / filerepo / file / File.php
1 <?php
2 /**
3 * @defgroup FileAbstraction File abstraction
4 * @ingroup FileRepo
5 *
6 * Represents files in a repository.
7 */
8
9 /**
10 * Base code for files.
11 *
12 * @file
13 * @ingroup FileAbstraction
14 */
15
16 /**
17 * Implements some public methods and some protected utility functions which
18 * are required by multiple child classes. Contains stub functionality for
19 * unimplemented public methods.
20 *
21 * Stub functions which should be overridden are marked with STUB. Some more
22 * concrete functions are also typically overridden by child classes.
23 *
24 * Note that only the repo object knows what its file class is called. You should
25 * never name a file class explictly outside of the repo class. Instead use the
26 * repo's factory functions to generate file objects, for example:
27 *
28 * RepoGroup::singleton()->getLocalRepo()->newFile($title);
29 *
30 * The convenience functions wfLocalFile() and wfFindFile() should be sufficient
31 * in most cases.
32 *
33 * @ingroup FileAbstraction
34 */
35 abstract class File {
36 const DELETED_FILE = 1;
37 const DELETED_COMMENT = 2;
38 const DELETED_USER = 4;
39 const DELETED_RESTRICTED = 8;
40
41 /** Force rendering in the current process */
42 const RENDER_NOW = 1;
43 /**
44 * Force rendering even if thumbnail already exist and using RENDER_NOW
45 * I.e. you have to pass both flags: File::RENDER_NOW | File::RENDER_FORCE
46 */
47 const RENDER_FORCE = 2;
48
49 const DELETE_SOURCE = 1;
50
51 /**
52 * Some member variables can be lazy-initialised using __get(). The
53 * initialisation function for these variables is always a function named
54 * like getVar(), where Var is the variable name with upper-case first
55 * letter.
56 *
57 * The following variables are initialised in this way in this base class:
58 * name, extension, handler, path, canRender, isSafeFile,
59 * transformScript, hashPath, pageCount, url
60 *
61 * Code within this class should generally use the accessor function
62 * directly, since __get() isn't re-entrant and therefore causes bugs that
63 * depend on initialisation order.
64 */
65
66 /**
67 * The following member variables are not lazy-initialised
68 */
69
70 /**
71 * @var FileRepo|false
72 */
73 var $repo;
74
75 /**
76 * @var Title|false
77 */
78 var $title;
79
80 var $lastError, $redirected, $redirectedTitle;
81
82 /**
83 * @var FSFile|false
84 */
85 protected $fsFile;
86
87 /**
88 * @var MediaHandler
89 */
90 protected $handler;
91
92 /**
93 * @var string
94 */
95 protected $url, $extension, $name, $path, $hashPath, $pageCount, $transformScript;
96
97 /**
98 * @var bool
99 */
100 protected $canRender, $isSafeFile;
101
102 /**
103 * @var string Required Repository class type
104 */
105 protected $repoClass = 'FileRepo';
106
107 /**
108 * Call this constructor from child classes.
109 *
110 * Both $title and $repo are optional, though some functions
111 * may return false or throw exceptions if they are not set.
112 * Most subclasses will want to call assertRepoDefined() here.
113 *
114 * @param $title Title|string|false
115 * @param $repo FileRepo|false
116 */
117 function __construct( $title, $repo ) {
118 if ( $title !== false ) { // subclasses may not use MW titles
119 $title = self::normalizeTitle( $title, 'exception' );
120 }
121 $this->title = $title;
122 $this->repo = $repo;
123 }
124
125 /**
126 * Given a string or Title object return either a
127 * valid Title object with namespace NS_FILE or null
128 *
129 * @param $title Title|string
130 * @param $exception string|false Use 'exception' to throw an error on bad titles
131 * @return Title|null
132 */
133 static function normalizeTitle( $title, $exception = false ) {
134 $ret = $title;
135 if ( $ret instanceof Title ) {
136 # Normalize NS_MEDIA -> NS_FILE
137 if ( $ret->getNamespace() == NS_MEDIA ) {
138 $ret = Title::makeTitleSafe( NS_FILE, $ret->getDBkey() );
139 # Sanity check the title namespace
140 } elseif ( $ret->getNamespace() !== NS_FILE ) {
141 $ret = null;
142 }
143 } else {
144 # Convert strings to Title objects
145 $ret = Title::makeTitleSafe( NS_FILE, (string)$ret );
146 }
147 if ( !$ret && $exception !== false ) {
148 throw new MWException( "`$title` is not a valid file title." );
149 }
150 return $ret;
151 }
152
153 function __get( $name ) {
154 $function = array( $this, 'get' . ucfirst( $name ) );
155 if ( !is_callable( $function ) ) {
156 return null;
157 } else {
158 $this->$name = call_user_func( $function );
159 return $this->$name;
160 }
161 }
162
163 /**
164 * Normalize a file extension to the common form, and ensure it's clean.
165 * Extensions with non-alphanumeric characters will be discarded.
166 *
167 * @param $ext string (without the .)
168 * @return string
169 */
170 static function normalizeExtension( $ext ) {
171 $lower = strtolower( $ext );
172 $squish = array(
173 'htm' => 'html',
174 'jpeg' => 'jpg',
175 'mpeg' => 'mpg',
176 'tiff' => 'tif',
177 'ogv' => 'ogg' );
178 if( isset( $squish[$lower] ) ) {
179 return $squish[$lower];
180 } elseif( preg_match( '/^[0-9a-z]+$/', $lower ) ) {
181 return $lower;
182 } else {
183 return '';
184 }
185 }
186
187 /**
188 * Checks if file extensions are compatible
189 *
190 * @param $old File Old file
191 * @param $new string New name
192 *
193 * @return bool|null
194 */
195 static function checkExtensionCompatibility( File $old, $new ) {
196 $oldMime = $old->getMimeType();
197 $n = strrpos( $new, '.' );
198 $newExt = self::normalizeExtension( $n ? substr( $new, $n + 1 ) : '' );
199 $mimeMagic = MimeMagic::singleton();
200 return $mimeMagic->isMatchingExtension( $newExt, $oldMime );
201 }
202
203 /**
204 * Upgrade the database row if there is one
205 * Called by ImagePage
206 * STUB
207 */
208 function upgradeRow() {}
209
210 /**
211 * Split an internet media type into its two components; if not
212 * a two-part name, set the minor type to 'unknown'.
213 *
214 * @param string $mime "text/html" etc
215 * @return array ("text", "html") etc
216 */
217 public static function splitMime( $mime ) {
218 if( strpos( $mime, '/' ) !== false ) {
219 return explode( '/', $mime, 2 );
220 } else {
221 return array( $mime, 'unknown' );
222 }
223 }
224
225 /**
226 * Return the name of this file
227 *
228 * @return string
229 */
230 public function getName() {
231 if ( !isset( $this->name ) ) {
232 $this->assertRepoDefined();
233 $this->name = $this->repo->getNameFromTitle( $this->title );
234 }
235 return $this->name;
236 }
237
238 /**
239 * Get the file extension, e.g. "svg"
240 *
241 * @return string
242 */
243 function getExtension() {
244 if ( !isset( $this->extension ) ) {
245 $n = strrpos( $this->getName(), '.' );
246 $this->extension = self::normalizeExtension(
247 $n ? substr( $this->getName(), $n + 1 ) : '' );
248 }
249 return $this->extension;
250 }
251
252 /**
253 * Return the associated title object
254 *
255 * @return Title|false
256 */
257 public function getTitle() {
258 return $this->title;
259 }
260
261 /**
262 * Return the title used to find this file
263 *
264 * @return Title
265 */
266 public function getOriginalTitle() {
267 if ( $this->redirected ) {
268 return $this->getRedirectedTitle();
269 }
270 return $this->title;
271 }
272
273 /**
274 * Return the URL of the file
275 *
276 * @return string
277 */
278 public function getUrl() {
279 if ( !isset( $this->url ) ) {
280 $this->assertRepoDefined();
281 $this->url = $this->repo->getZoneUrl( 'public' ) . '/' . $this->getUrlRel();
282 }
283 return $this->url;
284 }
285
286 /**
287 * Return a fully-qualified URL to the file.
288 * Upload URL paths _may or may not_ be fully qualified, so
289 * we check. Local paths are assumed to belong on $wgServer.
290 *
291 * @return String
292 */
293 public function getFullUrl() {
294 return wfExpandUrl( $this->getUrl(), PROTO_RELATIVE );
295 }
296
297 /**
298 * @return string
299 */
300 public function getCanonicalUrl() {
301 return wfExpandUrl( $this->getUrl(), PROTO_CANONICAL );
302 }
303
304 /**
305 * @return string
306 */
307 function getViewURL() {
308 if ( $this->mustRender() ) {
309 if ( $this->canRender() ) {
310 return $this->createThumb( $this->getWidth() );
311 } else {
312 wfDebug( __METHOD__.': supposed to render ' . $this->getName() .
313 ' (' . $this->getMimeType() . "), but can't!\n" );
314 return $this->getURL(); #hm... return NULL?
315 }
316 } else {
317 return $this->getURL();
318 }
319 }
320
321 /**
322 * Return the storage path to the file. Note that this does
323 * not mean that a file actually exists under that location.
324 *
325 * This path depends on whether directory hashing is active or not,
326 * i.e. whether the files are all found in the same directory,
327 * or in hashed paths like /images/3/3c.
328 *
329 * Most callers don't check the return value, but ForeignAPIFile::getPath
330 * returns false.
331 *
332 * @return string|false
333 */
334 public function getPath() {
335 if ( !isset( $this->path ) ) {
336 $this->assertRepoDefined();
337 $this->path = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
338 }
339 return $this->path;
340 }
341
342 /**
343 * Get an FS copy or original of this file and return the path.
344 * Returns false on failure. Callers must not alter the file.
345 * Temporary files are cleared automatically.
346 *
347 * @return string|false
348 */
349 public function getLocalRefPath() {
350 $this->assertRepoDefined();
351 if ( !isset( $this->fsFile ) ) {
352 $this->fsFile = $this->repo->getLocalReference( $this->getPath() );
353 if ( !$this->fsFile ) {
354 $this->fsFile = false; // null => false; cache negative hits
355 }
356 }
357 return ( $this->fsFile )
358 ? $this->fsFile->getPath()
359 : false;
360 }
361
362 /**
363 * Return the width of the image. Returns false if the width is unknown
364 * or undefined.
365 *
366 * STUB
367 * Overridden by LocalFile, UnregisteredLocalFile
368 *
369 * @param $page int
370 *
371 * @return number
372 */
373 public function getWidth( $page = 1 ) {
374 return false;
375 }
376
377 /**
378 * Return the height of the image. Returns false if the height is unknown
379 * or undefined
380 *
381 * STUB
382 * Overridden by LocalFile, UnregisteredLocalFile
383 *
384 * @param $page int
385 *
386 * @return false|number
387 */
388 public function getHeight( $page = 1 ) {
389 return false;
390 }
391
392 /**
393 * Returns ID or name of user who uploaded the file
394 * STUB
395 *
396 * @param $type string 'text' or 'id'
397 *
398 * @return string|int
399 */
400 public function getUser( $type = 'text' ) {
401 return null;
402 }
403
404 /**
405 * Get the duration of a media file in seconds
406 *
407 * @return number
408 */
409 public function getLength() {
410 $handler = $this->getHandler();
411 if ( $handler ) {
412 return $handler->getLength( $this );
413 } else {
414 return 0;
415 }
416 }
417
418 /**
419 * Return true if the file is vectorized
420 *
421 * @return bool
422 */
423 public function isVectorized() {
424 $handler = $this->getHandler();
425 if ( $handler ) {
426 return $handler->isVectorized( $this );
427 } else {
428 return false;
429 }
430 }
431
432 /**
433 * Get handler-specific metadata
434 * Overridden by LocalFile, UnregisteredLocalFile
435 * STUB
436 */
437 public function getMetadata() {
438 return false;
439 }
440
441 /**
442 * get versioned metadata
443 *
444 * @param $metadata Mixed Array or String of (serialized) metadata
445 * @param $version integer version number.
446 * @return Array containing metadata, or what was passed to it on fail (unserializing if not array)
447 */
448 public function convertMetadataVersion($metadata, $version) {
449 $handler = $this->getHandler();
450 if ( !is_array( $metadata ) ) {
451 // Just to make the return type consistent
452 $metadata = unserialize( $metadata );
453 }
454 if ( $handler ) {
455 return $handler->convertMetadataVersion( $metadata, $version );
456 } else {
457 return $metadata;
458 }
459 }
460
461 /**
462 * Return the bit depth of the file
463 * Overridden by LocalFile
464 * STUB
465 */
466 public function getBitDepth() {
467 return 0;
468 }
469
470 /**
471 * Return the size of the image file, in bytes
472 * Overridden by LocalFile, UnregisteredLocalFile
473 * STUB
474 */
475 public function getSize() {
476 return false;
477 }
478
479 /**
480 * Returns the mime type of the file.
481 * Overridden by LocalFile, UnregisteredLocalFile
482 * STUB
483 *
484 * @return string
485 */
486 function getMimeType() {
487 return 'unknown/unknown';
488 }
489
490 /**
491 * Return the type of the media in the file.
492 * Use the value returned by this function with the MEDIATYPE_xxx constants.
493 * Overridden by LocalFile,
494 * STUB
495 */
496 function getMediaType() {
497 return MEDIATYPE_UNKNOWN;
498 }
499
500 /**
501 * Checks if the output of transform() for this file is likely
502 * to be valid. If this is false, various user elements will
503 * display a placeholder instead.
504 *
505 * Currently, this checks if the file is an image format
506 * that can be converted to a format
507 * supported by all browsers (namely GIF, PNG and JPEG),
508 * or if it is an SVG image and SVG conversion is enabled.
509 *
510 * @return bool
511 */
512 function canRender() {
513 if ( !isset( $this->canRender ) ) {
514 $this->canRender = $this->getHandler() && $this->handler->canRender( $this );
515 }
516 return $this->canRender;
517 }
518
519 /**
520 * Accessor for __get()
521 */
522 protected function getCanRender() {
523 return $this->canRender();
524 }
525
526 /**
527 * Return true if the file is of a type that can't be directly
528 * rendered by typical browsers and needs to be re-rasterized.
529 *
530 * This returns true for everything but the bitmap types
531 * supported by all browsers, i.e. JPEG; GIF and PNG. It will
532 * also return true for any non-image formats.
533 *
534 * @return bool
535 */
536 function mustRender() {
537 return $this->getHandler() && $this->handler->mustRender( $this );
538 }
539
540 /**
541 * Alias for canRender()
542 *
543 * @return bool
544 */
545 function allowInlineDisplay() {
546 return $this->canRender();
547 }
548
549 /**
550 * Determines if this media file is in a format that is unlikely to
551 * contain viruses or malicious content. It uses the global
552 * $wgTrustedMediaFormats list to determine if the file is safe.
553 *
554 * This is used to show a warning on the description page of non-safe files.
555 * It may also be used to disallow direct [[media:...]] links to such files.
556 *
557 * Note that this function will always return true if allowInlineDisplay()
558 * or isTrustedFile() is true for this file.
559 *
560 * @return bool
561 */
562 function isSafeFile() {
563 if ( !isset( $this->isSafeFile ) ) {
564 $this->isSafeFile = $this->_getIsSafeFile();
565 }
566 return $this->isSafeFile;
567 }
568
569 /**
570 * Accessor for __get()
571 *
572 * @return bool
573 */
574 protected function getIsSafeFile() {
575 return $this->isSafeFile();
576 }
577
578 /**
579 * Uncached accessor
580 *
581 * @return bool
582 */
583 protected function _getIsSafeFile() {
584 global $wgTrustedMediaFormats;
585
586 if ( $this->allowInlineDisplay() ) {
587 return true;
588 }
589 if ($this->isTrustedFile()) {
590 return true;
591 }
592
593 $type = $this->getMediaType();
594 $mime = $this->getMimeType();
595 #wfDebug("LocalFile::isSafeFile: type= $type, mime= $mime\n");
596
597 if ( !$type || $type === MEDIATYPE_UNKNOWN ) {
598 return false; #unknown type, not trusted
599 }
600 if ( in_array( $type, $wgTrustedMediaFormats ) ) {
601 return true;
602 }
603
604 if ( $mime === "unknown/unknown" ) {
605 return false; #unknown type, not trusted
606 }
607 if ( in_array( $mime, $wgTrustedMediaFormats) ) {
608 return true;
609 }
610
611 return false;
612 }
613
614 /**
615 * Returns true if the file is flagged as trusted. Files flagged that way
616 * can be linked to directly, even if that is not allowed for this type of
617 * file normally.
618 *
619 * This is a dummy function right now and always returns false. It could be
620 * implemented to extract a flag from the database. The trusted flag could be
621 * set on upload, if the user has sufficient privileges, to bypass script-
622 * and html-filters. It may even be coupled with cryptographics signatures
623 * or such.
624 *
625 * @return bool
626 */
627 function isTrustedFile() {
628 #this could be implemented to check a flag in the database,
629 #look for signatures, etc
630 return false;
631 }
632
633 /**
634 * Returns true if file exists in the repository.
635 *
636 * Overridden by LocalFile to avoid unnecessary stat calls.
637 *
638 * @return boolean Whether file exists in the repository.
639 */
640 public function exists() {
641 return $this->getPath() && $this->repo->fileExists( $this->path );
642 }
643
644 /**
645 * Returns true if file exists in the repository and can be included in a page.
646 * It would be unsafe to include private images, making public thumbnails inadvertently
647 *
648 * @return boolean Whether file exists in the repository and is includable.
649 */
650 public function isVisible() {
651 return $this->exists();
652 }
653
654 /**
655 * @return string
656 */
657 function getTransformScript() {
658 if ( !isset( $this->transformScript ) ) {
659 $this->transformScript = false;
660 if ( $this->repo ) {
661 $script = $this->repo->getThumbScriptUrl();
662 if ( $script ) {
663 $this->transformScript = "$script?f=" . urlencode( $this->getName() );
664 }
665 }
666 }
667 return $this->transformScript;
668 }
669
670 /**
671 * Get a ThumbnailImage which is the same size as the source
672 *
673 * @param $handlerParams array
674 *
675 * @return string
676 */
677 function getUnscaledThumb( $handlerParams = array() ) {
678 $hp =& $handlerParams;
679 $page = isset( $hp['page'] ) ? $hp['page'] : false;
680 $width = $this->getWidth( $page );
681 if ( !$width ) {
682 return $this->iconThumb();
683 }
684 $hp['width'] = $width;
685 return $this->transform( $hp );
686 }
687
688 /**
689 * Return the file name of a thumbnail with the specified parameters
690 *
691 * @param $params Array: handler-specific parameters
692 * @private -ish
693 *
694 * @return string
695 */
696 function thumbName( $params ) {
697 return $this->generateThumbName( $this->getName(), $params );
698 }
699
700 /**
701 * Generate a thumbnail file name from a name and specified parameters
702 *
703 * @param string $name
704 * @param array $params Parameters which will be passed to MediaHandler::makeParamString
705 *
706 * @return string
707 */
708 function generateThumbName( $name, $params ) {
709 if ( !$this->getHandler() ) {
710 return null;
711 }
712 $extension = $this->getExtension();
713 list( $thumbExt, $thumbMime ) = $this->handler->getThumbType(
714 $extension, $this->getMimeType(), $params );
715 $thumbName = $this->handler->makeParamString( $params ) . '-' . $name;
716 if ( $thumbExt != $extension ) {
717 $thumbName .= ".$thumbExt";
718 }
719 return $thumbName;
720 }
721
722 /**
723 * Create a thumbnail of the image having the specified width/height.
724 * The thumbnail will not be created if the width is larger than the
725 * image's width. Let the browser do the scaling in this case.
726 * The thumbnail is stored on disk and is only computed if the thumbnail
727 * file does not exist OR if it is older than the image.
728 * Returns the URL.
729 *
730 * Keeps aspect ratio of original image. If both width and height are
731 * specified, the generated image will be no bigger than width x height,
732 * and will also have correct aspect ratio.
733 *
734 * @param $width Integer: maximum width of the generated thumbnail
735 * @param $height Integer: maximum height of the image (optional)
736 *
737 * @return string
738 */
739 public function createThumb( $width, $height = -1 ) {
740 $params = array( 'width' => $width );
741 if ( $height != -1 ) {
742 $params['height'] = $height;
743 }
744 $thumb = $this->transform( $params );
745 if ( is_null( $thumb ) || $thumb->isError() ) {
746 return '';
747 }
748 return $thumb->getUrl();
749 }
750
751 /**
752 * Return either a MediaTransformError or placeholder thumbnail (if $wgIgnoreImageErrors)
753 *
754 * @param $thumbPath string Thumbnail storage path
755 * @param $thumbUrl string Thumbnail URL
756 * @param $params Array
757 * @param $flags integer
758 * @return MediaTransformOutput
759 */
760 protected function transformErrorOutput( $thumbPath, $thumbUrl, $params, $flags ) {
761 global $wgIgnoreImageErrors;
762
763 if ( $wgIgnoreImageErrors && !( $flags & self::RENDER_NOW ) ) {
764 return $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
765 } else {
766 return new MediaTransformError( 'thumbnail_error',
767 $params['width'], 0, wfMsg( 'thumbnail-dest-create' ) );
768 }
769 }
770
771 /**
772 * Transform a media file
773 *
774 * @param $params Array: an associative array of handler-specific parameters.
775 * Typical keys are width, height and page.
776 * @param $flags Integer: a bitfield, may contain self::RENDER_NOW to force rendering
777 * @return MediaTransformOutput|false
778 */
779 function transform( $params, $flags = 0 ) {
780 global $wgUseSquid, $wgIgnoreImageErrors, $wgThumbnailEpoch;
781
782 wfProfileIn( __METHOD__ );
783 do {
784 if ( !$this->canRender() ) {
785 $thumb = $this->iconThumb();
786 break; // not a bitmap or renderable image, don't try
787 }
788
789 // Get the descriptionUrl to embed it as comment into the thumbnail. Bug 19791.
790 $descriptionUrl = $this->getDescriptionUrl();
791 if ( $descriptionUrl ) {
792 $params['descriptionUrl'] = wfExpandUrl( $descriptionUrl, PROTO_CANONICAL );
793 }
794
795 $script = $this->getTransformScript();
796 if ( $script && !( $flags & self::RENDER_NOW ) ) {
797 // Use a script to transform on client request, if possible
798 $thumb = $this->handler->getScriptedTransform( $this, $script, $params );
799 if ( $thumb ) {
800 break;
801 }
802 }
803
804 $normalisedParams = $params;
805 $this->handler->normaliseParams( $this, $normalisedParams );
806
807 $thumbName = $this->thumbName( $normalisedParams );
808 $thumbUrl = $this->getThumbUrl( $thumbName );
809 $thumbPath = $this->getThumbPath( $thumbName ); // final thumb path
810
811 if ( $this->repo ) {
812 // Defer rendering if a 404 handler is set up...
813 if ( $this->repo->canTransformVia404() && !( $flags & self::RENDER_NOW ) ) {
814 wfDebug( __METHOD__ . " transformation deferred." );
815 // XXX: Pass in the storage path even though we are not rendering anything
816 // and the path is supposed to be an FS path. This is due to getScalerType()
817 // getting called on the path and clobbering $thumb->getUrl() if it's false.
818 $thumb = $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
819 break;
820 }
821 // Clean up broken thumbnails as needed
822 $this->migrateThumbFile( $thumbName );
823 // Check if an up-to-date thumbnail already exists...
824 wfDebug( __METHOD__.": Doing stat for $thumbPath\n" );
825 if ( $this->repo->fileExists( $thumbPath ) && !( $flags & self::RENDER_FORCE ) ) {
826 $timestamp = $this->repo->getFileTimestamp( $thumbPath );
827 if ( $timestamp !== false && $timestamp >= $wgThumbnailEpoch ) {
828 // XXX: Pass in the storage path even though we are not rendering anything
829 // and the path is supposed to be an FS path. This is due to getScalerType()
830 // getting called on the path and clobbering $thumb->getUrl() if it's false.
831 $thumb = $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
832 $thumb->setStoragePath( $thumbPath );
833 break;
834 }
835 } elseif ( $flags & self::RENDER_FORCE ) {
836 wfDebug( __METHOD__ . " forcing rendering per flag File::RENDER_FORCE\n" );
837 }
838 }
839
840 // Create a temp FS file with the same extension and the thumbnail
841 $thumbExt = FileBackend::extensionFromPath( $thumbPath );
842 $tmpFile = TempFSFile::factory( 'transform_', $thumbExt );
843 if ( !$tmpFile ) {
844 $thumb = $this->transformErrorOutput( $thumbPath, $thumbUrl, $params, $flags );
845 break;
846 }
847 $tmpThumbPath = $tmpFile->getPath(); // path of 0-byte temp file
848
849 // Actually render the thumbnail...
850 $thumb = $this->handler->doTransform( $this, $tmpThumbPath, $thumbUrl, $params );
851 $tmpFile->bind( $thumb ); // keep alive with $thumb
852
853 if ( !$thumb ) { // bad params?
854 $thumb = null;
855 } elseif ( $thumb->isError() ) { // transform error
856 $this->lastError = $thumb->toText();
857 // Ignore errors if requested
858 if ( $wgIgnoreImageErrors && !( $flags & self::RENDER_NOW ) ) {
859 $thumb = $this->handler->getTransform( $this, $tmpThumbPath, $thumbUrl, $params );
860 }
861 } elseif ( $this->repo && $thumb->hasFile() && !$thumb->fileIsSource() ) {
862 $backend = $this->repo->getBackend();
863 // Copy the thumbnail from the file system into storage. This avoids using
864 // FileRepo::store(); getThumbPath() uses a different zone in some subclasses.
865 $backend->prepare( array( 'dir' => dirname( $thumbPath ) ) );
866 $status = $backend->store(
867 array( 'src' => $tmpThumbPath, 'dst' => $thumbPath, 'overwrite' => 1 ),
868 array( 'force' => 1, 'nonLocking' => 1, 'allowStale' => 1 )
869 );
870 if ( $status->isOK() ) {
871 $thumb->setStoragePath( $thumbPath );
872 } else {
873 $thumb = $this->transformErrorOutput( $thumbPath, $thumbUrl, $params, $flags );
874 }
875 }
876
877 // Purge. Useful in the event of Core -> Squid connection failure or squid
878 // purge collisions from elsewhere during failure. Don't keep triggering for
879 // "thumbs" which have the main image URL though (bug 13776)
880 if ( $wgUseSquid ) {
881 if ( !$thumb || $thumb->isError() || $thumb->getUrl() != $this->getURL() ) {
882 SquidUpdate::purge( array( $thumbUrl ) );
883 }
884 }
885 } while ( false );
886
887 wfProfileOut( __METHOD__ );
888 return is_object( $thumb ) ? $thumb : false;
889 }
890
891 /**
892 * Hook into transform() to allow migration of thumbnail files
893 * STUB
894 * Overridden by LocalFile
895 */
896 function migrateThumbFile( $thumbName ) {}
897
898 /**
899 * Get a MediaHandler instance for this file
900 *
901 * @return MediaHandler
902 */
903 function getHandler() {
904 if ( !isset( $this->handler ) ) {
905 $this->handler = MediaHandler::getHandler( $this->getMimeType() );
906 }
907 return $this->handler;
908 }
909
910 /**
911 * Get a ThumbnailImage representing a file type icon
912 *
913 * @return ThumbnailImage
914 */
915 function iconThumb() {
916 global $wgStylePath, $wgStyleDirectory;
917
918 $try = array( 'fileicon-' . $this->getExtension() . '.png', 'fileicon.png' );
919 foreach ( $try as $icon ) {
920 $path = '/common/images/icons/' . $icon;
921 $filepath = $wgStyleDirectory . $path;
922 if ( file_exists( $filepath ) ) { // always FS
923 return new ThumbnailImage( $this, $wgStylePath . $path, 120, 120 );
924 }
925 }
926 return null;
927 }
928
929 /**
930 * Get last thumbnailing error.
931 * Largely obsolete.
932 */
933 function getLastError() {
934 return $this->lastError;
935 }
936
937 /**
938 * Get all thumbnail names previously generated for this file
939 * STUB
940 * Overridden by LocalFile
941 */
942 function getThumbnails() {
943 return array();
944 }
945
946 /**
947 * Purge shared caches such as thumbnails and DB data caching
948 * STUB
949 * Overridden by LocalFile
950 * @param $options Array Options, which include:
951 * 'forThumbRefresh' : The purging is only to refresh thumbnails
952 */
953 function purgeCache( $options = array() ) {}
954
955 /**
956 * Purge the file description page, but don't go after
957 * pages using the file. Use when modifying file history
958 * but not the current data.
959 */
960 function purgeDescription() {
961 $title = $this->getTitle();
962 if ( $title ) {
963 $title->invalidateCache();
964 $title->purgeSquid();
965 }
966 }
967
968 /**
969 * Purge metadata and all affected pages when the file is created,
970 * deleted, or majorly updated.
971 */
972 function purgeEverything() {
973 // Delete thumbnails and refresh file metadata cache
974 $this->purgeCache();
975 $this->purgeDescription();
976
977 // Purge cache of all pages using this file
978 $title = $this->getTitle();
979 if ( $title ) {
980 $update = new HTMLCacheUpdate( $title, 'imagelinks' );
981 $update->doUpdate();
982 }
983 }
984
985 /**
986 * Return a fragment of the history of file.
987 *
988 * STUB
989 * @param $limit integer Limit of rows to return
990 * @param $start timestamp Only revisions older than $start will be returned
991 * @param $end timestamp Only revisions newer than $end will be returned
992 * @param $inc bool Include the endpoints of the time range
993 *
994 * @return array
995 */
996 function getHistory($limit = null, $start = null, $end = null, $inc=true) {
997 return array();
998 }
999
1000 /**
1001 * Return the history of this file, line by line. Starts with current version,
1002 * then old versions. Should return an object similar to an image/oldimage
1003 * database row.
1004 *
1005 * STUB
1006 * Overridden in LocalFile
1007 */
1008 public function nextHistoryLine() {
1009 return false;
1010 }
1011
1012 /**
1013 * Reset the history pointer to the first element of the history.
1014 * Always call this function after using nextHistoryLine() to free db resources
1015 * STUB
1016 * Overridden in LocalFile.
1017 */
1018 public function resetHistory() {}
1019
1020 /**
1021 * Get the filename hash component of the directory including trailing slash,
1022 * e.g. f/fa/
1023 * If the repository is not hashed, returns an empty string.
1024 *
1025 * @return string
1026 */
1027 function getHashPath() {
1028 if ( !isset( $this->hashPath ) ) {
1029 $this->assertRepoDefined();
1030 $this->hashPath = $this->repo->getHashPath( $this->getName() );
1031 }
1032 return $this->hashPath;
1033 }
1034
1035 /**
1036 * Get the path of the file relative to the public zone root.
1037 * This function is overriden in OldLocalFile to be like getArchiveRel().
1038 *
1039 * @return string
1040 */
1041 function getRel() {
1042 return $this->getHashPath() . $this->getName();
1043 }
1044
1045 /**
1046 * Get the path of an archived file relative to the public zone root
1047 *
1048 * @param $suffix bool|string if not false, the name of an archived thumbnail file
1049 *
1050 * @return string
1051 */
1052 function getArchiveRel( $suffix = false ) {
1053 $path = 'archive/' . $this->getHashPath();
1054 if ( $suffix === false ) {
1055 $path = substr( $path, 0, -1 );
1056 } else {
1057 $path .= $suffix;
1058 }
1059 return $path;
1060 }
1061
1062 /**
1063 * Get the path, relative to the thumbnail zone root, of the
1064 * thumbnail directory or a particular file if $suffix is specified
1065 *
1066 * @param $suffix bool|string if not false, the name of a thumbnail file
1067 *
1068 * @return string
1069 */
1070 function getThumbRel( $suffix = false ) {
1071 $path = $this->getRel();
1072 if ( $suffix !== false ) {
1073 $path .= '/' . $suffix;
1074 }
1075 return $path;
1076 }
1077
1078 /**
1079 * Get urlencoded path of the file relative to the public zone root.
1080 * This function is overriden in OldLocalFile to be like getArchiveUrl().
1081 *
1082 * @return string
1083 */
1084 function getUrlRel() {
1085 return $this->getHashPath() . rawurlencode( $this->getName() );
1086 }
1087
1088 /**
1089 * Get the path, relative to the thumbnail zone root, for an archived file's thumbs directory
1090 * or a specific thumb if the $suffix is given.
1091 *
1092 * @param $archiveName string the timestamped name of an archived image
1093 * @param $suffix bool|string if not false, the name of a thumbnail file
1094 *
1095 * @return string
1096 */
1097 function getArchiveThumbRel( $archiveName, $suffix = false ) {
1098 $path = 'archive/' . $this->getHashPath() . $archiveName . "/";
1099 if ( $suffix === false ) {
1100 $path = substr( $path, 0, -1 );
1101 } else {
1102 $path .= $suffix;
1103 }
1104 return $path;
1105 }
1106
1107 /**
1108 * Get the path of the archived file.
1109 *
1110 * @param $suffix bool|string if not false, the name of an archived file.
1111 *
1112 * @return string
1113 */
1114 function getArchivePath( $suffix = false ) {
1115 $this->assertRepoDefined();
1116 return $this->repo->getZonePath( 'public' ) . '/' . $this->getArchiveRel( $suffix );
1117 }
1118
1119 /**
1120 * Get the path of an archived file's thumbs, or a particular thumb if $suffix is specified
1121 *
1122 * @param $archiveName string the timestamped name of an archived image
1123 * @param $suffix bool|string if not false, the name of a thumbnail file
1124 *
1125 * @return string
1126 */
1127 function getArchiveThumbPath( $archiveName, $suffix = false ) {
1128 $this->assertRepoDefined();
1129 return $this->repo->getZonePath( 'thumb' ) . '/' .
1130 $this->getArchiveThumbRel( $archiveName, $suffix );
1131 }
1132
1133 /**
1134 * Get the path of the thumbnail directory, or a particular file if $suffix is specified
1135 *
1136 * @param $suffix bool|string if not false, the name of a thumbnail file
1137 *
1138 * @return string
1139 */
1140 function getThumbPath( $suffix = false ) {
1141 $this->assertRepoDefined();
1142 return $this->repo->getZonePath( 'thumb' ) . '/' . $this->getThumbRel( $suffix );
1143 }
1144
1145 /**
1146 * Get the URL of the archive directory, or a particular file if $suffix is specified
1147 *
1148 * @param $suffix bool|string if not false, the name of an archived file
1149 *
1150 * @return string
1151 */
1152 function getArchiveUrl( $suffix = false ) {
1153 $this->assertRepoDefined();
1154 $path = $this->repo->getZoneUrl( 'public' ) . '/archive/' . $this->getHashPath();
1155 if ( $suffix === false ) {
1156 $path = substr( $path, 0, -1 );
1157 } else {
1158 $path .= rawurlencode( $suffix );
1159 }
1160 return $path;
1161 }
1162
1163 /**
1164 * Get the URL of the archived file's thumbs, or a particular thumb if $suffix is specified
1165 *
1166 * @param $archiveName string the timestamped name of an archived image
1167 * @param $suffix bool|string if not false, the name of a thumbnail file
1168 *
1169 * @return string
1170 */
1171 function getArchiveThumbUrl( $archiveName, $suffix = false ) {
1172 $this->assertRepoDefined();
1173 $path = $this->repo->getZoneUrl( 'thumb' ) . '/archive/' .
1174 $this->getHashPath() . rawurlencode( $archiveName ) . "/";
1175 if ( $suffix === false ) {
1176 $path = substr( $path, 0, -1 );
1177 } else {
1178 $path .= rawurlencode( $suffix );
1179 }
1180 return $path;
1181 }
1182
1183 /**
1184 * Get the URL of the thumbnail directory, or a particular file if $suffix is specified
1185 *
1186 * @param $suffix bool|string if not false, the name of a thumbnail file
1187 *
1188 * @return path
1189 */
1190 function getThumbUrl( $suffix = false ) {
1191 $this->assertRepoDefined();
1192 $path = $this->repo->getZoneUrl( 'thumb' ) . '/' . $this->getUrlRel();
1193 if ( $suffix !== false ) {
1194 $path .= '/' . rawurlencode( $suffix );
1195 }
1196 return $path;
1197 }
1198
1199 /**
1200 * Get the public zone virtual URL for a current version source file
1201 *
1202 * @param $suffix bool|string if not false, the name of a thumbnail file
1203 *
1204 * @return string
1205 */
1206 function getVirtualUrl( $suffix = false ) {
1207 $this->assertRepoDefined();
1208 $path = $this->repo->getVirtualUrl() . '/public/' . $this->getUrlRel();
1209 if ( $suffix !== false ) {
1210 $path .= '/' . rawurlencode( $suffix );
1211 }
1212 return $path;
1213 }
1214
1215 /**
1216 * Get the public zone virtual URL for an archived version source file
1217 *
1218 * @param $suffix bool|string if not false, the name of a thumbnail file
1219 *
1220 * @return string
1221 */
1222 function getArchiveVirtualUrl( $suffix = false ) {
1223 $this->assertRepoDefined();
1224 $path = $this->repo->getVirtualUrl() . '/public/archive/' . $this->getHashPath();
1225 if ( $suffix === false ) {
1226 $path = substr( $path, 0, -1 );
1227 } else {
1228 $path .= rawurlencode( $suffix );
1229 }
1230 return $path;
1231 }
1232
1233 /**
1234 * Get the virtual URL for a thumbnail file or directory
1235 *
1236 * @param $suffix bool|string if not false, the name of a thumbnail file
1237 *
1238 * @return string
1239 */
1240 function getThumbVirtualUrl( $suffix = false ) {
1241 $this->assertRepoDefined();
1242 $path = $this->repo->getVirtualUrl() . '/thumb/' . $this->getUrlRel();
1243 if ( $suffix !== false ) {
1244 $path .= '/' . rawurlencode( $suffix );
1245 }
1246 return $path;
1247 }
1248
1249 /**
1250 * @return bool
1251 */
1252 function isHashed() {
1253 $this->assertRepoDefined();
1254 return $this->repo->isHashed();
1255 }
1256
1257 /**
1258 * @throws MWException
1259 */
1260 function readOnlyError() {
1261 throw new MWException( get_class($this) . ': write operations are not supported' );
1262 }
1263
1264 /**
1265 * Record a file upload in the upload log and the image table
1266 * STUB
1267 * Overridden by LocalFile
1268 * @param $oldver
1269 * @param $desc
1270 * @param $license string
1271 * @param $copyStatus string
1272 * @param $source string
1273 * @param $watch bool
1274 */
1275 function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '', $watch = false ) {
1276 $this->readOnlyError();
1277 }
1278
1279 /**
1280 * Move or copy a file to its public location. If a file exists at the
1281 * destination, move it to an archive. Returns a FileRepoStatus object with
1282 * the archive name in the "value" member on success.
1283 *
1284 * The archive name should be passed through to recordUpload for database
1285 * registration.
1286 *
1287 * @param $srcPath String: local filesystem path to the source image
1288 * @param $flags Integer: a bitwise combination of:
1289 * File::DELETE_SOURCE Delete the source file, i.e. move
1290 * rather than copy
1291 * @return FileRepoStatus object. On success, the value member contains the
1292 * archive name, or an empty string if it was a new file.
1293 *
1294 * STUB
1295 * Overridden by LocalFile
1296 */
1297 function publish( $srcPath, $flags = 0 ) {
1298 $this->readOnlyError();
1299 }
1300
1301 /**
1302 * @return bool
1303 */
1304 function formatMetadata() {
1305 if ( !$this->getHandler() ) {
1306 return false;
1307 }
1308 return $this->getHandler()->formatMetadata( $this, $this->getMetadata() );
1309 }
1310
1311 /**
1312 * Returns true if the file comes from the local file repository.
1313 *
1314 * @return bool
1315 */
1316 function isLocal() {
1317 return $this->repo && $this->repo->isLocal();
1318 }
1319
1320 /**
1321 * Returns the name of the repository.
1322 *
1323 * @return string
1324 */
1325 function getRepoName() {
1326 return $this->repo ? $this->repo->getName() : 'unknown';
1327 }
1328
1329 /**
1330 * Returns the repository
1331 *
1332 * @return FileRepo|false
1333 */
1334 function getRepo() {
1335 return $this->repo;
1336 }
1337
1338 /**
1339 * Returns true if the image is an old version
1340 * STUB
1341 *
1342 * @return bool
1343 */
1344 function isOld() {
1345 return false;
1346 }
1347
1348 /**
1349 * Is this file a "deleted" file in a private archive?
1350 * STUB
1351 *
1352 * @param $field
1353 *
1354 * @return bool
1355 */
1356 function isDeleted( $field ) {
1357 return false;
1358 }
1359
1360 /**
1361 * Return the deletion bitfield
1362 * STUB
1363 */
1364 function getVisibility() {
1365 return 0;
1366 }
1367
1368 /**
1369 * Was this file ever deleted from the wiki?
1370 *
1371 * @return bool
1372 */
1373 function wasDeleted() {
1374 $title = $this->getTitle();
1375 return $title && $title->isDeletedQuick();
1376 }
1377
1378 /**
1379 * Move file to the new title
1380 *
1381 * Move current, old version and all thumbnails
1382 * to the new filename. Old file is deleted.
1383 *
1384 * Cache purging is done; checks for validity
1385 * and logging are caller's responsibility
1386 *
1387 * @param $target Title New file name
1388 * @return FileRepoStatus object.
1389 */
1390 function move( $target ) {
1391 $this->readOnlyError();
1392 }
1393
1394 /**
1395 * Delete all versions of the file.
1396 *
1397 * Moves the files into an archive directory (or deletes them)
1398 * and removes the database rows.
1399 *
1400 * Cache purging is done; logging is caller's responsibility.
1401 *
1402 * @param $reason String
1403 * @param $suppress Boolean: hide content from sysops?
1404 * @return true on success, false on some kind of failure
1405 * STUB
1406 * Overridden by LocalFile
1407 */
1408 function delete( $reason, $suppress = false ) {
1409 $this->readOnlyError();
1410 }
1411
1412 /**
1413 * Restore all or specified deleted revisions to the given file.
1414 * Permissions and logging are left to the caller.
1415 *
1416 * May throw database exceptions on error.
1417 *
1418 * @param $versions array set of record ids of deleted items to restore,
1419 * or empty to restore all revisions.
1420 * @param $unsuppress bool remove restrictions on content upon restoration?
1421 * @return int|false the number of file revisions restored if successful,
1422 * or false on failure
1423 * STUB
1424 * Overridden by LocalFile
1425 */
1426 function restore( $versions = array(), $unsuppress = false ) {
1427 $this->readOnlyError();
1428 }
1429
1430 /**
1431 * Returns 'true' if this file is a type which supports multiple pages,
1432 * e.g. DJVU or PDF. Note that this may be true even if the file in
1433 * question only has a single page.
1434 *
1435 * @return Bool
1436 */
1437 function isMultipage() {
1438 return $this->getHandler() && $this->handler->isMultiPage( $this );
1439 }
1440
1441 /**
1442 * Returns the number of pages of a multipage document, or false for
1443 * documents which aren't multipage documents
1444 *
1445 * @return false|int
1446 */
1447 function pageCount() {
1448 if ( !isset( $this->pageCount ) ) {
1449 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
1450 $this->pageCount = $this->handler->pageCount( $this );
1451 } else {
1452 $this->pageCount = false;
1453 }
1454 }
1455 return $this->pageCount;
1456 }
1457
1458 /**
1459 * Calculate the height of a thumbnail using the source and destination width
1460 *
1461 * @param $srcWidth
1462 * @param $srcHeight
1463 * @param $dstWidth
1464 *
1465 * @return int
1466 */
1467 static function scaleHeight( $srcWidth, $srcHeight, $dstWidth ) {
1468 // Exact integer multiply followed by division
1469 if ( $srcWidth == 0 ) {
1470 return 0;
1471 } else {
1472 return round( $srcHeight * $dstWidth / $srcWidth );
1473 }
1474 }
1475
1476 /**
1477 * Get an image size array like that returned by getImageSize(), or false if it
1478 * can't be determined.
1479 *
1480 * @param $fileName String: The filename
1481 * @return Array
1482 */
1483 function getImageSize( $fileName ) {
1484 if ( !$this->getHandler() ) {
1485 return false;
1486 }
1487 return $this->handler->getImageSize( $this, $fileName );
1488 }
1489
1490 /**
1491 * Get the URL of the image description page. May return false if it is
1492 * unknown or not applicable.
1493 *
1494 * @return string
1495 */
1496 function getDescriptionUrl() {
1497 if ( $this->repo ) {
1498 return $this->repo->getDescriptionUrl( $this->getName() );
1499 } else {
1500 return false;
1501 }
1502 }
1503
1504 /**
1505 * Get the HTML text of the description page, if available
1506 *
1507 * @return string
1508 */
1509 function getDescriptionText() {
1510 global $wgMemc, $wgLang;
1511 if ( !$this->repo || !$this->repo->fetchDescription ) {
1512 return false;
1513 }
1514 $renderUrl = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgLang->getCode() );
1515 if ( $renderUrl ) {
1516 if ( $this->repo->descriptionCacheExpiry > 0 ) {
1517 wfDebug("Attempting to get the description from cache...");
1518 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', $wgLang->getCode(),
1519 $this->getName() );
1520 $obj = $wgMemc->get($key);
1521 if ($obj) {
1522 wfDebug("success!\n");
1523 return $obj;
1524 }
1525 wfDebug("miss\n");
1526 }
1527 wfDebug( "Fetching shared description from $renderUrl\n" );
1528 $res = Http::get( $renderUrl );
1529 if ( $res && $this->repo->descriptionCacheExpiry > 0 ) {
1530 $wgMemc->set( $key, $res, $this->repo->descriptionCacheExpiry );
1531 }
1532 return $res;
1533 } else {
1534 return false;
1535 }
1536 }
1537
1538 /**
1539 * Get discription of file revision
1540 * STUB
1541 *
1542 * @return string
1543 */
1544 function getDescription() {
1545 return null;
1546 }
1547
1548 /**
1549 * Get the 14-character timestamp of the file upload
1550 *
1551 * @return string|false TS_MW timestamp or false on failure
1552 */
1553 function getTimestamp() {
1554 $this->assertRepoDefined();
1555 return $this->repo->getFileTimestamp( $this->getPath() );
1556 }
1557
1558 /**
1559 * Get the SHA-1 base 36 hash of the file
1560 *
1561 * @return string
1562 */
1563 function getSha1() {
1564 $this->assertRepoDefined();
1565 return $this->repo->getFileSha1( $this->getPath() );
1566 }
1567
1568 /**
1569 * Get the deletion archive key, <sha1>.<ext>
1570 *
1571 * @return string
1572 */
1573 function getStorageKey() {
1574 $hash = $this->getSha1();
1575 if ( !$hash ) {
1576 return false;
1577 }
1578 $ext = $this->getExtension();
1579 $dotExt = $ext === '' ? '' : ".$ext";
1580 return $hash . $dotExt;
1581 }
1582
1583 /**
1584 * Determine if the current user is allowed to view a particular
1585 * field of this file, if it's marked as deleted.
1586 * STUB
1587 * @param $field Integer
1588 * @param $user User object to check, or null to use $wgUser
1589 * @return Boolean
1590 */
1591 function userCan( $field, User $user = null ) {
1592 return true;
1593 }
1594
1595 /**
1596 * Get an associative array containing information about a file in the local filesystem.
1597 *
1598 * @param $path String: absolute local filesystem path
1599 * @param $ext Mixed: the file extension, or true to extract it from the filename.
1600 * Set it to false to ignore the extension.
1601 *
1602 * @return array
1603 */
1604 static function getPropsFromPath( $path, $ext = true ) {
1605 wfDebug( __METHOD__.": Getting file info for $path\n" );
1606 wfDeprecated( __METHOD__, '1.19' );
1607
1608 $fsFile = new FSFile( $path );
1609 return $fsFile->getProps();
1610 }
1611
1612 /**
1613 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
1614 * encoding, zero padded to 31 digits.
1615 *
1616 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
1617 * fairly neatly.
1618 *
1619 * @param $path string
1620 *
1621 * @return false|string False on failure
1622 */
1623 static function sha1Base36( $path ) {
1624 wfDeprecated( __METHOD__, '1.19' );
1625
1626 $fsFile = new FSFile( $path );
1627 return $fsFile->getSha1Base36();
1628 }
1629
1630 /**
1631 * @return string
1632 */
1633 function getLongDesc() {
1634 $handler = $this->getHandler();
1635 if ( $handler ) {
1636 return $handler->getLongDesc( $this );
1637 } else {
1638 return MediaHandler::getGeneralLongDesc( $this );
1639 }
1640 }
1641
1642 /**
1643 * @return string
1644 */
1645 function getShortDesc() {
1646 $handler = $this->getHandler();
1647 if ( $handler ) {
1648 return $handler->getShortDesc( $this );
1649 } else {
1650 return MediaHandler::getGeneralShortDesc( $this );
1651 }
1652 }
1653
1654 /**
1655 * @return string
1656 */
1657 function getDimensionsString() {
1658 $handler = $this->getHandler();
1659 if ( $handler ) {
1660 return $handler->getDimensionsString( $this );
1661 } else {
1662 return '';
1663 }
1664 }
1665
1666 /**
1667 * @return
1668 */
1669 function getRedirected() {
1670 return $this->redirected;
1671 }
1672
1673 /**
1674 * @return Title
1675 */
1676 function getRedirectedTitle() {
1677 if ( $this->redirected ) {
1678 if ( !$this->redirectTitle ) {
1679 $this->redirectTitle = Title::makeTitle( NS_FILE, $this->redirected );
1680 }
1681 return $this->redirectTitle;
1682 }
1683 }
1684
1685 /**
1686 * @param $from
1687 * @return void
1688 */
1689 function redirectedFrom( $from ) {
1690 $this->redirected = $from;
1691 }
1692
1693 /**
1694 * @return bool
1695 */
1696 function isMissing() {
1697 return false;
1698 }
1699
1700 /**
1701 * Assert that $this->repo is set to a valid FileRepo instance
1702 * @throws MWException
1703 */
1704 protected function assertRepoDefined() {
1705 if ( !( $this->repo instanceof $this->repoClass ) ) {
1706 throw new MWException( "A {$this->repoClass} object is not set for this File.\n" );
1707 }
1708 }
1709
1710 /**
1711 * Assert that $this->title is set to a Title
1712 * @throws MWException
1713 */
1714 protected function assertTitleDefined() {
1715 if ( !( $this->title instanceof Title ) ) {
1716 throw new MWException( "A Title object is not set for this File.\n" );
1717 }
1718 }
1719 }