* Don't force rendering in File::getThumbnail(). The old code (pre-filerepo) required...
[lhc/web/wiklou.git] / includes / filerepo / File.php
1 <?php
2
3 /**
4 * Implements some public methods and some protected utility functions which
5 * are required by multiple child classes. Contains stub functionality for
6 * unimplemented public methods.
7 *
8 * Stub functions which should be overridden are marked with STUB. Some more
9 * concrete functions are also typically overridden by child classes.
10 *
11 * Note that only the repo object knows what its file class is called. You should
12 * never name a file class explictly outside of the repo class. Instead use the
13 * repo's factory functions to generate file objects, for example:
14 *
15 * RepoGroup::singleton()->getLocalRepo()->newFile($title);
16 *
17 * The convenience functions wfLocalFile() and wfFindFile() should be sufficient
18 * in most cases.
19 *
20 * @ingroup FileRepo
21 */
22 abstract class File {
23 const DELETED_FILE = 1;
24 const DELETED_COMMENT = 2;
25 const DELETED_USER = 4;
26 const DELETED_RESTRICTED = 8;
27 const RENDER_NOW = 1;
28
29 const DELETE_SOURCE = 1;
30
31 /**
32 * Some member variables can be lazy-initialised using __get(). The
33 * initialisation function for these variables is always a function named
34 * like getVar(), where Var is the variable name with upper-case first
35 * letter.
36 *
37 * The following variables are initialised in this way in this base class:
38 * name, extension, handler, path, canRender, isSafeFile,
39 * transformScript, hashPath, pageCount, url
40 *
41 * Code within this class should generally use the accessor function
42 * directly, since __get() isn't re-entrant and therefore causes bugs that
43 * depend on initialisation order.
44 */
45
46 /**
47 * The following member variables are not lazy-initialised
48 */
49 var $repo, $title, $lastError, $redirected, $redirectedTitle;
50
51 /**
52 * Call this constructor from child classes
53 */
54 function __construct( $title, $repo ) {
55 $this->title = $title;
56 $this->repo = $repo;
57 }
58
59 function __get( $name ) {
60 $function = array( $this, 'get' . ucfirst( $name ) );
61 if ( !is_callable( $function ) ) {
62 return null;
63 } else {
64 $this->$name = call_user_func( $function );
65 return $this->$name;
66 }
67 }
68
69 /**
70 * Normalize a file extension to the common form, and ensure it's clean.
71 * Extensions with non-alphanumeric characters will be discarded.
72 *
73 * @param $ext string (without the .)
74 * @return string
75 */
76 static function normalizeExtension( $ext ) {
77 $lower = strtolower( $ext );
78 $squish = array(
79 'htm' => 'html',
80 'jpeg' => 'jpg',
81 'mpeg' => 'mpg',
82 'tiff' => 'tif',
83 'ogv' => 'ogg' );
84 if( isset( $squish[$lower] ) ) {
85 return $squish[$lower];
86 } elseif( preg_match( '/^[0-9a-z]+$/', $lower ) ) {
87 return $lower;
88 } else {
89 return '';
90 }
91 }
92
93 /**
94 * Checks if file extensions are compatible
95 *
96 * @param $old File Old file
97 * @param $new string New name
98 */
99 static function checkExtensionCompatibility( File $old, $new ) {
100 $oldMime = $old->getMimeType();
101 $n = strrpos( $new, '.' );
102 $newExt = self::normalizeExtension(
103 $n ? substr( $new, $n + 1 ) : '' );
104 $mimeMagic = MimeMagic::singleton();
105 return $mimeMagic->isMatchingExtension( $newExt, $oldMime );
106 }
107
108 /**
109 * Upgrade the database row if there is one
110 * Called by ImagePage
111 * STUB
112 */
113 function upgradeRow() {}
114
115 /**
116 * Split an internet media type into its two components; if not
117 * a two-part name, set the minor type to 'unknown'.
118 *
119 * @param $mime "text/html" etc
120 * @return array ("text", "html") etc
121 */
122 static function splitMime( $mime ) {
123 if( strpos( $mime, '/' ) !== false ) {
124 return explode( '/', $mime, 2 );
125 } else {
126 return array( $mime, 'unknown' );
127 }
128 }
129
130 /**
131 * Return the name of this file
132 */
133 public function getName() {
134 if ( !isset( $this->name ) ) {
135 $this->name = $this->repo->getNameFromTitle( $this->title );
136 }
137 return $this->name;
138 }
139
140 /**
141 * Get the file extension, e.g. "svg"
142 */
143 function getExtension() {
144 if ( !isset( $this->extension ) ) {
145 $n = strrpos( $this->getName(), '.' );
146 $this->extension = self::normalizeExtension(
147 $n ? substr( $this->getName(), $n + 1 ) : '' );
148 }
149 return $this->extension;
150 }
151
152 /**
153 * Return the associated title object
154 */
155 public function getTitle() { return $this->title; }
156
157 /**
158 * Return the title used to find this file
159 */
160 public function getOriginalTitle() {
161 if ( $this->redirected )
162 return $this->getRedirectedTitle();
163 return $this->title;
164 }
165
166 /**
167 * Return the URL of the file
168 */
169 public function getUrl() {
170 if ( !isset( $this->url ) ) {
171 $this->url = $this->repo->getZoneUrl( 'public' ) . '/' . $this->getUrlRel();
172 }
173 return $this->url;
174 }
175
176 /**
177 * Return a fully-qualified URL to the file.
178 * Upload URL paths _may or may not_ be fully qualified, so
179 * we check. Local paths are assumed to belong on $wgServer.
180 * @return string
181 */
182 public function getFullUrl() {
183 return wfExpandUrl( $this->getUrl() );
184 }
185
186 function getViewURL() {
187 if( $this->mustRender()) {
188 if( $this->canRender() ) {
189 return $this->createThumb( $this->getWidth() );
190 }
191 else {
192 wfDebug(__METHOD__.': supposed to render '.$this->getName().' ('.$this->getMimeType()."), but can't!\n");
193 return $this->getURL(); #hm... return NULL?
194 }
195 } else {
196 return $this->getURL();
197 }
198 }
199
200 /**
201 * Return the full filesystem path to the file. Note that this does
202 * not mean that a file actually exists under that location.
203 *
204 * This path depends on whether directory hashing is active or not,
205 * i.e. whether the files are all found in the same directory,
206 * or in hashed paths like /images/3/3c.
207 *
208 * May return false if the file is not locally accessible.
209 */
210 public function getPath() {
211 if ( !isset( $this->path ) ) {
212 $this->path = $this->repo->getZonePath('public') . '/' . $this->getRel();
213 }
214 return $this->path;
215 }
216
217 /**
218 * Alias for getPath()
219 */
220 public function getFullPath() {
221 return $this->getPath();
222 }
223
224 /**
225 * Return the width of the image. Returns false if the width is unknown
226 * or undefined.
227 *
228 * STUB
229 * Overridden by LocalFile, UnregisteredLocalFile
230 */
231 public function getWidth( $page = 1 ) { return false; }
232
233 /**
234 * Return the height of the image. Returns false if the height is unknown
235 * or undefined
236 *
237 * STUB
238 * Overridden by LocalFile, UnregisteredLocalFile
239 */
240 public function getHeight( $page = 1 ) { return false; }
241
242 /**
243 * Returns ID or name of user who uploaded the file
244 * STUB
245 *
246 * @param $type string 'text' or 'id'
247 */
248 public function getUser( $type='text' ) { return null; }
249
250 /**
251 * Get the duration of a media file in seconds
252 */
253 public function getLength() {
254 $handler = $this->getHandler();
255 if ( $handler ) {
256 return $handler->getLength( $this );
257 } else {
258 return 0;
259 }
260 }
261
262 /**
263 * Get handler-specific metadata
264 * Overridden by LocalFile, UnregisteredLocalFile
265 * STUB
266 */
267 public function getMetadata() { return false; }
268
269 /**
270 * Return the bit depth of the file
271 * Overridden by LocalFile
272 * STUB
273 */
274 public function getBitDepth() { return 0; }
275
276 /**
277 * Return the size of the image file, in bytes
278 * Overridden by LocalFile, UnregisteredLocalFile
279 * STUB
280 */
281 public function getSize() { return false; }
282
283 /**
284 * Returns the mime type of the file.
285 * Overridden by LocalFile, UnregisteredLocalFile
286 * STUB
287 */
288 function getMimeType() { return 'unknown/unknown'; }
289
290 /**
291 * Return the type of the media in the file.
292 * Use the value returned by this function with the MEDIATYPE_xxx constants.
293 * Overridden by LocalFile,
294 * STUB
295 */
296 function getMediaType() { return MEDIATYPE_UNKNOWN; }
297
298 /**
299 * Checks if the output of transform() for this file is likely
300 * to be valid. If this is false, various user elements will
301 * display a placeholder instead.
302 *
303 * Currently, this checks if the file is an image format
304 * that can be converted to a format
305 * supported by all browsers (namely GIF, PNG and JPEG),
306 * or if it is an SVG image and SVG conversion is enabled.
307 */
308 function canRender() {
309 if ( !isset( $this->canRender ) ) {
310 $this->canRender = $this->getHandler() && $this->handler->canRender( $this );
311 }
312 return $this->canRender;
313 }
314
315 /**
316 * Accessor for __get()
317 */
318 protected function getCanRender() {
319 return $this->canRender();
320 }
321
322 /**
323 * Return true if the file is of a type that can't be directly
324 * rendered by typical browsers and needs to be re-rasterized.
325 *
326 * This returns true for everything but the bitmap types
327 * supported by all browsers, i.e. JPEG; GIF and PNG. It will
328 * also return true for any non-image formats.
329 *
330 * @return bool
331 */
332 function mustRender() {
333 return $this->getHandler() && $this->handler->mustRender( $this );
334 }
335
336 /**
337 * Alias for canRender()
338 */
339 function allowInlineDisplay() {
340 return $this->canRender();
341 }
342
343 /**
344 * Determines if this media file is in a format that is unlikely to
345 * contain viruses or malicious content. It uses the global
346 * $wgTrustedMediaFormats list to determine if the file is safe.
347 *
348 * This is used to show a warning on the description page of non-safe files.
349 * It may also be used to disallow direct [[media:...]] links to such files.
350 *
351 * Note that this function will always return true if allowInlineDisplay()
352 * or isTrustedFile() is true for this file.
353 */
354 function isSafeFile() {
355 if ( !isset( $this->isSafeFile ) ) {
356 $this->isSafeFile = $this->_getIsSafeFile();
357 }
358 return $this->isSafeFile;
359 }
360
361 /** Accessor for __get() */
362 protected function getIsSafeFile() {
363 return $this->isSafeFile();
364 }
365
366 /** Uncached accessor */
367 protected function _getIsSafeFile() {
368 if ($this->allowInlineDisplay()) return true;
369 if ($this->isTrustedFile()) return true;
370
371 global $wgTrustedMediaFormats;
372
373 $type= $this->getMediaType();
374 $mime= $this->getMimeType();
375 #wfDebug("LocalFile::isSafeFile: type= $type, mime= $mime\n");
376
377 if (!$type || $type===MEDIATYPE_UNKNOWN) return false; #unknown type, not trusted
378 if ( in_array( $type, $wgTrustedMediaFormats) ) return true;
379
380 if ($mime==="unknown/unknown") return false; #unknown type, not trusted
381 if ( in_array( $mime, $wgTrustedMediaFormats) ) return true;
382
383 return false;
384 }
385
386 /** Returns true if the file is flagged as trusted. Files flagged that way
387 * can be linked to directly, even if that is not allowed for this type of
388 * file normally.
389 *
390 * This is a dummy function right now and always returns false. It could be
391 * implemented to extract a flag from the database. The trusted flag could be
392 * set on upload, if the user has sufficient privileges, to bypass script-
393 * and html-filters. It may even be coupled with cryptographics signatures
394 * or such.
395 */
396 function isTrustedFile() {
397 #this could be implemented to check a flag in the databas,
398 #look for signatures, etc
399 return false;
400 }
401
402 /**
403 * Returns true if file exists in the repository.
404 *
405 * Overridden by LocalFile to avoid unnecessary stat calls.
406 *
407 * @return boolean Whether file exists in the repository.
408 */
409 public function exists() {
410 return $this->getPath() && file_exists( $this->path );
411 }
412
413 /**
414 * Returns true if file exists in the repository and can be included in a page.
415 * It would be unsafe to include private images, making public thumbnails inadvertently
416 *
417 * @return boolean Whether file exists in the repository and is includable.
418 * @public
419 */
420 function isVisible() {
421 return $this->exists();
422 }
423
424 function getTransformScript() {
425 if ( !isset( $this->transformScript ) ) {
426 $this->transformScript = false;
427 if ( $this->repo ) {
428 $script = $this->repo->getThumbScriptUrl();
429 if ( $script ) {
430 $this->transformScript = "$script?f=" . urlencode( $this->getName() );
431 }
432 }
433 }
434 return $this->transformScript;
435 }
436
437 /**
438 * Get a ThumbnailImage which is the same size as the source
439 */
440 function getUnscaledThumb( $page = false ) {
441 $width = $this->getWidth( $page );
442 if ( !$width ) {
443 return $this->iconThumb();
444 }
445 if ( $page ) {
446 $params = array(
447 'page' => $page,
448 'width' => $this->getWidth( $page )
449 );
450 } else {
451 $params = array( 'width' => $this->getWidth() );
452 }
453 return $this->transform( $params );
454 }
455
456 /**
457 * Return the file name of a thumbnail with the specified parameters
458 *
459 * @param array $params Handler-specific parameters
460 * @private -ish
461 */
462 function thumbName( $params ) {
463 if ( !$this->getHandler() ) {
464 return null;
465 }
466 $extension = $this->getExtension();
467 list( $thumbExt, $thumbMime ) = $this->handler->getThumbType( $extension, $this->getMimeType() );
468 $thumbName = $this->handler->makeParamString( $params ) . '-' . $this->getName();
469 if ( $thumbExt != $extension ) {
470 $thumbName .= ".$thumbExt";
471 }
472 return $thumbName;
473 }
474
475 /**
476 * Create a thumbnail of the image having the specified width/height.
477 * The thumbnail will not be created if the width is larger than the
478 * image's width. Let the browser do the scaling in this case.
479 * The thumbnail is stored on disk and is only computed if the thumbnail
480 * file does not exist OR if it is older than the image.
481 * Returns the URL.
482 *
483 * Keeps aspect ratio of original image. If both width and height are
484 * specified, the generated image will be no bigger than width x height,
485 * and will also have correct aspect ratio.
486 *
487 * @param integer $width maximum width of the generated thumbnail
488 * @param integer $height maximum height of the image (optional)
489 */
490 public function createThumb( $width, $height = -1 ) {
491 $params = array( 'width' => $width );
492 if ( $height != -1 ) {
493 $params['height'] = $height;
494 }
495 $thumb = $this->transform( $params );
496 if( is_null( $thumb ) || $thumb->isError() ) return '';
497 return $thumb->getUrl();
498 }
499
500 /**
501 * As createThumb, but returns a ThumbnailImage object. This can
502 * provide access to the actual file, the real size of the thumb,
503 * and can produce a convenient <img> tag for you.
504 *
505 * For non-image formats, this may return a filetype-specific icon.
506 *
507 * @param integer $width maximum width of the generated thumbnail
508 * @param integer $height maximum height of the image (optional)
509 * @param boolean $render Deprecated
510 *
511 * @return ThumbnailImage or null on failure
512 *
513 * @deprecated use transform()
514 */
515 public function getThumbnail( $width, $height=-1, $render = true ) {
516 $params = array( 'width' => $width );
517 if ( $height != -1 ) {
518 $params['height'] = $height;
519 }
520 return $this->transform( $params, $flags );
521 }
522
523 /**
524 * Transform a media file
525 *
526 * @param array $params An associative array of handler-specific parameters. Typical
527 * keys are width, height and page.
528 * @param integer $flags A bitfield, may contain self::RENDER_NOW to force rendering
529 * @return MediaTransformOutput
530 */
531 function transform( $params, $flags = 0 ) {
532 global $wgUseSquid, $wgIgnoreImageErrors;
533
534 wfProfileIn( __METHOD__ );
535 do {
536 if ( !$this->canRender() ) {
537 // not a bitmap or renderable image, don't try.
538 $thumb = $this->iconThumb();
539 break;
540 }
541
542 $script = $this->getTransformScript();
543 if ( $script && !($flags & self::RENDER_NOW) ) {
544 // Use a script to transform on client request, if possible
545 $thumb = $this->handler->getScriptedTransform( $this, $script, $params );
546 if( $thumb ) {
547 break;
548 }
549 }
550
551 $normalisedParams = $params;
552 $this->handler->normaliseParams( $this, $normalisedParams );
553 $thumbName = $this->thumbName( $normalisedParams );
554 $thumbPath = $this->getThumbPath( $thumbName );
555 $thumbUrl = $this->getThumbUrl( $thumbName );
556
557 if ( $this->repo->canTransformVia404() && !($flags & self::RENDER_NOW ) ) {
558 $thumb = $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
559 break;
560 }
561
562 wfDebug( __METHOD__.": Doing stat for $thumbPath\n" );
563 $this->migrateThumbFile( $thumbName );
564 if ( file_exists( $thumbPath ) ) {
565 $thumb = $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
566 break;
567 }
568 $thumb = $this->handler->doTransform( $this, $thumbPath, $thumbUrl, $params );
569
570 // Ignore errors if requested
571 if ( !$thumb ) {
572 $thumb = null;
573 } elseif ( $thumb->isError() ) {
574 $this->lastError = $thumb->toText();
575 if ( $wgIgnoreImageErrors && !($flags & self::RENDER_NOW) ) {
576 $thumb = $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
577 }
578 }
579
580 // Purge. Useful in the event of Core -> Squid connection failure or squid
581 // purge collisions from elsewhere during failure. Don't keep triggering for
582 // "thumbs" which have the main image URL though (bug 13776)
583 if ( $wgUseSquid && ( !$thumb || $thumb->isError() || $thumb->getUrl() != $this->getURL()) ) {
584 SquidUpdate::purge( array( $thumbUrl ) );
585 }
586 } while (false);
587
588 wfProfileOut( __METHOD__ );
589 return $thumb;
590 }
591
592 /**
593 * Hook into transform() to allow migration of thumbnail files
594 * STUB
595 * Overridden by LocalFile
596 */
597 function migrateThumbFile( $thumbName ) {}
598
599 /**
600 * Get a MediaHandler instance for this file
601 */
602 function getHandler() {
603 if ( !isset( $this->handler ) ) {
604 $this->handler = MediaHandler::getHandler( $this->getMimeType() );
605 }
606 return $this->handler;
607 }
608
609 /**
610 * Get a ThumbnailImage representing a file type icon
611 * @return ThumbnailImage
612 */
613 function iconThumb() {
614 global $wgStylePath, $wgStyleDirectory;
615
616 $try = array( 'fileicon-' . $this->getExtension() . '.png', 'fileicon.png' );
617 foreach( $try as $icon ) {
618 $path = '/common/images/icons/' . $icon;
619 $filepath = $wgStyleDirectory . $path;
620 if( file_exists( $filepath ) ) {
621 return new ThumbnailImage( $this, $wgStylePath . $path, 120, 120 );
622 }
623 }
624 return null;
625 }
626
627 /**
628 * Get last thumbnailing error.
629 * Largely obsolete.
630 */
631 function getLastError() {
632 return $this->lastError;
633 }
634
635 /**
636 * Get all thumbnail names previously generated for this file
637 * STUB
638 * Overridden by LocalFile
639 */
640 function getThumbnails() { return array(); }
641
642 /**
643 * Purge shared caches such as thumbnails and DB data caching
644 * STUB
645 * Overridden by LocalFile
646 */
647 function purgeCache() {}
648
649 /**
650 * Purge the file description page, but don't go after
651 * pages using the file. Use when modifying file history
652 * but not the current data.
653 */
654 function purgeDescription() {
655 $title = $this->getTitle();
656 if ( $title ) {
657 $title->invalidateCache();
658 $title->purgeSquid();
659 }
660 }
661
662 /**
663 * Purge metadata and all affected pages when the file is created,
664 * deleted, or majorly updated.
665 */
666 function purgeEverything() {
667 // Delete thumbnails and refresh file metadata cache
668 $this->purgeCache();
669 $this->purgeDescription();
670
671 // Purge cache of all pages using this file
672 $title = $this->getTitle();
673 if ( $title ) {
674 $update = new HTMLCacheUpdate( $title, 'imagelinks' );
675 $update->doUpdate();
676 }
677 }
678
679 /**
680 * Return a fragment of the history of file.
681 *
682 * STUB
683 * @param $limit integer Limit of rows to return
684 * @param $start timestamp Only revisions older than $start will be returned
685 * @param $end timestamp Only revisions newer than $end will be returned
686 */
687 function getHistory($limit = null, $start = null, $end = null) {
688 return array();
689 }
690
691 /**
692 * Return the history of this file, line by line. Starts with current version,
693 * then old versions. Should return an object similar to an image/oldimage
694 * database row.
695 *
696 * STUB
697 * Overridden in LocalFile
698 */
699 public function nextHistoryLine() {
700 return false;
701 }
702
703 /**
704 * Reset the history pointer to the first element of the history.
705 * Always call this function after using nextHistoryLine() to free db resources
706 * STUB
707 * Overridden in LocalFile.
708 */
709 public function resetHistory() {}
710
711 /**
712 * Get the filename hash component of the directory including trailing slash,
713 * e.g. f/fa/
714 * If the repository is not hashed, returns an empty string.
715 */
716 function getHashPath() {
717 if ( !isset( $this->hashPath ) ) {
718 $this->hashPath = $this->repo->getHashPath( $this->getName() );
719 }
720 return $this->hashPath;
721 }
722
723 /**
724 * Get the path of the file relative to the public zone root
725 */
726 function getRel() {
727 return $this->getHashPath() . $this->getName();
728 }
729
730 /**
731 * Get urlencoded relative path of the file
732 */
733 function getUrlRel() {
734 return $this->getHashPath() . rawurlencode( $this->getName() );
735 }
736
737 /** Get the relative path for an archive file */
738 function getArchiveRel( $suffix = false ) {
739 $path = 'archive/' . $this->getHashPath();
740 if ( $suffix === false ) {
741 $path = substr( $path, 0, -1 );
742 } else {
743 $path .= $suffix;
744 }
745 return $path;
746 }
747
748 /** Get relative path for a thumbnail file */
749 function getThumbRel( $suffix = false ) {
750 $path = $this->repo->thumbDir . $this->getRel();
751 if ( $suffix !== false ) {
752 $path .= '/' . $suffix;
753 }
754 return $path;
755 }
756
757 /** Get the path of the archive directory, or a particular file if $suffix is specified */
758 function getArchivePath( $suffix = false ) {
759 return $this->repo->getZonePath('public') . '/' . $this->getArchiveRel( $suffix );
760 }
761
762 /** Get the path of the thumbnail directory, or a particular file if $suffix is specified */
763 function getThumbPath( $suffix = false ) {
764 return $this->repo->getZonePath('public') . '/' . $this->getThumbRel( $suffix );
765 }
766
767 /** Get the URL of the archive directory, or a particular file if $suffix is specified */
768 function getArchiveUrl( $suffix = false ) {
769 $path = $this->repo->getZoneUrl('public') . '/archive/' . $this->getHashPath();
770 if ( $suffix === false ) {
771 $path = substr( $path, 0, -1 );
772 } else {
773 $path .= rawurlencode( $suffix );
774 }
775 return $path;
776 }
777
778 /** Get the URL of the thumbnail directory, or a particular file if $suffix is specified */
779 function getThumbUrl( $suffix = false ) {
780 $path = $this->repo->getZoneUrl('public') . '/'. $this->repo->thumbDir . $this->getUrlRel();
781 if ( $suffix !== false ) {
782 $path .= '/' . rawurlencode( $suffix );
783 }
784 return $path;
785 }
786
787 /** Get the virtual URL for an archive file or directory */
788 function getArchiveVirtualUrl( $suffix = false ) {
789 $path = $this->repo->getVirtualUrl() . '/public/archive/' . $this->getHashPath();
790 if ( $suffix === false ) {
791 $path = substr( $path, 0, -1 );
792 } else {
793 $path .= rawurlencode( $suffix );
794 }
795 return $path;
796 }
797
798 /** Get the virtual URL for a thumbnail file or directory */
799 function getThumbVirtualUrl( $suffix = false ) {
800 $path = $this->repo->getVirtualUrl() . '/public/' . $this->repo->thumbDir . $this->getUrlRel();
801 if ( $suffix !== false ) {
802 $path .= '/' . rawurlencode( $suffix );
803 }
804 return $path;
805 }
806
807 /** Get the virtual URL for the file itself */
808 function getVirtualUrl( $suffix = false ) {
809 $path = $this->repo->getVirtualUrl() . '/public/' . $this->getUrlRel();
810 if ( $suffix !== false ) {
811 $path .= '/' . rawurlencode( $suffix );
812 }
813 return $path;
814 }
815
816 /**
817 * @return bool
818 */
819 function isHashed() {
820 return $this->repo->isHashed();
821 }
822
823 function readOnlyError() {
824 throw new MWException( get_class($this) . ': write operations are not supported' );
825 }
826
827 /**
828 * Record a file upload in the upload log and the image table
829 * STUB
830 * Overridden by LocalFile
831 */
832 function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '', $watch = false ) {
833 $this->readOnlyError();
834 }
835
836 /**
837 * Move or copy a file to its public location. If a file exists at the
838 * destination, move it to an archive. Returns the archive name on success
839 * or an empty string if it was a new file, and a wikitext-formatted
840 * WikiError object on failure.
841 *
842 * The archive name should be passed through to recordUpload for database
843 * registration.
844 *
845 * @param string $sourcePath Local filesystem path to the source image
846 * @param integer $flags A bitwise combination of:
847 * File::DELETE_SOURCE Delete the source file, i.e. move
848 * rather than copy
849 * @return The archive name on success or an empty string if it was a new
850 * file, and a wikitext-formatted WikiError object on failure.
851 *
852 * STUB
853 * Overridden by LocalFile
854 */
855 function publish( $srcPath, $flags = 0 ) {
856 $this->readOnlyError();
857 }
858
859 /**
860 * Get an array of Title objects which are articles which use this file
861 * Also adds their IDs to the link cache
862 *
863 * This is mostly copied from Title::getLinksTo()
864 *
865 * @deprecated Use HTMLCacheUpdate, this function uses too much memory
866 */
867 function getLinksTo( $options = '' ) {
868 wfProfileIn( __METHOD__ );
869
870 // Note: use local DB not repo DB, we want to know local links
871 if ( $options ) {
872 $db = wfGetDB( DB_MASTER );
873 } else {
874 $db = wfGetDB( DB_SLAVE );
875 }
876 $linkCache = LinkCache::singleton();
877
878 list( $page, $imagelinks ) = $db->tableNamesN( 'page', 'imagelinks' );
879 $encName = $db->addQuotes( $this->getName() );
880 $sql = "SELECT page_namespace,page_title,page_id,page_len,page_is_redirect,
881 FROM $page,$imagelinks WHERE page_id=il_from AND il_to=$encName $options";
882 $res = $db->query( $sql, __METHOD__ );
883
884 $retVal = array();
885 if ( $db->numRows( $res ) ) {
886 while ( $row = $db->fetchObject( $res ) ) {
887 if ( $titleObj = Title::newFromRow( $row ) ) {
888 $linkCache->addGoodLinkObj( $row->page_id, $titleObj, $row->page_len, $row->page_is_redirect );
889 $retVal[] = $titleObj;
890 }
891 }
892 }
893 $db->freeResult( $res );
894 wfProfileOut( __METHOD__ );
895 return $retVal;
896 }
897
898 function formatMetadata() {
899 if ( !$this->getHandler() ) {
900 return false;
901 }
902 return $this->getHandler()->formatMetadata( $this, $this->getMetadata() );
903 }
904
905 /**
906 * Returns true if the file comes from the local file repository.
907 *
908 * @return bool
909 */
910 function isLocal() {
911 return $this->getRepoName() == 'local';
912 }
913
914 /**
915 * Returns the name of the repository.
916 *
917 * @return string
918 */
919 function getRepoName() {
920 return $this->repo ? $this->repo->getName() : 'unknown';
921 }
922 /*
923 * Returns the repository
924 */
925 function getRepo() {
926 return $this->repo;
927 }
928
929 /**
930 * Returns true if the image is an old version
931 * STUB
932 */
933 function isOld() {
934 return false;
935 }
936
937 /**
938 * Is this file a "deleted" file in a private archive?
939 * STUB
940 */
941 function isDeleted( $field ) {
942 return false;
943 }
944
945 /**
946 * Was this file ever deleted from the wiki?
947 *
948 * @return bool
949 */
950 function wasDeleted() {
951 $title = $this->getTitle();
952 return $title && $title->isDeleted() > 0;
953 }
954
955 /**
956 * Move file to the new title
957 *
958 * Move current, old version and all thumbnails
959 * to the new filename. Old file is deleted.
960 *
961 * Cache purging is done; checks for validity
962 * and logging are caller's responsibility
963 *
964 * @param $target Title New file name
965 * @return FileRepoStatus object.
966 */
967 function move( $target ) {
968 $this->readOnlyError();
969 }
970
971 /**
972 * Delete all versions of the file.
973 *
974 * Moves the files into an archive directory (or deletes them)
975 * and removes the database rows.
976 *
977 * Cache purging is done; logging is caller's responsibility.
978 *
979 * @param $reason
980 * @param $suppress, hide content from sysops?
981 * @return true on success, false on some kind of failure
982 * STUB
983 * Overridden by LocalFile
984 */
985 function delete( $reason, $suppress = false ) {
986 $this->readOnlyError();
987 }
988
989 /**
990 * Restore all or specified deleted revisions to the given file.
991 * Permissions and logging are left to the caller.
992 *
993 * May throw database exceptions on error.
994 *
995 * @param $versions set of record ids of deleted items to restore,
996 * or empty to restore all revisions.
997 * @param $unsuppress, remove restrictions on content upon restoration?
998 * @return the number of file revisions restored if successful,
999 * or false on failure
1000 * STUB
1001 * Overridden by LocalFile
1002 */
1003 function restore( $versions=array(), $unsuppress=false ) {
1004 $this->readOnlyError();
1005 }
1006
1007 /**
1008 * Returns 'true' if this image is a multipage document, e.g. a DJVU
1009 * document.
1010 *
1011 * @return Bool
1012 */
1013 function isMultipage() {
1014 return $this->getHandler() && $this->handler->isMultiPage( $this );
1015 }
1016
1017 /**
1018 * Returns the number of pages of a multipage document, or NULL for
1019 * documents which aren't multipage documents
1020 */
1021 function pageCount() {
1022 if ( !isset( $this->pageCount ) ) {
1023 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
1024 $this->pageCount = $this->handler->pageCount( $this );
1025 } else {
1026 $this->pageCount = false;
1027 }
1028 }
1029 return $this->pageCount;
1030 }
1031
1032 /**
1033 * Calculate the height of a thumbnail using the source and destination width
1034 */
1035 static function scaleHeight( $srcWidth, $srcHeight, $dstWidth ) {
1036 // Exact integer multiply followed by division
1037 if ( $srcWidth == 0 ) {
1038 return 0;
1039 } else {
1040 return round( $srcHeight * $dstWidth / $srcWidth );
1041 }
1042 }
1043
1044 /**
1045 * Get an image size array like that returned by getimagesize(), or false if it
1046 * can't be determined.
1047 *
1048 * @param string $fileName The filename
1049 * @return array
1050 */
1051 function getImageSize( $fileName ) {
1052 if ( !$this->getHandler() ) {
1053 return false;
1054 }
1055 return $this->handler->getImageSize( $this, $fileName );
1056 }
1057
1058 /**
1059 * Get the URL of the image description page. May return false if it is
1060 * unknown or not applicable.
1061 */
1062 function getDescriptionUrl() {
1063 return $this->repo->getDescriptionUrl( $this->getName() );
1064 }
1065
1066 /**
1067 * Get the HTML text of the description page, if available
1068 */
1069 function getDescriptionText() {
1070 global $wgMemc;
1071 if ( !$this->repo->fetchDescription ) {
1072 return false;
1073 }
1074 $renderUrl = $this->repo->getDescriptionRenderUrl( $this->getName() );
1075 if ( $renderUrl ) {
1076 if ( $this->repo->descriptionCacheExpiry > 0 ) {
1077 wfDebug("Attempting to get the description from cache...");
1078 $key = wfMemcKey( 'RemoteFileDescription', 'url', md5($renderUrl) );
1079 $obj = $wgMemc->get($key);
1080 if ($obj) {
1081 wfDebug("success!\n");
1082 return $obj;
1083 }
1084 wfDebug("miss\n");
1085 }
1086 wfDebug( "Fetching shared description from $renderUrl\n" );
1087 $res = Http::get( $renderUrl );
1088 if ( $res && $this->repo->descriptionCacheExpiry > 0 ) $wgMemc->set( $key, $res, $this->repo->descriptionCacheExpiry );
1089 return $res;
1090 } else {
1091 return false;
1092 }
1093 }
1094
1095 /**
1096 * Get discription of file revision
1097 * STUB
1098 */
1099 function getDescription() {
1100 return null;
1101 }
1102
1103 /**
1104 * Get the 14-character timestamp of the file upload, or false if
1105 * it doesn't exist
1106 */
1107 function getTimestamp() {
1108 $path = $this->getPath();
1109 if ( !file_exists( $path ) ) {
1110 return false;
1111 }
1112 return wfTimestamp( TS_MW, filemtime( $path ) );
1113 }
1114
1115 /**
1116 * Get the SHA-1 base 36 hash of the file
1117 */
1118 function getSha1() {
1119 return self::sha1Base36( $this->getPath() );
1120 }
1121
1122 /**
1123 * Determine if the current user is allowed to view a particular
1124 * field of this file, if it's marked as deleted.
1125 * STUB
1126 * @param int $field
1127 * @return bool
1128 */
1129 function userCan( $field ) {
1130 return true;
1131 }
1132
1133 /**
1134 * Get an associative array containing information about a file in the local filesystem.
1135 *
1136 * @param string $path Absolute local filesystem path
1137 * @param mixed $ext The file extension, or true to extract it from the filename.
1138 * Set it to false to ignore the extension.
1139 */
1140 static function getPropsFromPath( $path, $ext = true ) {
1141 wfProfileIn( __METHOD__ );
1142 wfDebug( __METHOD__.": Getting file info for $path\n" );
1143 $info = array(
1144 'fileExists' => file_exists( $path ) && !is_dir( $path )
1145 );
1146 $gis = false;
1147 if ( $info['fileExists'] ) {
1148 $magic = MimeMagic::singleton();
1149
1150 $info['mime'] = $magic->guessMimeType( $path, $ext );
1151 list( $info['major_mime'], $info['minor_mime'] ) = self::splitMime( $info['mime'] );
1152 $info['media_type'] = $magic->getMediaType( $path, $info['mime'] );
1153
1154 # Get size in bytes
1155 $info['size'] = filesize( $path );
1156
1157 # Height, width and metadata
1158 $handler = MediaHandler::getHandler( $info['mime'] );
1159 if ( $handler ) {
1160 $tempImage = (object)array();
1161 $info['metadata'] = $handler->getMetadata( $tempImage, $path );
1162 $gis = $handler->getImageSize( $tempImage, $path, $info['metadata'] );
1163 } else {
1164 $gis = false;
1165 $info['metadata'] = '';
1166 }
1167 $info['sha1'] = self::sha1Base36( $path );
1168
1169 wfDebug(__METHOD__.": $path loaded, {$info['size']} bytes, {$info['mime']}.\n");
1170 } else {
1171 $info['mime'] = NULL;
1172 $info['media_type'] = MEDIATYPE_UNKNOWN;
1173 $info['metadata'] = '';
1174 $info['sha1'] = '';
1175 wfDebug(__METHOD__.": $path NOT FOUND!\n");
1176 }
1177 if( $gis ) {
1178 # NOTE: $gis[2] contains a code for the image type. This is no longer used.
1179 $info['width'] = $gis[0];
1180 $info['height'] = $gis[1];
1181 if ( isset( $gis['bits'] ) ) {
1182 $info['bits'] = $gis['bits'];
1183 } else {
1184 $info['bits'] = 0;
1185 }
1186 } else {
1187 $info['width'] = 0;
1188 $info['height'] = 0;
1189 $info['bits'] = 0;
1190 }
1191 wfProfileOut( __METHOD__ );
1192 return $info;
1193 }
1194
1195 /**
1196 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
1197 * encoding, zero padded to 31 digits.
1198 *
1199 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
1200 * fairly neatly.
1201 *
1202 * Returns false on failure
1203 */
1204 static function sha1Base36( $path ) {
1205 wfSuppressWarnings();
1206 $hash = sha1_file( $path );
1207 wfRestoreWarnings();
1208 if ( $hash === false ) {
1209 return false;
1210 } else {
1211 return wfBaseConvert( $hash, 16, 36, 31 );
1212 }
1213 }
1214
1215 function getLongDesc() {
1216 $handler = $this->getHandler();
1217 if ( $handler ) {
1218 return $handler->getLongDesc( $this );
1219 } else {
1220 return MediaHandler::getLongDesc( $this );
1221 }
1222 }
1223
1224 function getShortDesc() {
1225 $handler = $this->getHandler();
1226 if ( $handler ) {
1227 return $handler->getShortDesc( $this );
1228 } else {
1229 return MediaHandler::getShortDesc( $this );
1230 }
1231 }
1232
1233 function getDimensionsString() {
1234 $handler = $this->getHandler();
1235 if ( $handler ) {
1236 return $handler->getDimensionsString( $this );
1237 } else {
1238 return '';
1239 }
1240 }
1241
1242 function getRedirected() {
1243 return $this->redirected;
1244 }
1245
1246 function getRedirectedTitle() {
1247 if ( $this->redirected ) {
1248 if ( !$this->redirectTitle )
1249 $this->redirectTitle = Title::makeTitle( NS_IMAGE, $this->redirected );
1250 return $this->redirectTitle;
1251 }
1252 }
1253
1254 function redirectedFrom( $from ) {
1255 $this->redirected = $from;
1256 }
1257 }
1258 /**
1259 * Aliases for backwards compatibility with 1.6
1260 */
1261 define( 'MW_IMG_DELETED_FILE', File::DELETED_FILE );
1262 define( 'MW_IMG_DELETED_COMMENT', File::DELETED_COMMENT );
1263 define( 'MW_IMG_DELETED_USER', File::DELETED_USER );
1264 define( 'MW_IMG_DELETED_RESTRICTED', File::DELETED_RESTRICTED );