From c666fb76e4cd4c7519c5a8e4973481b7bbf36de7 Mon Sep 17 00:00:00 2001 From: Mathieu Loiseau <mathieu.loiseau@liris.cnrs.fr> Date: Fri, 6 May 2022 15:53:18 +0200 Subject: [PATCH] LOGGEDIN & USERNAME = OK --- Connected.i18n.magic.php | 8 ++++++ Connected.php | 59 ++++++++++++++++++++++++++++++++++++++++ README.md | 3 +- 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 Connected.i18n.magic.php create mode 100644 Connected.php diff --git a/Connected.i18n.magic.php b/Connected.i18n.magic.php new file mode 100644 index 0000000..4d5f911 --- /dev/null +++ b/Connected.i18n.magic.php @@ -0,0 +1,8 @@ +<?php +$magicWords = []; + +$magicWords['en'] = [ + 'connected' => [1, 'LOGGEDIN'], + 'u_name' => [1, 'USERNAME'], + 'iflogged' => [0, 'ifloggedin'] +]; diff --git a/Connected.php b/Connected.php new file mode 100644 index 0000000..259b650 --- /dev/null +++ b/Connected.php @@ -0,0 +1,59 @@ +<?php +/** + * Connected + * + * @license GPL + * @package Connected + * @link https://gitlab.liris.cnrs.fr/mloiseau/mw_connected + * + **/ + $wgExtensionMessagesFiles['Connected'] = __DIR__ . '/Connected.i18n.magic.php'; + + $wgHooks['ParserGetVariableValueSwitch'][] = 'wfConnectedAssignValue'; + function wfConnectedAssignValue( &$parser, &$cache, &$magicWordId, &$ret ) { + global $wgUser; + $result = false; + if($wgUser->isSafeToLoad()){ + if ( !$wgUser->isLoggedIn() ) { + switch($magicWordId){ + case 'connected' : + $ret = 0; + $result = true; + break; + case 'u_name': + $ret = $wgUser->getName(); + $result = true; + break; + } + } + else{ + switch($magicWordId){ + case 'connected' : + $ret = 1; + $result = true; + break; + case 'u_name': + $ret = $wgUser->getName(); + $result = true; + break; + } + } + } + return $result; +} + +$wgExtensionCredits['variable'][] = [ + 'name' => 'Connected', + 'author' => 'Mathieu Loiseau', + 'version' => '0', + 'description' => 'A small mediawiki extension to have a variable to know if the user is logged in or not.', + 'url' => 'https://gitlab.liris.cnrs.fr/mloiseau/mw_connected', + ]; + + $wgHooks['MagicWordwgVariableIDs'][] = 'connectedMyDeclareVarIds'; + function connectedMyDeclareVarIds( &$customVariableIds ) { + // $customVariableIds is where MediaWiki wants to store its list of custom + // variable IDs. We oblige by adding ours: + $customVariableIds[] = 'connected'; + $customVariableIds[] = 'u_name'; + } diff --git a/README.md b/README.md index 9b05dbd..279c35e 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,10 @@ full text and terms of the license. Connected is an extension to MediaWiki that provides elements to adapt the content of a page to the user (or lack thereof). The extension defines the following parser functions and variables: -* ``connected`` : ``1`` if the user is logged in, ``0`` otherwise ; +* ``LOGGEDIN`` : ``1`` if the user is logged in, ``0`` otherwise ; * ``USERNAME`` : the user's screen name, if they are logged in, empty string otherwise ; * ``#ifloggedin`` : displays the first parameter if the user is logged in, the second otherwise. + ## Installation TODO To install the extension, place the entire 'Cargo' directory within -- GitLab