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