Rillke has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/141240

Change subject: Allow third party code to hook-up MIME type detection
......................................................................

Allow third party code to hook-up MIME type detection

Adding 4 hooks to MimeMagic.php

- MimeMagicCustomInfo: Add assignments of MIME types -> Media types
- MimeMagicCustomTypes: Add assignments of MIME types -> File extensions
- MimeMagicImproveFromExtension: Further improve the MIME type detected
  by considering the file extension
- MimeMagicGuessFromContent: Guess the MIME from file content

This is the successor of Icf9eec10bec7c0a7e.

PHP's own module fileinfo module is not capable detecting Chemical
table files. Instead, they are reported as text/plain.

MediaHandlers can be attached by MIME type only. That's why these
changes are required for [[Extension:MolHandler]] to work.

Change-Id: I67f3e4e83b47e6df0d9e8371f09a741a8aa77651
---
M docs/hooks.txt
M includes/MimeMagic.php
2 files changed, 47 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/40/141240/1

diff --git a/docs/hooks.txt b/docs/hooks.txt
index 80ac174..c8b7ce7 100644
--- a/docs/hooks.txt
+++ b/docs/hooks.txt
@@ -1750,6 +1750,32 @@
 $title: name of the page changed.
 $text: new contents of the page.
 
+'MimeMagicCustomInfo': Before processing the list mapping MIME types to media
+types.
+&$mimeMagic: Instance of MimeMagic.
+$addToList: Call-back function expecting a string parameter containing extra
+  assignments. Format is the same as in mime.info.
+
+'MimeMagicCustomTypes': Before processing the list mapping MIME types to file
+extensions.
+&$mimeMagic: Instance of MimeMagic.
+$addToList: Call-back function expecting a string parameter containing extra
+  assignments. Format is the same as in mime.types.
+
+'MimeMagicImproveFromExtension': Allows MW extensions to further improve the
+MIME type detected by considering the file extension.
+&$mimeMagic: Instance of MimeMagic.
+$ext: File extension.
+&$mime: MIME type (in/out).
+
+'MimeMagicGuessFromContent': Allows MW extensions guess the MIME by content.
+&$mimeMagic: Instance of MimeMagic.
+&$head: First 1024 bytes of the file in a string (in - Do not alter!).
+&$tail: More or equal than last 65558 bytes of the file in a string
+  (in - Do not alter!).
+$file: File path.
+&$mime: MIME type (out).
+
 'ModifyExportQuery': Modify the query used by the exporter.
 $db: The database object to be queried.
 &$tables: Tables in the query.
diff --git a/includes/MimeMagic.php b/includes/MimeMagic.php
index f4d4697..a7bd6ea 100644
--- a/includes/MimeMagic.php
+++ b/includes/MimeMagic.php
@@ -197,6 +197,11 @@
                        wfDebug( __METHOD__ . ": no mime types file defined, 
using build-ins only.\n" );
                }
 
+               # Allow MediaHanler extensions adding MIME-types
+               wfRunHooks( 'MimeMagicCustomTypes', array( &$this, function ( 
$extraTypes ) use ( &$types ) {
+                       $types = $types . "\n" . $extraTypes;
+               } ) );
+
                $types = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", 
"\r" ), "\n", $types );
                $types = str_replace( "\t", " ", $types );
 
@@ -271,6 +276,11 @@
                } else {
                        wfDebug( __METHOD__ . ": no mime info file defined, 
using build-ins only.\n" );
                }
+
+               # Allow MediaHanler extensions adding MIME-info
+               wfRunHooks( 'MimeMagicCustomInfo', array( &$this, function ( 
$extraInfo ) use ( &$info ) {
+                       $info = $info . "\n" . $extraInfo;
+               } ) );
 
                $info = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", 
"\r" ), "\n", $info );
                $info = str_replace( "\t", " ", $info );
@@ -520,6 +530,9 @@
                        }
                }
 
+               # Media handling extensions can improve the MIME detected
+               wfRunHooks( 'MimeMagicImproveFromExtension', array( &$this, 
$ext, &$mime ) );
+
                if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
                        $mime = $this->mMimeTypeAliases[$mime];
                }
@@ -746,7 +759,14 @@
                        return 'image/vnd.djvu';
                }
 
-               return false;
+               # Media handling extensions can guess the MIME by content
+               # It's intentionally here so that if core is wrong about a type 
(false positive),
+               # people will hopefully nag and submit patches :)
+               $mime = false;
+               # Some strings by reference for performance - assuming 
well-behaved hooks
+               wfRunHooks( 'MimeMagicGuessFromContent', array( &$this, &$head, 
&$tail, $file, &$mime ) );
+
+               return $mime;
        }
 
        /**

-- 
To view, visit https://gerrit.wikimedia.org/r/141240
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I67f3e4e83b47e6df0d9e8371f09a741a8aa77651
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Rillke <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to