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