10bf502781954f5877becc9b3bfdd76c56a6b58c
[lhc/web/wiklou.git] / extensions / ConditionalShowSection / ConditionalShowSection.php
1 <?php
2 /*
3 * ConditionalShowSection MediaWiki extension
4 *
5 * @author Jean-Lou Dupont
6 * @package MediaWiki
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.
11 *
12 * Add to LocalSettings.php
13 * with: require_once("extensions/ConditionalShowSection/ConditionalShowSection.php");
14 *
15 * HISTORY:
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 ","
22 */
23 $wgExtensionCredits['other'][] = array(
24 'name' => "ConditionalShowSection [http://www.bluecortex.com]",
25 'url' => 'http://www.mediawiki.org/wiki/Extension:ConditionalShow',
26 'version'=> '1.5',
27 'author' => 'Jean-Lou Dupont [http://www.bluecortex.com]'
28 );
29
30
31 if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
32 $wgHooks['ParserFirstCallInit'][] = 'wfConditionalShowSection';
33 } else { // Otherwise do things the old fashioned way
34 $wgExtensionFunctions[] = 'wfConditionalShowSection';
35 }
36
37 function wfConditionalShowSection() {
38 global $wgParser;
39 $wgParser->setHook( "cshow", "ConditionalShowSection" );
40 return true;
41 }
42
43 function ConditionalShowSection( $input, $argv, &$parser ) {
44 #
45 # By default, the section is HIDDEN unless the following conditions are met:
46 # Argument: Logged = '1' or '0'
47 # Argument: InGroup = 'group XYZ'
48 #
49 # If no arguments are provided for:
50 # - Logged --> assume 'don't care'
51 # - InGroup --> assume '' (no group)
52 #
53 # In other words, if no 'ingroup' parameter is given,
54 # then the condition 'ingroup' is never met.
55 #
56 # If no "logged" parameter is given, then this condition is always met.
57 #
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.
60 #
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.
63 #
64 global $wgUser;
65
66 $userReqLogged = null; # default is "don't care"
67 $userReqGroup = "" ; # assuming no group membership required
68 $output = ""; # assuming the section is hidden by default.
69
70 $cond1 = false;
71 $cond2 = false; # by default, don't show the section to just anybody
72
73 # Extract the parameters passed
74 # the parser lowers the case of all the parameters passed...
75 if (isset($argv["logged"]))
76 {
77 $userReqLogged = $argv["logged"];
78
79 if ($userReqLogged==="1" && ($wgUser->isLoggedIn()===true))
80 $cond1=true;
81
82 if ($userReqLogged==="0" && ($wgUser->isLoggedIn()===false))
83 $cond1=true;
84 } else $cond1=true;
85 if (isset($argv["ingroup"]))
86 {
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))
91 $cond2=true;
92 } else $cond1=true;
93 # if both conditions are met, then SHOW else HIDE
94 if (($cond1===true) and ($cond2===true)){
95 $output=$parser->recursiveTagParse($input);
96 }
97
98 return $output;
99 }
100 ?>