* Split off ultimate base class FileRepo from FSRepo
[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( __METHOD__.": 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() . rawurlencode( $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 .= rawurlencode( $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 .= '/' . rawurlencode( $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 .= rawurlencode( $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 .= '/' . rawurlencode( $suffix );
725 }
726 return $path;
727 }
728
729 /** Get the virtual URL for the file itself */
730 function getVirtualUrl( $suffix = false ) {
731 $path = $this->repo->getVirtualUrl() . '/public/' . $this->getUrlRel();
732 if ( $suffix !== false ) {
733 $path .= '/' . rawurlencode( $suffix );
734 }
735 return $path;
736 }
737
738 /**
739 * @return bool
740 */
741 function isHashed() {
742 return $this->repo->isHashed();
743 }
744
745 function readOnlyError() {
746 throw new MWException( get_class($this) . ': write operations are not supported' );
747 }
748
749 /**
750 * Record a file upload in the upload log and the image table
751 * STUB
752 * Overridden by LocalFile
753 */
754 function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '', $watch = false ) {
755 $this->readOnlyError();
756 }
757
758 /**
759 * Move or copy a file to its public location. If a file exists at the
760 * destination, move it to an archive. Returns the archive name on success
761 * or an empty string if it was a new file, and a wikitext-formatted
762 * WikiError object on failure.
763 *
764 * The archive name should be passed through to recordUpload for database
765 * registration.
766 *
767 * @param string $sourcePath Local filesystem path to the source image
768 * @param integer $flags A bitwise combination of:
769 * File::DELETE_SOURCE Delete the source file, i.e. move
770 * rather than copy
771 * @return The archive name on success or an empty string if it was a new
772 * file, and a wikitext-formatted WikiError object on failure.
773 *
774 * STUB
775 * Overridden by LocalFile
776 */
777 function publish( $srcPath, $flags = 0 ) {
778 $this->readOnlyError();
779 }
780
781 /**
782 * Get an array of Title objects which are articles which use this file
783 * Also adds their IDs to the link cache
784 *
785 * This is mostly copied from Title::getLinksTo()
786 *
787 * @deprecated Use HTMLCacheUpdate, this function uses too much memory
788 */
789 function getLinksTo( $options = '' ) {
790 wfProfileIn( __METHOD__ );
791
792 // Note: use local DB not repo DB, we want to know local links
793 if ( $options ) {
794 $db = wfGetDB( DB_MASTER );
795 } else {
796 $db = wfGetDB( DB_SLAVE );
797 }
798 $linkCache =& LinkCache::singleton();
799
800 list( $page, $imagelinks ) = $db->tableNamesN( 'page', 'imagelinks' );
801 $encName = $db->addQuotes( $this->getName() );
802 $sql = "SELECT page_namespace,page_title,page_id FROM $page,$imagelinks WHERE page_id=il_from AND il_to=$encName $options";
803 $res = $db->query( $sql, __METHOD__ );
804
805 $retVal = array();
806 if ( $db->numRows( $res ) ) {
807 while ( $row = $db->fetchObject( $res ) ) {
808 if ( $titleObj = Title::makeTitle( $row->page_namespace, $row->page_title ) ) {
809 $linkCache->addGoodLinkObj( $row->page_id, $titleObj );
810 $retVal[] = $titleObj;
811 }
812 }
813 }
814 $db->freeResult( $res );
815 wfProfileOut( __METHOD__ );
816 return $retVal;
817 }
818
819 function getExifData() {
820 if ( !$this->getHandler() || $this->handler->getMetadataType( $this ) != 'exif' ) {
821 return array();
822 }
823 $metadata = $this->getMetadata();
824 if ( !$metadata ) {
825 return array();
826 }
827 $exif = unserialize( $metadata );
828 if ( !$exif ) {
829 return array();
830 }
831 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
832 $format = new FormatExif( $exif );
833
834 return $format->getFormattedData();
835 }
836
837 /**
838 * Returns true if the file comes from the local file repository.
839 *
840 * @return bool
841 */
842 function isLocal() {
843 return $this->repo && $this->repo->getName() == 'local';
844 }
845
846 /**
847 * Returns true if the image is an old version
848 * STUB
849 */
850 function isOld() {
851 return false;
852 }
853
854 /**
855 * Is this file a "deleted" file in a private archive?
856 * STUB
857 */
858 function isDeleted( $field ) {
859 return false;
860 }
861
862 /**
863 * Was this file ever deleted from the wiki?
864 *
865 * @return bool
866 */
867 function wasDeleted() {
868 $title = $this->getTitle();
869 return $title && $title->isDeleted() > 0;
870 }
871
872 /**
873 * Delete all versions of the file.
874 *
875 * Moves the files into an archive directory (or deletes them)
876 * and removes the database rows.
877 *
878 * Cache purging is done; logging is caller's responsibility.
879 *
880 * @param $reason
881 * @return true on success, false on some kind of failure
882 * STUB
883 * Overridden by LocalFile
884 */
885 function delete( $reason, $suppress=false ) {
886 $this->readOnlyError();
887 }
888
889 /**
890 * Restore all or specified deleted revisions to the given file.
891 * Permissions and logging are left to the caller.
892 *
893 * May throw database exceptions on error.
894 *
895 * @param $versions set of record ids of deleted items to restore,
896 * or empty to restore all revisions.
897 * @return the number of file revisions restored if successful,
898 * or false on failure
899 * STUB
900 * Overridden by LocalFile
901 */
902 function restore( $versions=array(), $Unsuppress=false ) {
903 $this->readOnlyError();
904 }
905
906 /**
907 * Returns 'true' if this image is a multipage document, e.g. a DJVU
908 * document.
909 *
910 * @return Bool
911 */
912 function isMultipage() {
913 return $this->getHandler() && $this->handler->isMultiPage();
914 }
915
916 /**
917 * Returns the number of pages of a multipage document, or NULL for
918 * documents which aren't multipage documents
919 */
920 function pageCount() {
921 if ( !isset( $this->pageCount ) ) {
922 if ( $this->getHandler() && $this->handler->isMultiPage() ) {
923 $this->pageCount = $this->handler->pageCount( $this );
924 } else {
925 $this->pageCount = false;
926 }
927 }
928 return $this->pageCount;
929 }
930
931 /**
932 * Calculate the height of a thumbnail using the source and destination width
933 */
934 static function scaleHeight( $srcWidth, $srcHeight, $dstWidth ) {
935 // Exact integer multiply followed by division
936 if ( $srcWidth == 0 ) {
937 return 0;
938 } else {
939 return round( $srcHeight * $dstWidth / $srcWidth );
940 }
941 }
942
943 /**
944 * Get an image size array like that returned by getimagesize(), or false if it
945 * can't be determined.
946 *
947 * @param string $fileName The filename
948 * @return array
949 */
950 function getImageSize( $fileName ) {
951 if ( !$this->getHandler() ) {
952 return false;
953 }
954 return $this->handler->getImageSize( $this, $fileName );
955 }
956
957 /**
958 * Get the URL of the image description page. May return false if it is
959 * unknown or not applicable.
960 */
961 function getDescriptionUrl() {
962 return $this->repo->getDescriptionUrl( $this->getName() );
963 }
964
965 /**
966 * Get the HTML text of the description page, if available
967 */
968 function getDescriptionText() {
969 if ( !$this->repo->fetchDescription ) {
970 return false;
971 }
972 $renderUrl = $this->repo->getDescriptionRenderUrl( $this->getName() );
973 if ( $renderUrl ) {
974 wfDebug( "Fetching shared description from $renderUrl\n" );
975 return Http::get( $renderUrl );
976 } else {
977 return false;
978 }
979 }
980
981 /**
982 * Get the 14-character timestamp of the file upload, or false if
983 */
984 function getTimestmap() {
985 $path = $this->getPath();
986 if ( !file_exists( $path ) ) {
987 return false;
988 }
989 return wfTimestamp( filemtime( $path ) );
990 }
991
992 /**
993 * Determine if the current user is allowed to view a particular
994 * field of this file, if it's marked as deleted.
995 * STUB
996 * @param int $field
997 * @return bool
998 */
999 function userCan( $field ) {
1000 return true;
1001 }
1002
1003 /**
1004 * Get an associative array containing information about a file in the local filesystem
1005 */
1006 static function getPropsFromPath( $path ) {
1007 wfProfileIn( __METHOD__ );
1008 wfDebug( __METHOD__.": Getting file info for $path\n" );
1009 $info = array( 'fileExists' => file_exists( $path ) );
1010 $gis = false;
1011 if ( $info['fileExists'] ) {
1012 $magic = MimeMagic::singleton();
1013
1014 $info['mime'] = $magic->guessMimeType( $path, true );
1015 list( $info['major_mime'], $info['minor_mime'] ) = self::splitMime( $info['mime'] );
1016 $info['media_type'] = $magic->getMediaType( $path, $info['mime'] );
1017
1018 # Get size in bytes
1019 $info['size'] = filesize( $path );
1020
1021 # Height, width and metadata
1022 $handler = MediaHandler::getHandler( $info['mime'] );
1023 if ( $handler ) {
1024 $tempImage = (object)array();
1025 $gis = $handler->getImageSize( $tempImage, $path );
1026 $info['metadata'] = $handler->getMetadata( $tempImage, $path );
1027 } else {
1028 $gis = false;
1029 $info['metadata'] = '';
1030 }
1031
1032 wfDebug(__METHOD__.": $path loaded, {$info['size']} bytes, {$info['mime']}.\n");
1033 } else {
1034 $info['mime'] = NULL;
1035 $info['media_type'] = MEDIATYPE_UNKNOWN;
1036 $info['metadata'] = '';
1037 wfDebug(__METHOD__.": $path NOT FOUND!\n");
1038 }
1039 if( $gis ) {
1040 # NOTE: $gis[2] contains a code for the image type. This is no longer used.
1041 $info['width'] = $gis[0];
1042 $info['height'] = $gis[1];
1043 if ( isset( $gis['bits'] ) ) {
1044 $info['bits'] = $gis['bits'];
1045 } else {
1046 $info['bits'] = 0;
1047 }
1048 } else {
1049 $info['width'] = 0;
1050 $info['height'] = 0;
1051 }
1052 wfProfileOut( __METHOD__ );
1053 return $info;
1054 }
1055 }
1056
1057 ?>