Uploading forbidden file types in Mediawiki

MediaWiki is a wonderful piece of software, and very well designed. But sometimes the defaults are just… impractical. Like the strict ban on uploading any Microsoft Office file types. Below are the lines I’ve added to my LocalSettings.php to resolve that problem.

# End of automatically generated settings.
# Add more configuration options below.

# Loosen file extension security
$wgStrictFileExtensions = false;

# Allow office file extensions
$wgFileExtensions[] = 'pdf';
$wgFileExtensions[] = 'vsd';
$wgFileExtensions[] = 'doc';
$wgFileExtensions[] = 'xls';
$wgFileExtensions[] = 'mpp';
$wgFileExtensions[] = 'ppt';
$wgFileExtensions[] = 'docx';
$wgFileExtensions[] = 'xlsx';
$wgFileExtensions[] = 'pptx';
$wgFileExtensions[] = 'odt';
$wgFileExtensions[] = 'ods';

$wgFileExtensions[] = 'zip';

# Exempt office docs by removing them from mime type
# blacklist
$wgMimeTypeBlacklist = array_diff(
 $wgMimeTypeBlacklist, array(
  'application/msword'
 )
);
$wgMimeTypeBlacklist = array_diff(
 $wgMimeTypeBlacklist, array(
  'application/vnd.ms-powerpoint'
 )
);
$wgMimeTypeBlacklist = array_diff(
 $wgMimeTypeBlacklist, array(
  'application/vnd.msexcel'
 )
);

$wgMimeTypeBlacklist = array_diff(
 $wgMimeTypeBlacklist, array(
  'application/zip'
 )
);

The blacklist stuff above is a little tricky. I did try the suggestion in the manual:

Manual:Configuring file uploads

that gets casually cited a lot, but that didn’t work.

The code in the example only manipulates the contents of the file type blacklist, whereas the restriction on MS Office docs is at the mime type level.

I finally found this comment providing some example code based on a change in the Mediawiki source that makes the mime type blacklist override the file type blacklist.

Earlier I had tried the suggestion in this entry that got around the restriction by republishing the whole $wgMimeTypeBlacklist without what you want to allow, which worked.

In that case I added these lines:

$wgMimeTypeBlacklist = array(
 'text/html', 'text/javascript','text/x-javascript',
 'application/x-shellscript','application/x-php',
 'text/x-php','text/x-python', 'text/x-perl',
 'text/x-bash', 'text/x-sh', 'text/x-csh',
 'text/scriptlet', 'application/x-msdownload',
 'application/x-msmetafile',
 'application/zip',
 'application/x-opc+zip',

);

that consist of the the entire mime type blacklist minus these lines:

'application/msword',
'application/vnd.ms-powerpoint',
'application/vnd.msexcel',
'application/zip',

Please note that none of the above means I don’t fully understand why the Mediawiki developers have gone to such great lengths to prevent the uploading of Microsoft Office document types. Allowing these files types has another downside: their contents are not indexed in the wiki database and so can’t be searched without installing and configuring an add-on like Extension:FileIndexer (which you’ll probably want in any event to make .pdf content searchable).