Enhanced existing extension to conditionally show content based on user group membership.
authorJack D. Pond <jdpond@users.mediawiki.org>
Fri, 11 Sep 2009 02:30:48 +0000 (02:30 +0000)
committerJack D. Pond <jdpond@users.mediawiki.org>
Fri, 11 Sep 2009 02:30:48 +0000 (02:30 +0000)
extensions/ConditionalShowSection/ConditionalShowSection.php [new file with mode: 0644]
extensions/ConditionalShowSection/README [new file with mode: 0644]

diff --git a/extensions/ConditionalShowSection/ConditionalShowSection.php b/extensions/ConditionalShowSection/ConditionalShowSection.php
new file mode 100644 (file)
index 0000000..10bf502
--- /dev/null
@@ -0,0 +1,100 @@
+<?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/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'.
+ * 1.5: Allow to check multiple groups - separated by ","
+ */
+$wgExtensionCredits['other'][] = array(
+       'name'   => "ConditionalShowSection [http://www.bluecortex.com]",
+       'url'    => 'http://www.mediawiki.org/wiki/Extension:ConditionalShow',
+       'version'=> '1.5',
+       'author' => 'Jean-Lou Dupont [http://www.bluecortex.com]' 
+);
+
+if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
+       $wgHooks['ParserFirstCallInit'][] = 'wfConditionalShowSection';
+} else { // Otherwise do things the old fashioned way
+       $wgExtensionFunctions[] = 'wfConditionalShowSection';
+}
+
+function wfConditionalShowSection() {
+    global $wgParser;
+    $wgParser->setHook( "cshow", "ConditionalShowSection" );
+    return true;
+}
+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;
+       } else $cond1=true;
+       if (isset($argv["ingroup"]))
+       {
+               $userReqGroup  = explode(',',$argv["ingroup"]);
+               # which groups is the user part of?
+               $ugroups = $wgUser->getEffectiveGroups();  // changed in v1.4
+               if(array_intersect($userReqGroup,$ugroups))
+                       $cond2=true;
+       } else $cond1=true;
+       # if both conditions are met, then SHOW else HIDE
+       if (($cond1===true) and ($cond2===true)){
+               $output=$parser->recursiveTagParse($input);
+       }
+       return $output;
+}
+?>
\ No newline at end of file
diff --git a/extensions/ConditionalShowSection/README b/extensions/ConditionalShowSection/README
new file mode 100644 (file)
index 0000000..bf559f1
--- /dev/null
@@ -0,0 +1,126 @@
+/*\r
+ * ConditionalShowSection MediaWiki extension\r
+ *\r
+ * @author Jean-Lou Dupont\r
+ * @package MediaWiki\r
+ * @subpackage Extensions\r
+ * @licence GNU General Public Licence 2.0\r
+ * This extension enables to conditionally show/hide a section\r
+ * of wikitext that appears between the <cshow> </cshow> tags.\r
+ *\r
+ * Add to LocalSettings.php\r
+ * with: require_once("extensions/ConditionalShowSection/ConditionalShowSection.php");\r
+ *\r
+ * HISTORY:\r
+ * 1.1: corrected bug when "ingroup" option not present, wrong results.\r
+ * 1.2: used "recursiveTagParse" to get cleaner parsing.\r
+ * 1.3: corrected error with default initialisation of userReqLogged\r
+ * 1.4: changed to using 'getEffectiveGroups' in order to have also access\r
+ *      to the implicit '*' group and the default group for logged users 'user'.\r
+ * 1.5: Allow to check multiple groups - separated by ","\r
+ */\r
+\r
+\r
+\r
+{{Extension|templatemode =\r
+|name          = ConditionalShow\r
+|status        = stable\r
+|type1         = tag\r
+|type2         = user rights \r
+|type3         = mywiki \r
+|hook1         = ParserFirstCallInit\r
+|hook2         =\r
+|username      = [[user:jldupont]]\r
+|author        =\r
+|description   = Conditionally show a wikitext section based on user group rights\r
+|image         =\r
+|imagesize     =\r
+|version       = 1.5\r
+|update        = 2009-09-10\r
+|mediawiki     = tested on 1.8.2, 1.9.3, 1.10, 1.14, 1.15, 1.16A\r
+|php           =\r
+|license       = GNU General Public Licence 2.0\r
+|download      = [http://wiki.jldupont.com/Extension:ConditionalContent http version <1.5]\r
+|readme        =\r
+|changelog     =\r
+|parameters    =\r
+|tags          = <cshow>,</cshow>\r
+|rights        =\r
+|example       =\r
+|compatibility =\r
+}}\r
+\r
+==What can this extension do?==\r
+This extension implementes the <nowiki><cshow></nowiki> tag which conditionally shows wikitext within the tags based on user group rights and certain other parameters.\r
+\r
+Using the tags should be to make the user experience less confusing or more useful by only showing information relevant to the user and specific groups that user belongs to.\r
+<div style="width: 80%; margin-left: auto; margin-right: auto; padding: 4px; border: 2px solid #FF0000; background-color: #FFDDDD; text-align: center;">\r
+'''PLEASE NOTE!!!'''\r
+\r
+This tag does not protect information or instructions from being disclosed to the reader.\r
+\r
+The user can still see the information by editing the page or even by "view source".  If you are looking to actually protect information, this is '''NOT''' the extension you want to use!\r
+\r
+This extension only helps you selectively show content or navigation based on groups the user belongs to.\r
+</div>\r
+==Note==\r
+\r
+This extension is not 'cache' friendly; if one requires this feature, then [[Extension:BizzWiki|BizzWiki platform]] provides a solution through [[Extension:ParserPhase2|Parser Phase 2 extension]].\r
+\r
+==Usage==\r
+\r
+Like other tags, this can be used two ways - by using the tag itself within wikitext, or by using the tag function within templates.\r
+\r
+If you are using it within normal wikitext, the <nowiki><cshow>. . .</cshow></nowiki>  syntax suffices.  If you are using it within a template, you will need to use the parser tag function as illustrated [[#Example within a Template | below]].\r
+\r
+===Example in Wikitext===\r
+By default, the section of wikitext within the tags is HIDDEN unless the conditions are met - in this case the user must be logged in and belong the group 'sysops'\r
+\r
+<pre>\r
+<cshow logged=1 ingroup='sysop'> This text will appear if a user with membership to 'sysop' group views this page</cshow>\r
+</pre>\r
+\r
+===Example within a Template===\r
+\r
+If you are using this within a template or as part of a template, you need to use the <nowiki>{{##tag: . . .}}</nowiki> syntax.\r
+\r
+<pre>\r
+{{#tag:cshow |\r
+This text will appear if a user with membership to 'sysop' group views this page\r
+| logged=1 ingroup='sysop'\r
+}}\r
+</pre>\r
+\r
+==Arguments==\r
+\r
+By default, the tagged section is HIDDEN unless the following conditions are met:\r
+* Argument: logged = '1' or '0'  \r
+* Argument: ingroup = 'group XYZ'  (Note, as of version 1.5, you can list multiple groups, e.g. 'sysop,approved'\r
+\r
+If no arguments are provided for:\r
+logged   --> assume 'don't care' \r
+innGroup  --> assume ''           (no group)\r
+\r
+In other words, if no 'ingroup' parameter is given, then the condition 'ingroup' is never met.\r
+\r
+If no "logged" parameter is given, then this condition is always met.\r
+\r
+==Download instructions==\r
+\r
+The current version is available from SVN.\r
+\r
+Source Code and additional information can also be found at [http://bluecortex.com/index.php?title=Bluecortex:ConditionalShow].\r
+The code should be loaded to:\r
+\r
+<code>$IP/extensions/ConditionalShowSection/ConditionalShowSection.php</code>.  ''Note: [[Manual:$IP|$IP]] stands for the root directory of your MediaWiki installation, the same directory that holds [[Manual:LocalSettings.php|LocalSettings.php]]''.\r
+\r
+==Installation==\r
+To install this extension, add the following to [[Manual:LocalSettings.php|LocalSettings.php]]:\r
+<source lang="php">\r
+require_once("$IP/extensions/ConditionalShowSection/ConditionalShowSection.php");\r
+</source>\r
+\r
+== Other Options ==\r
+* [[Extension:ConditionalContent]]\r
+\r
+[[Category:View page extensions]]\r