3 * ConditionalShowSection MediaWiki extension
5 * @author Jean-Lou Dupont
7 * @subpackage Extensions
8 * @licence GNU General Public Licence 2.0
9 * This extension enables to conditionally show/hide a section
10 * of wikitext that appears between the <cshow> </cshow> tags.
12 * Add to LocalSettings.php
13 * with: require_once("extensions/ConditionalShowSection/ConditionalShowSection.php");
16 * 1.1: corrected bug when "ingroup" option not present, wrong results.
17 * 1.2: used "recursiveTagParse" to get cleaner parsing.
18 * 1.3: corrected error with default initialisation of userReqLogged
19 * 1.4: changed to using 'getEffectiveGroups' in order to have also access
20 * to the implicit '*' group and the default group for logged users 'user'.
21 * 1.5: Allow to check multiple groups - separated by ","
23 $wgExtensionCredits['other'][] = array(
24 'name' => "ConditionalShowSection [http://www.bluecortex.com]",
25 'url' => 'http://www.mediawiki.org/wiki/Extension:ConditionalShow',
27 'author' => 'Jean-Lou Dupont [http://www.bluecortex.com]'
31 if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
32 $wgHooks['ParserFirstCallInit'][] = 'wfConditionalShowSection';
33 } else { // Otherwise do things the old fashioned way
34 $wgExtensionFunctions[] = 'wfConditionalShowSection';
37 function wfConditionalShowSection() {
39 $wgParser->setHook( "cshow", "ConditionalShowSection" );
43 function ConditionalShowSection( $input, $argv, &$parser ) {
45 # By default, the section is HIDDEN unless the following conditions are met:
46 # Argument: Logged = '1' or '0'
47 # Argument: InGroup = 'group XYZ'
49 # If no arguments are provided for:
50 # - Logged --> assume 'don't care'
51 # - InGroup --> assume '' (no group)
53 # In other words, if no 'ingroup' parameter is given,
54 # then the condition 'ingroup' is never met.
56 # If no "logged" parameter is given, then this condition is always met.
58 # Examples: <cshow Logged="1" InGroup="sysop"> text to show upon conditions met </cshow>
59 # if the user viewing the page is LOGGED and part of the SYSOP group then show the section.
61 # <cshow ingroup='user'> Test </cshow>
62 # shows 'Test' if the user viewing the page is logged and by default part of the 'user' group.
66 $userReqLogged = null; # default is "don't care"
67 $userReqGroup = "" ; # assuming no group membership required
68 $output = ""; # assuming the section is hidden by default.
71 $cond2 = false; # by default, don't show the section to just anybody
73 # Extract the parameters passed
74 # the parser lowers the case of all the parameters passed...
75 if (isset($argv["logged"]))
77 $userReqLogged = $argv["logged"];
79 if ($userReqLogged==="1" && ($wgUser->isLoggedIn()===true))
82 if ($userReqLogged==="0" && ($wgUser->isLoggedIn()===false))
85 if (isset($argv["ingroup"]))
87 $userReqGroup = explode(',',$argv["ingroup"]);
88 # which groups is the user part of?
89 $ugroups = $wgUser->getEffectiveGroups(); // changed in v1.4
90 if(array_intersect($userReqGroup,$ugroups))
93 # if both conditions are met, then SHOW else HIDE
94 if (($cond1===true) and ($cond2===true)){
95 $output=$parser->recursiveTagParse($input);