From c63c9cdd06e7952146a9dded1a9981e2b2316f2a Mon Sep 17 00:00:00 2001
From: Mathieu Loiseau <mathieu.loiseau@liris.cnrs.fr>
Date: Tue, 10 May 2022 11:49:03 +0200
Subject: [PATCH] Documentation

---
 README.md         | 25 +++++++++++++------------
 extension.json    | 13 ++++++++++---
 i18n/en.json      |  7 +++++++
 i18n/fr.json      |  7 +++++++
 src/Connected.php | 34 ++++++++++++++++++++++++++++++++++
 5 files changed, 71 insertions(+), 15 deletions(-)
 create mode 100644 i18n/en.json
 create mode 100644 i18n/fr.json

diff --git a/README.md b/README.md
index 279c35e..98cecc6 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
+    Version 0.1
     Mathieu Loiseau
 
 This is free software licensed under the GNU General Public License. Please
@@ -15,18 +15,19 @@ 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, empty string 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.
 
-## Installation
-TODO
-To install the extension, place the entire 'Cargo' directory within
-your MediaWiki 'extensions' directory, then add the following line to
-your 'LocalSettings.php' file:
-
-     wfLoadExtension( 'Cargo' );
+The extension hides the sidebar for not logged in users.
 
-Then, go to core MediaWiki's /maintenance directory, and call the
-following:
+It also provides two css depending on the status of the user :
+* ``MediaWiki:NotLoggedIn.css``
+* ``MediaWiki:LoggedIn.css``
 
-     php update.php
+## Installation
+To install the extension, place the entire 'Connected' directory within
+your MediaWiki `extensions` directory, then add the following line to
+your `LocalSettings.php` file:
+```php
+     wfLoadExtension( 'Connected' );
+```
diff --git a/extension.json b/extension.json
index 197fc30..8cdba58 100644
--- a/extension.json
+++ b/extension.json
@@ -1,11 +1,16 @@
 {
 	"name": "Connected",
 	"author": "Mathieu Loiseau",
-	"version": "0.0",
+	"version": "0.1",
 	"url": "https://gitlab.liris.cnrs.fr/mloiseau/mw_connected",
-	"descriptionmsg": "A small mediawiki extension to have a variable to know if the user is logged in or not.",
 	"license-name": "GPL-2.0-or-later",
+	"descriptionmsg": "connected-desc",
 	"type": "parserhook",
+	"MessagesDirs": {
+		"Connected": [
+			"i18n"
+		]
+	},
 	"AutoloadClasses": {
 		"ConnectedHooks": "src/Connected.php"
 	},
@@ -15,7 +20,9 @@
 	"Hooks": {
 		"ParserFirstCallInit": "ConnectedHooks::onParserFirstCallInit",
 		"ParserGetVariableValueSwitch": "ConnectedHooks::wfConnectedAssignValue",
-		"MagicWordwgVariableIDs": "ConnectedHooks::connectedDeclareVarIds"
+		"MagicWordwgVariableIDs": "ConnectedHooks::connectedDeclareVarIds",
+		"SkinBuildSidebar": "ConnectedHooks::lfHideSidebar",
+		"BeforePageDisplay": "ConnectedHooks::add_NotLoggedIncss"
 	},
 	"manifest_version": 1
 }
diff --git a/i18n/en.json b/i18n/en.json
new file mode 100644
index 0000000..278e45b
--- /dev/null
+++ b/i18n/en.json
@@ -0,0 +1,7 @@
+{"@metadata": {
+		"authors": [
+			"Loizbek"
+		]
+	},
+	"connected-desc": "Small mediawiki to have a different view for logged-in users.\n*Hides the side-bar for external users.\n* Provides differentiated css [[MediaWiki:NotLoggedIn.css]] and [[MediaWiki:LoggedIn.css]] loaded accordingly.\n*Also provides <code><nowiki>{{LOGGEDIN}}</nowiki></code>, <code><nowiki>{{USERNAME}}</nowiki></code> [https://www.mediawiki.org/wiki/Help:Magic_words/en magic words] and <code><nowiki>{{#ifloggedin:yes|no}}</nowiki></code> [https://www.mediawiki.org/wiki/Manual:Parser_functions/en parser function]."
+}
diff --git a/i18n/fr.json b/i18n/fr.json
new file mode 100644
index 0000000..0e0eb58
--- /dev/null
+++ b/i18n/fr.json
@@ -0,0 +1,7 @@
+{"@metadata": {
+		"authors": [
+			"Loizbek"
+		]
+	},
+	"connected-desc": "Petite extension mediawiki pour un affichage différent selon que l'utilisateur est connecté ou non.\n* Supprime la barre latérale pour les utilisateurs non connectés ;\n* Fournit des css différentiées [[MediaWiki:NotLoggedIn.css]] et [[MediaWiki:LoggedIn.css]].\n* Fournit les ''[https://www.mediawiki.org/wiki/Help:Magic_words/fr mots magiques]'' <code><nowiki>{{LOGGEDIN}}</nowiki></code>, <code><nowiki>{{USERNAME}}</nowiki></code> et la ''[https://www.mediawiki.org/wiki/Manual:Parser_functions/fr fonction d'analyse]'' <code><nowiki>{{#ifloggedin:yes|no}}</nowiki></code>."
+}
diff --git a/src/Connected.php b/src/Connected.php
index 9cd1de7..4b5fe6f 100755
--- a/src/Connected.php
+++ b/src/Connected.php
@@ -68,4 +68,38 @@ class ConnectedHooks {
 		$customVariableIds[] = 'connected';
 		$customVariableIds[] = 'u_name';
 	}
+
+	#Cacher la barre de menu quand on n'est pas logué et charger une css supplémentaire
+//https://www.mediawiki.org/wiki/Manual:Interface/Sidebar#Change_sidebar_content_when_logged_in_(PHP)
+//https://www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
+	public static function lfHideSidebar( $skin, &$bar ) {
+		global $wgUser;
+		// Hide sidebar for anonymous users
+		if ( !$wgUser->isLoggedIn() ) {
+			$bar = array(
+				'navigation' => array(
+					array(
+						'text'   => wfMessage( 'login' ) -> text(),
+						'href'   => SpecialPage::getTitleFor( 'Login' )->getLocalURL(),
+						'id'     => 'n-login',
+						'active' => ''
+					)
+				)
+			);
+		}
+		return true;
+	}
+
+	#Ajouter une feuille de style quand pas connecté
+	public static function add_NotLoggedIncss( OutputPage $out, Skin $skin ) {
+		global $wgUser;
+		if ( !$wgUser->isLoggedIn() ) {
+			$out->addStyle('/w/index.php?title=MediaWiki:NotLoggedIn.css&action=raw&ctype=text/css');
+			$out->addMeta( "logged", "no" );
+		}
+		else{
+			$out->addStyle('/w/index.php?title=MediaWiki:LoggedIn.css&action=raw&ctype=text/css');
+			$out->addMeta( "logged", "yes" );
+		}
+	}
 }
-- 
GitLab