Merge "add type check and bail out when title could not be created"
[lhc/web/wiklou.git] / includes / media / Generic.php
1 <?php
2 /**
3 * Media-handling base classes and generic functionality.
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 /**
25 * Base media handler class
26 *
27 * @ingroup Media
28 */
29 abstract class MediaHandler {
30 const TRANSFORM_LATER = 1;
31 const METADATA_GOOD = true;
32 const METADATA_BAD = false;
33 const METADATA_COMPATIBLE = 2; // for old but backwards compatible.
34 /**
35 * Instance cache
36 */
37 static $handlers = array();
38
39 /**
40 * Get a MediaHandler for a given MIME type from the instance cache
41 *
42 * @param $type string
43 *
44 * @return MediaHandler
45 */
46 static function getHandler( $type ) {
47 global $wgMediaHandlers;
48 if ( !isset( $wgMediaHandlers[$type] ) ) {
49 wfDebug( __METHOD__ . ": no handler found for $type.\n");
50 return false;
51 }
52 $class = $wgMediaHandlers[$type];
53 if ( !isset( self::$handlers[$class] ) ) {
54 self::$handlers[$class] = new $class;
55 if ( !self::$handlers[$class]->isEnabled() ) {
56 self::$handlers[$class] = false;
57 }
58 }
59 return self::$handlers[$class];
60 }
61
62 /**
63 * Get an associative array mapping magic word IDs to parameter names.
64 * Will be used by the parser to identify parameters.
65 */
66 abstract function getParamMap();
67
68 /**
69 * Validate a thumbnail parameter at parse time.
70 * Return true to accept the parameter, and false to reject it.
71 * If you return false, the parser will do something quiet and forgiving.
72 *
73 * @param $name
74 * @param $value
75 */
76 abstract function validateParam( $name, $value );
77
78 /**
79 * Merge a parameter array into a string appropriate for inclusion in filenames
80 *
81 * @param $params array
82 */
83 abstract function makeParamString( $params );
84
85 /**
86 * Parse a param string made with makeParamString back into an array
87 *
88 * @param $str string
89 */
90 abstract function parseParamString( $str );
91
92 /**
93 * Changes the parameter array as necessary, ready for transformation.
94 * Should be idempotent.
95 * Returns false if the parameters are unacceptable and the transform should fail
96 * @param $image
97 * @param $params
98 */
99 abstract function normaliseParams( $image, &$params );
100
101 /**
102 * Get an image size array like that returned by getimagesize(), or false if it
103 * can't be determined.
104 *
105 * @param $image File: the image object, or false if there isn't one
106 * @param $path String: the filename
107 * @return Array Follow the format of PHP getimagesize() internal function. See http://www.php.net/getimagesize
108 */
109 abstract function getImageSize( $image, $path );
110
111 /**
112 * Get handler-specific metadata which will be saved in the img_metadata field.
113 *
114 * @param $image File: the image object, or false if there isn't one.
115 * Warning, FSFile::getPropsFromPath might pass an (object)array() instead (!)
116 * @param $path String: the filename
117 * @return String
118 */
119 function getMetadata( $image, $path ) { return ''; }
120
121 /**
122 * Get metadata version.
123 *
124 * This is not used for validating metadata, this is used for the api when returning
125 * metadata, since api content formats should stay the same over time, and so things
126 * using ForiegnApiRepo can keep backwards compatibility
127 *
128 * All core media handlers share a common version number, and extensions can
129 * use the GetMetadataVersion hook to append to the array (they should append a unique
130 * string so not to get confusing). If there was a media handler named 'foo' with metadata
131 * version 3 it might add to the end of the array the element 'foo=3'. if the core metadata
132 * version is 2, the end version string would look like '2;foo=3'.
133 *
134 * @return string version string
135 */
136 static function getMetadataVersion () {
137 $version = Array( '2' ); // core metadata version
138 wfRunHooks('GetMetadataVersion', Array(&$version));
139 return implode( ';', $version);
140 }
141
142 /**
143 * Convert metadata version.
144 *
145 * By default just returns $metadata, but can be used to allow
146 * media handlers to convert between metadata versions.
147 *
148 * @param $metadata Mixed String or Array metadata array (serialized if string)
149 * @param $version Integer target version
150 * @return Array serialized metadata in specified version, or $metadata on fail.
151 */
152 function convertMetadataVersion( $metadata, $version = 1 ) {
153 if ( !is_array( $metadata ) ) {
154
155 //unserialize to keep return parameter consistent.
156 wfSuppressWarnings();
157 $ret = unserialize( $metadata );
158 wfRestoreWarnings();
159 return $ret;
160 }
161 return $metadata;
162 }
163
164 /**
165 * Get a string describing the type of metadata, for display purposes.
166 *
167 * @return string
168 */
169 function getMetadataType( $image ) { return false; }
170
171 /**
172 * Check if the metadata string is valid for this handler.
173 * If it returns MediaHandler::METADATA_BAD (or false), Image
174 * will reload the metadata from the file and update the database.
175 * MediaHandler::METADATA_GOOD for if the metadata is a-ok,
176 * MediaHanlder::METADATA_COMPATIBLE if metadata is old but backwards
177 * compatible (which may or may not trigger a metadata reload).
178 * @return bool
179 */
180 function isMetadataValid( $image, $metadata ) {
181 return self::METADATA_GOOD;
182 }
183
184
185 /**
186 * Get a MediaTransformOutput object representing an alternate of the transformed
187 * output which will call an intermediary thumbnail assist script.
188 *
189 * Used when the repository has a thumbnailScriptUrl option configured.
190 *
191 * Return false to fall back to the regular getTransform().
192 * @return bool
193 */
194 function getScriptedTransform( $image, $script, $params ) {
195 return false;
196 }
197
198 /**
199 * Get a MediaTransformOutput object representing the transformed output. Does not
200 * actually do the transform.
201 *
202 * @param $image File: the image object
203 * @param $dstPath String: filesystem destination path
204 * @param $dstUrl String: Destination URL to use in output HTML
205 * @param $params Array: Arbitrary set of parameters validated by $this->validateParam()
206 * @return MediaTransformOutput
207 */
208 final function getTransform( $image, $dstPath, $dstUrl, $params ) {
209 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
210 }
211
212 /**
213 * Get a MediaTransformOutput object representing the transformed output. Does the
214 * transform unless $flags contains self::TRANSFORM_LATER.
215 *
216 * @param $image File: the image object
217 * @param $dstPath String: filesystem destination path
218 * @param $dstUrl String: destination URL to use in output HTML
219 * @param $params Array: arbitrary set of parameters validated by $this->validateParam()
220 * @param $flags Integer: a bitfield, may contain self::TRANSFORM_LATER
221 *
222 * @return MediaTransformOutput
223 */
224 abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
225
226 /**
227 * Get the thumbnail extension and MIME type for a given source MIME type
228 * @return array thumbnail extension and MIME type
229 */
230 function getThumbType( $ext, $mime, $params = null ) {
231 $magic = MimeMagic::singleton();
232 if ( !$ext || $magic->isMatchingExtension( $ext, $mime ) === false ) {
233 // The extension is not valid for this mime type and we do
234 // recognize the mime type
235 $extensions = $magic->getExtensionsForType( $mime );
236 if ( $extensions ) {
237 return array( strtok( $extensions, ' ' ), $mime );
238 }
239 }
240
241 // The extension is correct (true) or the mime type is unknown to
242 // MediaWiki (null)
243 return array( $ext, $mime );
244 }
245
246 /**
247 * True if the handled types can be transformed
248 * @return bool
249 */
250 function canRender( $file ) { return true; }
251 /**
252 * True if handled types cannot be displayed directly in a browser
253 * but can be rendered
254 * @return bool
255 */
256 function mustRender( $file ) { return false; }
257 /**
258 * True if the type has multi-page capabilities
259 * @return bool
260 */
261 function isMultiPage( $file ) { return false; }
262 /**
263 * Page count for a multi-page document, false if unsupported or unknown
264 * @return bool
265 */
266 function pageCount( $file ) { return false; }
267 /**
268 * The material is vectorized and thus scaling is lossless
269 * @return bool
270 */
271 function isVectorized( $file ) { return false; }
272 /**
273 * False if the handler is disabled for all files
274 * @return bool
275 */
276 function isEnabled() { return true; }
277
278 /**
279 * Get an associative array of page dimensions
280 * Currently "width" and "height" are understood, but this might be
281 * expanded in the future.
282 * Returns false if unknown or if the document is not multi-page.
283 *
284 * @param $image File
285 * @return array
286 */
287 function getPageDimensions( $image, $page ) {
288 $gis = $this->getImageSize( $image, $image->getLocalRefPath() );
289 return array(
290 'width' => $gis[0],
291 'height' => $gis[1]
292 );
293 }
294
295 /**
296 * Generic getter for text layer.
297 * Currently overloaded by PDF and DjVu handlers
298 * @return bool
299 */
300 function getPageText( $image, $page ) {
301 return false;
302 }
303
304 /**
305 * Get an array structure that looks like this:
306 *
307 * array(
308 * 'visible' => array(
309 * 'Human-readable name' => 'Human readable value',
310 * ...
311 * ),
312 * 'collapsed' => array(
313 * 'Human-readable name' => 'Human readable value',
314 * ...
315 * )
316 * )
317 * The UI will format this into a table where the visible fields are always
318 * visible, and the collapsed fields are optionally visible.
319 *
320 * The function should return false if there is no metadata to display.
321 */
322
323 /**
324 * @todo FIXME: I don't really like this interface, it's not very flexible
325 * I think the media handler should generate HTML instead. It can do
326 * all the formatting according to some standard. That makes it possible
327 * to do things like visual indication of grouped and chained streams
328 * in ogg container files.
329 * @return bool
330 */
331 function formatMetadata( $image ) {
332 return false;
333 }
334
335 /** sorts the visible/invisible field.
336 * Split off from ImageHandler::formatMetadata, as used by more than
337 * one type of handler.
338 *
339 * This is used by the media handlers that use the FormatMetadata class
340 *
341 * @param $metadataArray Array metadata array
342 * @return array for use displaying metadata.
343 */
344 function formatMetadataHelper( $metadataArray ) {
345 $result = array(
346 'visible' => array(),
347 'collapsed' => array()
348 );
349
350 $formatted = FormatMetadata::getFormattedData( $metadataArray );
351 // Sort fields into visible and collapsed
352 $visibleFields = $this->visibleMetadataFields();
353 foreach ( $formatted as $name => $value ) {
354 $tag = strtolower( $name );
355 self::addMeta( $result,
356 in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
357 'exif',
358 $tag,
359 $value
360 );
361 }
362 return $result;
363 }
364
365 /**
366 * Get a list of metadata items which should be displayed when
367 * the metadata table is collapsed.
368 *
369 * @return array of strings
370 * @access protected
371 */
372 function visibleMetadataFields() {
373 $fields = array();
374 $lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) );
375 foreach( $lines as $line ) {
376 $matches = array();
377 if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
378 $fields[] = $matches[1];
379 }
380 }
381 $fields = array_map( 'strtolower', $fields );
382 return $fields;
383 }
384
385
386 /**
387 * This is used to generate an array element for each metadata value
388 * That array is then used to generate the table of metadata values
389 * on the image page
390 *
391 * @param &$array Array An array containing elements for each type of visibility
392 * and each of those elements being an array of metadata items. This function adds
393 * a value to that array.
394 * @param $visibility string ('visible' or 'collapsed') if this value is hidden
395 * by default.
396 * @param $type String type of metadata tag (currently always 'exif')
397 * @param $id String the name of the metadata tag (like 'artist' for example).
398 * its name in the table displayed is the message "$type-$id" (Ex exif-artist ).
399 * @param $value String thingy goes into a wikitext table; it used to be escaped but
400 * that was incompatible with previous practise of customized display
401 * with wikitext formatting via messages such as 'exif-model-value'.
402 * So the escaping is taken back out, but generally this seems a confusing
403 * interface.
404 * @param $param String value to pass to the message for the name of the field
405 * as $1. Currently this parameter doesn't seem to ever be used.
406 *
407 * Note, everything here is passed through the parser later on (!)
408 */
409 protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
410 $msg = wfMessage( "$type-$id", $param );
411 if ( $msg->exists() ) {
412 $name = $msg->text();
413 } else {
414 // This is for future compatibility when using instant commons.
415 // So as to not display as ugly a name if a new metadata
416 // property is defined that we don't know about
417 // (not a major issue since such a property would be collapsed
418 // by default).
419 wfDebug( __METHOD__ . ' Unknown metadata name: ' . $id . "\n" );
420 $name = wfEscapeWikiText( $id );
421 }
422 $array[$visibility][] = array(
423 'id' => "$type-$id",
424 'name' => $name,
425 'value' => $value
426 );
427 }
428
429 /**
430 * @param $file File
431 * @return string
432 */
433 function getShortDesc( $file ) {
434 global $wgLang;
435 return htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
436 }
437
438 /**
439 * @param $file File
440 * @return string
441 */
442 function getLongDesc( $file ) {
443 global $wgLang;
444 return wfMessage( 'file-info', htmlspecialchars( $wgLang->formatSize( $file->getSize() ) ),
445 $file->getMimeType() )->parse();
446 }
447
448 /**
449 * @param $file File
450 * @return string
451 */
452 static function getGeneralShortDesc( $file ) {
453 global $wgLang;
454 return $wgLang->formatSize( $file->getSize() );
455 }
456
457 /**
458 * @param $file File
459 * @return string
460 */
461 static function getGeneralLongDesc( $file ) {
462 global $wgLang;
463 return wfMessage( 'file-info', $wgLang->formatSize( $file->getSize() ),
464 $file->getMimeType() )->parse();
465 }
466
467 /**
468 * Calculate the largest thumbnail width for a given original file size
469 * such that the thumbnail's height is at most $maxHeight.
470 * @param $boxWidth Integer Width of the thumbnail box.
471 * @param $boxHeight Integer Height of the thumbnail box.
472 * @param $maxHeight Integer Maximum height expected for the thumbnail.
473 * @return Integer.
474 */
475 public static function fitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
476 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
477 $roundedUp = ceil( $idealWidth );
478 if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight ) {
479 return floor( $idealWidth );
480 } else {
481 return $roundedUp;
482 }
483 }
484
485 function getDimensionsString( $file ) {
486 return '';
487 }
488
489 /**
490 * Modify the parser object post-transform
491 */
492 function parserTransformHook( $parser, $file ) {}
493
494 /**
495 * File validation hook called on upload.
496 *
497 * If the file at the given local path is not valid, or its MIME type does not
498 * match the handler class, a Status object should be returned containing
499 * relevant errors.
500 *
501 * @param $fileName string The local path to the file.
502 * @return Status object
503 */
504 function verifyUpload( $fileName ) {
505 return Status::newGood();
506 }
507
508 /**
509 * Check for zero-sized thumbnails. These can be generated when
510 * no disk space is available or some other error occurs
511 *
512 * @param $dstPath string The location of the suspect file
513 * @param $retval int Return value of some shell process, file will be deleted if this is non-zero
514 * @return bool True if removed, false otherwise
515 */
516 function removeBadFile( $dstPath, $retval = 0 ) {
517 if( file_exists( $dstPath ) ) {
518 $thumbstat = stat( $dstPath );
519 if( $thumbstat['size'] == 0 || $retval != 0 ) {
520 $result = unlink( $dstPath );
521
522 if ( $result ) {
523 wfDebugLog( 'thumbnail',
524 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() succeeded',
525 $thumbstat['size'], $dstPath ) );
526 } else {
527 wfDebugLog( 'thumbnail',
528 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() failed',
529 $thumbstat['size'], $dstPath ) );
530 }
531 return true;
532 }
533 }
534 return false;
535 }
536
537 /**
538 * Remove files from the purge list
539 *
540 * @param array $files
541 * @param array $options
542 */
543 public function filterThumbnailPurgeList( &$files, $options ) {
544 // Do nothing
545 }
546 }
547
548 /**
549 * Media handler abstract base class for images
550 *
551 * @ingroup Media
552 */
553 abstract class ImageHandler extends MediaHandler {
554
555 /**
556 * @param $file File
557 * @return bool
558 */
559 function canRender( $file ) {
560 return ( $file->getWidth() && $file->getHeight() );
561 }
562
563 function getParamMap() {
564 return array( 'img_width' => 'width' );
565 }
566
567 function validateParam( $name, $value ) {
568 if ( in_array( $name, array( 'width', 'height' ) ) ) {
569 if ( $value <= 0 ) {
570 return false;
571 } else {
572 return true;
573 }
574 } else {
575 return false;
576 }
577 }
578
579 function makeParamString( $params ) {
580 if ( isset( $params['physicalWidth'] ) ) {
581 $width = $params['physicalWidth'];
582 } elseif ( isset( $params['width'] ) ) {
583 $width = $params['width'];
584 } else {
585 throw new MWException( 'No width specified to '.__METHOD__ );
586 }
587 # Removed for ProofreadPage
588 #$width = intval( $width );
589 return "{$width}px";
590 }
591
592 function parseParamString( $str ) {
593 $m = false;
594 if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
595 return array( 'width' => $m[1] );
596 } else {
597 return false;
598 }
599 }
600
601 function getScriptParams( $params ) {
602 return array( 'width' => $params['width'] );
603 }
604
605 /**
606 * @param $image File
607 * @param $params
608 * @return bool
609 */
610 function normaliseParams( $image, &$params ) {
611 $mimeType = $image->getMimeType();
612
613 if ( !isset( $params['width'] ) ) {
614 return false;
615 }
616
617 if ( !isset( $params['page'] ) ) {
618 $params['page'] = 1;
619 } else {
620 if ( $params['page'] > $image->pageCount() ) {
621 $params['page'] = $image->pageCount();
622 }
623
624 if ( $params['page'] < 1 ) {
625 $params['page'] = 1;
626 }
627 }
628
629 $srcWidth = $image->getWidth( $params['page'] );
630 $srcHeight = $image->getHeight( $params['page'] );
631
632 if ( isset( $params['height'] ) && $params['height'] != -1 ) {
633 # Height & width were both set
634 if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
635 # Height is the relative smaller dimension, so scale width accordingly
636 $params['width'] = self::fitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
637
638 if ( $params['width'] == 0 ) {
639 # Very small image, so we need to rely on client side scaling :(
640 $params['width'] = 1;
641 }
642
643 $params['physicalWidth'] = $params['width'];
644 } else {
645 # Height was crap, unset it so that it will be calculated later
646 unset( $params['height'] );
647 }
648 }
649
650 if ( !isset( $params['physicalWidth'] ) ) {
651 # Passed all validations, so set the physicalWidth
652 $params['physicalWidth'] = $params['width'];
653 }
654
655 # Because thumbs are only referred to by width, the height always needs
656 # to be scaled by the width to keep the thumbnail sizes consistent,
657 # even if it was set inside the if block above
658 $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight,
659 $params['physicalWidth'] );
660
661 # Set the height if it was not validated in the if block higher up
662 if ( !isset( $params['height'] ) || $params['height'] == -1 ) {
663 $params['height'] = $params['physicalHeight'];
664 }
665
666
667 if ( !$this->validateThumbParams( $params['physicalWidth'],
668 $params['physicalHeight'], $srcWidth, $srcHeight, $mimeType ) ) {
669 return false;
670 }
671 return true;
672 }
673
674 /**
675 * Validate thumbnail parameters and fill in the correct height
676 *
677 * @param $width Integer: specified width (input/output)
678 * @param $height Integer: height (output only)
679 * @param $srcWidth Integer: width of the source image
680 * @param $srcHeight Integer: height of the source image
681 * @param $mimeType
682 * @return bool False to indicate that an error should be returned to the user.
683 */
684 function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
685 $width = intval( $width );
686
687 # Sanity check $width
688 if( $width <= 0) {
689 wfDebug( __METHOD__.": Invalid destination width: $width\n" );
690 return false;
691 }
692 if ( $srcWidth <= 0 ) {
693 wfDebug( __METHOD__.": Invalid source width: $srcWidth\n" );
694 return false;
695 }
696
697 $height = File::scaleHeight( $srcWidth, $srcHeight, $width );
698 if ( $height == 0 ) {
699 # Force height to be at least 1 pixel
700 $height = 1;
701 }
702 return true;
703 }
704
705 /**
706 * @param $image File
707 * @param $script
708 * @param $params
709 * @return bool|ThumbnailImage
710 */
711 function getScriptedTransform( $image, $script, $params ) {
712 if ( !$this->normaliseParams( $image, $params ) ) {
713 return false;
714 }
715 $url = $script . '&' . wfArrayToCGI( $this->getScriptParams( $params ) );
716 $page = isset( $params['page'] ) ? $params['page'] : false;
717
718 if( $image->mustRender() || $params['width'] < $image->getWidth() ) {
719 return new ThumbnailImage( $image, $url, $params['width'], $params['height'], $page );
720 }
721 }
722
723 function getImageSize( $image, $path ) {
724 wfSuppressWarnings();
725 $gis = getimagesize( $path );
726 wfRestoreWarnings();
727 return $gis;
728 }
729
730 function isAnimatedImage( $image ) {
731 return false;
732 }
733
734 /**
735 * @param $file File
736 * @return string
737 */
738 function getShortDesc( $file ) {
739 global $wgLang;
740 $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
741 $widthheight = wfMessage( 'widthheight' )->numParams( $file->getWidth(), $file->getHeight() )->escaped();
742
743 return "$widthheight ($nbytes)";
744 }
745
746 /**
747 * @param $file File
748 * @return string
749 */
750 function getLongDesc( $file ) {
751 global $wgLang;
752 $pages = $file->pageCount();
753 $size = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
754 if ( $pages === false || $pages <= 1 ) {
755 $msg = wfMessage( 'file-info-size' )->numParams( $file->getWidth(),
756 $file->getHeight() )->params( $size,
757 $file->getMimeType() )->parse();
758 } else {
759 $msg = wfMessage( 'file-info-size-pages' )->numParams( $file->getWidth(),
760 $file->getHeight() )->params( $size,
761 $file->getMimeType() )->numParams( $pages )->parse();
762 }
763 return $msg;
764 }
765
766 /**
767 * @param $file File
768 * @return string
769 */
770 function getDimensionsString( $file ) {
771 $pages = $file->pageCount();
772 if ( $pages > 1 ) {
773 return wfMessage( 'widthheightpage' )->numParams( $file->getWidth(), $file->getHeight(), $pages )->text();
774 } else {
775 return wfMessage( 'widthheight' )->numParams( $file->getWidth(), $file->getHeight() )->text();
776 }
777 }
778 }