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.
8 * Stub functions which should be overridden are marked with STUB. Some more
9 * concrete functions are also typically overridden by child classes.
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:
15 * RepoGroup::singleton()->getLocalRepo()->newFile($title);
17 * The convenience functions wfLocalFile() and wfFindFile() should be sufficient
20 * @addtogroup FileRepo
23 const DELETED_FILE = 1;
24 const DELETED_COMMENT = 2;
25 const DELETED_USER = 4;
26 const DELETED_RESTRICTED = 8;
29 const DELETE_SOURCE = 1;
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
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
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.
47 * The following member variables are not lazy-initialised
49 var $repo, $title, $lastError;
52 * Call this constructor from child classes
54 function __construct( $title, $repo ) {
55 $this->title = $title;
59 function __get( $name ) {
60 $function = array( $this, 'get' . ucfirst( $name ) );
61 if ( !is_callable( $function ) ) {
64 $this->$name = call_user_func( $function );
70 * Normalize a file extension to the common form, and ensure it's clean.
71 * Extensions with non-alphanumeric characters will be discarded.
73 * @param $ext string (without the .)
76 static function normalizeExtension( $ext ) {
77 $lower = strtolower( $ext );
83 if( isset( $squish[$lower] ) ) {
84 return $squish[$lower];
85 } elseif( preg_match( '/^[0-9a-z]+$/', $lower ) ) {
93 * Upgrade the database row if there is one
97 function upgradeRow() {}
100 * Split an internet media type into its two components; if not
101 * a two-part name, set the minor type to 'unknown'.
103 * @param $mime "text/html" etc
104 * @return array ("text", "html") etc
106 static function splitMime( $mime ) {
107 if( strpos( $mime, '/' ) !== false ) {
108 return explode( '/', $mime, 2 );
110 return array( $mime, 'unknown' );
115 * Return the name of this file
117 public function getName() {
118 if ( !isset( $this->name ) ) {
119 $this->name = $this->repo->getNameFromTitle( $this->title );
125 * Get the file extension, e.g. "svg"
127 function getExtension() {
128 if ( !isset( $this->extension ) ) {
129 $n = strrpos( $this->getName(), '.' );
130 $this->extension = self::normalizeExtension(
131 $n ? substr( $this->getName(), $n + 1 ) : '' );
133 return $this->extension;
137 * Return the associated title object
140 function getTitle() { return $this->title; }
143 * Return the URL of the file
147 if ( !isset( $this->url ) ) {
148 $this->url = $this->repo->getZoneUrl( 'public' ) . '/' . $this->getUrlRel();
153 function getViewURL() {
154 if( $this->mustRender()) {
155 if( $this->canRender() ) {
156 return $this->createThumb( $this->getWidth() );
159 wfDebug(__METHOD__.': supposed to render '.$this->getName().' ('.$this->getMimeType()."), but can't!\n");
160 return $this->getURL(); #hm... return NULL?
163 return $this->getURL();
168 * Return the full filesystem path to the file. Note that this does
169 * not mean that a file actually exists under that location.
171 * This path depends on whether directory hashing is active or not,
172 * i.e. whether the files are all found in the same directory,
173 * or in hashed paths like /images/3/3c.
175 * May return false if the file is not locally accessible.
180 if ( !isset( $this->path ) ) {
181 $this->path = $this->repo->getZonePath('public') . '/' . $this->getRel();
187 * Alias for getPath()
190 function getFullPath() {
191 return $this->getPath();
195 * Return the width of the image. Returns false if the width is unknown
199 * Overridden by LocalFile, UnregisteredLocalFile
201 public function getWidth( $page = 1 ) { return false; }
204 * Return the height of the image. Returns false if the height is unknown
208 * Overridden by LocalFile, UnregisteredLocalFile
210 public function getHeight( $page = 1 ) { return false; }
213 * Get the duration of a media file in seconds
215 public function getLength() {
216 $handler = $this->getHandler();
218 return $handler->getLength( $this );
225 * Get handler-specific metadata
226 * Overridden by LocalFile, UnregisteredLocalFile
229 function getMetadata() { return false; }
232 * Return the size of the image file, in bytes
233 * Overridden by LocalFile, UnregisteredLocalFile
236 public function getSize() { return false; }
239 * Returns the mime type of the file.
240 * Overridden by LocalFile, UnregisteredLocalFile
243 function getMimeType() { return 'unknown/unknown'; }
246 * Return the type of the media in the file.
247 * Use the value returned by this function with the MEDIATYPE_xxx constants.
248 * Overridden by LocalFile,
251 function getMediaType() { return MEDIATYPE_UNKNOWN; }
254 * Checks if the output of transform() for this file is likely
255 * to be valid. If this is false, various user elements will
256 * display a placeholder instead.
258 * Currently, this checks if the file is an image format
259 * that can be converted to a format
260 * supported by all browsers (namely GIF, PNG and JPEG),
261 * or if it is an SVG image and SVG conversion is enabled.
263 function canRender() {
264 if ( !isset( $this->canRender ) ) {
265 $this->canRender = $this->getHandler() && $this->handler->canRender( $this );
267 return $this->canRender;
271 * Accessor for __get()
273 protected function getCanRender() {
274 return $this->canRender();
278 * Return true if the file is of a type that can't be directly
279 * rendered by typical browsers and needs to be re-rasterized.
281 * This returns true for everything but the bitmap types
282 * supported by all browsers, i.e. JPEG; GIF and PNG. It will
283 * also return true for any non-image formats.
287 function mustRender() {
288 return $this->getHandler() && $this->handler->mustRender( $this );
292 * Alias for canRender()
294 function allowInlineDisplay() {
295 return $this->canRender();
299 * Determines if this media file is in a format that is unlikely to
300 * contain viruses or malicious content. It uses the global
301 * $wgTrustedMediaFormats list to determine if the file is safe.
303 * This is used to show a warning on the description page of non-safe files.
304 * It may also be used to disallow direct [[media:...]] links to such files.
306 * Note that this function will always return true if allowInlineDisplay()
307 * or isTrustedFile() is true for this file.
309 function isSafeFile() {
310 if ( !isset( $this->isSafeFile ) ) {
311 $this->isSafeFile = $this->_getIsSafeFile();
313 return $this->isSafeFile;
316 /** Accessor for __get() */
317 protected function getIsSafeFile() {
318 return $this->isSafeFile();
321 /** Uncached accessor */
322 protected function _getIsSafeFile() {
323 if ($this->allowInlineDisplay()) return true;
324 if ($this->isTrustedFile()) return true;
326 global $wgTrustedMediaFormats;
328 $type= $this->getMediaType();
329 $mime= $this->getMimeType();
330 #wfDebug("LocalFile::isSafeFile: type= $type, mime= $mime\n");
332 if (!$type || $type===MEDIATYPE_UNKNOWN) return false; #unknown type, not trusted
333 if ( in_array( $type, $wgTrustedMediaFormats) ) return true;
335 if ($mime==="unknown/unknown") return false; #unknown type, not trusted
336 if ( in_array( $mime, $wgTrustedMediaFormats) ) return true;
341 /** Returns true if the file is flagged as trusted. Files flagged that way
342 * can be linked to directly, even if that is not allowed for this type of
345 * This is a dummy function right now and always returns false. It could be
346 * implemented to extract a flag from the database. The trusted flag could be
347 * set on upload, if the user has sufficient privileges, to bypass script-
348 * and html-filters. It may even be coupled with cryptographics signatures
351 function isTrustedFile() {
352 #this could be implemented to check a flag in the databas,
353 #look for signatures, etc
358 * Returns true if file exists in the repository.
360 * Overridden by LocalFile to avoid unnecessary stat calls.
362 * @return boolean Whether file exists in the repository.
364 public function exists() {
365 return $this->getPath() && file_exists( $this->path );
368 function getTransformScript() {
369 if ( !isset( $this->transformScript ) ) {
370 $this->transformScript = false;
372 $script = $this->repo->getThumbScriptUrl();
374 $this->transformScript = "$script?f=" . urlencode( $this->getName() );
378 return $this->transformScript;
382 * Get a ThumbnailImage which is the same size as the source
384 function getUnscaledThumb( $page = false ) {
385 $width = $this->getWidth( $page );
387 return $this->iconThumb();
392 'width' => $this->getWidth( $page )
395 $params = array( 'width' => $this->getWidth() );
397 return $this->transform( $params );
401 * Return the file name of a thumbnail with the specified parameters
403 * @param array $params Handler-specific parameters
406 function thumbName( $params ) {
407 if ( !$this->getHandler() ) {
410 $extension = $this->getExtension();
411 list( $thumbExt, $thumbMime ) = $this->handler->getThumbType( $extension, $this->getMimeType() );
412 $thumbName = $this->handler->makeParamString( $params ) . '-' . $this->getName();
413 if ( $thumbExt != $extension ) {
414 $thumbName .= ".$thumbExt";
420 * Create a thumbnail of the image having the specified width/height.
421 * The thumbnail will not be created if the width is larger than the
422 * image's width. Let the browser do the scaling in this case.
423 * The thumbnail is stored on disk and is only computed if the thumbnail
424 * file does not exist OR if it is older than the image.
427 * Keeps aspect ratio of original image. If both width and height are
428 * specified, the generated image will be no bigger than width x height,
429 * and will also have correct aspect ratio.
431 * @param integer $width maximum width of the generated thumbnail
432 * @param integer $height maximum height of the image (optional)
434 public function createThumb( $width, $height = -1 ) {
435 $params = array( 'width' => $width );
436 if ( $height != -1 ) {
437 $params['height'] = $height;
439 $thumb = $this->transform( $params );
440 if( is_null( $thumb ) || $thumb->isError() ) return '';
441 return $thumb->getUrl();
445 * As createThumb, but returns a ThumbnailImage object. This can
446 * provide access to the actual file, the real size of the thumb,
447 * and can produce a convenient <img> tag for you.
449 * For non-image formats, this may return a filetype-specific icon.
451 * @param integer $width maximum width of the generated thumbnail
452 * @param integer $height maximum height of the image (optional)
453 * @param boolean $render True to render the thumbnail if it doesn't exist,
454 * false to just return the URL
456 * @return ThumbnailImage or null on failure
458 * @deprecated use transform()
460 public function getThumbnail( $width, $height=-1, $render = true ) {
461 $params = array( 'width' => $width );
462 if ( $height != -1 ) {
463 $params['height'] = $height;
465 $flags = $render ? self::RENDER_NOW : 0;
466 return $this->transform( $params, $flags );
470 * Transform a media file
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
477 function transform( $params, $flags = 0 ) {
478 global $wgUseSquid, $wgIgnoreImageErrors;
480 wfProfileIn( __METHOD__ );
482 if ( !$this->canRender() ) {
483 // not a bitmap or renderable image, don't try.
484 $thumb = $this->iconThumb();
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 );
495 $normalisedParams = $params;
496 $this->handler->normaliseParams( $this, $normalisedParams );
497 $thumbName = $this->thumbName( $normalisedParams );
498 $thumbPath = $this->getThumbPath( $thumbName );
499 $thumbUrl = $this->getThumbUrl( $thumbName );
501 if ( $this->repo->canTransformVia404() && !($flags & self::RENDER_NOW ) ) {
502 $thumb = $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
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 );
512 $thumb = $this->handler->doTransform( $this, $thumbPath, $thumbUrl, $params );
514 // Ignore errors if requested
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 );
525 wfPurgeSquidServers( array( $thumbUrl ) );
529 wfProfileOut( __METHOD__ );
534 * Hook into transform() to allow migration of thumbnail files
536 * Overridden by LocalFile
538 function migrateThumbFile( $thumbName ) {}
541 * Get a MediaHandler instance for this file
543 function getHandler() {
544 if ( !isset( $this->handler ) ) {
545 $this->handler = MediaHandler::getHandler( $this->getMimeType() );
547 return $this->handler;
551 * Get a ThumbnailImage representing a file type icon
552 * @return ThumbnailImage
554 function iconThumb() {
555 global $wgStylePath, $wgStyleDirectory;
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( $this, $wgStylePath . $path, 120, 120 );
569 * Get last thumbnailing error.
572 function getLastError() {
573 return $this->lastError;
577 * Get all thumbnail names previously generated for this file
579 * Overridden by LocalFile
581 function getThumbnails() { return array(); }
584 * Purge shared caches such as thumbnails and DB data caching
586 * Overridden by LocalFile
588 function purgeCache( $archiveFiles = array() ) {}
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.
595 function purgeDescription() {
596 $title = $this->getTitle();
598 $title->invalidateCache();
599 $title->purgeSquid();
604 * Purge metadata and all affected pages when the file is created,
605 * deleted, or majorly updated.
607 function purgeEverything() {
608 // Delete thumbnails and refresh file metadata cache
610 $this->purgeDescription();
612 // Purge cache of all pages using this file
613 $title = $this->getTitle();
615 $update = new HTMLCacheUpdate( $title, 'imagelinks' );
621 * Return the history of this file, line by line. Starts with current version,
622 * then old versions. Should return an object similar to an image/oldimage
626 * Overridden in LocalFile
628 public function nextHistoryLine() {
633 * Reset the history pointer to the first element of the history.
634 * Always call this function after using nextHistoryLine() to free db resources
636 * Overridden in LocalFile.
638 public function resetHistory() {}
641 * Get the filename hash component of the directory including trailing slash,
643 * If the repository is not hashed, returns an empty string.
645 function getHashPath() {
646 if ( !isset( $this->hashPath ) ) {
647 $this->hashPath = $this->repo->getHashPath( $this->getName() );
649 return $this->hashPath;
653 * Get the path of the file relative to the public zone root
656 return $this->getHashPath() . $this->getName();
660 * Get urlencoded relative path of the file
662 function getUrlRel() {
663 return $this->getHashPath() . rawurlencode( $this->getName() );
666 /** Get the relative path for an archive file */
667 function getArchiveRel( $suffix = false ) {
668 $path = 'archive/' . $this->getHashPath();
669 if ( $suffix === false ) {
670 $path = substr( $path, 0, -1 );
677 /** Get relative path for a thumbnail file */
678 function getThumbRel( $suffix = false ) {
679 $path = 'thumb/' . $this->getRel();
680 if ( $suffix !== false ) {
681 $path .= '/' . $suffix;
686 /** Get the path of the archive directory, or a particular file if $suffix is specified */
687 function getArchivePath( $suffix = false ) {
688 return $this->repo->getZonePath('public') . '/' . $this->getArchiveRel();
691 /** Get the path of the thumbnail directory, or a particular file if $suffix is specified */
692 function getThumbPath( $suffix = false ) {
693 return $this->repo->getZonePath('public') . '/' . $this->getThumbRel( $suffix );
696 /** Get the URL of the archive directory, or a particular file if $suffix is specified */
697 function getArchiveUrl( $suffix = false ) {
698 $path = $this->repo->getZoneUrl('public') . '/archive/' . $this->getHashPath();
699 if ( $suffix === false ) {
700 $path = substr( $path, 0, -1 );
702 $path .= rawurlencode( $suffix );
707 /** Get the URL of the thumbnail directory, or a particular file if $suffix is specified */
708 function getThumbUrl( $suffix = false ) {
709 $path = $this->repo->getZoneUrl('public') . '/thumb/' . $this->getUrlRel();
710 if ( $suffix !== false ) {
711 $path .= '/' . rawurlencode( $suffix );
716 /** Get the virtual URL for an archive file or directory */
717 function getArchiveVirtualUrl( $suffix = false ) {
718 $path = $this->repo->getVirtualUrl() . '/public/archive/' . $this->getHashPath();
719 if ( $suffix === false ) {
720 $path = substr( $path, 0, -1 );
722 $path .= rawurlencode( $suffix );
727 /** Get the virtual URL for a thumbnail file or directory */
728 function getThumbVirtualUrl( $suffix = false ) {
729 $path = $this->repo->getVirtualUrl() . '/public/thumb/' . $this->getUrlRel();
730 if ( $suffix !== false ) {
731 $path .= '/' . rawurlencode( $suffix );
736 /** Get the virtual URL for the file itself */
737 function getVirtualUrl( $suffix = false ) {
738 $path = $this->repo->getVirtualUrl() . '/public/' . $this->getUrlRel();
739 if ( $suffix !== false ) {
740 $path .= '/' . rawurlencode( $suffix );
748 function isHashed() {
749 return $this->repo->isHashed();
752 function readOnlyError() {
753 throw new MWException( get_class($this) . ': write operations are not supported' );
757 * Record a file upload in the upload log and the image table
759 * Overridden by LocalFile
761 function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '', $watch = false ) {
762 $this->readOnlyError();
766 * Move or copy a file to its public location. If a file exists at the
767 * destination, move it to an archive. Returns the archive name on success
768 * or an empty string if it was a new file, and a wikitext-formatted
769 * WikiError object on failure.
771 * The archive name should be passed through to recordUpload for database
774 * @param string $sourcePath Local filesystem path to the source image
775 * @param integer $flags A bitwise combination of:
776 * File::DELETE_SOURCE Delete the source file, i.e. move
778 * @return The archive name on success or an empty string if it was a new
779 * file, and a wikitext-formatted WikiError object on failure.
782 * Overridden by LocalFile
784 function publish( $srcPath, $flags = 0 ) {
785 $this->readOnlyError();
789 * Get an array of Title objects which are articles which use this file
790 * Also adds their IDs to the link cache
792 * This is mostly copied from Title::getLinksTo()
794 * @deprecated Use HTMLCacheUpdate, this function uses too much memory
796 function getLinksTo( $options = '' ) {
797 wfProfileIn( __METHOD__ );
799 // Note: use local DB not repo DB, we want to know local links
801 $db = wfGetDB( DB_MASTER );
803 $db = wfGetDB( DB_SLAVE );
805 $linkCache =& LinkCache::singleton();
807 list( $page, $imagelinks ) = $db->tableNamesN( 'page', 'imagelinks' );
808 $encName = $db->addQuotes( $this->getName() );
809 $sql = "SELECT page_namespace,page_title,page_id FROM $page,$imagelinks WHERE page_id=il_from AND il_to=$encName $options";
810 $res = $db->query( $sql, __METHOD__ );
813 if ( $db->numRows( $res ) ) {
814 while ( $row = $db->fetchObject( $res ) ) {
815 if ( $titleObj = Title::makeTitle( $row->page_namespace, $row->page_title ) ) {
816 $linkCache->addGoodLinkObj( $row->page_id, $titleObj );
817 $retVal[] = $titleObj;
821 $db->freeResult( $res );
822 wfProfileOut( __METHOD__ );
826 function formatMetadata() {
827 if ( !$this->getHandler() ) {
830 return $this->getHandler()->formatMetadata( $this, $this->getMetadata() );
834 * Returns true if the file comes from the local file repository.
839 return $this->getRepoName() == 'local';
843 * Returns the name of the repository.
847 function getRepoName() {
848 return $this->repo ? $this->repo->getName() : 'unknown';
852 * Returns true if the image is an old version
860 * Is this file a "deleted" file in a private archive?
863 function isDeleted( $field ) {
868 * Was this file ever deleted from the wiki?
872 function wasDeleted() {
873 $title = $this->getTitle();
874 return $title && $title->isDeleted() > 0;
878 * Delete all versions of the file.
880 * Moves the files into an archive directory (or deletes them)
881 * and removes the database rows.
883 * Cache purging is done; logging is caller's responsibility.
886 * @return true on success, false on some kind of failure
888 * Overridden by LocalFile
890 function delete( $reason, $suppress=false ) {
891 $this->readOnlyError();
895 * Restore all or specified deleted revisions to the given file.
896 * Permissions and logging are left to the caller.
898 * May throw database exceptions on error.
900 * @param $versions set of record ids of deleted items to restore,
901 * or empty to restore all revisions.
902 * @return the number of file revisions restored if successful,
903 * or false on failure
905 * Overridden by LocalFile
907 function restore( $versions=array(), $Unsuppress=false ) {
908 $this->readOnlyError();
912 * Returns 'true' if this image is a multipage document, e.g. a DJVU
917 function isMultipage() {
918 return $this->getHandler() && $this->handler->isMultiPage( $this );
922 * Returns the number of pages of a multipage document, or NULL for
923 * documents which aren't multipage documents
925 function pageCount() {
926 if ( !isset( $this->pageCount ) ) {
927 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
928 $this->pageCount = $this->handler->pageCount( $this );
930 $this->pageCount = false;
933 return $this->pageCount;
937 * Calculate the height of a thumbnail using the source and destination width
939 static function scaleHeight( $srcWidth, $srcHeight, $dstWidth ) {
940 // Exact integer multiply followed by division
941 if ( $srcWidth == 0 ) {
944 return round( $srcHeight * $dstWidth / $srcWidth );
949 * Get an image size array like that returned by getimagesize(), or false if it
950 * can't be determined.
952 * @param string $fileName The filename
955 function getImageSize( $fileName ) {
956 if ( !$this->getHandler() ) {
959 return $this->handler->getImageSize( $this, $fileName );
963 * Get the URL of the image description page. May return false if it is
964 * unknown or not applicable.
966 function getDescriptionUrl() {
967 return $this->repo->getDescriptionUrl( $this->getName() );
971 * Get the HTML text of the description page, if available
973 function getDescriptionText() {
974 if ( !$this->repo->fetchDescription ) {
977 $renderUrl = $this->repo->getDescriptionRenderUrl( $this->getName() );
979 wfDebug( "Fetching shared description from $renderUrl\n" );
980 return Http::get( $renderUrl );
987 * Get the 14-character timestamp of the file upload, or false if
990 function getTimestamp() {
991 $path = $this->getPath();
992 if ( !file_exists( $path ) ) {
995 return wfTimestamp( filemtime( $path ) );
999 * Get the SHA-1 base 36 hash of the file
1001 function getSha1() {
1002 return self::sha1Base36( $this->getPath() );
1006 * Determine if the current user is allowed to view a particular
1007 * field of this file, if it's marked as deleted.
1012 function userCan( $field ) {
1017 * Get an associative array containing information about a file in the local filesystem\
1019 * @param string $path Absolute local filesystem path
1020 * @param mixed $ext The file extension, or true to extract it from the filename.
1021 * Set it to false to ignore the extension.
1023 static function getPropsFromPath( $path, $ext = true ) {
1024 wfProfileIn( __METHOD__ );
1025 wfDebug( __METHOD__.": Getting file info for $path\n" );
1027 'fileExists' => file_exists( $path ) && !is_dir( $path )
1030 if ( $info['fileExists'] ) {
1031 $magic = MimeMagic::singleton();
1033 $info['mime'] = $magic->guessMimeType( $path, $ext );
1034 list( $info['major_mime'], $info['minor_mime'] ) = self::splitMime( $info['mime'] );
1035 $info['media_type'] = $magic->getMediaType( $path, $info['mime'] );
1038 $info['size'] = filesize( $path );
1040 # Height, width and metadata
1041 $handler = MediaHandler::getHandler( $info['mime'] );
1043 $tempImage = (object)array();
1044 $info['metadata'] = $handler->getMetadata( $tempImage, $path );
1045 $gis = $handler->getImageSize( $tempImage, $path, $info['metadata'] );
1048 $info['metadata'] = '';
1050 $info['sha1'] = self::sha1Base36( $path );
1052 wfDebug(__METHOD__.": $path loaded, {$info['size']} bytes, {$info['mime']}.\n");
1054 $info['mime'] = NULL;
1055 $info['media_type'] = MEDIATYPE_UNKNOWN;
1056 $info['metadata'] = '';
1058 wfDebug(__METHOD__.": $path NOT FOUND!\n");
1061 # NOTE: $gis[2] contains a code for the image type. This is no longer used.
1062 $info['width'] = $gis[0];
1063 $info['height'] = $gis[1];
1064 if ( isset( $gis['bits'] ) ) {
1065 $info['bits'] = $gis['bits'];
1071 $info['height'] = 0;
1074 wfProfileOut( __METHOD__ );
1079 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
1080 * encoding, zero padded to 31 digits.
1082 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
1085 * Returns false on failure
1087 static function sha1Base36( $path ) {
1088 wfSuppressWarnings();
1089 $hash = sha1_file( $path );
1090 wfRestoreWarnings();
1091 if ( $hash === false ) {
1094 return wfBaseConvert( $hash, 16, 36, 31 );
1098 function getLongDesc() {
1099 $handler = $this->getHandler();
1101 return $handler->getLongDesc( $this );
1103 return MediaHandler::getLongDesc( $this );
1107 function getShortDesc() {
1108 $handler = $this->getHandler();
1110 return $handler->getShortDesc( $this );
1112 return MediaHandler::getShortDesc( $this );
1116 function getDimensionsString() {
1117 $handler = $this->getHandler();
1119 return $handler->getDimensionsString( $this );
1126 * Aliases for backwards compatibility with 1.6
1128 define( 'MW_IMG_DELETED_FILE', File::DELETED_FILE );
1129 define( 'MW_IMG_DELETED_COMMENT', File::DELETED_COMMENT );
1130 define( 'MW_IMG_DELETED_USER', File::DELETED_USER );
1131 define( 'MW_IMG_DELETED_RESTRICTED', File::DELETED_RESTRICTED );