diff --git a/README.md b/README.md index 4dcdcb5380c8c4ced2afc9e2e6d923dcb0645aec..d3a901ed0f0fda396adc0a5338fb5b353ce68ab0 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ A small mediawiki extension to have a variable to know if the user is connected or not. - Version 0.3 + Version 0.4 Mathieu Loiseau This is free software licensed under the GNU General Public License. Please @@ -16,7 +16,7 @@ Connected is an extension to MediaWiki that provides elements to adapt the conte The extension defines the following parser functions and variables: * ``LOGGEDIN`` : ``1`` if the user is logged in, ``0`` otherwise ; * ``USERNAME`` : the user's screen name, if they are logged in, their ip address otherwise ; -* ``#ifloggedin`` : displays the first parameter if the user is logged in, the second otherwise. +* ``#ifloggedin`` : displays the first parameter if the user is logged in, the second otherwise. If a third parameter is provided, it can specify that this text should not be included in a div (which is default). The extension hides the sidebar for not logged in users. @@ -31,3 +31,6 @@ your `LocalSettings.php` file: ```php wfLoadExtension( 'Connected' ); ``` +## Configuration +* `$wgConnectedPrivateCSSClass` : css class for loggedIn content +* `$wgConnectedPublicCSSClass` : css class for not loggedIn content diff --git a/extension.json b/extension.json index 22a6042c112e4a068d194c52ee859c95302e8b7f..78d900a20f4a9b9e817607900d7a7e3deee19362 100644 --- a/extension.json +++ b/extension.json @@ -1,7 +1,7 @@ { "name": "Connected", "author": "Mathieu Loiseau", - "version": "0.2", + "version": "0.4", "url": "https://gitlab.liris.cnrs.fr/mloiseau/mw_connected", "license-name": "GPL-2.0-or-later", "descriptionmsg": "connected-desc", @@ -28,5 +28,18 @@ "SidebarBeforeOutput": "ConnectedHooks::lfHideSidebar", "BeforePageDisplay": "ConnectedHooks::add_NotLoggedIncss" }, - "manifest_version": 1 + "config": { + "ConnectedPrivateCSSClass": { + "value": "", + "description": "If not empty, puts every ifLoggedIn wiki code in a div with the said class (unless notDiv parameter is provided)." + }, + "ConnectedPublicCSSClass": { + "value": "", + "description": "If not empty, puts every ifNotLoggedIn wiki code in a div with the said class (unless notDiv parameter is provided)." + } + }, + "ConfigRegistry": { + "Connected": "GlobalVarConfig::newInstance" + }, + "manifest_version": 2 } diff --git a/src/Connected.php b/src/Connected.php index 9cce7b632846cd902d5460051712660cd819851b..fea83644900a062efdd00147a1199cf8e206eb34 100755 --- a/src/Connected.php +++ b/src/Connected.php @@ -7,14 +7,22 @@ * @link https://gitlab.liris.cnrs.fr/mloiseau/mw_connected * **/ + use MediaWiki\MediaWikiServices; class ConnectedHooks { private static $user = false; + private static $privateCSSClass = false; + private static $publicCSSClass = false; // Register any render callbacks with the parser public static function onParserFirstCallInit( Parser $parser ) { // Create a function hook associating the "example" magic word with renderExample() $parser->setFunctionHook( 'iflogged', [ self::class, 'ifConnectedRender' ] ); + // Get the $wgConnectedPrivateCSSClass configured value + if (self::$privateCSSClass === false){ + self::$privateCSSClass = MediaWikiServices::getInstance()->getMainConfig()->get( 'ConnectedPrivateCSSClass' ); + self::$publicCSSClass = MediaWikiServices::getInstance()->getMainConfig()->get( 'ConnectedPublicCSSClass' ); + } } //try to get the user anyway I can @@ -32,17 +40,26 @@ class ConnectedHooks { } // Render the output of {{#ifloggedin:}} i.e. iflogged function. - public static function ifConnectedRender( Parser $parser, $ifLoggedIn = '', $ifNot = '') { + //isDiv is tested to see if it is a div with a class… + public static function ifConnectedRender( Parser $parser, $ifLoggedIn = '', $ifNot = '', $isDiv='') { // The input parameters are wikitext with templates expanded. // The output should be wikitext too. $parser->getOutput()->updateCacheExpiry( 0 ); self::get_user($parser); + + if (self::is_registered()) { $output = $ifLoggedIn; + if (($isDiv == '') && (self::$privateCSSClass != '')){ + $output = "<div class='".self::$privateCSSClass."'>".$output."</div>"; + } } else{ $output = $ifNot; + if (($isDiv == '') && (self::$publicCSSClass != '')){ + $output = "<div class='".self::$publicCSSClass."'>".$output."</div>"; + } } return [$output, 'noparse' => false ]; }