Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
29.32% |
78 / 266 |
|
17.24% |
5 / 29 |
CRAP | |
0.00% |
0 / 1 |
SvgHandler | |
29.32% |
78 / 266 |
|
17.24% |
5 / 29 |
3559.18 | |
0.00% |
0 / 1 |
isEnabled | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
3.01 | |||
allowRenderingByUserAgent | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
mustRender | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isVectorized | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isAnimatedImage | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getAvailableLanguages | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
getMatchedLanguage | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
6.03 | |||
getLanguageFromParams | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDefaultRenderLanguage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
canAnimateThumbnail | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
normaliseParams | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
normaliseParamsInternal | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
6 | |||
doTransform | |
0.00% |
0 / 59 |
|
0.00% |
0 / 1 |
132 | |||
rasterize | |
0.00% |
0 / 36 |
|
0.00% |
0 / 1 |
72 | |||
rasterizeImagickExt | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
getThumbType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getLongDesc | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
getSizeAndMetadata | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
validateMetadata | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
getMetadataType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isFileMetadataValid | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
visibleMetadataFields | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
formatMetadata | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
56 | |||
validateParam | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
30 | |||
makeParamString | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
parseParamString | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
4.03 | |||
getParamMap | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getScriptParams | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getCommonMetaArray | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
7.03 |
1 | <?php |
2 | /** |
3 | * Handler for SVG images. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | * @ingroup Media |
22 | */ |
23 | |
24 | use MediaWiki\Context\IContextSource; |
25 | use MediaWiki\FileRepo\File\File; |
26 | use MediaWiki\Language\LanguageCode; |
27 | use MediaWiki\MainConfigNames; |
28 | use MediaWiki\MediaWikiServices; |
29 | use MediaWiki\Shell\Shell; |
30 | use Wikimedia\AtEase\AtEase; |
31 | use Wikimedia\ScopedCallback; |
32 | |
33 | /** |
34 | * Handler for SVG images. |
35 | * |
36 | * @ingroup Media |
37 | */ |
38 | class SvgHandler extends ImageHandler { |
39 | public const SVG_METADATA_VERSION = 2; |
40 | |
41 | private const SVG_DEFAULT_RENDER_LANG = 'en'; |
42 | |
43 | /** @var array A list of metadata tags that can be converted |
44 | * to the commonly used exif tags. This allows messages |
45 | * to be reused, and consistent tag names for {{#formatmetadata:..}} |
46 | */ |
47 | private static $metaConversion = [ |
48 | 'originalwidth' => 'ImageWidth', |
49 | 'originalheight' => 'ImageLength', |
50 | 'description' => 'ImageDescription', |
51 | 'title' => 'ObjectName', |
52 | ]; |
53 | |
54 | public function isEnabled() { |
55 | $config = MediaWikiServices::getInstance()->getMainConfig(); |
56 | $svgConverters = $config->get( MainConfigNames::SVGConverters ); |
57 | $svgConverter = $config->get( MainConfigNames::SVGConverter ); |
58 | if ( $config->get( MainConfigNames::SVGNativeRendering ) === true ) { |
59 | return true; |
60 | } |
61 | if ( !isset( $svgConverters[$svgConverter] ) ) { |
62 | wfDebug( "\$wgSVGConverter is invalid, disabling SVG rendering." ); |
63 | |
64 | return false; |
65 | } |
66 | |
67 | return true; |
68 | } |
69 | |
70 | public function allowRenderingByUserAgent( $file ) { |
71 | $svgNativeRendering = MediaWikiServices::getInstance() |
72 | ->getMainConfig()->get( MainConfigNames::SVGNativeRendering ); |
73 | if ( $svgNativeRendering === true ) { |
74 | // Don't do any transform for any SVG. |
75 | return true; |
76 | } |
77 | if ( $svgNativeRendering !== 'partial' ) { |
78 | // SVG images are always rasterized to PNG |
79 | return false; |
80 | } |
81 | $maxSVGFilesize = MediaWikiServices::getInstance() |
82 | ->getMainConfig()->get( MainConfigNames::SVGNativeRenderingSizeLimit ); |
83 | // Browsers don't really support SVG translations, so always render them to PNG |
84 | // Files bigger than the limit are also rendered as PNG, as big files might be a tax on the user agent |
85 | return count( $this->getAvailableLanguages( $file ) ) <= 1 |
86 | && $file->getSize() <= $maxSVGFilesize; |
87 | } |
88 | |
89 | public function mustRender( $file ) { |
90 | return !$this->allowRenderingByUserAgent( $file ); |
91 | } |
92 | |
93 | public function isVectorized( $file ) { |
94 | return true; |
95 | } |
96 | |
97 | /** |
98 | * @param File $file |
99 | * @return bool |
100 | */ |
101 | public function isAnimatedImage( $file ) { |
102 | # @todo Detect animated SVGs |
103 | $metadata = $this->validateMetadata( $file->getMetadataArray() ); |
104 | if ( isset( $metadata['animated'] ) ) { |
105 | return $metadata['animated']; |
106 | } |
107 | |
108 | return false; |
109 | } |
110 | |
111 | /** |
112 | * Which languages (systemLanguage attribute) is supported. |
113 | * |
114 | * @note This list is not guaranteed to be exhaustive. |
115 | * To avoid OOM errors, we only look at first bit of a file. |
116 | * Thus all languages on this list are present in the file, |
117 | * but its possible for the file to have a language not on |
118 | * this list. |
119 | * |
120 | * @param File $file |
121 | * @return string[] Array of language codes, or empty if no language switching supported. |
122 | */ |
123 | public function getAvailableLanguages( File $file ) { |
124 | $langList = []; |
125 | $metadata = $this->validateMetadata( $file->getMetadataArray() ); |
126 | if ( isset( $metadata['translations'] ) ) { |
127 | foreach ( $metadata['translations'] as $lang => $langType ) { |
128 | if ( $langType === SVGReader::LANG_FULL_MATCH ) { |
129 | $langList[] = strtolower( $lang ); |
130 | } |
131 | } |
132 | } |
133 | return array_unique( $langList ); |
134 | } |
135 | |
136 | /** |
137 | * SVG's systemLanguage matching rules state: |
138 | * 'The `systemLanguage` attribute ... [e]valuates to "true" if one of the languages indicated |
139 | * by user preferences exactly equals one of the languages given in the value of this parameter, |
140 | * or if one of the languages indicated by user preferences exactly equals a prefix of one of |
141 | * the languages given in the value of this parameter such that the first tag character |
142 | * following the prefix is "-".' |
143 | * |
144 | * Return the first element of $svgLanguages that matches $userPreferredLanguage |
145 | * |
146 | * @see https://www.w3.org/TR/SVG/struct.html#SystemLanguageAttribute |
147 | * @param string $userPreferredLanguage |
148 | * @param string[] $svgLanguages |
149 | * @return string|null |
150 | */ |
151 | public function getMatchedLanguage( $userPreferredLanguage, array $svgLanguages ) { |
152 | // Explicitly requested undetermined language (text without svg systemLanguage attribute) |
153 | if ( $userPreferredLanguage === 'und' ) { |
154 | return 'und'; |
155 | } |
156 | foreach ( $svgLanguages as $svgLang ) { |
157 | if ( strcasecmp( $svgLang, $userPreferredLanguage ) === 0 ) { |
158 | return $svgLang; |
159 | } |
160 | $trimmedSvgLang = $svgLang; |
161 | while ( strpos( $trimmedSvgLang, '-' ) !== false ) { |
162 | $trimmedSvgLang = substr( $trimmedSvgLang, 0, strrpos( $trimmedSvgLang, '-' ) ); |
163 | if ( strcasecmp( $trimmedSvgLang, $userPreferredLanguage ) === 0 ) { |
164 | return $svgLang; |
165 | } |
166 | } |
167 | } |
168 | return null; |
169 | } |
170 | |
171 | /** |
172 | * Determines render language from image parameters |
173 | * This is a lowercase IETF language |
174 | * |
175 | * @param array $params |
176 | * @return string |
177 | */ |
178 | protected function getLanguageFromParams( array $params ) { |
179 | return $params['lang'] ?? $params['targetlang'] ?? self::SVG_DEFAULT_RENDER_LANG; |
180 | } |
181 | |
182 | /** |
183 | * What language to render file in if none selected |
184 | * |
185 | * @param File $file Language code |
186 | * @return string |
187 | */ |
188 | public function getDefaultRenderLanguage( File $file ) { |
189 | return self::SVG_DEFAULT_RENDER_LANG; |
190 | } |
191 | |
192 | /** |
193 | * We do not support making animated svg thumbnails |
194 | * @param File $file |
195 | * @return bool |
196 | */ |
197 | public function canAnimateThumbnail( $file ) { |
198 | return $this->allowRenderingByUserAgent( $file ); |
199 | } |
200 | |
201 | /** |
202 | * @param File $image |
203 | * @param array &$params |
204 | * @return bool |
205 | */ |
206 | public function normaliseParams( $image, &$params ) { |
207 | if ( parent::normaliseParams( $image, $params ) ) { |
208 | $params = $this->normaliseParamsInternal( $image, $params ); |
209 | return true; |
210 | } |
211 | |
212 | return false; |
213 | } |
214 | |
215 | /** |
216 | * Code taken out of normaliseParams() for testability |
217 | * |
218 | * @since 1.33 |
219 | * |
220 | * @param File $image |
221 | * @param array $params |
222 | * @return array Modified $params |
223 | */ |
224 | protected function normaliseParamsInternal( $image, $params ) { |
225 | $svgMaxSize = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::SVGMaxSize ); |
226 | |
227 | # Don't make an image bigger than wgMaxSVGSize on the smaller side |
228 | if ( $params['physicalWidth'] <= $params['physicalHeight'] ) { |
229 | if ( $params['physicalWidth'] > $svgMaxSize ) { |
230 | $srcWidth = $image->getWidth( $params['page'] ); |
231 | $srcHeight = $image->getHeight( $params['page'] ); |
232 | $params['physicalWidth'] = $svgMaxSize; |
233 | $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $svgMaxSize ); |
234 | } |
235 | } elseif ( $params['physicalHeight'] > $svgMaxSize ) { |
236 | $srcWidth = $image->getWidth( $params['page'] ); |
237 | $srcHeight = $image->getHeight( $params['page'] ); |
238 | $params['physicalWidth'] = File::scaleHeight( $srcHeight, $srcWidth, $svgMaxSize ); |
239 | $params['physicalHeight'] = $svgMaxSize; |
240 | } |
241 | // To prevent the proliferation of thumbnails in languages not present in SVGs, unless |
242 | // explicitly forced by user. |
243 | if ( isset( $params['targetlang'] ) && !$image->getMatchedLanguage( $params['targetlang'] ) ) { |
244 | unset( $params['targetlang'] ); |
245 | } |
246 | |
247 | return $params; |
248 | } |
249 | |
250 | /** |
251 | * @param File $image |
252 | * @param string $dstPath |
253 | * @param string $dstUrl |
254 | * @param array $params |
255 | * @param int $flags |
256 | * @return MediaTransformError|ThumbnailImage|TransformParameterError|false |
257 | */ |
258 | public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) { |
259 | if ( !$this->normaliseParams( $image, $params ) ) { |
260 | return new TransformParameterError( $params ); |
261 | } |
262 | $clientWidth = $params['width']; |
263 | $clientHeight = $params['height']; |
264 | $physicalWidth = $params['physicalWidth']; |
265 | $physicalHeight = $params['physicalHeight']; |
266 | $lang = $this->getLanguageFromParams( $params ); |
267 | |
268 | if ( $this->allowRenderingByUserAgent( $image ) ) { |
269 | // No transformation required for native rendering |
270 | return new ThumbnailImage( $image, $image->getURL(), false, $params ); |
271 | } |
272 | |
273 | if ( $flags & self::TRANSFORM_LATER ) { |
274 | return new ThumbnailImage( $image, $dstUrl, $dstPath, $params ); |
275 | } |
276 | |
277 | $metadata = $this->validateMetadata( $image->getMetadataArray() ); |
278 | if ( isset( $metadata['error'] ) ) { |
279 | $err = wfMessage( 'svg-long-error', $metadata['error']['message'] ); |
280 | |
281 | return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err ); |
282 | } |
283 | |
284 | if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) { |
285 | return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, |
286 | wfMessage( 'thumbnail_dest_directory' ) ); |
287 | } |
288 | |
289 | $srcPath = $image->getLocalRefPath(); |
290 | if ( $srcPath === false ) { // Failed to get local copy |
291 | wfDebugLog( 'thumbnail', |
292 | sprintf( 'Thumbnail failed on %s: could not get local copy of "%s"', |
293 | wfHostname(), $image->getName() ) ); |
294 | |
295 | return new MediaTransformError( 'thumbnail_error', |
296 | $params['width'], $params['height'], |
297 | wfMessage( 'filemissing' ) |
298 | ); |
299 | } |
300 | |
301 | // Make a temp dir with a symlink to the local copy in it. |
302 | // This plays well with rsvg-convert policy for external entities. |
303 | // https://git.gnome.org/browse/librsvg/commit/?id=f01aded72c38f0e18bc7ff67dee800e380251c8e |
304 | $tmpDir = wfTempDir() . '/svg_' . wfRandomString( 24 ); |
305 | $lnPath = "$tmpDir/" . basename( $srcPath ); |
306 | $ok = mkdir( $tmpDir, 0771 ); |
307 | if ( !$ok ) { |
308 | wfDebugLog( 'thumbnail', |
309 | sprintf( 'Thumbnail failed on %s: could not create temporary directory %s', |
310 | wfHostname(), $tmpDir ) ); |
311 | return new MediaTransformError( 'thumbnail_error', |
312 | $params['width'], $params['height'], |
313 | wfMessage( 'thumbnail-temp-create' )->text() |
314 | ); |
315 | } |
316 | // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged |
317 | $ok = @symlink( $srcPath, $lnPath ); |
318 | /** @noinspection PhpUnusedLocalVariableInspection */ |
319 | $cleaner = new ScopedCallback( static function () use ( $tmpDir, $lnPath ) { |
320 | AtEase::suppressWarnings(); |
321 | unlink( $lnPath ); |
322 | rmdir( $tmpDir ); |
323 | AtEase::restoreWarnings(); |
324 | } ); |
325 | if ( !$ok ) { |
326 | // Fallback because symlink often fails on Windows |
327 | $ok = copy( $srcPath, $lnPath ); |
328 | } |
329 | if ( !$ok ) { |
330 | wfDebugLog( 'thumbnail', |
331 | sprintf( 'Thumbnail failed on %s: could not link %s to %s', |
332 | wfHostname(), $lnPath, $srcPath ) ); |
333 | return new MediaTransformError( 'thumbnail_error', |
334 | $params['width'], $params['height'], |
335 | wfMessage( 'thumbnail-temp-create' ) |
336 | ); |
337 | } |
338 | |
339 | $status = $this->rasterize( $lnPath, $dstPath, $physicalWidth, $physicalHeight, $lang ); |
340 | if ( $status === true ) { |
341 | return new ThumbnailImage( $image, $dstUrl, $dstPath, $params ); |
342 | } |
343 | |
344 | return $status; // MediaTransformError |
345 | } |
346 | |
347 | /** |
348 | * Transform an SVG file to PNG |
349 | * This function can be called outside of thumbnail contexts |
350 | * @param string $srcPath |
351 | * @param string $dstPath |
352 | * @param int $width |
353 | * @param int $height |
354 | * @param string|false $lang Language code of the language to render the SVG in |
355 | * @return bool|MediaTransformError |
356 | */ |
357 | public function rasterize( $srcPath, $dstPath, $width, $height, $lang = false ) { |
358 | $mainConfig = MediaWikiServices::getInstance()->getMainConfig(); |
359 | $svgConverters = $mainConfig->get( MainConfigNames::SVGConverters ); |
360 | $svgConverter = $mainConfig->get( MainConfigNames::SVGConverter ); |
361 | $svgConverterPath = $mainConfig->get( MainConfigNames::SVGConverterPath ); |
362 | $err = false; |
363 | $retval = ''; |
364 | if ( isset( $svgConverters[$svgConverter] ) ) { |
365 | if ( is_array( $svgConverters[$svgConverter] ) ) { |
366 | // This is a PHP callable |
367 | $func = $svgConverters[$svgConverter][0]; |
368 | if ( !is_callable( $func ) ) { |
369 | throw new UnexpectedValueException( "$func is not callable" ); |
370 | } |
371 | $err = $func( $srcPath, |
372 | $dstPath, |
373 | $width, |
374 | $height, |
375 | $lang, |
376 | ...array_slice( $svgConverters[$svgConverter], 1 ) |
377 | ); |
378 | $retval = (bool)$err; |
379 | } else { |
380 | // External command |
381 | $cmd = strtr( $svgConverters[$svgConverter], [ |
382 | '$path/' => $svgConverterPath ? Shell::escape( "$svgConverterPath/" ) : '', |
383 | '$width' => (int)$width, |
384 | '$height' => (int)$height, |
385 | '$input' => Shell::escape( $srcPath ), |
386 | '$output' => Shell::escape( $dstPath ), |
387 | ] ); |
388 | |
389 | $env = []; |
390 | if ( $lang !== false ) { |
391 | $env['LANG'] = $lang; |
392 | } |
393 | |
394 | wfDebug( __METHOD__ . ": $cmd" ); |
395 | $err = wfShellExecWithStderr( $cmd, $retval, $env ); |
396 | } |
397 | } |
398 | $removed = $this->removeBadFile( $dstPath, $retval ); |
399 | if ( $retval != 0 || $removed ) { |
400 | // @phan-suppress-next-next-line PhanPossiblyUndeclaredVariable cmd is set when used |
401 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable cmd is set when used |
402 | $this->logErrorForExternalProcess( $retval, $err, $cmd ); |
403 | return new MediaTransformError( 'thumbnail_error', $width, $height, $err ); |
404 | } |
405 | |
406 | return true; |
407 | } |
408 | |
409 | public static function rasterizeImagickExt( $srcPath, $dstPath, $width, $height ) { |
410 | $im = new Imagick( $srcPath ); |
411 | $im->setBackgroundColor( 'transparent' ); |
412 | $im->readImage( $srcPath ); |
413 | $im->setImageFormat( 'png' ); |
414 | $im->setImageDepth( 8 ); |
415 | |
416 | if ( !$im->thumbnailImage( (int)$width, (int)$height, /* fit */ false ) ) { |
417 | return 'Could not resize image'; |
418 | } |
419 | if ( !$im->writeImage( $dstPath ) ) { |
420 | return "Could not write to $dstPath"; |
421 | } |
422 | } |
423 | |
424 | public function getThumbType( $ext, $mime, $params = null ) { |
425 | return [ 'png', 'image/png' ]; |
426 | } |
427 | |
428 | /** |
429 | * Subtitle for the image. Different from the base |
430 | * class so it can be denoted that SVG's have |
431 | * a "nominal" resolution, and not a fixed one, |
432 | * as well as so animation can be denoted. |
433 | * |
434 | * @param File $file |
435 | * @return string |
436 | */ |
437 | public function getLongDesc( $file ) { |
438 | $metadata = $this->validateMetadata( $file->getMetadataArray() ); |
439 | if ( isset( $metadata['error'] ) ) { |
440 | return wfMessage( 'svg-long-error', $metadata['error']['message'] )->text(); |
441 | } |
442 | |
443 | if ( $this->isAnimatedImage( $file ) ) { |
444 | $msg = wfMessage( 'svg-long-desc-animated' ); |
445 | } else { |
446 | $msg = wfMessage( 'svg-long-desc' ); |
447 | } |
448 | |
449 | return $msg->numParams( $file->getWidth(), $file->getHeight() )->sizeParams( $file->getSize() )->parse(); |
450 | } |
451 | |
452 | /** |
453 | * @param MediaHandlerState $state |
454 | * @param string $filename |
455 | * @return array |
456 | */ |
457 | public function getSizeAndMetadata( $state, $filename ) { |
458 | $metadata = [ 'version' => self::SVG_METADATA_VERSION ]; |
459 | |
460 | try { |
461 | $svgReader = new SVGReader( $filename ); |
462 | $metadata += $svgReader->getMetadata(); |
463 | } catch ( InvalidSVGException $e ) { |
464 | // File not found, broken, etc. |
465 | $metadata['error'] = [ |
466 | 'message' => $e->getMessage(), |
467 | 'code' => $e->getCode() |
468 | ]; |
469 | wfDebug( __METHOD__ . ': ' . $e->getMessage() ); |
470 | } |
471 | |
472 | return [ |
473 | 'width' => $metadata['width'] ?? 0, |
474 | 'height' => $metadata['height'] ?? 0, |
475 | 'metadata' => $metadata |
476 | ]; |
477 | } |
478 | |
479 | protected function validateMetadata( $unser ) { |
480 | if ( isset( $unser['version'] ) && $unser['version'] === self::SVG_METADATA_VERSION ) { |
481 | return $unser; |
482 | } |
483 | |
484 | return null; |
485 | } |
486 | |
487 | public function getMetadataType( $image ) { |
488 | return 'parsed-svg'; |
489 | } |
490 | |
491 | public function isFileMetadataValid( $image ) { |
492 | $meta = $this->validateMetadata( $image->getMetadataArray() ); |
493 | if ( !$meta ) { |
494 | return self::METADATA_BAD; |
495 | } |
496 | if ( !isset( $meta['originalWidth'] ) ) { |
497 | // Old but compatible |
498 | return self::METADATA_COMPATIBLE; |
499 | } |
500 | |
501 | return self::METADATA_GOOD; |
502 | } |
503 | |
504 | protected function visibleMetadataFields() { |
505 | return [ 'objectname', 'imagedescription' ]; |
506 | } |
507 | |
508 | /** |
509 | * @param File $file |
510 | * @param IContextSource|false $context |
511 | * @return array[]|false |
512 | */ |
513 | public function formatMetadata( $file, $context = false ) { |
514 | $result = [ |
515 | 'visible' => [], |
516 | 'collapsed' => [] |
517 | ]; |
518 | $metadata = $this->validateMetadata( $file->getMetadataArray() ); |
519 | if ( !$metadata || isset( $metadata['error'] ) ) { |
520 | return false; |
521 | } |
522 | |
523 | /* @todo Add a formatter |
524 | $format = new FormatSVG( $metadata ); |
525 | $formatted = $format->getFormattedData(); |
526 | */ |
527 | |
528 | // Sort fields into visible and collapsed |
529 | $visibleFields = $this->visibleMetadataFields(); |
530 | |
531 | $showMeta = false; |
532 | foreach ( $metadata as $name => $value ) { |
533 | $tag = strtolower( $name ); |
534 | if ( isset( self::$metaConversion[$tag] ) ) { |
535 | $tag = strtolower( self::$metaConversion[$tag] ); |
536 | } else { |
537 | // Do not output other metadata not in list |
538 | continue; |
539 | } |
540 | $showMeta = true; |
541 | self::addMeta( $result, |
542 | in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed', |
543 | 'exif', |
544 | $tag, |
545 | $value |
546 | ); |
547 | } |
548 | |
549 | return $showMeta ? $result : false; |
550 | } |
551 | |
552 | /** |
553 | * @param string $name Parameter name |
554 | * @param mixed $value Parameter value |
555 | * @return bool Validity |
556 | */ |
557 | public function validateParam( $name, $value ) { |
558 | if ( in_array( $name, [ 'width', 'height' ] ) ) { |
559 | // Reject negative heights, widths |
560 | return ( $value > 0 ); |
561 | } |
562 | if ( $name === 'lang' ) { |
563 | // Validate $code |
564 | if ( $value === '' |
565 | || !LanguageCode::isWellFormedLanguageTag( $value ) |
566 | ) { |
567 | return false; |
568 | } |
569 | |
570 | return true; |
571 | } |
572 | |
573 | // Only lang, width and height are acceptable keys |
574 | return false; |
575 | } |
576 | |
577 | /** |
578 | * @param array $params Name=>value pairs of parameters |
579 | * @return string|false Filename to use |
580 | */ |
581 | public function makeParamString( $params ) { |
582 | $lang = ''; |
583 | $code = $this->getLanguageFromParams( $params ); |
584 | if ( $code !== self::SVG_DEFAULT_RENDER_LANG ) { |
585 | $lang = 'lang' . strtolower( $code ) . '-'; |
586 | } |
587 | |
588 | if ( isset( $params['physicalWidth'] ) && $params['physicalWidth'] ) { |
589 | return "$lang{$params['physicalWidth']}px"; |
590 | } |
591 | |
592 | if ( !isset( $params['width'] ) ) { |
593 | return false; |
594 | } |
595 | |
596 | return "$lang{$params['width']}px"; |
597 | } |
598 | |
599 | public function parseParamString( $str ) { |
600 | $m = false; |
601 | // Language codes are supposed to be lowercase |
602 | if ( preg_match( '/^lang([a-z]+(?:-[a-z]+)*)-(\d+)px$/', $str, $m ) ) { |
603 | if ( LanguageCode::isWellFormedLanguageTag( $m[1] ) ) { |
604 | return [ 'width' => array_pop( $m ), 'lang' => $m[1] ]; |
605 | } |
606 | return [ 'width' => array_pop( $m ), 'lang' => self::SVG_DEFAULT_RENDER_LANG ]; |
607 | } |
608 | if ( preg_match( '/^(\d+)px$/', $str, $m ) ) { |
609 | return [ 'width' => $m[1], 'lang' => self::SVG_DEFAULT_RENDER_LANG ]; |
610 | } |
611 | return false; |
612 | } |
613 | |
614 | public function getParamMap() { |
615 | return [ 'img_lang' => 'lang', 'img_width' => 'width' ]; |
616 | } |
617 | |
618 | /** |
619 | * @param array $params |
620 | * @return array |
621 | */ |
622 | protected function getScriptParams( $params ) { |
623 | $scriptParams = [ 'width' => $params['width'] ]; |
624 | if ( isset( $params['lang'] ) ) { |
625 | $scriptParams['lang'] = $params['lang']; |
626 | } |
627 | |
628 | return $scriptParams; |
629 | } |
630 | |
631 | public function getCommonMetaArray( File $file ) { |
632 | $metadata = $this->validateMetadata( $file->getMetadataArray() ); |
633 | if ( !$metadata || isset( $metadata['error'] ) ) { |
634 | return []; |
635 | } |
636 | $stdMetadata = []; |
637 | foreach ( $metadata as $name => $value ) { |
638 | $tag = strtolower( $name ); |
639 | if ( $tag === 'originalwidth' || $tag === 'originalheight' ) { |
640 | // Skip these. In the exif metadata stuff, it is assumed these |
641 | // are measured in px, which is not the case here. |
642 | continue; |
643 | } |
644 | if ( isset( self::$metaConversion[$tag] ) ) { |
645 | $tag = self::$metaConversion[$tag]; |
646 | $stdMetadata[$tag] = $value; |
647 | } |
648 | } |
649 | |
650 | return $stdMetadata; |
651 | } |
652 | } |