MediaWiki PassThroughFile Extension Version 1

MediaWiki recommends against using the top level of a domain to load wiki pages from. If only a section of the site contains a wiki, http://example.com/wiki/Main_Page makes sense. MediaWiki supports this. If the entire site is a wiki, then http://example.com/Main_Page makes sense. MediaWiki does not support this, and recommends against it.

The problem with the latter is that it renders files such as robots.txt as a wiki page at http://example.com/Robots.txt. This can be avoided by using MediaWiki’s extensions capability.

To get around this issue, I wrote an extension.

Note: This license is released under the Creative Commons Attribution License.

<?php

if( !defined( 'MEDIAWIKI' ) ){
    die( "This is not a valid entry point.\n" );
}

#################################
## ADD FILES TO PASS THRU HERE ##
#################################

// A list of files MediaWiki should load from file.  These must be in the same
// folder as MediaWiki executes from.
$passThroughFiles = array(
    'robots.txt',
    'favicon.ico'
);

#################################
## DO NOT EDIT ANYTHING BELOW  ##
#################################

$wgExtensionCredits['parserhook'][] = array(
    'path' => __FILE__,
    'name' => "Pass File Through",
    'description' => "Passes specified files through, rather than trying to render an article.",
    'version' => 1.1,
    'author' => "Christopher Fritz",
    'url' => "http://kurifuri.com/mediawiki-passthroughfile-extention",
);

$wgHooks['BeforeInitialize'][] = 'passFileThrough';

function passFileThrough( &$title, &$article, &$output, &$user, $request, $mediaWiki ) {

    global $passThroughFiles;
    define( 'NonPassReturnValue', false ); // should this be true or false?

    $file = $title->mUserCaseDBKey;

    // Skip this page if we didn't specify to load a file for it.
    if ( !in_array($file, $passThroughFiles) ) {
        return NonPassReturnValue;
    }

    // Don't try loading this file if it doesn't exist.
    if ( !file_exists($file) ) {
        return NonPassReturnValue;
    }

    include($file);
    exit;
}

?>

Copy this text and save it in the extensions folder as pass_file_through.php.

Add the following to the bottom of LocalSettings.php:

require_once( “$IP/extensions/pass_file_through.php” );

By default, the extension only supports robots.txt and favicon.ico, but others can be added.

My PHP is rusty, and I’m not read up on MediaWiki’s programming guidelines for whitespace and layout. If you see anywhere that this extension can improve, feel free to leave a comment and let me know!

Leave a Reply

Your email address will not be published. Required fields are marked *