Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
57.69% covered (warning)
57.69%
15 / 26
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
TiffHandler
60.00% covered (warning)
60.00%
15 / 25
0.00% covered (danger)
0.00%
0 / 5
14.18
0.00% covered (danger)
0.00%
0 / 1
 canRender
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 mustRender
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getThumbType
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getSizeAndMetadata
93.75% covered (success)
93.75%
15 / 16
0.00% covered (danger)
0.00%
0 / 1
4.00
 isExpensiveToThumbnail
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Handler for Tiff images.
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 * @ingroup Media
8 */
9
10namespace MediaWiki\Media;
11
12use MediaWiki\FileRepo\File\File;
13use MediaWiki\FileRepo\ForeignAPIRepo;
14use MediaWiki\MainConfigNames;
15use MediaWiki\MediaWikiServices;
16
17/**
18 * Handler for Tiff images.
19 *
20 * @ingroup Media
21 */
22class TiffHandler extends ExifBitmapHandler {
23    /**
24     * TIFF files over 10M are considered expensive to thumbnail
25     */
26    private const EXPENSIVE_SIZE_LIMIT = 10_485_760;
27
28    /**
29     * Conversion to PNG for inline display can be disabled here...
30     * Note scaling should work with ImageMagick, but may not with GD scaling.
31     *
32     * Files pulled from an another MediaWiki instance via ForeignAPIRepo /
33     * InstantCommons will have thumbnails managed from the remote instance,
34     * so we can skip this check.
35     *
36     * @param File $file
37     * @return bool
38     */
39    public function canRender( $file ) {
40        $tiffThumbnailType = MediaWikiServices::getInstance()->getMainConfig()
41            ->get( MainConfigNames::TiffThumbnailType );
42
43        return (bool)$tiffThumbnailType
44            || $file->getRepo() instanceof ForeignAPIRepo;
45    }
46
47    /**
48     * Browsers don't support TIFF inline generally...
49     * For inline display, we need to convert to PNG.
50     *
51     * @param File $file
52     * @return bool
53     */
54    public function mustRender( $file ) {
55        return true;
56    }
57
58    /**
59     * @param string $ext
60     * @param string $mime
61     * @param array|null $params
62     * @return array
63     */
64    public function getThumbType( $ext, $mime, $params = null ) {
65        $tiffThumbnailType = MediaWikiServices::getInstance()->getMainConfig()
66            ->get( MainConfigNames::TiffThumbnailType );
67
68        return $tiffThumbnailType;
69    }
70
71    /** @inheritDoc */
72    public function getSizeAndMetadata( $state, $filename ) {
73        $showEXIF = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::ShowEXIF );
74
75        try {
76            $meta = BitmapMetadataHandler::Tiff( $filename );
77            if ( !is_array( $meta ) ) {
78                // This should never happen, but doesn't hurt to be paranoid.
79                throw new InvalidTiffException( 'Metadata array is not an array' );
80            }
81            $info = [
82                'width' => $meta['ImageWidth'] ?? 0,
83                'height' => $meta['ImageLength'] ?? 0,
84            ];
85            $info = $this->applyExifRotation( $info, $meta );
86            if ( $showEXIF ) {
87                $meta['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
88                $info['metadata'] = $meta;
89            }
90            return $info;
91        } catch ( InvalidTiffException $e ) {
92            // BitmapMetadataHandler throws an exception in certain exceptional
93            // cases like if file does not exist.
94            wfDebug( __METHOD__ . ': ' . $e->getMessage() );
95
96            return [ 'metadata' => [ '_error' => ExifBitmapHandler::BROKEN_FILE ] ];
97        }
98    }
99
100    /** @inheritDoc */
101    public function isExpensiveToThumbnail( $file ) {
102        return $file->getSize() > static::EXPENSIVE_SIZE_LIMIT;
103    }
104}
105
106/** @deprecated class alias since 1.46 */
107class_alias( TiffHandler::class, 'TiffHandler' );