Index: trunk/phase3/includes/MimeMagic.php |
— | — | @@ -117,11 +117,11 @@ |
118 | 118 | END_STRING |
119 | 119 | ); |
120 | 120 | |
121 | | -#note: because this file is possibly included by a function, |
122 | | -#we need to access the global scope explicitely! |
| 121 | +// Note: because this file is possibly included by a function, |
| 122 | +// we need to access the global scope explicitely! |
123 | 123 | global $wgLoadFileinfoExtension; |
124 | 124 | |
125 | | -if ($wgLoadFileinfoExtension) { |
| 125 | +if ( $wgLoadFileinfoExtension ) { |
126 | 126 | wfDl( 'fileinfo' ); |
127 | 127 | } |
128 | 128 | |
— | — | @@ -138,19 +138,19 @@ |
139 | 139 | * Mapping of media types to arrays of mime types. |
140 | 140 | * This is used by findMediaType and getMediaType, respectively |
141 | 141 | */ |
142 | | - var $mMediaTypes= null; |
| 142 | + var $mMediaTypes = null; |
143 | 143 | |
144 | 144 | /** Map of mime type aliases |
145 | 145 | */ |
146 | | - var $mMimeTypeAliases= null; |
| 146 | + var $mMimeTypeAliases = null; |
147 | 147 | |
148 | 148 | /** map of mime types to file extensions (as a space seprarated list) |
149 | 149 | */ |
150 | | - var $mMimeToExt= null; |
| 150 | + var $mMimeToExt = null; |
151 | 151 | |
152 | 152 | /** map of file extensions types to mime types (as a space seprarated list) |
153 | 153 | */ |
154 | | - var $mExtToMime= null; |
| 154 | + var $mExtToMime = null; |
155 | 155 | |
156 | 156 | /** IEContentAnalyzer instance |
157 | 157 | */ |
— | — | @@ -311,46 +311,68 @@ |
312 | 312 | * Get an instance of this class |
313 | 313 | * @return MimeMagic |
314 | 314 | */ |
315 | | - static function &singleton() { |
| 315 | + public static function &singleton() { |
316 | 316 | if ( !isset( self::$instance ) ) { |
317 | 317 | self::$instance = new MimeMagic; |
318 | 318 | } |
319 | 319 | return self::$instance; |
320 | 320 | } |
321 | 321 | |
322 | | - /** returns a list of file extensions for a given mime type |
323 | | - * as a space separated string. |
324 | | - */ |
325 | | - function getExtensionsForType( $mime ) { |
| 322 | + /** |
| 323 | + * Returns a list of file extensions for a given mime type as a space |
| 324 | + * separated string or null if the mime type was unrecognized. Resolves |
| 325 | + * mime type aliases. |
| 326 | + * |
| 327 | + * @param $mime string |
| 328 | + * @return string|null |
| 329 | + */ |
| 330 | + public function getExtensionsForType( $mime ) { |
326 | 331 | $mime = strtolower( $mime ); |
327 | 332 | |
328 | | - $r = @$this->mMimeToExt[$mime]; |
| 333 | + // Check the mime-to-ext map |
| 334 | + if ( isset( $this->mMimeToExt[$mime] ) ) { |
| 335 | + return $this->mMimeToExt[$mime]; |
| 336 | + } |
329 | 337 | |
330 | | - if ( @!$r && isset( $this->mMimeTypeAliases[$mime] ) ) { |
| 338 | + // Resolve the mime type to the canonical type |
| 339 | + if ( isset( $this->mMimeTypeAliases[$mime] ) ) { |
331 | 340 | $mime = $this->mMimeTypeAliases[$mime]; |
332 | | - $r = @$this->mMimeToExt[$mime]; |
| 341 | + if ( isset( $this->mMimeToExt[$mime] ) ) { |
| 342 | + return $this->mMimeToExt[$mime]; |
| 343 | + } |
333 | 344 | } |
334 | 345 | |
335 | | - return $r; |
| 346 | + return null; |
336 | 347 | } |
337 | 348 | |
338 | | - /** returns a list of mime types for a given file extension |
339 | | - * as a space separated string. |
340 | | - */ |
341 | | - function getTypesForExtension( $ext ) { |
| 349 | + /** |
| 350 | + * Returns a list of mime types for a given file extension as a space |
| 351 | + * separated string or null if the extension was unrecognized. |
| 352 | + * |
| 353 | + * @param $ext string |
| 354 | + * @return string|null |
| 355 | + */ |
| 356 | + public function getTypesForExtension( $ext ) { |
342 | 357 | $ext = strtolower( $ext ); |
343 | 358 | |
344 | 359 | $r = isset( $this->mExtToMime[$ext] ) ? $this->mExtToMime[$ext] : null; |
345 | 360 | return $r; |
346 | 361 | } |
347 | 362 | |
348 | | - /** returns a single mime type for a given file extension. |
349 | | - * This is always the first type from the list returned by getTypesForExtension($ext). |
350 | | - */ |
351 | | - function guessTypesForExtension( $ext ) { |
| 363 | + /** |
| 364 | + * Returns a single mime type for a given file extension or null if unknown. |
| 365 | + * This is always the first type from the list returned by getTypesForExtension($ext). |
| 366 | + * |
| 367 | + * @param $ext string |
| 368 | + * @return string|null |
| 369 | + */ |
| 370 | + public function guessTypesForExtension( $ext ) { |
352 | 371 | $m = $this->getTypesForExtension( $ext ); |
353 | | - if ( is_null( $m ) ) return null; |
| 372 | + if ( is_null( $m ) ) { |
| 373 | + return null; |
| 374 | + } |
354 | 375 | |
| 376 | + // TODO: Check if this is needed; strtok( $m, ' ' ) should be sufficient |
355 | 377 | $m = trim( $m ); |
356 | 378 | $m = preg_replace( '/\s.*$/', '', $m ); |
357 | 379 | |
— | — | @@ -358,32 +380,34 @@ |
359 | 381 | } |
360 | 382 | |
361 | 383 | |
362 | | - /** tests if the extension matches the given mime type. |
363 | | - * returns true if a match was found, NULL if the mime type is unknown, |
364 | | - * and false if the mime type is known but no matches where found. |
365 | | - */ |
366 | | - function isMatchingExtension( $extension, $mime ) { |
| 384 | + /** |
| 385 | + * Tests if the extension matches the given mime type. Returns true if a |
| 386 | + * match was found, null if the mime type is unknown, and false if the |
| 387 | + * mime type is known but no matches where found. |
| 388 | + * |
| 389 | + * @param $extension string |
| 390 | + * @param $mime string |
| 391 | + * @return bool|null |
| 392 | + */ |
| 393 | + public function isMatchingExtension( $extension, $mime ) { |
367 | 394 | $ext = $this->getExtensionsForType( $mime ); |
368 | 395 | |
369 | 396 | if ( !$ext ) { |
370 | | - return null; //unknown |
| 397 | + return null; // Unknown mime type |
371 | 398 | } |
372 | 399 | |
373 | 400 | $ext = explode( ' ', $ext ); |
374 | 401 | |
375 | 402 | $extension = strtolower( $extension ); |
376 | | - if ( in_array( $extension, $ext ) ) { |
377 | | - return true; |
378 | | - } |
379 | | - |
380 | | - return false; |
| 403 | + return in_array( $extension, $ext ); |
381 | 404 | } |
382 | 405 | |
383 | | - /** returns true if the mime type is known to represent |
384 | | - * an image format supported by the PHP GD library. |
385 | | - */ |
386 | | - function isPHPImageType( $mime ) { |
387 | | - #as defined by imagegetsize and image_type_to_mime |
| 406 | + /** |
| 407 | + * Returns true if the mime type is known to represent an image format |
| 408 | + * supported by the PHP GD library. |
| 409 | + */ |
| 410 | + public function isPHPImageType( $mime ) { |
| 411 | + // As defined by imagegetsize and image_type_to_mime |
388 | 412 | static $types = array( |
389 | 413 | 'image/gif', 'image/jpeg', 'image/png', |
390 | 414 | 'image/x-bmp', 'image/xbm', 'image/tiff', |
— | — | @@ -427,44 +451,45 @@ |
428 | 452 | return in_array( strtolower( $extension ), $types ); |
429 | 453 | } |
430 | 454 | |
431 | | - /** improves a mime type using the file extension. Some file formats are very generic, |
432 | | - * so their mime type is not very meaningful. A more useful mime type can be derived |
433 | | - * by looking at the file extension. Typically, this method would be called on the |
434 | | - * result of guessMimeType(). |
435 | | - * |
436 | | - * Currently, this method does the following: |
437 | | - * |
438 | | - * If $mime is "unknown/unknown" and isRecognizableExtension( $ext ) returns false, |
439 | | - * return the result of guessTypesForExtension($ext). |
440 | | - * |
441 | | - * If $mime is "application/x-opc+zip" and isMatchingExtension( $ext, $mime ) |
442 | | - * gives true, return the result of guessTypesForExtension($ext). |
443 | | - * |
444 | | - * @param $mime String: the mime type, typically guessed from a file's content. |
445 | | - * @param $ext String: the file extension, as taken from the file name |
446 | | - * |
447 | | - * @return string the mime type |
448 | | - */ |
449 | | - function improveTypeFromExtension( $mime, $ext ) { |
450 | | - if ( $mime === "unknown/unknown" ) { |
451 | | - if( $this->isRecognizableExtension( $ext ) ) { |
452 | | - wfDebug( __METHOD__. ": refusing to guess mime type for .$ext file, " . |
453 | | - "we should have recognized it\n" ); |
| 455 | + /** |
| 456 | + * Improves a mime type using the file extension. Some file formats are very generic, |
| 457 | + * so their mime type is not very meaningful. A more useful mime type can be derived |
| 458 | + * by looking at the file extension. Typically, this method would be called on the |
| 459 | + * result of guessMimeType(). |
| 460 | + * |
| 461 | + * Currently, this method does the following: |
| 462 | + * |
| 463 | + * If $mime is "unknown/unknown" and isRecognizableExtension( $ext ) returns false, |
| 464 | + * return the result of guessTypesForExtension($ext). |
| 465 | + * |
| 466 | + * If $mime is "application/x-opc+zip" and isMatchingExtension( $ext, $mime ) |
| 467 | + * gives true, return the result of guessTypesForExtension($ext). |
| 468 | + * |
| 469 | + * @param $mime String: the mime type, typically guessed from a file's content. |
| 470 | + * @param $ext String: the file extension, as taken from the file name |
| 471 | + * |
| 472 | + * @return string the mime type |
| 473 | + */ |
| 474 | + public function improveTypeFromExtension( $mime, $ext ) { |
| 475 | + if ( $mime === 'unknown/unknown' ) { |
| 476 | + if ( $this->isRecognizableExtension( $ext ) ) { |
| 477 | + wfDebug( __METHOD__. ': refusing to guess mime type for .' . |
| 478 | + "$ext file, we should have recognized it\n" ); |
454 | 479 | } else { |
455 | | - /* Not something we can detect, so simply |
456 | | - * trust the file extension */ |
| 480 | + // Not something we can detect, so simply |
| 481 | + // trust the file extension |
457 | 482 | $mime = $this->guessTypesForExtension( $ext ); |
458 | 483 | } |
459 | 484 | } |
460 | | - else if ( $mime === "application/x-opc+zip" ) { |
| 485 | + elseif ( $mime === 'application/x-opc+zip' ) { |
461 | 486 | if ( $this->isMatchingExtension( $ext, $mime ) ) { |
462 | | - /* A known file extension for an OPC file, |
463 | | - * find the proper mime type for that file extension */ |
| 487 | + // A known file extension for an OPC file, |
| 488 | + // find the proper mime type for that file extension |
464 | 489 | $mime = $this->guessTypesForExtension( $ext ); |
465 | 490 | } else { |
466 | 491 | wfDebug( __METHOD__. ": refusing to guess better type for $mime file, " . |
467 | 492 | ".$ext is not a known OPC extension.\n" ); |
468 | | - $mime = "application/zip"; |
| 493 | + $mime = 'application/zip'; |
469 | 494 | } |
470 | 495 | } |
471 | 496 | |
— | — | @@ -476,20 +501,22 @@ |
477 | 502 | return $mime; |
478 | 503 | } |
479 | 504 | |
480 | | - /** mime type detection. This uses detectMimeType to detect the mime type of the file, |
481 | | - * but applies additional checks to determine some well known file formats that may be missed |
482 | | - * or misinterpreter by the default mime detection (namely XML based formats like XHTML or SVG, |
483 | | - * as well as ZIP based formats like OPC/ODF files). |
484 | | - * |
485 | | - * @param $file String: the file to check |
486 | | - * @param $ext Mixed: the file extension, or true (default) to extract it from the filename. |
487 | | - * Set it to false to ignore the extension. DEPRECATED! Set to false, use |
488 | | - * improveTypeFromExtension($mime, $ext) later to improve mime type. |
489 | | - * |
490 | | - * @return string the mime type of $file |
491 | | - */ |
492 | | - function guessMimeType( $file, $ext = true ) { |
493 | | - if( $ext ) { # TODO: make $ext default to false. Or better, remove it. |
| 505 | + /** |
| 506 | + * Mime type detection. This uses detectMimeType to detect the mime type |
| 507 | + * of the file, but applies additional checks to determine some well known |
| 508 | + * file formats that may be missed or misinterpreter by the default mime |
| 509 | + * detection (namely XML based formats like XHTML or SVG, as well as ZIP |
| 510 | + * based formats like OPC/ODF files). |
| 511 | + * |
| 512 | + * @param $file String: the file to check |
| 513 | + * @param $ext Mixed: the file extension, or true (default) to extract it from the filename. |
| 514 | + * Set it to false to ignore the extension. DEPRECATED! Set to false, use |
| 515 | + * improveTypeFromExtension($mime, $ext) later to improve mime type. |
| 516 | + * |
| 517 | + * @return string the mime type of $file |
| 518 | + */ |
| 519 | + public function guessMimeType( $file, $ext = true ) { |
| 520 | + if ( $ext ) { // TODO: make $ext default to false. Or better, remove it. |
494 | 521 | wfDebug( __METHOD__.": WARNING: use of the \$ext parameter is deprecated. " . |
495 | 522 | "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" ); |
496 | 523 | } |
— | — | @@ -509,12 +536,21 @@ |
510 | 537 | return $mime; |
511 | 538 | } |
512 | 539 | |
513 | | - private function doGuessMimeType( $file, $ext ) { # TODO: remove $ext param |
| 540 | + /** |
| 541 | + * Guess the mime type from the file contents. |
| 542 | + * |
| 543 | + * @param string $file |
| 544 | + * @param mixed $ext |
| 545 | + */ |
| 546 | + private function doGuessMimeType( $file, $ext ) { // TODO: remove $ext param |
514 | 547 | // Read a chunk of the file |
515 | 548 | wfSuppressWarnings(); |
516 | | - $f = fopen( $file, "rt" ); |
| 549 | + $f = fopen( $file, 'rt' ); // FIXME: Shouldn't this be rb? |
517 | 550 | wfRestoreWarnings(); |
518 | | - if( !$f ) return "unknown/unknown"; |
| 551 | + |
| 552 | + if( !$f ) { |
| 553 | + return 'unknown/unknown'; |
| 554 | + } |
519 | 555 | $head = fread( $f, 1024 ); |
520 | 556 | fseek( $f, -65558, SEEK_END ); |
521 | 557 | $tail = fread( $f, 65558 ); // 65558 = maximum size of a zip EOCDR |
— | — | @@ -541,23 +577,23 @@ |
542 | 578 | "\x7fELF" => 'application/octet-stream', // ELF binary |
543 | 579 | ); |
544 | 580 | |
545 | | - foreach( $headers as $magic => $candidate ) { |
546 | | - if( strncmp( $head, $magic, strlen( $magic ) ) == 0 ) { |
| 581 | + foreach ( $headers as $magic => $candidate ) { |
| 582 | + if ( strncmp( $head, $magic, strlen( $magic ) ) == 0 ) { |
547 | 583 | wfDebug( __METHOD__ . ": magic header in $file recognized as $candidate\n" ); |
548 | 584 | return $candidate; |
549 | 585 | } |
550 | 586 | } |
551 | 587 | |
552 | 588 | /* Look for WebM and Matroska files */ |
553 | | - if( strncmp( $head, pack( "C4", 0x1a, 0x45, 0xdf, 0xa3 ), 4 ) == 0 ) { |
| 589 | + if ( strncmp( $head, pack( "C4", 0x1a, 0x45, 0xdf, 0xa3 ), 4 ) == 0 ) { |
554 | 590 | $doctype = strpos( $head, "\x42\x82" ); |
555 | | - if( $doctype ) { |
| 591 | + if ( $doctype ) { |
556 | 592 | // Next byte is datasize, then data (sizes larger than 1 byte are very stupid muxers) |
557 | 593 | $data = substr($head, $doctype+3, 8); |
558 | | - if( strncmp( $data, "matroska", 8 ) == 0 ) { |
| 594 | + if ( strncmp( $data, "matroska", 8 ) == 0 ) { |
559 | 595 | wfDebug( __METHOD__ . ": recognized file as video/x-matroska\n" ); |
560 | 596 | return "video/x-matroska"; |
561 | | - } else if ( strncmp( $data, "webm", 4 ) == 0 ) { |
| 597 | + } elseif ( strncmp( $data, "webm", 4 ) == 0 ) { |
562 | 598 | wfDebug( __METHOD__ . ": recognized file as video/webm\n" ); |
563 | 599 | return "video/webm"; |
564 | 600 | } |
— | — | @@ -567,7 +603,7 @@ |
568 | 604 | } |
569 | 605 | |
570 | 606 | /* Look for WebP */ |
571 | | - if( strncmp( $head, "RIFF", 4 ) == 0 && strncmp( substr( $head, 8, 8), "WEBPVP8 ", 8 ) == 0 ) { |
| 607 | + if ( strncmp( $head, "RIFF", 4 ) == 0 && strncmp( substr( $head, 8, 8), "WEBPVP8 ", 8 ) == 0 ) { |
572 | 608 | wfDebug( __METHOD__ . ": recognized file as image/webp\n" ); |
573 | 609 | return "image/webp"; |
574 | 610 | } |
— | — | @@ -584,7 +620,7 @@ |
585 | 621 | * 16583). The heuristic has been cut down to exclude three-character |
586 | 622 | * strings like "<? ", but should it be axed completely? |
587 | 623 | */ |
588 | | - if( ( strpos( $head, '<?php' ) !== false ) || |
| 624 | + if ( ( strpos( $head, '<?php' ) !== false ) || |
589 | 625 | |
590 | 626 | ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) || |
591 | 627 | ( strpos( $head, "<\x00?\x00 " ) !== false ) || |
— | — | @@ -593,16 +629,16 @@ |
594 | 630 | ( strpos( $head, "<\x00?\x00=" ) !== false ) ) { |
595 | 631 | |
596 | 632 | wfDebug( __METHOD__ . ": recognized $file as application/x-php\n" ); |
597 | | - return "application/x-php"; |
| 633 | + return 'application/x-php'; |
598 | 634 | } |
599 | 635 | |
600 | 636 | /* |
601 | 637 | * look for XML formats (XHTML and SVG) |
602 | 638 | */ |
603 | 639 | $xml = new XmlTypeCheck( $file ); |
604 | | - if( $xml->wellFormed ) { |
| 640 | + if ( $xml->wellFormed ) { |
605 | 641 | global $wgXMLMimeTypes; |
606 | | - if( isset( $wgXMLMimeTypes[$xml->getRootElement()] ) ) { |
| 642 | + if ( isset( $wgXMLMimeTypes[$xml->getRootElement()] ) ) { |
607 | 643 | return $wgXMLMimeTypes[$xml->getRootElement()]; |
608 | 644 | } else { |
609 | 645 | return 'application/xml'; |
— | — | @@ -719,18 +755,18 @@ |
720 | 756 | |
721 | 757 | $openxmlRegex = "/^\[Content_Types\].xml/"; |
722 | 758 | |
723 | | - if( preg_match( $opendocRegex, substr( $header, 30 ), $matches ) ) { |
| 759 | + if ( preg_match( $opendocRegex, substr( $header, 30 ), $matches ) ) { |
724 | 760 | $mime = $matches[1]; |
725 | 761 | wfDebug( __METHOD__.": detected $mime from ZIP archive\n" ); |
726 | | - } elseif( preg_match( $openxmlRegex, substr( $header, 30 ) ) ) { |
| 762 | + } elseif ( preg_match( $openxmlRegex, substr( $header, 30 ) ) ) { |
727 | 763 | $mime = "application/x-opc+zip"; |
728 | 764 | # TODO: remove the block below, as soon as improveTypeFromExtension is used everywhere |
729 | | - if( $ext !== true && $ext !== false ) { |
| 765 | + if ( $ext !== true && $ext !== false ) { |
730 | 766 | /** This is the mode used by getPropsFromPath |
731 | 767 | * These mime's are stored in the database, where we don't really want |
732 | 768 | * x-opc+zip, because we use it only for internal purposes |
733 | 769 | */ |
734 | | - if( $this->isMatchingExtension( $ext, $mime) ) { |
| 770 | + if ( $this->isMatchingExtension( $ext, $mime) ) { |
735 | 771 | /* A known file extension for an OPC file, |
736 | 772 | * find the proper mime type for that file extension */ |
737 | 773 | $mime = $this->guessTypesForExtension( $ext ); |
— | — | @@ -739,10 +775,10 @@ |
740 | 776 | } |
741 | 777 | } |
742 | 778 | wfDebug( __METHOD__.": detected an Open Packaging Conventions archive: $mime\n" ); |
743 | | - } else if( substr( $header, 0, 8 ) == "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" && |
| 779 | + } elseif ( substr( $header, 0, 8 ) == "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" && |
744 | 780 | ($headerpos = strpos( $tail, "PK\x03\x04" ) ) !== false && |
745 | 781 | preg_match( $openxmlRegex, substr( $tail, $headerpos + 30 ) ) ) { |
746 | | - if( substr( $header, 512, 4) == "\xEC\xA5\xC1\x00" ) { |
| 782 | + if ( substr( $header, 512, 4) == "\xEC\xA5\xC1\x00" ) { |
747 | 783 | $mime = "application/msword"; |
748 | 784 | } |
749 | 785 | switch( substr( $header, 512, 6) ) { |
— | — | @@ -774,31 +810,34 @@ |
775 | 811 | return $mime; |
776 | 812 | } |
777 | 813 | |
778 | | - /** Internal mime type detection, please use guessMimeType() for application code instead. |
779 | | - * Detection is done using an external program, if $wgMimeDetectorCommand is set. |
780 | | - * Otherwise, the fileinfo extension and mime_content_type are tried (in this order), if they are available. |
781 | | - * If the dections fails and $ext is not false, the mime type is guessed from the file extension, using |
782 | | - * guessTypesForExtension. |
783 | | - * If the mime type is still unknown, getimagesize is used to detect the mime type if the file is an image. |
784 | | - * If no mime type can be determined, this function returns "unknown/unknown". |
785 | | - * |
786 | | - * @param $file String: the file to check |
787 | | - * @param $ext Mixed: the file extension, or true (default) to extract it from the filename. |
788 | | - * Set it to false to ignore the extension. DEPRECATED! Set to false, use |
789 | | - * improveTypeFromExtension($mime, $ext) later to improve mime type. |
790 | | - * |
791 | | - * @return string the mime type of $file |
792 | | - * @access private |
793 | | - */ |
| 814 | + /** |
| 815 | + * Internal mime type detection. Detection is done using an external |
| 816 | + * program, if $wgMimeDetectorCommand is set. Otherwise, the fileinfo |
| 817 | + * extension and mime_content_type are tried (in this order), if they |
| 818 | + * are available. If the dections fails and $ext is not false, the mime |
| 819 | + * type is guessed from the file extension, using guessTypesForExtension. |
| 820 | + * |
| 821 | + * If the mime type is still unknown, getimagesize is used to detect the |
| 822 | + * mime type if the file is an image. If no mime type can be determined, |
| 823 | + * this function returns 'unknown/unknown'. |
| 824 | + * |
| 825 | + * @param $file String: the file to check |
| 826 | + * @param $ext Mixed: the file extension, or true (default) to extract it from the filename. |
| 827 | + * Set it to false to ignore the extension. DEPRECATED! Set to false, use |
| 828 | + * improveTypeFromExtension($mime, $ext) later to improve mime type. |
| 829 | + * |
| 830 | + * @return string the mime type of $file |
| 831 | + */ |
794 | 832 | private function detectMimeType( $file, $ext = true ) { |
795 | 833 | global $wgMimeDetectorCommand; |
796 | 834 | |
797 | | - if( $ext ) { # TODO: make $ext default to false. Or better, remove it. |
| 835 | + if ( $ext ) { # TODO: make $ext default to false. Or better, remove it. |
798 | 836 | wfDebug( __METHOD__.": WARNING: use of the \$ext parameter is deprecated. Use improveTypeFromExtension(\$mime, \$ext) instead.\n" ); |
799 | 837 | } |
800 | 838 | |
801 | 839 | $m = null; |
802 | 840 | if ( $wgMimeDetectorCommand ) { |
| 841 | + // FIXME: Use wfShellExec |
803 | 842 | $fn = wfEscapeShellArg( $file ); |
804 | 843 | $m = `$wgMimeDetectorCommand $fn`; |
805 | 844 | } elseif ( function_exists( "finfo_open" ) && function_exists( "finfo_file" ) ) { |
— | — | @@ -813,9 +852,9 @@ |
814 | 853 | # If you may need to load the fileinfo extension at runtime, set |
815 | 854 | # $wgLoadFileinfoExtension in LocalSettings.php |
816 | 855 | |
817 | | - $mime_magic_resource = finfo_open(FILEINFO_MIME); /* return mime type ala mimetype extension */ |
| 856 | + $mime_magic_resource = finfo_open( FILEINFO_MIME ); /* return mime type ala mimetype extension */ |
818 | 857 | |
819 | | - if ($mime_magic_resource) { |
| 858 | + if ( $mime_magic_resource ) { |
820 | 859 | $m = finfo_file( $mime_magic_resource, $file ); |
821 | 860 | finfo_close( $mime_magic_resource ); |
822 | 861 | } else { |
— | — | @@ -851,7 +890,7 @@ |
852 | 891 | } |
853 | 892 | } |
854 | 893 | |
855 | | - # if desired, look at extension as a fallback. |
| 894 | + // If desired, look at extension as a fallback. |
856 | 895 | if ( $ext === true ) { |
857 | 896 | $i = strrpos( $file, '.' ); |
858 | 897 | $ext = strtolower( $i ? substr( $file, $i + 1 ) : '' ); |
— | — | @@ -868,36 +907,40 @@ |
869 | 908 | } |
870 | 909 | } |
871 | 910 | |
872 | | - #unknown type |
873 | | - wfDebug( __METHOD__.": failed to guess mime type for $file!\n" ); |
874 | | - return "unknown/unknown"; |
| 911 | + // Unknown type |
| 912 | + wfDebug( __METHOD__ . ": failed to guess mime type for $file!\n" ); |
| 913 | + return 'unknown/unknown'; |
875 | 914 | } |
876 | 915 | |
877 | 916 | /** |
878 | | - * Determine the media type code for a file, using its mime type, name and possibly |
879 | | - * its contents. |
880 | | - * |
881 | | - * This function relies on the findMediaType(), mapping extensions and mime |
882 | | - * types to media types. |
883 | | - * |
884 | | - * @todo analyse file if need be |
885 | | - * @todo look at multiple extension, separately and together. |
886 | | - * |
887 | | - * @param $path String: full path to the image file, in case we have to look at the contents |
888 | | - * (if null, only the mime type is used to determine the media type code). |
889 | | - * @param $mime String: mime type. If null it will be guessed using guessMimeType. |
890 | | - * |
891 | | - * @return (int?string?) a value to be used with the MEDIATYPE_xxx constants. |
892 | | - */ |
| 917 | + * Determine the media type code for a file, using its mime type, name and |
| 918 | + * possibly its contents. |
| 919 | + * |
| 920 | + * This function relies on the findMediaType(), mapping extensions and mime |
| 921 | + * types to media types. |
| 922 | + * |
| 923 | + * @todo analyse file if need be |
| 924 | + * @todo look at multiple extension, separately and together. |
| 925 | + * |
| 926 | + * @param $path String: full path to the image file, in case we have to look at the contents |
| 927 | + * (if null, only the mime type is used to determine the media type code). |
| 928 | + * @param $mime String: mime type. If null it will be guessed using guessMimeType. |
| 929 | + * |
| 930 | + * @return (int?string?) a value to be used with the MEDIATYPE_xxx constants. |
| 931 | + */ |
893 | 932 | function getMediaType( $path = null, $mime = null ) { |
894 | | - if( !$mime && !$path ) return MEDIATYPE_UNKNOWN; |
| 933 | + if( !$mime && !$path ) { |
| 934 | + return MEDIATYPE_UNKNOWN; |
| 935 | + } |
895 | 936 | |
896 | | - # If mime type is unknown, guess it |
897 | | - if( !$mime ) $mime = $this->guessMimeType( $path, false ); |
| 937 | + // If mime type is unknown, guess it |
| 938 | + if( !$mime ) { |
| 939 | + $mime = $this->guessMimeType( $path, false ); |
| 940 | + } |
898 | 941 | |
899 | | - # Special code for ogg - detect if it's video (theora), |
900 | | - # else label it as sound. |
901 | | - if( $mime == "application/ogg" && file_exists( $path ) ) { |
| 942 | + // Special code for ogg - detect if it's video (theora), |
| 943 | + // else label it as sound. |
| 944 | + if ( $mime == 'application/ogg' && file_exists( $path ) ) { |
902 | 945 | |
903 | 946 | // Read a chunk of the file |
904 | 947 | $f = fopen( $path, "rt" ); |
— | — | @@ -907,7 +950,7 @@ |
908 | 951 | |
909 | 952 | $head = strtolower( $head ); |
910 | 953 | |
911 | | - # This is an UGLY HACK, file should be parsed correctly |
| 954 | + // This is an UGLY HACK, file should be parsed correctly |
912 | 955 | if ( strpos( $head, 'theora' ) !== false ) return MEDIATYPE_VIDEO; |
913 | 956 | elseif ( strpos( $head, 'vorbis' ) !== false ) return MEDIATYPE_AUDIO; |
914 | 957 | elseif ( strpos( $head, 'flac' ) !== false ) return MEDIATYPE_AUDIO; |
— | — | @@ -915,58 +958,69 @@ |
916 | 959 | else return MEDIATYPE_MULTIMEDIA; |
917 | 960 | } |
918 | 961 | |
919 | | - # check for entry for full mime type |
| 962 | + // Check for entry for full mime type |
920 | 963 | if( $mime ) { |
921 | 964 | $type = $this->findMediaType( $mime ); |
922 | | - if( $type !== MEDIATYPE_UNKNOWN ) return $type; |
| 965 | + if ( $type !== MEDIATYPE_UNKNOWN ) { |
| 966 | + return $type; |
| 967 | + } |
923 | 968 | } |
924 | 969 | |
925 | | - # Check for entry for file extension |
| 970 | + // Check for entry for file extension |
926 | 971 | if ( $path ) { |
927 | 972 | $i = strrpos( $path, '.' ); |
928 | 973 | $e = strtolower( $i ? substr( $path, $i + 1 ) : '' ); |
929 | 974 | |
930 | | - # TODO: look at multi-extension if this fails, parse from full path |
931 | | - |
| 975 | + // TODO: look at multi-extension if this fails, parse from full path |
932 | 976 | $type = $this->findMediaType( '.' . $e ); |
933 | | - if ( $type !== MEDIATYPE_UNKNOWN ) return $type; |
| 977 | + if ( $type !== MEDIATYPE_UNKNOWN ) { |
| 978 | + return $type; |
| 979 | + } |
934 | 980 | } |
935 | 981 | |
936 | | - # Check major mime type |
937 | | - if( $mime ) { |
| 982 | + // Check major mime type |
| 983 | + if ( $mime ) { |
938 | 984 | $i = strpos( $mime, '/' ); |
939 | | - if( $i !== false ) { |
| 985 | + if ( $i !== false ) { |
940 | 986 | $major = substr( $mime, 0, $i ); |
941 | 987 | $type = $this->findMediaType( $major ); |
942 | | - if( $type !== MEDIATYPE_UNKNOWN ) return $type; |
| 988 | + if ( $type !== MEDIATYPE_UNKNOWN ) { |
| 989 | + return $type; |
| 990 | + } |
943 | 991 | } |
944 | 992 | } |
945 | 993 | |
946 | | - if( !$type ) $type = MEDIATYPE_UNKNOWN; |
| 994 | + if( !$type ) { |
| 995 | + $type = MEDIATYPE_UNKNOWN; |
| 996 | + } |
947 | 997 | |
948 | 998 | return $type; |
949 | 999 | } |
950 | 1000 | |
951 | | - /** returns a media code matching the given mime type or file extension. |
952 | | - * File extensions are represented by a string starting with a dot (.) to |
953 | | - * distinguish them from mime types. |
954 | | - * |
955 | | - * This funktion relies on the mapping defined by $this->mMediaTypes |
956 | | - * @access private |
957 | | - */ |
| 1001 | + /** |
| 1002 | + * Returns a media code matching the given mime type or file extension. |
| 1003 | + * File extensions are represented by a string starting with a dot (.) to |
| 1004 | + * distinguish them from mime types. |
| 1005 | + * |
| 1006 | + * This funktion relies on the mapping defined by $this->mMediaTypes |
| 1007 | + * @access private |
| 1008 | + */ |
958 | 1009 | function findMediaType( $extMime ) { |
959 | | - if ( strpos( $extMime, '.' ) === 0 ) { #if it's an extension, look up the mime types |
| 1010 | + if ( strpos( $extMime, '.' ) === 0 ) { |
| 1011 | + // If it's an extension, look up the mime types |
960 | 1012 | $m = $this->getTypesForExtension( substr( $extMime, 1 ) ); |
961 | | - if ( !$m ) return MEDIATYPE_UNKNOWN; |
| 1013 | + if ( !$m ) { |
| 1014 | + return MEDIATYPE_UNKNOWN; |
| 1015 | + } |
962 | 1016 | |
963 | 1017 | $m = explode( ' ', $m ); |
964 | 1018 | } else { |
965 | | - # Normalize mime type |
| 1019 | + // Normalize mime type |
966 | 1020 | if ( isset( $this->mMimeTypeAliases[$extMime] ) ) { |
967 | 1021 | $extMime = $this->mMimeTypeAliases[$extMime]; |
968 | 1022 | } |
969 | 1023 | |
970 | | - $m = array($extMime); |
| 1024 | + $m = array( $extMime ); |
971 | 1025 | } |
972 | 1026 | |
973 | 1027 | foreach ( $m as $mime ) { |