Open main menu

Extension:IncludeOnlyNS

MediaWiki extensions manual
OOjs UI icon advanced.svg
IncludeOnlyNS
Release status: experimental
Implementation User interface
Description Allows namespace(s) to be designated as "include-only"
Author(s) Christopher Wilson (Gwsuperfantalk)
Latest version 0.9.1 (2011-03-29)
MediaWiki 1.16
License Public domain
Download See below
Translate the IncludeOnlyNS extension if it is available at translatewiki.net
Check usage and version matrix.

IncludeOnlyNS is designed to allow for one or more namespaces within a wiki to be designated as "Include Only", meaning that the page content may only be viewed if it is included on another page. The idea is to replicate the theoretical functionality of having a transcluded page with

<noinclude>#REDIRECT [[{{PAGENAME}}]]</noinclude>

in the code. The use case for which this was developed was a wiki using Semantic MediaWiki and Semantic Forms. Semantic Forms does not allow for a form-based template to be placed in the middle of a page, so the solution was to "hide" the template on a related page in another namespace, and transclude it via a template.

InstallationEdit

IncludeOnlyNS.phpEdit

Save the following code as extensions/IncludeOnlyNS/IncludeOnlyNS.php

<?php
if ( !defined( 'MEDIAWIKI' ) ) {
        die( 'This file is a MediaWiki extension, it is not a valid entry point' );
}

$wgExtensionCredits['other'][] = array(
        'path'           => __FILE__,
        'name'           => 'IncludeOnlyNS',
        'version'        => '0.9.1',
        'author'         => 'Christopher Wilson',
        'description' => 'Forced redirection to main namespace on view of articles in an "include only" namespace',
        'url'            => 'http://www.mediawiki.org/wiki/Extension:IncludeOnlyNS',
);

function getAddress() {
    /*** check for https ***/
    if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' ) {
        $protocol =  'https';
    } else {
        $protocol = 'http';
    }
    /*** return the full address ***/
    return $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
 }

function fnRedirectByNS( &$title, &$article, &$output, &$user, $request, $mediaWiki ) {
        global $wgIncludeOnlyNamespaces;
        global $wgContLang;
        $currentaddress = getAddress();
        $namespace = $title->getNamespace();
        $namespaceText = $wgContLang->getNsText( $namespace );
        if ( in_array( $namespace, $wgIncludeOnlyNamespaces ) ) {
                if ( $request->getVal( 'action', 'view' ) != 'view' ) {
                        return true;
                } else {
                        $redirAddress = preg_replace("/$namespaceText:/", '', $currentaddress);
                        header("Location: $redirAddress");
                        return false;
                } 
        } else {        
                return true;
        }
}

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

Changes to LocalSettings.phpEdit

Add the following line to LocalSettings:

require_once( "$IP/extensions/IncludeOnlyNS/IncludeOnlyNS.php" );

Defining Namespaces as "Include-Only"Edit

This would likely be done only with a custom namespace. For information on setting up custom namespaces, see the Manual on using custom namespaces.

Below the line to include the extension, define the array of namespaces which should be include-only. To define a "Data:" namespace as include-only, you would use:

$wgIncludeOnlyNamespaces = array(
        NS_DATA
);