Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.99% covered (danger)
0.99%
1 / 101
6.25% covered (danger)
6.25%
1 / 16
CRAP
0.00% covered (danger)
0.00%
0 / 1
ImageHandler
0.99% covered (danger)
0.99%
1 / 101
6.25% covered (danger)
6.25%
1 / 16
1923.06
0.00% covered (danger)
0.00%
0 / 1
 canRender
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 getParamMap
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 validateParam
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 makeParamString
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 parseParamString
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getScriptParams
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 normaliseParams
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
182
 validateThumbParams
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
 getScriptedTransform
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 getImageSize
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getSizeAndMetadata
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 getImageArea
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getShortDesc
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getLongDesc
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 getDimensionsString
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 sanitizeParamsForBucketing
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Media-handling base classes and generic functionality.
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 * @ingroup Media
8 */
9
10use MediaWiki\FileRepo\File\File;
11use Wikimedia\AtEase\AtEase;
12
13/**
14 * Media handler abstract base class for images
15 *
16 * @stable to extend
17 *
18 * @ingroup Media
19 */
20abstract class ImageHandler extends MediaHandler {
21    /**
22     * @inheritDoc
23     * @stable to override
24     * @param File $file
25     * @return bool
26     */
27    public function canRender( $file ) {
28        return ( $file->getWidth() && $file->getHeight() );
29    }
30
31    /**
32     * @inheritDoc
33     * @stable to override
34     * @return string[]
35     */
36    public function getParamMap() {
37        return [ 'img_width' => 'width' ];
38    }
39
40    /**
41     * @inheritDoc
42     * @stable to override
43     */
44    public function validateParam( $name, $value ) {
45        return in_array( $name, [ 'width', 'height' ] ) && $value > 0;
46    }
47
48    /**
49     * @inheritDoc
50     * @stable to override
51     * @throws MediaTransformInvalidParametersException
52     */
53    public function makeParamString( $params ) {
54        if ( isset( $params['physicalWidth'] ) ) {
55            $width = $params['physicalWidth'];
56        } elseif ( isset( $params['width'] ) ) {
57            $width = $params['width'];
58        } else {
59            throw new MediaTransformInvalidParametersException( 'No width specified to ' . __METHOD__ );
60        }
61
62        # Removed for ProofreadPage
63        # $width = intval( $width );
64        return "{$width}px";
65    }
66
67    /**
68     * @inheritDoc
69     * @stable to override
70     */
71    public function parseParamString( $str ) {
72        $m = false;
73        if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
74            return [ 'width' => $m[1] ];
75        }
76        return false;
77    }
78
79    /**
80     * @stable to override
81     * @param array $params
82     * @return array
83     */
84    protected function getScriptParams( $params ) {
85        return [ 'width' => $params['width'] ];
86    }
87
88    /**
89     * @inheritDoc
90     * @stable to override
91     * @param File $image
92     * @param array &$params @phan-ignore-reference
93     * @return bool
94     * @phan-assert array{width:int,physicalWidth:int,height:int,physicalHeight:int,page:int} $params
95     */
96    public function normaliseParams( $image, &$params ) {
97        if ( !isset( $params['width'] ) ) {
98            return false;
99        }
100
101        if ( !isset( $params['page'] ) ) {
102            $params['page'] = 1;
103        } else {
104            $params['page'] = (int)$params['page'];
105            if ( $params['page'] > $image->pageCount() ) {
106                $params['page'] = $image->pageCount();
107            }
108
109            if ( $params['page'] < 1 ) {
110                $params['page'] = 1;
111            }
112        }
113
114        $srcWidth = $image->getWidth( $params['page'] );
115        $srcHeight = $image->getHeight( $params['page'] );
116
117        if ( isset( $params['height'] ) && $params['height'] !== -1 ) {
118            # Height & width were both set
119            if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
120                # Height is the relative smaller dimension, so scale width accordingly
121                $params['width'] = self::fitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
122
123                if ( $params['width'] == 0 ) {
124                    # Very small image, so we need to rely on client side scaling :(
125                    $params['width'] = 1;
126                }
127
128                $params['physicalWidth'] = $params['width'];
129            } else {
130                # Height was crap, unset it so that it will be calculated later
131                unset( $params['height'] );
132            }
133        }
134
135        if ( !isset( $params['physicalWidth'] ) ) {
136            # Passed all validations, so set the physicalWidth
137            // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive, checked above
138            $params['physicalWidth'] = $params['width'];
139        }
140
141        # Because thumbs are only referred to by width, the height always needs
142        # to be scaled by the width to keep the thumbnail sizes consistent,
143        # even if it was set inside the if block above
144        $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight,
145            $params['physicalWidth'] );
146
147        # Set the height if it was not validated in the if block higher up
148        if ( !isset( $params['height'] ) || $params['height'] === -1 ) {
149            $params['height'] = $params['physicalHeight'];
150        }
151
152        if ( !$this->validateThumbParams( $params['physicalWidth'],
153            $params['physicalHeight'], $srcWidth, $srcHeight )
154        ) {
155            return false;
156        }
157
158        return true;
159    }
160
161    /**
162     * Validate thumbnail parameters and fill in the correct height
163     *
164     * @param int &$width Specified width (input/output)
165     * @param int &$height Height (output only)
166     * @param int $srcWidth Width of the source image
167     * @param int $srcHeight Height of the source image
168     * @return bool False to indicate that an error should be returned to the user.
169     */
170    private function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight ) {
171        $width = (int)$width;
172
173        if ( $width <= 0 ) {
174            wfDebug( __METHOD__ . ": Invalid destination width: $width" );
175
176            return false;
177        }
178        if ( $srcWidth <= 0 ) {
179            wfDebug( __METHOD__ . ": Invalid source width: $srcWidth" );
180
181            return false;
182        }
183
184        $height = File::scaleHeight( $srcWidth, $srcHeight, $width );
185        if ( $height == 0 ) {
186            # Force height to be at least 1 pixel
187            $height = 1;
188        }
189
190        return true;
191    }
192
193    /**
194     * @inheritDoc
195     * @stable to override
196     * @param File $image
197     * @param string $script
198     * @param array $params
199     * @return MediaTransformOutput|false
200     */
201    public function getScriptedTransform( $image, $script, $params ) {
202        if ( !$this->normaliseParams( $image, $params ) ) {
203            return false;
204        }
205        $url = wfAppendQuery( $script, $this->getScriptParams( $params ) );
206
207        if ( $image->mustRender() || $params['width'] < $image->getWidth() ) {
208            return new ThumbnailImage( $image, $url, false, $params );
209        }
210    }
211
212    /** @inheritDoc */
213    public function getImageSize( $image, $path ) {
214        AtEase::suppressWarnings();
215        $gis = getimagesize( $path );
216        AtEase::restoreWarnings();
217
218        return $gis;
219    }
220
221    /** @inheritDoc */
222    public function getSizeAndMetadata( $state, $path ) {
223        // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
224        $gis = @getimagesize( $path );
225        if ( $gis ) {
226            $info = [
227                'width' => $gis[0],
228                'height' => $gis[1],
229            ];
230            if ( isset( $gis['bits'] ) ) {
231                $info['bits'] = $gis['bits'];
232            }
233        } else {
234            $info = [];
235        }
236        return $info;
237    }
238
239    /**
240     * Function that returns the number of pixels to be thumbnailed.
241     * Intended for animated GIFs to multiply by the number of frames.
242     *
243     * If the file doesn't support a notion of "area" return 0.
244     * @stable to override
245     *
246     * @param File $image
247     * @return int
248     */
249    public function getImageArea( $image ) {
250        return $image->getWidth() * $image->getHeight();
251    }
252
253    /**
254     * @inheritDoc
255     * @stable to override
256     * @param File $file
257     * @return string
258     */
259    public function getShortDesc( $file ) {
260        global $wgLang;
261        $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ), ENT_QUOTES );
262        $widthheight = wfMessage( 'widthheight' )
263            ->numParams( $file->getWidth(), $file->getHeight() )
264            ->escaped();
265
266        return "$widthheight ($nbytes)";
267    }
268
269    /**
270     * @inheritDoc
271     * @stable to override
272     * @param File $file
273     * @return string
274     */
275    public function getLongDesc( $file ) {
276        $pages = $file->pageCount();
277        if ( $pages === false || $pages <= 1 ) {
278            $msg = wfMessage( 'file-info-size' )
279                ->numParams( $file->getWidth(), $file->getHeight() )
280                ->sizeParams( $file->getSize() )
281                ->params( '<span class="mime-type">' . $file->getMimeType() . '</span>' )
282                ->parse();
283        } else {
284            $msg = wfMessage( 'file-info-size-pages' )
285                ->numParams( $file->getWidth(), $file->getHeight() )
286                ->sizeParams( $file->getSize() )
287                ->params( '<span class="mime-type">' . $file->getMimeType() . '</span>' )->numParams( $pages )
288                ->parse();
289        }
290
291        return $msg;
292    }
293
294    /**
295     * @inheritDoc
296     * @stable to override
297     * @param File $file
298     * @return string
299     */
300    public function getDimensionsString( $file ) {
301        $pages = $file->pageCount();
302        if ( $pages > 1 ) {
303            return wfMessage( 'widthheightpage' )
304                ->numParams( $file->getWidth(), $file->getHeight(), $pages )->text();
305        }
306        return wfMessage( 'widthheight' )
307            ->numParams( $file->getWidth(), $file->getHeight() )->text();
308    }
309
310    /**
311     * @inheritDoc
312     * @stable to override
313     */
314    public function sanitizeParamsForBucketing( $params ) {
315        $params = parent::sanitizeParamsForBucketing( $params );
316
317        // We unset the height parameters in order to let normaliseParams recalculate them
318        // Otherwise there might be a height discrepancy
319        unset( $params['height'] );
320        unset( $params['physicalHeight'] );
321
322        return $params;
323    }
324}