Merge "Less false positives for MEDIATYPE_VIDEO"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Sun, 29 Jun 2014 23:40:20 +0000 (23:40 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Sun, 29 Jun 2014 23:40:20 +0000 (23:40 +0000)
1  2 
includes/MimeMagic.php

diff --combined includes/MimeMagic.php
@@@ -142,29 -142,30 +142,29 @@@ END_STRIN
   * of MimeMagic. Please use MimeMagic::singleton() to get that instance.
   */
  class MimeMagic {
 -
        /**
 -       * Mapping of media types to arrays of mime types.
 +       * @var array Mapping of media types to arrays of mime types.
         * This is used by findMediaType and getMediaType, respectively
         */
 -      var $mMediaTypes = null;
 +      protected $mMediaTypes = null;
  
 -      /** Map of mime type aliases
 +      /** @var array Map of mime type aliases
         */
 -      var $mMimeTypeAliases = null;
 +      protected $mMimeTypeAliases = null;
  
 -      /** map of mime types to file extensions (as a space separated list)
 +      /** @var array Map of mime types to file extensions (as a space separated list)
         */
 -      var $mMimeToExt = null;
 +      protected $mMimeToExt = null;
  
 -      /** map of file extensions types to mime types (as a space separated list)
 +      /** @var array Map of file extensions types to mime types (as a space separated list)
         */
 -      var $mExtToMime = null;
 +      public $mExtToMime = null;
  
 -      /** IEContentAnalyzer instance
 +      /** @var IEContentAnalyzer
         */
 -      var $mIEAnalyzer;
 +      protected $mIEAnalyzer;
  
 -      /** The singleton instance
 +      /** @var MimeMagic The singleton instance
         */
        private static $instance = null;
  
  
                        if ( count( $m ) > 1 ) {
                                $main = $m[0];
 -                              for ( $i = 1; $i < count( $m ); $i += 1 ) {
 +                              $mCount = count( $m );
 +                              for ( $i = 1; $i < $mCount; $i += 1 ) {
                                        $mime = $m[$i];
                                        $this->mMimeTypeAliases[$mime] = $main;
                                }
                        }
                }
 -
        }
  
        /**
         * @param string $file
         * @param mixed $ext
         * @return bool|string
 +       * @throws MWException
         */
        private function doGuessMimeType( $file, $ext ) { // TODO: remove $ext param
                // Read a chunk of the file
                wfSuppressWarnings();
 -              // @todo FIXME: Shouldn't this be rb?
 -              $f = fopen( $file, 'rt' );
 +              $f = fopen( $file, 'rb' );
                wfRestoreWarnings();
  
                if ( !$f ) {
                        return 'unknown/unknown';
                }
 +
 +              $fsize = filesize( $file );
 +              if ( $fsize === false ) {
 +                      return 'unknown/unknown';
 +              }
 +
                $head = fread( $f, 1024 );
 -              fseek( $f, -65558, SEEK_END );
 -              $tail = fread( $f, 65558 ); // 65558 = maximum size of a zip EOCDR
 +              $tailLength = min( 65558, $fsize ); // 65558 = maximum size of a zip EOCDR
 +              if ( fseek( $f, -1 * $tailLength, SEEK_END ) === -1 ) {
 +                      throw new MWException(
 +                              "Seeking $tailLength bytes from EOF failed in " . __METHOD__ );
 +              }
 +              $tail = fread( $f, $tailLength );
                fclose( $f );
  
                wfDebug( __METHOD__ . ": analyzing head and tail of $file for magic numbers.\n" );
        private function detectMimeType( $file, $ext = true ) {
                global $wgMimeDetectorCommand;
  
 -              if ( $ext ) { # TODO:  make $ext default to false. Or better, remove it.
 -                      wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
 +              /** @todo Make $ext default to false. Or better, remove it. */
 +              if ( $ext ) {
 +                      wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. "
 +                              . "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
                }
  
                $m = null;
                } elseif ( function_exists( "mime_content_type" ) ) {
  
                        # NOTE: this function is available since PHP 4.3.0, but only if
 -                      # PHP was compiled with --with-mime-magic or, before 4.3.2, with --enable-mime-magic.
 +                      # PHP was compiled with --with-mime-magic or, before 4.3.2, with
 +                      # --enable-mime-magic.
                        #
 -                      # On Windows, you must set mime_magic.magicfile in php.ini to point to the mime.magic file bundled with PHP;
 -                      # sometimes, this may even be needed under linus/unix.
 +                      # On Windows, you must set mime_magic.magicfile in php.ini to point
 +                      # to the mime.magic file bundled with PHP; sometimes, this may even
 +                      # be needed under *nix.
                        #
 -                      # Also note that this has been DEPRECATED in favor of the fileinfo extension by PECL, see above.
 -                      # see http://www.php.net/manual/en/ref.mime-magic.php for details.
 +                      # Also note that this has been DEPRECATED in favor of the fileinfo
 +                      # extension by PECL, see above.
 +                      # See http://www.php.net/manual/en/ref.mime-magic.php for details.
  
                        $m = mime_content_type( $file );
                } else {
                }
                if ( $ext ) {
                        if ( $this->isRecognizableExtension( $ext ) ) {
 -                              wfDebug( __METHOD__ . ": refusing to guess mime type for .$ext file, we should have recognized it\n" );
 +                              wfDebug( __METHOD__ . ": refusing to guess mime type for .$ext file, "
 +                                      . "we should have recognized it\n" );
                        } else {
                                $m = $this->guessTypesForExtension( $ext );
                                if ( $m ) {
                        $head = fread( $f, 256 );
                        fclose( $f );
  
-                       $head = strtolower( $head );
+                       $head = str_replace( 'ffmpeg2theora', '', strtolower( $head ) );
  
                        // This is an UGLY HACK, file should be parsed correctly
                        if ( strpos( $head, 'theora' ) !== false ) {