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