Jump to content

Extension:IncludeOnlyNS

From mediawiki.org
MediaWiki extensions manual
IncludeOnlyNS
Release status: unmaintained
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

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.

Installation

[edit]

IncludeOnlyNS.php

[edit]

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.php

[edit]

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
);