r56166 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r56165‎ | r56166 | r56167 >
Date:02:30, 11 September 2009
Author:jdpond
Status:deferred
Tags:
Comment:
Enhanced existing extension to conditionally show content based on user group membership.
Modified paths:
  • /trunk/phase3/extensions/ConditionalShowSection (added) (history)
  • /trunk/phase3/extensions/ConditionalShowSection/ConditionalShowSection.php (added) (history)
  • /trunk/phase3/extensions/ConditionalShowSection/README (added) (history)

Diff [purge]

Index: trunk/phase3/extensions/ConditionalShowSection/ConditionalShowSection.php
@@ -0,0 +1,100 @@
 2+<?php
 3+/*
 4+ * ConditionalShowSection MediaWiki extension
 5+ *
 6+ * @author Jean-Lou Dupont
 7+ * @package MediaWiki
 8+ * @subpackage Extensions
 9+ * @licence GNU General Public Licence 2.0
 10+ * This extension enables to conditionally show/hide a section
 11+ * of wikitext that appears between the <cshow> </cshow> tags.
 12+ *
 13+ * Add to LocalSettings.php
 14+ * with: require_once("extensions/ConditionalShowSection/ConditionalShowSection.php");
 15+ *
 16+ * HISTORY:
 17+ * 1.1: corrected bug when "ingroup" option not present, wrong results.
 18+ * 1.2: used "recursiveTagParse" to get cleaner parsing.
 19+ * 1.3: corrected error with default initialisation of userReqLogged
 20+ * 1.4: changed to using 'getEffectiveGroups' in order to have also access
 21+ * to the implicit '*' group and the default group for logged users 'user'.
 22+ * 1.5: Allow to check multiple groups - separated by ","
 23+ */
 24+$wgExtensionCredits['other'][] = array(
 25+ 'name' => "ConditionalShowSection [http://www.bluecortex.com]",
 26+ 'url' => 'http://www.mediawiki.org/wiki/Extension:ConditionalShow',
 27+ 'version'=> '1.5',
 28+ 'author' => 'Jean-Lou Dupont [http://www.bluecortex.com]'
 29+);
 30+
 31+
 32+if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
 33+ $wgHooks['ParserFirstCallInit'][] = 'wfConditionalShowSection';
 34+} else { // Otherwise do things the old fashioned way
 35+ $wgExtensionFunctions[] = 'wfConditionalShowSection';
 36+}
 37+
 38+function wfConditionalShowSection() {
 39+ global $wgParser;
 40+ $wgParser->setHook( "cshow", "ConditionalShowSection" );
 41+ return true;
 42+}
 43+
 44+function ConditionalShowSection( $input, $argv, &$parser ) {
 45+ #
 46+ # By default, the section is HIDDEN unless the following conditions are met:
 47+ # Argument: Logged = '1' or '0'
 48+ # Argument: InGroup = 'group XYZ'
 49+ #
 50+ # If no arguments are provided for:
 51+ # - Logged --> assume 'don't care'
 52+ # - InGroup --> assume '' (no group)
 53+ #
 54+ # In other words, if no 'ingroup' parameter is given,
 55+ # then the condition 'ingroup' is never met.
 56+ #
 57+ # If no "logged" parameter is given, then this condition is always met.
 58+ #
 59+ # Examples: <cshow Logged="1" InGroup="sysop"> text to show upon conditions met </cshow>
 60+ # if the user viewing the page is LOGGED and part of the SYSOP group then show the section.
 61+ #
 62+ # <cshow ingroup='user'> Test </cshow>
 63+ # shows 'Test' if the user viewing the page is logged and by default part of the 'user' group.
 64+ #
 65+ global $wgUser;
 66+
 67+ $userReqLogged = null; # default is "don't care"
 68+ $userReqGroup = "" ; # assuming no group membership required
 69+ $output = ""; # assuming the section is hidden by default.
 70+
 71+ $cond1 = false;
 72+ $cond2 = false; # by default, don't show the section to just anybody
 73+
 74+ # Extract the parameters passed
 75+ # the parser lowers the case of all the parameters passed...
 76+ if (isset($argv["logged"]))
 77+ {
 78+ $userReqLogged = $argv["logged"];
 79+
 80+ if ($userReqLogged==="1" && ($wgUser->isLoggedIn()===true))
 81+ $cond1=true;
 82+
 83+ if ($userReqLogged==="0" && ($wgUser->isLoggedIn()===false))
 84+ $cond1=true;
 85+ } else $cond1=true;
 86+ if (isset($argv["ingroup"]))
 87+ {
 88+ $userReqGroup = explode(',',$argv["ingroup"]);
 89+ # which groups is the user part of?
 90+ $ugroups = $wgUser->getEffectiveGroups(); // changed in v1.4
 91+ if(array_intersect($userReqGroup,$ugroups))
 92+ $cond2=true;
 93+ } else $cond1=true;
 94+ # if both conditions are met, then SHOW else HIDE
 95+ if (($cond1===true) and ($cond2===true)){
 96+ $output=$parser->recursiveTagParse($input);
 97+ }
 98+
 99+ return $output;
 100+}
 101+?>
\ No newline at end of file
Property changes on: trunk/phase3/extensions/ConditionalShowSection/ConditionalShowSection.php
___________________________________________________________________
Name: svn:eol-style
1102 + native
Index: trunk/phase3/extensions/ConditionalShowSection/README
@@ -0,0 +1,126 @@
 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+
 24+
 25+
 26+{{Extension|templatemode =
 27+|name = ConditionalShow
 28+|status = stable
 29+|type1 = tag
 30+|type2 = user rights
 31+|type3 = mywiki
 32+|hook1 = ParserFirstCallInit
 33+|hook2 =
 34+|username = [[user:jldupont]]
 35+|author =
 36+|description = Conditionally show a wikitext section based on user group rights
 37+|image =
 38+|imagesize =
 39+|version = 1.5
 40+|update = 2009-09-10
 41+|mediawiki = tested on 1.8.2, 1.9.3, 1.10, 1.14, 1.15, 1.16A
 42+|php =
 43+|license = GNU General Public Licence 2.0
 44+|download = [http://wiki.jldupont.com/Extension:ConditionalContent http version <1.5]
 45+|readme =
 46+|changelog =
 47+|parameters =
 48+|tags = <cshow>,</cshow>
 49+|rights =
 50+|example =
 51+|compatibility =
 52+}}
 53+
 54+==What can this extension do?==
 55+This extension implementes the <nowiki><cshow></nowiki> tag which conditionally shows wikitext within the tags based on user group rights and certain other parameters.
 56+
 57+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.
 58+<div style="width: 80%; margin-left: auto; margin-right: auto; padding: 4px; border: 2px solid #FF0000; background-color: #FFDDDD; text-align: center;">
 59+'''PLEASE NOTE!!!'''
 60+
 61+This tag does not protect information or instructions from being disclosed to the reader.
 62+
 63+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!
 64+
 65+This extension only helps you selectively show content or navigation based on groups the user belongs to.
 66+</div>
 67+==Note==
 68+
 69+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]].
 70+
 71+==Usage==
 72+
 73+Like other tags, this can be used two ways - by using the tag itself within wikitext, or by using the tag function within templates.
 74+
 75+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]].
 76+
 77+===Example in Wikitext===
 78+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'
 79+
 80+<pre>
 81+<cshow logged=1 ingroup='sysop'> This text will appear if a user with membership to 'sysop' group views this page</cshow>
 82+</pre>
 83+
 84+===Example within a Template===
 85+
 86+If you are using this within a template or as part of a template, you need to use the <nowiki>{{##tag: . . .}}</nowiki> syntax.
 87+
 88+<pre>
 89+{{#tag:cshow |
 90+This text will appear if a user with membership to 'sysop' group views this page
 91+| logged=1 ingroup='sysop'
 92+}}
 93+</pre>
 94+
 95+==Arguments==
 96+
 97+By default, the tagged section is HIDDEN unless the following conditions are met:
 98+* Argument: logged = '1' or '0'
 99+* Argument: ingroup = 'group XYZ' (Note, as of version 1.5, you can list multiple groups, e.g. 'sysop,approved'
 100+
 101+If no arguments are provided for:
 102+logged --> assume 'don't care'
 103+innGroup --> assume '' (no group)
 104+
 105+In other words, if no 'ingroup' parameter is given, then the condition 'ingroup' is never met.
 106+
 107+If no "logged" parameter is given, then this condition is always met.
 108+
 109+==Download instructions==
 110+
 111+The current version is available from SVN.
 112+
 113+Source Code and additional information can also be found at [http://bluecortex.com/index.php?title=Bluecortex:ConditionalShow].
 114+The code should be loaded to:
 115+
 116+<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]]''.
 117+
 118+==Installation==
 119+To install this extension, add the following to [[Manual:LocalSettings.php|LocalSettings.php]]:
 120+<source lang="php">
 121+require_once("$IP/extensions/ConditionalShowSection/ConditionalShowSection.php");
 122+</source>
 123+
 124+== Other Options ==
 125+* [[Extension:ConditionalContent]]
 126+
 127+[[Category:View page extensions]]

Status & tagging log