Fixed some errant double-slashes appearing in file paths. Added unit tests for the...
[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->repo->getNameFromTitle( $this->title );
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 = substr( $path, 0, -1 );
674 } else {
675 $path .= $suffix;
676 }
677 return $path;
678 }
679
680 /** Get the path of the thumbnail directory, or a particular file if $suffix is specified */
681 function getThumbPath( $suffix = false ) {
682 $path = $this->repo->getZonePath('public') . '/thumb/' . $this->getRel();
683 if ( $suffix !== false ) {
684 $path .= '/' . $suffix;
685 }
686 return $path;
687 }
688
689 /** Get the URL of the archive directory, or a particular file if $suffix is specified */
690 function getArchiveUrl( $suffix = false ) {
691 $path = $this->repo->getZoneUrl('public') . '/archive/' . $this->getHashPath();
692 if ( $suffix === false ) {
693 $path = substr( $path, 0, -1 );
694 } else {
695 $path .= urlencode( $suffix );
696 }
697 return $path;
698 }
699
700 /** Get the URL of the thumbnail directory, or a particular file if $suffix is specified */
701 function getThumbUrl( $suffix = false ) {
702 $path = $this->repo->getZoneUrl('public') . '/thumb/' . $this->getUrlRel();
703 if ( $suffix !== false ) {
704 $path .= '/' . urlencode( $suffix );
705 }
706 return $path;
707 }
708
709 /** Get the virtual URL for an archive file or directory */
710 function getArchiveVirtualUrl( $suffix = false ) {
711 $path = $this->repo->getVirtualUrl() . '/public/archive/' . $this->getHashPath();
712 if ( $suffix === false ) {
713 $path = substr( $path, 0, -1 );
714 } else {
715 $path .= urlencode( $suffix );
716 }
717 return $path;
718 }
719
720 /** Get the virtual URL for a thumbnail file or directory */
721 function getThumbVirtualUrl( $suffix = false ) {
722 $path = $this->repo->getVirtualUrl() . '/public/thumb/' . $this->getUrlRel();
723 if ( $suffix !== false ) {
724 $path .= '/' . urlencode( $suffix );
725 }
726 return $path;
727 }
728
729 /**
730 * @return bool
731 */
732 function isHashed() {
733 return $this->repo->isHashed();
734 }
735
736 function readOnlyError() {
737 throw new MWException( get_class($this) . ': write operations are not supported' );
738 }
739
740 /**
741 * Record a file upload in the upload log and the image table
742 * STUB
743 * Overridden by LocalFile
744 */
745 function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '', $watch = false ) {
746 $this->readOnlyError();
747 }
748
749 /**
750 * Move or copy a file to its public location. If a file exists at the
751 * destination, move it to an archive. Returns the archive name on success
752 * or an empty string if it was a new file, and a wikitext-formatted
753 * WikiError object on failure.
754 *
755 * The archive name should be passed through to recordUpload for database
756 * registration.
757 *
758 * @param string $sourcePath Local filesystem path to the source image
759 * @param integer $flags A bitwise combination of:
760 * File::DELETE_SOURCE Delete the source file, i.e. move
761 * rather than copy
762 * @return The archive name on success or an empty string if it was a new
763 * file, and a wikitext-formatted WikiError object on failure.
764 *
765 * STUB
766 * Overridden by LocalFile
767 */
768 function publish( $srcPath, $flags = 0 ) {
769 $this->readOnlyError();
770 }
771
772 /**
773 * Get an array of Title objects which are articles which use this file
774 * Also adds their IDs to the link cache
775 *
776 * This is mostly copied from Title::getLinksTo()
777 *
778 * @deprecated Use HTMLCacheUpdate, this function uses too much memory
779 */
780 function getLinksTo( $options = '' ) {
781 wfProfileIn( __METHOD__ );
782
783 // Note: use local DB not repo DB, we want to know local links
784 if ( $options ) {
785 $db = wfGetDB( DB_MASTER );
786 } else {
787 $db = wfGetDB( DB_SLAVE );
788 }
789 $linkCache =& LinkCache::singleton();
790
791 list( $page, $imagelinks ) = $db->tableNamesN( 'page', 'imagelinks' );
792 $encName = $db->addQuotes( $this->getName() );
793 $sql = "SELECT page_namespace,page_title,page_id FROM $page,$imagelinks WHERE page_id=il_from AND il_to=$encName $options";
794 $res = $db->query( $sql, __METHOD__ );
795
796 $retVal = array();
797 if ( $db->numRows( $res ) ) {
798 while ( $row = $db->fetchObject( $res ) ) {
799 if ( $titleObj = Title::makeTitle( $row->page_namespace, $row->page_title ) ) {
800 $linkCache->addGoodLinkObj( $row->page_id, $titleObj );
801 $retVal[] = $titleObj;
802 }
803 }
804 }
805 $db->freeResult( $res );
806 wfProfileOut( __METHOD__ );
807 return $retVal;
808 }
809
810 function getExifData() {
811 if ( !$this->getHandler() || $this->handler->getMetadataType( $this ) != 'exif' ) {
812 return array();
813 }
814 $metadata = $this->getMetadata();
815 if ( !$metadata ) {
816 return array();
817 }
818 $exif = unserialize( $metadata );
819 if ( !$exif ) {
820 return array();
821 }
822 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
823 $format = new FormatExif( $exif );
824
825 return $format->getFormattedData();
826 }
827
828 /**
829 * Returns true if the file comes from the local file repository.
830 *
831 * @return bool
832 */
833 function isLocal() {
834 return $this->repo && $this->repo->getName() == 'local';
835 }
836
837 /**
838 * Returns true if the image is an old version
839 * STUB
840 */
841 function isOld() {
842 return false;
843 }
844
845 /**
846 * Is this file a "deleted" file in a private archive?
847 * STUB
848 */
849 function isDeleted( $field ) {
850 return false;
851 }
852
853 /**
854 * Was this file ever deleted from the wiki?
855 *
856 * @return bool
857 */
858 function wasDeleted() {
859 $title = $this->getTitle();
860 return $title && $title->isDeleted() > 0;
861 }
862
863 /**
864 * Delete all versions of the file.
865 *
866 * Moves the files into an archive directory (or deletes them)
867 * and removes the database rows.
868 *
869 * Cache purging is done; logging is caller's responsibility.
870 *
871 * @param $reason
872 * @return true on success, false on some kind of failure
873 * STUB
874 * Overridden by LocalFile
875 */
876 function delete( $reason, $suppress=false ) {
877 $this->readOnlyError();
878 }
879
880 /**
881 * Restore all or specified deleted revisions to the given file.
882 * Permissions and logging are left to the caller.
883 *
884 * May throw database exceptions on error.
885 *
886 * @param $versions set of record ids of deleted items to restore,
887 * or empty to restore all revisions.
888 * @return the number of file revisions restored if successful,
889 * or false on failure
890 * STUB
891 * Overridden by LocalFile
892 */
893 function restore( $versions=array(), $Unsuppress=false ) {
894 $this->readOnlyError();
895 }
896
897 /**
898 * Returns 'true' if this image is a multipage document, e.g. a DJVU
899 * document.
900 *
901 * @return Bool
902 */
903 function isMultipage() {
904 return $this->getHandler() && $this->handler->isMultiPage();
905 }
906
907 /**
908 * Returns the number of pages of a multipage document, or NULL for
909 * documents which aren't multipage documents
910 */
911 function pageCount() {
912 if ( !isset( $this->pageCount ) ) {
913 if ( $this->getHandler() && $this->handler->isMultiPage() ) {
914 $this->pageCount = $this->handler->pageCount( $this );
915 } else {
916 $this->pageCount = false;
917 }
918 }
919 return $this->pageCount;
920 }
921
922 /**
923 * Calculate the height of a thumbnail using the source and destination width
924 */
925 static function scaleHeight( $srcWidth, $srcHeight, $dstWidth ) {
926 // Exact integer multiply followed by division
927 if ( $srcWidth == 0 ) {
928 return 0;
929 } else {
930 return round( $srcHeight * $dstWidth / $srcWidth );
931 }
932 }
933
934 /**
935 * Get an image size array like that returned by getimagesize(), or false if it
936 * can't be determined.
937 *
938 * @param string $fileName The filename
939 * @return array
940 */
941 function getImageSize( $fileName ) {
942 if ( !$this->getHandler() ) {
943 return false;
944 }
945 return $this->handler->getImageSize( $this, $fileName );
946 }
947
948 /**
949 * Get the URL of the image description page. May return false if it is
950 * unknown or not applicable.
951 */
952 function getDescriptionUrl() {
953 return $this->repo->getDescriptionUrl( $this->getName() );
954 }
955
956 /**
957 * Get the HTML text of the description page, if available
958 */
959 function getDescriptionText() {
960 if ( !$this->repo->fetchDescription ) {
961 return false;
962 }
963 $renderUrl = $this->repo->getDescriptionRenderUrl( $this->getName() );
964 if ( $renderUrl ) {
965 wfDebug( "Fetching shared description from $renderUrl\n" );
966 return Http::get( $renderUrl );
967 } else {
968 return false;
969 }
970 }
971
972 /**
973 * Get the 14-character timestamp of the file upload, or false if
974 */
975 function getTimestmap() {
976 $path = $this->getPath();
977 if ( !file_exists( $path ) ) {
978 return false;
979 }
980 return wfTimestamp( filemtime( $path ) );
981 }
982
983 /**
984 * Determine if the current user is allowed to view a particular
985 * field of this file, if it's marked as deleted.
986 * STUB
987 * @param int $field
988 * @return bool
989 */
990 function userCan( $field ) {
991 return true;
992 }
993 }
994
995 ?>