*Title fix
[lhc/web/wiklou.git] / includes / filerepo / File.php
1 <?php
2
3 /**
4 * Base file class. Do not instantiate.
5 *
6 * Implements some public methods and some protected utility functions which
7 * are required by multiple child classes. Contains stub functionality for
8 * unimplemented public methods.
9 *
10 * Stub functions which should be overridden are marked with STUB. Some more
11 * concrete functions are also typically overridden by child classes.
12 *
13 * Note that only the repo object knows what its file class is called. You should
14 * never name a file class explictly outside of the repo class. Instead use the
15 * repo's factory functions to generate file objects, for example:
16 *
17 * RepoGroup::singleton()->getLocalRepo()->newFile($title);
18 *
19 * The convenience functions wfLocalFile() and wfFindFile() should be sufficient
20 * in most cases.
21 *
22 * @addtogroup FileRepo
23 */
24 class File {
25 const DELETED_FILE = 1;
26 const DELETED_COMMENT = 2;
27 const DELETED_USER = 4;
28 const DELETED_RESTRICTED = 8;
29 const RENDER_NOW = 1;
30
31 const DELETE_SOURCE = 1;
32
33 /**
34 * Some member variables can be lazy-initialised using __get(). The
35 * initialisation function for these variables is always a function named
36 * like getVar(), where Var is the variable name with upper-case first
37 * letter.
38 *
39 * The following variables are initialised in this way in this base class:
40 * name, extension, handler, path, canRender, isSafeFile,
41 * transformScript, hashPath, pageCount, url
42 *
43 * Code within this class should generally use the accessor function
44 * directly, since __get() isn't re-entrant and therefore causes bugs that
45 * depend on initialisation order.
46 */
47
48 /**
49 * The following member variables are not lazy-initialised
50 */
51 var $repo, $title, $lastError;
52
53 /**
54 * Call this constructor from child classes
55 */
56 function __construct( $title, $repo ) {
57 $this->title = $title;
58 $this->repo = $repo;
59 }
60
61 function __get( $name ) {
62 $function = array( $this, 'get' . ucfirst( $name ) );
63 if ( !is_callable( $function ) ) {
64 return null;
65 } else {
66 $this->$name = call_user_func( $function );
67 return $this->$name;
68 }
69 }
70
71 /**
72 * Normalize a file extension to the common form, and ensure it's clean.
73 * Extensions with non-alphanumeric characters will be discarded.
74 *
75 * @param $ext string (without the .)
76 * @return string
77 */
78 static function normalizeExtension( $ext ) {
79 $lower = strtolower( $ext );
80 $squish = array(
81 'htm' => 'html',
82 'jpeg' => 'jpg',
83 'mpeg' => 'mpg',
84 'tiff' => 'tif' );
85 if( isset( $squish[$lower] ) ) {
86 return $squish[$lower];
87 } elseif( preg_match( '/^[0-9a-z]+$/', $lower ) ) {
88 return $lower;
89 } else {
90 return '';
91 }
92 }
93
94 /**
95 * Upgrade the database row if there is one
96 * Called by ImagePage
97 * STUB
98 */
99 function upgradeRow() {}
100
101 /**
102 * Split an internet media type into its two components; if not
103 * a two-part name, set the minor type to 'unknown'.
104 *
105 * @param $mime "text/html" etc
106 * @return array ("text", "html") etc
107 */
108 static function splitMime( $mime ) {
109 if( strpos( $mime, '/' ) !== false ) {
110 return explode( '/', $mime, 2 );
111 } else {
112 return array( $mime, 'unknown' );
113 }
114 }
115
116 /**
117 * Return the name of this file
118 * @public
119 */
120 function getName() {
121 if ( !isset( $this->name ) ) {
122 $this->name = $this->title->getDBkey();
123 }
124 return $this->name;
125 }
126
127 /**
128 * Get the file extension, e.g. "svg"
129 */
130 function getExtension() {
131 if ( !isset( $this->extension ) ) {
132 $n = strrpos( $this->getName(), '.' );
133 $this->extension = self::normalizeExtension(
134 $n ? substr( $this->getName(), $n + 1 ) : '' );
135 }
136 return $this->extension;
137 }
138
139 /**
140 * Return the associated title object
141 * @public
142 */
143 function getTitle() { return $this->title; }
144
145 /**
146 * Return the URL of the file
147 * @public
148 */
149 function getUrl() {
150 if ( !isset( $this->url ) ) {
151 $this->url = $this->repo->getZoneUrl( 'public' ) . '/' . $this->getUrlRel();
152 }
153 return $this->url;
154 }
155
156 function getViewURL() {
157 if( $this->mustRender()) {
158 if( $this->canRender() ) {
159 return $this->createThumb( $this->getWidth() );
160 }
161 else {
162 wfDebug(__METHOD__.': supposed to render '.$this->getName().' ('.$this->getMimeType()."), but can't!\n");
163 return $this->getURL(); #hm... return NULL?
164 }
165 } else {
166 return $this->getURL();
167 }
168 }
169
170 /**
171 * Return the full filesystem path to the file. Note that this does
172 * not mean that a file actually exists under that location.
173 *
174 * This path depends on whether directory hashing is active or not,
175 * i.e. whether the files are all found in the same directory,
176 * or in hashed paths like /images/3/3c.
177 *
178 * May return false if the file is not locally accessible.
179 *
180 * @public
181 */
182 function getPath() {
183 if ( !isset( $this->path ) ) {
184 $this->path = $this->repo->getZonePath('public') . '/' . $this->getRel();
185 }
186 return $this->path;
187 }
188
189 /**
190 * Alias for getPath()
191 * @public
192 */
193 function getFullPath() {
194 return $this->getPath();
195 }
196
197 /**
198 * Return the width of the image. Returns false if the width is unknown
199 * or undefined.
200 *
201 * STUB
202 * Overridden by LocalFile, UnregisteredLocalFile
203 * @public
204 */
205 function getWidth( $page = 1 ) { return false; }
206
207 /**
208 * Return the height of the image. Returns false if the height is unknown
209 * or undefined
210 *
211 * STUB
212 * Overridden by LocalFile, UnregisteredLocalFile
213 * @public
214 */
215 function getHeight( $page = 1 ) { return false; }
216
217 /**
218 * Get handler-specific metadata
219 * Overridden by LocalFile, UnregisteredLocalFile
220 * STUB
221 */
222 function getMetadata() { return false; }
223
224 /**
225 * Return the size of the image file, in bytes
226 * Overridden by LocalFile, UnregisteredLocalFile
227 * STUB
228 * @public
229 */
230 function getSize() { return false; }
231
232 /**
233 * Returns the mime type of the file.
234 * Overridden by LocalFile, UnregisteredLocalFile
235 * STUB
236 */
237 function getMimeType() { return 'unknown/unknown'; }
238
239 /**
240 * Return the type of the media in the file.
241 * Use the value returned by this function with the MEDIATYPE_xxx constants.
242 * Overridden by LocalFile,
243 * STUB
244 */
245 function getMediaType() { return MEDIATYPE_UNKNOWN; }
246
247 /**
248 * Checks if the file can be presented to the browser as a bitmap.
249 *
250 * Currently, this checks if the file is an image format
251 * that can be converted to a format
252 * supported by all browsers (namely GIF, PNG and JPEG),
253 * or if it is an SVG image and SVG conversion is enabled.
254 */
255 function canRender() {
256 if ( !isset( $this->canRender ) ) {
257 $this->canRender = $this->getHandler() && $this->handler->canRender();
258 }
259 return $this->canRender;
260 }
261
262 /**
263 * Accessor for __get()
264 */
265 protected function getCanRender() {
266 return $this->canRender();
267 }
268
269 /**
270 * Return true if the file is of a type that can't be directly
271 * rendered by typical browsers and needs to be re-rasterized.
272 *
273 * This returns true for everything but the bitmap types
274 * supported by all browsers, i.e. JPEG; GIF and PNG. It will
275 * also return true for any non-image formats.
276 *
277 * @return bool
278 */
279 function mustRender() {
280 return $this->getHandler() && $this->handler->mustRender();
281 }
282
283 /**
284 * Determines if this media file may be shown inline on a page.
285 *
286 * This is currently synonymous to canRender(), but this could be
287 * extended to also allow inline display of other media,
288 * like flash animations or videos. If you do so, please keep in mind that
289 * that could be a security risk.
290 */
291 function allowInlineDisplay() {
292 return $this->canRender();
293 }
294
295 /**
296 * Determines if this media file is in a format that is unlikely to
297 * contain viruses or malicious content. It uses the global
298 * $wgTrustedMediaFormats list to determine if the file is safe.
299 *
300 * This is used to show a warning on the description page of non-safe files.
301 * It may also be used to disallow direct [[media:...]] links to such files.
302 *
303 * Note that this function will always return true if allowInlineDisplay()
304 * or isTrustedFile() is true for this file.
305 */
306 function isSafeFile() {
307 if ( !isset( $this->isSafeFile ) ) {
308 $this->isSafeFile = $this->_getIsSafeFile();
309 }
310 return $this->isSafeFile;
311 }
312
313 /** Accessor for __get() */
314 protected function getIsSafeFile() {
315 return $this->isSafeFile();
316 }
317
318 /** Uncached accessor */
319 protected function _getIsSafeFile() {
320 if ($this->allowInlineDisplay()) return true;
321 if ($this->isTrustedFile()) return true;
322
323 global $wgTrustedMediaFormats;
324
325 $type= $this->getMediaType();
326 $mime= $this->getMimeType();
327 #wfDebug("LocalFile::isSafeFile: type= $type, mime= $mime\n");
328
329 if (!$type || $type===MEDIATYPE_UNKNOWN) return false; #unknown type, not trusted
330 if ( in_array( $type, $wgTrustedMediaFormats) ) return true;
331
332 if ($mime==="unknown/unknown") return false; #unknown type, not trusted
333 if ( in_array( $mime, $wgTrustedMediaFormats) ) return true;
334
335 return false;
336 }
337
338 /** Returns true if the file is flagged as trusted. Files flagged that way
339 * can be linked to directly, even if that is not allowed for this type of
340 * file normally.
341 *
342 * This is a dummy function right now and always returns false. It could be
343 * implemented to extract a flag from the database. The trusted flag could be
344 * set on upload, if the user has sufficient privileges, to bypass script-
345 * and html-filters. It may even be coupled with cryptographics signatures
346 * or such.
347 */
348 function isTrustedFile() {
349 #this could be implemented to check a flag in the databas,
350 #look for signatures, etc
351 return false;
352 }
353
354 /**
355 * Returns true if file exists in the repository.
356 *
357 * Overridden by LocalFile to avoid unnecessary stat calls.
358 *
359 * @return boolean Whether file exists in the repository.
360 * @public
361 */
362 function exists() {
363 return $this->getPath() && file_exists( $this->path );
364 }
365
366 function getTransformScript() {
367 if ( !isset( $this->transformScript ) ) {
368 $this->transformScript = false;
369 if ( $this->repo ) {
370 $script = $this->repo->getThumbScriptUrl();
371 if ( $script ) {
372 $this->transformScript = "$script?f=" . urlencode( $this->getName() );
373 }
374 }
375 }
376 return $this->transformScript;
377 }
378
379 /**
380 * Get a ThumbnailImage which is the same size as the source
381 */
382 function getUnscaledThumb( $page = false ) {
383 $width = $this->getWidth( $page );
384 if ( !$width ) {
385 return $this->iconThumb();
386 }
387 if ( $page ) {
388 $params = array(
389 'page' => $page,
390 'width' => $this->getWidth( $page )
391 );
392 } else {
393 $params = array( 'width' => $this->getWidth() );
394 }
395 return $this->transform( $params );
396 }
397
398 /**
399 * Return the file name of a thumbnail with the specified parameters
400 *
401 * @param array $params Handler-specific parameters
402 * @private
403 */
404 function thumbName( $params ) {
405 if ( !$this->getHandler() ) {
406 return null;
407 }
408 $extension = $this->getExtension();
409 list( $thumbExt, $thumbMime ) = $this->handler->getThumbType( $extension, $this->getMimeType() );
410 $thumbName = $this->handler->makeParamString( $params ) . '-' . $this->getName();
411 if ( $thumbExt != $extension ) {
412 $thumbName .= ".$thumbExt";
413 }
414 return $thumbName;
415 }
416
417 /**
418 * Create a thumbnail of the image having the specified width/height.
419 * The thumbnail will not be created if the width is larger than the
420 * image's width. Let the browser do the scaling in this case.
421 * The thumbnail is stored on disk and is only computed if the thumbnail
422 * file does not exist OR if it is older than the image.
423 * Returns the URL.
424 *
425 * Keeps aspect ratio of original image. If both width and height are
426 * specified, the generated image will be no bigger than width x height,
427 * and will also have correct aspect ratio.
428 *
429 * @param integer $width maximum width of the generated thumbnail
430 * @param integer $height maximum height of the image (optional)
431 * @public
432 */
433 function createThumb( $width, $height = -1 ) {
434 $params = array( 'width' => $width );
435 if ( $height != -1 ) {
436 $params['height'] = $height;
437 }
438 $thumb = $this->transform( $params );
439 if( is_null( $thumb ) || $thumb->isError() ) return '';
440 return $thumb->getUrl();
441 }
442
443 /**
444 * As createThumb, but returns a ThumbnailImage object. This can
445 * provide access to the actual file, the real size of the thumb,
446 * and can produce a convenient <img> tag for you.
447 *
448 * For non-image formats, this may return a filetype-specific icon.
449 *
450 * @param integer $width maximum width of the generated thumbnail
451 * @param integer $height maximum height of the image (optional)
452 * @param boolean $render True to render the thumbnail if it doesn't exist,
453 * false to just return the URL
454 *
455 * @return ThumbnailImage or null on failure
456 * @public
457 *
458 * @deprecated use transform()
459 */
460 function getThumbnail( $width, $height=-1, $render = true ) {
461 $params = array( 'width' => $width );
462 if ( $height != -1 ) {
463 $params['height'] = $height;
464 }
465 $flags = $render ? self::RENDER_NOW : 0;
466 return $this->transform( $params, $flags );
467 }
468
469 /**
470 * Transform a media file
471 *
472 * @param array $params An associative array of handler-specific parameters. Typical
473 * keys are width, height and page.
474 * @param integer $flags A bitfield, may contain self::RENDER_NOW to force rendering
475 * @return MediaTransformOutput
476 */
477 function transform( $params, $flags = 0 ) {
478 global $wgUseSquid, $wgIgnoreImageErrors;
479
480 wfProfileIn( __METHOD__ );
481 do {
482 if ( !$this->getHandler() || !$this->handler->canRender() ) {
483 // not a bitmap or renderable image, don't try.
484 $thumb = $this->iconThumb();
485 break;
486 }
487
488 $script = $this->getTransformScript();
489 if ( $script && !($flags & self::RENDER_NOW) ) {
490 // Use a script to transform on client request
491 $thumb = $this->handler->getScriptedTransform( $this, $script, $params );
492 break;
493 }
494
495 $normalisedParams = $params;
496 $this->handler->normaliseParams( $this, $normalisedParams );
497 $thumbName = $this->thumbName( $normalisedParams );
498 $thumbPath = $this->getThumbPath( $thumbName );
499 $thumbUrl = $this->getThumbUrl( $thumbName );
500
501 if ( $this->repo->canTransformVia404() && !($flags & self::RENDER_NOW ) ) {
502 $thumb = $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
503 break;
504 }
505
506 wfDebug( "Doing stat for $thumbPath\n" );
507 $this->migrateThumbFile( $thumbName );
508 if ( file_exists( $thumbPath ) ) {
509 $thumb = $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
510 break;
511 }
512 $thumb = $this->handler->doTransform( $this, $thumbPath, $thumbUrl, $params );
513
514 // Ignore errors if requested
515 if ( !$thumb ) {
516 $thumb = null;
517 } elseif ( $thumb->isError() ) {
518 $this->lastError = $thumb->toText();
519 if ( $wgIgnoreImageErrors && !($flags & self::RENDER_NOW) ) {
520 $thumb = $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
521 }
522 }
523
524 if ( $wgUseSquid ) {
525 wfPurgeSquidServers( array( $thumbUrl ) );
526 }
527 } while (false);
528
529 wfProfileOut( __METHOD__ );
530 return $thumb;
531 }
532
533 /**
534 * Hook into transform() to allow migration of thumbnail files
535 * STUB
536 * Overridden by LocalFile
537 */
538 function migrateThumbFile() {}
539
540 /**
541 * Get a MediaHandler instance for this file
542 */
543 function getHandler() {
544 if ( !isset( $this->handler ) ) {
545 $this->handler = MediaHandler::getHandler( $this->getMimeType() );
546 }
547 return $this->handler;
548 }
549
550 /**
551 * Get a ThumbnailImage representing a file type icon
552 * @return ThumbnailImage
553 */
554 function iconThumb() {
555 global $wgStylePath, $wgStyleDirectory;
556
557 $try = array( 'fileicon-' . $this->getExtension() . '.png', 'fileicon.png' );
558 foreach( $try as $icon ) {
559 $path = '/common/images/icons/' . $icon;
560 $filepath = $wgStyleDirectory . $path;
561 if( file_exists( $filepath ) ) {
562 return new ThumbnailImage( $wgStylePath . $path, 120, 120 );
563 }
564 }
565 return null;
566 }
567
568 /**
569 * Get last thumbnailing error.
570 * Largely obsolete.
571 */
572 function getLastError() {
573 return $this->lastError;
574 }
575
576 /**
577 * Get all thumbnail names previously generated for this file
578 * STUB
579 * Overridden by LocalFile
580 */
581 function getThumbnails() { return array(); }
582
583 /**
584 * Purge shared caches such as thumbnails and DB data caching
585 * STUB
586 * Overridden by LocalFile
587 */
588 function purgeCache( $archiveFiles = array() ) {}
589
590 /**
591 * Purge the file description page, but don't go after
592 * pages using the file. Use when modifying file history
593 * but not the current data.
594 */
595 function purgeDescription() {
596 $title = $this->getTitle();
597 if ( $title ) {
598 $title->invalidateCache();
599 $title->purgeSquid();
600 }
601 }
602
603 /**
604 * Purge metadata and all affected pages when the file is created,
605 * deleted, or majorly updated. A set of additional URLs may be
606 * passed to purge, such as specific file files which have changed.
607 * @param $urlArray array
608 */
609 function purgeEverything( $urlArr=array() ) {
610 // Delete thumbnails and refresh file metadata cache
611 $this->purgeCache();
612 $this->purgeDescription();
613
614 // Purge cache of all pages using this file
615 $title = $this->getTitle();
616 if ( $title ) {
617 $update = new HTMLCacheUpdate( $title, 'imagelinks' );
618 $update->doUpdate();
619 }
620 }
621
622 /**
623 * Return the history of this file, line by line. Starts with current version,
624 * then old versions. Should return an object similar to an image/oldimage
625 * database row.
626 *
627 * @public
628 * STUB
629 * Overridden in LocalFile
630 */
631 function nextHistoryLine() {
632 return false;
633 }
634
635 /**
636 * Reset the history pointer to the first element of the history
637 * @public
638 * STUB
639 * Overridden in LocalFile.
640 */
641 function resetHistory() {}
642
643 /**
644 * Get the filename hash component of the directory including trailing slash,
645 * e.g. f/fa/
646 * If the repository is not hashed, returns an empty string.
647 */
648 function getHashPath() {
649 if ( !isset( $this->hashPath ) ) {
650 $this->hashPath = $this->repo->getHashPath( $this->getName() );
651 }
652 return $this->hashPath;
653 }
654
655 /**
656 * Get the path of the file relative to the public zone root
657 */
658 function getRel() {
659 return $this->getHashPath() . $this->getName();
660 }
661
662 /**
663 * Get urlencoded relative path of the file
664 */
665 function getUrlRel() {
666 return $this->getHashPath() . urlencode( $this->getName() );
667 }
668
669 /** Get the path of the archive directory, or a particular file if $suffix is specified */
670 function getArchivePath( $suffix = false ) {
671 $path = $this->repo->getZonePath('public') . '/archive/' . $this->getHashPath();
672 if ( $suffix !== false ) {
673 $path .= '/' . $suffix;
674 }
675 return $path;
676 }
677
678 /** Get the path of the thumbnail directory, or a particular file if $suffix is specified */
679 function getThumbPath( $suffix = false ) {
680 $path = $this->repo->getZonePath('public') . '/thumb/' . $this->getRel();
681 if ( $suffix !== false ) {
682 $path .= '/' . $suffix;
683 }
684 return $path;
685 }
686
687 /** Get the URL of the archive directory, or a particular file if $suffix is specified */
688 function getArchiveUrl( $suffix = false ) {
689 $path = $this->repo->getZoneUrl('public') . '/archive/' . $this->getHashPath();
690 if ( $suffix !== false ) {
691 $path .= '/' . urlencode( $suffix );
692 }
693 return $path;
694 }
695
696 /** Get the URL of the thumbnail directory, or a particular file if $suffix is specified */
697 function getThumbUrl( $suffix = false ) {
698 $path = $this->repo->getZoneUrl('public') . '/thumb/' . $this->getUrlRel();
699 if ( $suffix !== false ) {
700 $path .= '/' . urlencode( $suffix );
701 }
702 return $path;
703 }
704
705 /** Get the virtual URL for an archive file or directory */
706 function getArchiveVirtualUrl( $suffix = false ) {
707 $path = $this->repo->getVirtualUrl() . '/public/archive/' . $this->getHashPath();
708 if ( $suffix !== false ) {
709 $path .= '/' . urlencode( $suffix );
710 }
711 return $path;
712 }
713
714 /** Get the virtual URL for a thumbnail file or directory */
715 function getThumbVirtualUrl( $suffix = false ) {
716 $path = $this->repo->getVirtualUrl() . '/public/thumb/' . $this->getHashPath();
717 if ( $suffix !== false ) {
718 $path .= '/' . urlencode( $suffix );
719 }
720 return $path;
721 }
722
723 /**
724 * @return bool
725 */
726 function isHashed() {
727 return $this->repo->isHashed();
728 }
729
730 function readOnlyError() {
731 throw new MWException( get_class($this) . ': write operations are not supported' );
732 }
733
734 /**
735 * Record a file upload in the upload log and the image table
736 * STUB
737 * Overridden by LocalFile
738 */
739 function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '', $watch = false ) {
740 $this->readOnlyError();
741 }
742
743 /**
744 * Move or copy a file to its public location. If a file exists at the
745 * destination, move it to an archive. Returns the archive name on success
746 * or an empty string if it was a new file, and a wikitext-formatted
747 * WikiError object on failure.
748 *
749 * The archive name should be passed through to recordUpload for database
750 * registration.
751 *
752 * @param string $sourcePath Local filesystem path to the source image
753 * @param integer $flags A bitwise combination of:
754 * File::DELETE_SOURCE Delete the source file, i.e. move
755 * rather than copy
756 * @return The archive name on success or an empty string if it was a new
757 * file, and a wikitext-formatted WikiError object on failure.
758 *
759 * STUB
760 * Overridden by LocalFile
761 */
762 function publish( $srcPath, $flags = 0 ) {
763 $this->readOnlyError();
764 }
765
766 /**
767 * Get an array of Title objects which are articles which use this file
768 * Also adds their IDs to the link cache
769 *
770 * This is mostly copied from Title::getLinksTo()
771 *
772 * @deprecated Use HTMLCacheUpdate, this function uses too much memory
773 */
774 function getLinksTo( $options = '' ) {
775 wfProfileIn( __METHOD__ );
776
777 // Note: use local DB not repo DB, we want to know local links
778 if ( $options ) {
779 $db = wfGetDB( DB_MASTER );
780 } else {
781 $db = wfGetDB( DB_SLAVE );
782 }
783 $linkCache =& LinkCache::singleton();
784
785 list( $page, $imagelinks ) = $db->tableNamesN( 'page', 'imagelinks' );
786 $encName = $db->addQuotes( $this->getName() );
787 $sql = "SELECT page_namespace,page_title,page_id FROM $page,$imagelinks WHERE page_id=il_from AND il_to=$encName $options";
788 $res = $db->query( $sql, __METHOD__ );
789
790 $retVal = array();
791 if ( $db->numRows( $res ) ) {
792 while ( $row = $db->fetchObject( $res ) ) {
793 if ( $titleObj = Title::makeTitle( $row->page_namespace, $row->page_title ) ) {
794 $linkCache->addGoodLinkObj( $row->page_id, $titleObj );
795 $retVal[] = $titleObj;
796 }
797 }
798 }
799 $db->freeResult( $res );
800 wfProfileOut( __METHOD__ );
801 return $retVal;
802 }
803
804 function getExifData() {
805 if ( !$this->getHandler() || $this->handler->getMetadataType( $this ) != 'exif' ) {
806 return array();
807 }
808 $metadata = $this->getMetadata();
809 if ( !$metadata ) {
810 return array();
811 }
812 $exif = unserialize( $metadata );
813 if ( !$exif ) {
814 return array();
815 }
816 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
817 $format = new FormatExif( $exif );
818
819 return $format->getFormattedData();
820 }
821
822 /**
823 * Returns true if the file comes from the local file repository.
824 *
825 * @return bool
826 */
827 function isLocal() {
828 return $this->repo && $this->repo->getName() == 'local';
829 }
830
831 /**
832 * Returns true if the image is an old version
833 * STUB
834 */
835 function isOld() {
836 return false;
837 }
838
839 /**
840 * Is this file a "deleted" file in a private archive?
841 * STUB
842 */
843 function isDeleted( $field ) {
844 return false;
845 }
846
847 /**
848 * Was this file ever deleted from the wiki?
849 *
850 * @return bool
851 */
852 function wasDeleted() {
853 $title = $this->getTitle();
854 return $title && $title->isDeleted() > 0;
855 }
856
857 /**
858 * Delete all versions of the file.
859 *
860 * Moves the files into an archive directory (or deletes them)
861 * and removes the database rows.
862 *
863 * Cache purging is done; logging is caller's responsibility.
864 *
865 * @param $reason
866 * @return true on success, false on some kind of failure
867 * STUB
868 * Overridden by LocalFile
869 */
870 function delete( $reason, $suppress=false ) {
871 $this->readOnlyError();
872 }
873
874 /**
875 * Restore all or specified deleted revisions to the given file.
876 * Permissions and logging are left to the caller.
877 *
878 * May throw database exceptions on error.
879 *
880 * @param $versions set of record ids of deleted items to restore,
881 * or empty to restore all revisions.
882 * @return the number of file revisions restored if successful,
883 * or false on failure
884 * STUB
885 * Overridden by LocalFile
886 */
887 function restore( $versions=array(), $Unsuppress=false ) {
888 $this->readOnlyError();
889 }
890
891 /**
892 * Returns 'true' if this image is a multipage document, e.g. a DJVU
893 * document.
894 *
895 * @return Bool
896 */
897 function isMultipage() {
898 return $this->getHandler() && $this->handler->isMultiPage();
899 }
900
901 /**
902 * Returns the number of pages of a multipage document, or NULL for
903 * documents which aren't multipage documents
904 */
905 function pageCount() {
906 if ( !isset( $this->pageCount ) ) {
907 if ( $this->getHandler() && $this->handler->isMultiPage() ) {
908 $this->pageCount = $this->handler->pageCount( $this );
909 } else {
910 $this->pageCount = false;
911 }
912 }
913 return $this->pageCount;
914 }
915
916 /**
917 * Calculate the height of a thumbnail using the source and destination width
918 */
919 static function scaleHeight( $srcWidth, $srcHeight, $dstWidth ) {
920 // Exact integer multiply followed by division
921 if ( $srcWidth == 0 ) {
922 return 0;
923 } else {
924 return round( $srcHeight * $dstWidth / $srcWidth );
925 }
926 }
927
928 /**
929 * Get an image size array like that returned by getimagesize(), or false if it
930 * can't be determined.
931 *
932 * @param string $fileName The filename
933 * @return array
934 */
935 function getImageSize( $fileName ) {
936 if ( !$this->getHandler() ) {
937 return false;
938 }
939 return $this->handler->getImageSize( $this, $fileName );
940 }
941
942 /**
943 * Get the URL of the image description page. May return false if it is
944 * unknown or not applicable.
945 */
946 function getDescriptionUrl() {
947 return $this->repo->getDescriptionUrl( $this->getName() );
948 }
949
950 /**
951 * Get the HTML text of the description page, if available
952 */
953 function getDescriptionText() {
954 if ( !$this->repo->fetchDescription ) {
955 return false;
956 }
957 $renderUrl = $this->repo->getDescriptionRenderUrl( $this->getName() );
958 if ( $renderUrl ) {
959 wfDebug( "Fetching shared description from $renderUrl\n" );
960 return Http::get( $renderUrl );
961 } else {
962 return false;
963 }
964 }
965
966 /**
967 * Get the 14-character timestamp of the file upload, or false if
968 */
969 function getTimestmap() {
970 $path = $this->getPath();
971 if ( !file_exists( $path ) ) {
972 return false;
973 }
974 return wfTimestamp( filemtime( $path ) );
975 }
976
977 /**
978 * Determine if the current user is allowed to view a particular
979 * field of this file, if it's marked as deleted.
980 * STUB
981 * @param int $field
982 * @return bool
983 */
984 function userCan( $field ) {
985 return true;
986 }
987 }
988
989 ?>