Allowing doc/ppt/xls uploads to MediaWiki and getting proper MIME types back

MediaWiki

The (terrible) doc/ppt/xls Microsoft Office document formats all have the same MIME type. This causes a lot of grief when using MediaWiki, as MediaWiki checks MIME types against file extensions for security purposes. In this article I’ll describe how to allow uploading for these file types, and how to get around the “The file is corrupt or has an incorrect extension” problem.

Allow doc/ppt/xls to be uploaded

Add the following lines to your LocalSettings.php to allow these formats:

$wgFileExtensions[] = 'doc';
$wgFileExtensions[] = 'ppt';
$wgFileExtensions[] = 'xls';

Fix the “The file is corrupt or has an incorrect extension” problem

MediaWiki is going to see all of these formats as “application/msword”, as they all have the same MIME type. So, we need to tell MediaWiki to handle them as such. Change the following line in “includes/mime.types” from:

application/msword doc

to:

application/msword doc ppt xls

Now MediaWiki will treat all these formats as Microsoft Word documents. Unfortunately, that also means that when users open these files from your wiki, they’ll open in Word.

Display the proper MIME types back to your users

Fixing the displayed MIME type when using img_auth.php

If you have a locked down wiki, you are likely using img_auth.php instead of your web server to send files back. We need to do some code modification to make this work properly.

Fixing “includes/MimeMagic.php” in MediaWiki 1.12+

Apply the following patch:

--- MimeMagic.php.old   2009-04-21 13:03:40.000000000 -0500
+++ MimeMagic.php       2009-04-21 12:26:15.000000000 -0500
@@ -334,6 +334,12 @@
                $m = trim( $m );
                $m = preg_replace( '/s.*$/', '', $m );
+               if ( $ext == 'ppt' ) {
+                       $m = 'application/vnd.ms-powerpoint';
+               } else if ( $ext == 'xls' ) {
+                       $m = 'application/vnd.ms-excel';
+               }
+
                return $m;
        }

Fixing “includes/MimeMagic.php” in MediaWiki 1.11 and below

Apply the following patch:

--- MimeMagic.php.old   2009-04-21 13:07:49.000000000 -0500
+++ MimeMagic.php       2009-04-21 13:08:58.000000000 -0500
@@ -497,6 +497,19 @@

                }

+               # New excel files have the same magic number as msword files
+               if ( $mime == "application/msword" ) {
+                       $i = strrpos( $file, '.' );
+                       $e = strtolower( $i ? substr( $file, $i + 1 ) : '' );
+
+                       if ( $e == "xls" ) {
+                               $mime= "application/vnd.ms-excel";
+                       }
+                       if ( $e == "ppt" ) {
+                               $mime= "application/vnd.ms-powerpoint";
+                       }
+               }
+
                if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
                        $mime = $this->mMimeTypeAliases[$mime];
                }

The above patches tell MediaWiki to send the proper MIME types based upon the type of extension the file has, not the one it detects.

Fixing the displayed MIME type when using Apache

Using mod_mime, you can tell Apache to send out the proper MIME types based on extension:

AddType application/vnd.ms-excel .xls
AddType application/vnd.ms-powerpoint .ppt

Testing

Now upload test doc, ppt, and xls files. Ensure that you can upload them, and that when you download them, that they open in the correct applications.

2 Comments

2 Responses to “Allowing doc/ppt/xls uploads to MediaWiki and getting proper MIME types back”

  1. Tony says:

    I am going to try this out. How do you then get thumbnails that represent the ms office mimetypes? Currently the thumbnail is a generic “blank file” png.

Leave a Comment