Extension:ConditionalShow

From MediaWiki.org

Jump to: navigation, search
If you need per-page or partial page access restrictions, you are advised to install an appropriate content management package. MediaWiki was not written to provide per-page access restrictions, and almost all hacks or patches promising to add them will likely have flaws somewhere, which could lead to exposure of confidential data. We are not responsible for anything being leaked, leading to loss of funds or one's job.
For further details, see Security issues with authorization extensions


Manual on MediaWiki Extensions
List of MediaWiki Extensions
ConditionalShow

Release status: stable

Implementation Tag, User rights
Description Enables conditionally showing a wikitext section.
Author(s) user:jldupont
Version 1.4
MediaWiki tested on 1.8.2, 1.9.3, 1.10
Download [1]

Contents

[edit] Purpose

This extension enables showing a section of wikitext upon successful evaluation of conditions.

[edit] Example

<cshow ingroup='sysop'> This text will appear if a user with membership to 'sysop' group views this page</cshow>

[edit] Note

This extension is not 'cache' friendly; if one requires this feature, then BizzWiki platform provides a solution through Parser Phase 2 extension.

[edit] Source Code

Source Code and additional information can be found at [2] or below.

<?php
/*
 * ConditionalShowSection MediaWiki extension
 *
 * @author Jean-Lou Dupont
 * @package MediaWiki
 * @subpackage Extensions
 * @licence GNU General Public Licence 2.0
 * This extension enables to conditionally show/hide a section
 * of wikitext that appears between the <cshow> </cshow> tags.
 *
 * Add to LocalSettings.php
 * with: require_once("extensions/ConditionalShowSection.php");
 *
 * HISTORY:
 * 1.1: corrected bug when "ingroup" option not present, wrong results.
 * 1.2: used "recursiveTagParse" to get cleaner parsing.
 * 1.3: corrected error with default initialisation of userReqLogged
 * 1.4: changed to using 'getEffectiveGroups' in order to have also access
 *      to the implicit '*' group and the default group for logged users 'user'.
 */
$wgExtensionCredits['other'][] = array(
        'name'   => "ConditionalShowSection [http://www.bluecortex.com]",
        'url'    => 'http://www.mediawiki.org/wiki/Extension:ConditionalShow',
        'version'=> '1.4',
        'author' => 'Jean-Lou Dupont [http://www.bluecortex.com]' 
);
 
$wgExtensionFunctions[] = "wfConditionalShowSection";
 
function wfConditionalShowSection() {
    global $wgParser;
    $wgParser->setHook( "cshow", "ConditionalShowSection" );
}
 
function ConditionalShowSection( $input, $argv, &$parser ) {
    #
    # By default, the section is HIDDEN unless the following conditions are met:
    # Argument: Logged = '1' or '0'  
    # Argument: InGroup = 'group XYZ'
    #
    # If no arguments are provided for:
    # - Logged   --> assume 'don't care' 
    # - InGroup  --> assume ''           (no group)
        #
        # In other words, if no 'ingroup' parameter is given,
        # then the condition 'ingroup' is never met.
    #
        # If no "logged" parameter is given, then this condition is always met.
    # 
    # Examples: <cshow Logged="1" InGroup="sysop"> text to show upon conditions met </cshow>
    # if the user viewing the page is LOGGED and part of the SYSOP group then show the section.
        #
        # <cshow ingroup='user'> Test </cshow>
        # shows 'Test' if the user viewing the page is logged and by default part of the 'user' group. 
    #
        global $wgUser;
 
        $userReqLogged = null;         # default is "don't care"
        $userReqGroup  = "" ;    # assuming no group membership required
        $output = "";                # assuming the section is hidden by default.
 
        $cond1 = false;                        
        $cond2 = false;                        # by default, don't show the section to just anybody
 
        # Extract the parameters passed
        # the parser lowers the case of all the parameters passed...
        if (isset($argv["logged"]))
        {
                $userReqLogged = $argv["logged"];
 
                if ($userReqLogged==="1" && ($wgUser->isLoggedIn()===true)) 
                        $cond1=true;
 
                if ($userReqLogged==="0" && ($wgUser->isLoggedIn()===false))
                        $cond1=true;
        }
        if (isset($argv["ingroup"]))
        {
                $userReqGroup  = $argv["ingroup"];
                # which groups is the user part of?
                $ugroups = $wgUser->getEffectiveGroups();  // changed in v1.4
                $result  = array_search($userReqGroup,$ugroups);
 
                if ($result!==false)
                        $cond2=true;
        }
        # if both conditions are met, then SHOW else HIDE
        if (($cond1===true) and ($cond2===true))
         $output=$parser->recursiveTagParse($input);
 
        return $output;
}

[edit] Better Option

Personal tools