Merge "Revert "Log the reason why revision->getContent() returns null""
[lhc/web/wiklou.git] / includes / media / SVG.php
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 use Wikimedia\ScopedCallback;
24
25 /**
26 * Handler for SVG images.
27 *
28 * @ingroup Media
29 */
30 class SvgHandler extends ImageHandler {
31 const SVG_METADATA_VERSION = 2;
32
33 /** @var array A list of metadata tags that can be converted
34 * to the commonly used exif tags. This allows messages
35 * to be reused, and consistent tag names for {{#formatmetadata:..}}
36 */
37 private static $metaConversion = [
38 'originalwidth' => 'ImageWidth',
39 'originalheight' => 'ImageLength',
40 'description' => 'ImageDescription',
41 'title' => 'ObjectName',
42 ];
43
44 function isEnabled() {
45 global $wgSVGConverters, $wgSVGConverter;
46 if ( !isset( $wgSVGConverters[$wgSVGConverter] ) ) {
47 wfDebug( "\$wgSVGConverter is invalid, disabling SVG rendering.\n" );
48
49 return false;
50 } else {
51 return true;
52 }
53 }
54
55 public function mustRender( $file ) {
56 return true;
57 }
58
59 function isVectorized( $file ) {
60 return true;
61 }
62
63 /**
64 * @param File $file
65 * @return bool
66 */
67 function isAnimatedImage( $file ) {
68 # @todo Detect animated SVGs
69 $metadata = $file->getMetadata();
70 if ( $metadata ) {
71 $metadata = $this->unpackMetadata( $metadata );
72 if ( isset( $metadata['animated'] ) ) {
73 return $metadata['animated'];
74 }
75 }
76
77 return false;
78 }
79
80 /**
81 * Which languages (systemLanguage attribute) is supported.
82 *
83 * @note This list is not guaranteed to be exhaustive.
84 * To avoid OOM errors, we only look at first bit of a file.
85 * Thus all languages on this list are present in the file,
86 * but its possible for the file to have a language not on
87 * this list.
88 *
89 * @param File $file
90 * @return array Array of language codes, or empty if no language switching supported.
91 */
92 public function getAvailableLanguages( File $file ) {
93 $metadata = $file->getMetadata();
94 $langList = [];
95 if ( $metadata ) {
96 $metadata = $this->unpackMetadata( $metadata );
97 if ( isset( $metadata['translations'] ) ) {
98 foreach ( $metadata['translations'] as $lang => $langType ) {
99 if ( $langType === SVGReader::LANG_FULL_MATCH ) {
100 $langList[] = strtolower( $lang );
101 }
102 }
103 }
104 }
105 return array_unique( $langList );
106 }
107
108 /**
109 * SVG's systemLanguage matching rules state:
110 * 'The `systemLanguage` attribute ... [e]valuates to "true" if one of the languages indicated
111 * by user preferences exactly equals one of the languages given in the value of this parameter,
112 * or if one of the languages indicated by user preferences exactly equals a prefix of one of
113 * the languages given in the value of this parameter such that the first tag character
114 * following the prefix is "-".'
115 *
116 * Return the first element of $svgLanguages that matches $userPreferredLanguage
117 *
118 * @see https://www.w3.org/TR/SVG/struct.html#SystemLanguageAttribute
119 * @param string $userPreferredLanguage
120 * @param array $svgLanguages
121 * @return string|null
122 */
123 public function getMatchedLanguage( $userPreferredLanguage, array $svgLanguages ) {
124 foreach ( $svgLanguages as $svgLang ) {
125 if ( strcasecmp( $svgLang, $userPreferredLanguage ) === 0 ) {
126 return $svgLang;
127 }
128 $trimmedSvgLang = $svgLang;
129 while ( strpos( $trimmedSvgLang, '-' ) !== false ) {
130 $trimmedSvgLang = substr( $trimmedSvgLang, 0, strrpos( $trimmedSvgLang, '-' ) );
131 if ( strcasecmp( $trimmedSvgLang, $userPreferredLanguage ) === 0 ) {
132 return $svgLang;
133 }
134 }
135 }
136 return null;
137 }
138
139 /**
140 * What language to render file in if none selected
141 *
142 * @param File $file Language code
143 * @return string
144 */
145 public function getDefaultRenderLanguage( File $file ) {
146 return 'en';
147 }
148
149 /**
150 * We do not support making animated svg thumbnails
151 * @param File $file
152 * @return bool
153 */
154 function canAnimateThumbnail( $file ) {
155 return false;
156 }
157
158 /**
159 * @param File $image
160 * @param array &$params
161 * @return bool
162 */
163 function normaliseParams( $image, &$params ) {
164 global $wgSVGMaxSize;
165 if ( !parent::normaliseParams( $image, $params ) ) {
166 return false;
167 }
168 # Don't make an image bigger than wgMaxSVGSize on the smaller side
169 if ( $params['physicalWidth'] <= $params['physicalHeight'] ) {
170 if ( $params['physicalWidth'] > $wgSVGMaxSize ) {
171 $srcWidth = $image->getWidth( $params['page'] );
172 $srcHeight = $image->getHeight( $params['page'] );
173 $params['physicalWidth'] = $wgSVGMaxSize;
174 $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
175 }
176 } else {
177 if ( $params['physicalHeight'] > $wgSVGMaxSize ) {
178 $srcWidth = $image->getWidth( $params['page'] );
179 $srcHeight = $image->getHeight( $params['page'] );
180 $params['physicalWidth'] = File::scaleHeight( $srcHeight, $srcWidth, $wgSVGMaxSize );
181 $params['physicalHeight'] = $wgSVGMaxSize;
182 }
183 }
184
185 return true;
186 }
187
188 /**
189 * @param File $image
190 * @param string $dstPath
191 * @param string $dstUrl
192 * @param array $params
193 * @param int $flags
194 * @return bool|MediaTransformError|ThumbnailImage|TransformParameterError
195 */
196 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
197 if ( !$this->normaliseParams( $image, $params ) ) {
198 return new TransformParameterError( $params );
199 }
200 $clientWidth = $params['width'];
201 $clientHeight = $params['height'];
202 $physicalWidth = $params['physicalWidth'];
203 $physicalHeight = $params['physicalHeight'];
204 $lang = isset( $params['lang'] ) ? $params['lang'] : $this->getDefaultRenderLanguage( $image );
205
206 if ( $flags & self::TRANSFORM_LATER ) {
207 return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
208 }
209
210 $metadata = $this->unpackMetadata( $image->getMetadata() );
211 if ( isset( $metadata['error'] ) ) { // sanity check
212 $err = wfMessage( 'svg-long-error', $metadata['error']['message'] );
213
214 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
215 }
216
217 if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) {
218 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
219 wfMessage( 'thumbnail_dest_directory' ) );
220 }
221
222 $srcPath = $image->getLocalRefPath();
223 if ( $srcPath === false ) { // Failed to get local copy
224 wfDebugLog( 'thumbnail',
225 sprintf( 'Thumbnail failed on %s: could not get local copy of "%s"',
226 wfHostname(), $image->getName() ) );
227
228 return new MediaTransformError( 'thumbnail_error',
229 $params['width'], $params['height'],
230 wfMessage( 'filemissing' )
231 );
232 }
233
234 // Make a temp dir with a symlink to the local copy in it.
235 // This plays well with rsvg-convert policy for external entities.
236 // https://git.gnome.org/browse/librsvg/commit/?id=f01aded72c38f0e18bc7ff67dee800e380251c8e
237 $tmpDir = wfTempDir() . '/svg_' . wfRandomString( 24 );
238 $lnPath = "$tmpDir/" . basename( $srcPath );
239 $ok = mkdir( $tmpDir, 0771 );
240 if ( !$ok ) {
241 wfDebugLog( 'thumbnail',
242 sprintf( 'Thumbnail failed on %s: could not create temporary directory %s',
243 wfHostname(), $tmpDir ) );
244 return new MediaTransformError( 'thumbnail_error',
245 $params['width'], $params['height'],
246 wfMessage( 'thumbnail-temp-create' )->text()
247 );
248 }
249 $ok = symlink( $srcPath, $lnPath );
250 /** @noinspection PhpUnusedLocalVariableInspection */
251 $cleaner = new ScopedCallback( function () use ( $tmpDir, $lnPath ) {
252 Wikimedia\suppressWarnings();
253 unlink( $lnPath );
254 rmdir( $tmpDir );
255 Wikimedia\restoreWarnings();
256 } );
257 if ( !$ok ) {
258 wfDebugLog( 'thumbnail',
259 sprintf( 'Thumbnail failed on %s: could not link %s to %s',
260 wfHostname(), $lnPath, $srcPath ) );
261 return new MediaTransformError( 'thumbnail_error',
262 $params['width'], $params['height'],
263 wfMessage( 'thumbnail-temp-create' )
264 );
265 }
266
267 $status = $this->rasterize( $lnPath, $dstPath, $physicalWidth, $physicalHeight, $lang );
268 if ( $status === true ) {
269 return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
270 } else {
271 return $status; // MediaTransformError
272 }
273 }
274
275 /**
276 * Transform an SVG file to PNG
277 * This function can be called outside of thumbnail contexts
278 * @param string $srcPath
279 * @param string $dstPath
280 * @param string $width
281 * @param string $height
282 * @param bool|string $lang Language code of the language to render the SVG in
283 * @throws MWException
284 * @return bool|MediaTransformError
285 */
286 public function rasterize( $srcPath, $dstPath, $width, $height, $lang = false ) {
287 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
288 $err = false;
289 $retval = '';
290 if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
291 if ( is_array( $wgSVGConverters[$wgSVGConverter] ) ) {
292 // This is a PHP callable
293 $func = $wgSVGConverters[$wgSVGConverter][0];
294 $args = array_merge( [ $srcPath, $dstPath, $width, $height, $lang ],
295 array_slice( $wgSVGConverters[$wgSVGConverter], 1 ) );
296 if ( !is_callable( $func ) ) {
297 throw new MWException( "$func is not callable" );
298 }
299 $err = call_user_func_array( $func, $args );
300 $retval = (bool)$err;
301 } else {
302 // External command
303 $cmd = str_replace(
304 [ '$path/', '$width', '$height', '$input', '$output' ],
305 [ $wgSVGConverterPath ? wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
306 intval( $width ),
307 intval( $height ),
308 wfEscapeShellArg( $srcPath ),
309 wfEscapeShellArg( $dstPath ) ],
310 $wgSVGConverters[$wgSVGConverter]
311 );
312
313 $env = [];
314 if ( $lang !== false ) {
315 $env['LANG'] = $lang;
316 }
317
318 wfDebug( __METHOD__ . ": $cmd\n" );
319 $err = wfShellExecWithStderr( $cmd, $retval, $env );
320 }
321 }
322 $removed = $this->removeBadFile( $dstPath, $retval );
323 if ( $retval != 0 || $removed ) {
324 $this->logErrorForExternalProcess( $retval, $err, $cmd );
325 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
326 }
327
328 return true;
329 }
330
331 public static function rasterizeImagickExt( $srcPath, $dstPath, $width, $height ) {
332 $im = new Imagick( $srcPath );
333 $im->setImageFormat( 'png' );
334 $im->setBackgroundColor( 'transparent' );
335 $im->setImageDepth( 8 );
336
337 if ( !$im->thumbnailImage( intval( $width ), intval( $height ), /* fit */ false ) ) {
338 return 'Could not resize image';
339 }
340 if ( !$im->writeImage( $dstPath ) ) {
341 return "Could not write to $dstPath";
342 }
343 }
344
345 /**
346 * @param File|FSFile $file
347 * @param string $path Unused
348 * @param bool|array $metadata
349 * @return array
350 */
351 function getImageSize( $file, $path, $metadata = false ) {
352 if ( $metadata === false && $file instanceof File ) {
353 $metadata = $file->getMetadata();
354 }
355 $metadata = $this->unpackMetadata( $metadata );
356
357 if ( isset( $metadata['width'] ) && isset( $metadata['height'] ) ) {
358 return [ $metadata['width'], $metadata['height'], 'SVG',
359 "width=\"{$metadata['width']}\" height=\"{$metadata['height']}\"" ];
360 } else { // error
361 return [ 0, 0, 'SVG', "width=\"0\" height=\"0\"" ];
362 }
363 }
364
365 function getThumbType( $ext, $mime, $params = null ) {
366 return [ 'png', 'image/png' ];
367 }
368
369 /**
370 * Subtitle for the image. Different from the base
371 * class so it can be denoted that SVG's have
372 * a "nominal" resolution, and not a fixed one,
373 * as well as so animation can be denoted.
374 *
375 * @param File $file
376 * @return string
377 */
378 function getLongDesc( $file ) {
379 global $wgLang;
380
381 $metadata = $this->unpackMetadata( $file->getMetadata() );
382 if ( isset( $metadata['error'] ) ) {
383 return wfMessage( 'svg-long-error', $metadata['error']['message'] )->text();
384 }
385
386 $size = $wgLang->formatSize( $file->getSize() );
387
388 if ( $this->isAnimatedImage( $file ) ) {
389 $msg = wfMessage( 'svg-long-desc-animated' );
390 } else {
391 $msg = wfMessage( 'svg-long-desc' );
392 }
393
394 $msg->numParams( $file->getWidth(), $file->getHeight() )->params( $size );
395
396 return $msg->parse();
397 }
398
399 /**
400 * @param File|FSFile $file
401 * @param string $filename
402 * @return string Serialised metadata
403 */
404 function getMetadata( $file, $filename ) {
405 $metadata = [ 'version' => self::SVG_METADATA_VERSION ];
406 try {
407 $metadata += SVGMetadataExtractor::getMetadata( $filename );
408 } catch ( Exception $e ) { // @todo SVG specific exceptions
409 // File not found, broken, etc.
410 $metadata['error'] = [
411 'message' => $e->getMessage(),
412 'code' => $e->getCode()
413 ];
414 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
415 }
416
417 return serialize( $metadata );
418 }
419
420 function unpackMetadata( $metadata ) {
421 Wikimedia\suppressWarnings();
422 $unser = unserialize( $metadata );
423 Wikimedia\restoreWarnings();
424 if ( isset( $unser['version'] ) && $unser['version'] == self::SVG_METADATA_VERSION ) {
425 return $unser;
426 } else {
427 return false;
428 }
429 }
430
431 function getMetadataType( $image ) {
432 return 'parsed-svg';
433 }
434
435 function isMetadataValid( $image, $metadata ) {
436 $meta = $this->unpackMetadata( $metadata );
437 if ( $meta === false ) {
438 return self::METADATA_BAD;
439 }
440 if ( !isset( $meta['originalWidth'] ) ) {
441 // Old but compatible
442 return self::METADATA_COMPATIBLE;
443 }
444
445 return self::METADATA_GOOD;
446 }
447
448 protected function visibleMetadataFields() {
449 $fields = [ 'objectname', 'imagedescription' ];
450
451 return $fields;
452 }
453
454 /**
455 * @param File $file
456 * @param bool|IContextSource $context Context to use (optional)
457 * @return array|bool
458 */
459 function formatMetadata( $file, $context = false ) {
460 $result = [
461 'visible' => [],
462 'collapsed' => []
463 ];
464 $metadata = $file->getMetadata();
465 if ( !$metadata ) {
466 return false;
467 }
468 $metadata = $this->unpackMetadata( $metadata );
469 if ( !$metadata || isset( $metadata['error'] ) ) {
470 return false;
471 }
472
473 /* @todo Add a formatter
474 $format = new FormatSVG( $metadata );
475 $formatted = $format->getFormattedData();
476 */
477
478 // Sort fields into visible and collapsed
479 $visibleFields = $this->visibleMetadataFields();
480
481 $showMeta = false;
482 foreach ( $metadata as $name => $value ) {
483 $tag = strtolower( $name );
484 if ( isset( self::$metaConversion[$tag] ) ) {
485 $tag = strtolower( self::$metaConversion[$tag] );
486 } else {
487 // Do not output other metadata not in list
488 continue;
489 }
490 $showMeta = true;
491 self::addMeta( $result,
492 in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
493 'exif',
494 $tag,
495 $value
496 );
497 }
498
499 return $showMeta ? $result : false;
500 }
501
502 /**
503 * @param string $name Parameter name
504 * @param mixed $value Parameter value
505 * @return bool Validity
506 */
507 public function validateParam( $name, $value ) {
508 if ( in_array( $name, [ 'width', 'height' ] ) ) {
509 // Reject negative heights, widths
510 return ( $value > 0 );
511 } elseif ( $name == 'lang' ) {
512 // Validate $code
513 if ( $value === '' || !Language::isValidCode( $value ) ) {
514 return false;
515 }
516
517 return true;
518 }
519
520 // Only lang, width and height are acceptable keys
521 return false;
522 }
523
524 /**
525 * @param array $params Name=>value pairs of parameters
526 * @return string Filename to use
527 */
528 public function makeParamString( $params ) {
529 $lang = '';
530 if ( isset( $params['lang'] ) && $params['lang'] !== 'en' ) {
531 $lang = 'lang' . strtolower( $params['lang'] ) . '-';
532 }
533 if ( !isset( $params['width'] ) ) {
534 return false;
535 }
536
537 return "$lang{$params['width']}px";
538 }
539
540 public function parseParamString( $str ) {
541 $m = false;
542 if ( preg_match( '/^lang([a-z]+(?:-[a-z]+)*)-(\d+)px$/i', $str, $m ) ) {
543 return [ 'width' => array_pop( $m ), 'lang' => $m[1] ];
544 } elseif ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
545 return [ 'width' => $m[1], 'lang' => 'en' ];
546 } else {
547 return false;
548 }
549 }
550
551 public function getParamMap() {
552 return [ 'img_lang' => 'lang', 'img_width' => 'width' ];
553 }
554
555 /**
556 * @param array $params
557 * @return array
558 */
559 function getScriptParams( $params ) {
560 $scriptParams = [ 'width' => $params['width'] ];
561 if ( isset( $params['lang'] ) ) {
562 $scriptParams['lang'] = $params['lang'];
563 }
564
565 return $scriptParams;
566 }
567
568 public function getCommonMetaArray( File $file ) {
569 $metadata = $file->getMetadata();
570 if ( !$metadata ) {
571 return [];
572 }
573 $metadata = $this->unpackMetadata( $metadata );
574 if ( !$metadata || isset( $metadata['error'] ) ) {
575 return [];
576 }
577 $stdMetadata = [];
578 foreach ( $metadata as $name => $value ) {
579 $tag = strtolower( $name );
580 if ( $tag === 'originalwidth' || $tag === 'originalheight' ) {
581 // Skip these. In the exif metadata stuff, it is assumed these
582 // are measured in px, which is not the case here.
583 continue;
584 }
585 if ( isset( self::$metaConversion[$tag] ) ) {
586 $tag = self::$metaConversion[$tag];
587 $stdMetadata[$tag] = $value;
588 }
589 }
590
591 return $stdMetadata;
592 }
593 }