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