Skip to content
Snippets Groups Projects
Commit d7531b15 authored by Mathieu Loiseau's avatar Mathieu Loiseau
Browse files

update 1.42

parent 31f02fc7
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,7 @@
A small mediawiki extension to have a variable to know if the user is connected or not.
Version 0.2
Version 0.3
Mathieu Loiseau
This is free software licensed under the GNU General Public License. Please
......
......@@ -18,7 +18,7 @@
"ConnectedHooks": "src/Connected.php"
},
"ExtensionMessagesFiles": {
"ExampleExtensionMagic": "Connected.i18n.magic.php"
"ConnectedMagic": "i18n/Connected.php"
},
"Hooks": {
"ParserFirstCallInit": "ConnectedHooks::onParserFirstCallInit",
......
......@@ -6,3 +6,9 @@ $magicWords['en'] = [
'u_name' => [1, 'USERNAME'],
'iflogged' => [0, 'ifloggedin']
];
$magicWords['fr'] = [
'connected' => [1, 'CONNECTE'],
'u_name' => [1, 'NOM'],
'iflogged' => [0, 'siconnecte']
];
......@@ -7,68 +7,54 @@
* @link https://gitlab.liris.cnrs.fr/mloiseau/mw_connected
*
**/
class ConnectedHooks {
private static $user = 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' ] );
}
//try to get the user anyway I can
//through user or parser or output or wgUser…
//and set it for next time
private static function get_user($parserOrOther=null){
if(self::$user === false){
$res = false;
if (!is_null($parserOrOther)){
if(get_class($parserOrOther) == "User"){
$res = $parserOrOther;
}
else if(get_class($parserOrOther) == "Parser"){
//cf. https://github.com/wikimedia/mediawiki-extensions-WikiTextLoggedInOut/blob/d764bd3972ed6b84a4af3ed423a3a5fd9b82c493/includes/WikiTextLoggedInOut.php#L39
if ( method_exists( $parserOrOther, 'getUserIdentity' ) ) {
// MW 1.36+
$res = $parserOrOther->getUserIdentity();
}
else {
if (method_exists( $parserOrOther, 'getUser' )){
$res = $parserOrOther->getUser();
}
}
}
else if(get_class($parserOrOther) == "OutputPage"){
$res = $parserOrOther->getUser();
}
}
self::$user = $res;
if (self::$user === false){
self::$user = RequestContext::getMain()->getUser();
}
else{
$res = self::$user;
}
return $res;
return self::$user;
}
// 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' ] );
private static function is_registered(){
return self::$user != false && self::$user->mId !=0;
}
// Render the output of {{#ifloggedin:}} i.e. iflogged function.
public static function ifConnectedRender( Parser $parser, $ifLoggedIn = '', $ifNot = '') {
// The input parameters are wikitext with templates expanded.
// The output should be wikitext too.
if (!self::get_user($parser)->isRegistered()) {
$output = $ifNot;
$parser->getOutput()->updateCacheExpiry( 0 );
self::get_user($parser);
if (self::is_registered()) {
$output = $ifLoggedIn;
}
else{
$output = $ifLoggedIn;
$output = $ifNot;
}
return [ $output, 'noparse' => false ];
return [$output, 'noparse' => false ];
}
//calcul des variables
public static function wfConnectedAssignValue( Parser $parser, &$cache, $magicWordId, &$ret, $frame) {
$result = false;
self::get_user();
$parser->getOutput()->updateCacheExpiry( 0 );
switch($magicWordId){
case 'connected' :
if (!self::get_user($parser)->isRegistered()) {
if (!self::is_registered()) {
$ret = 0;
}
else{
......@@ -77,15 +63,8 @@ class ConnectedHooks {
$result = true;
break;
case 'u_name':
if (!self::get_user($parser)->isRegistered()) {
//$ret = self::$theUser->getName();
$ret = self::get_user($parser)->getName();
}
else{
//$ret = self::$theUser->getName();
$ret = self::get_user($parser)->getName();
}
$resutl = true;
$ret = self::$user->mName;
$result = true;
break;
}
return $result;
......@@ -104,7 +83,8 @@ class ConnectedHooks {
//https://www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
public static function lfHideSidebar( $skin, &$bar ) {
// Hide sidebar for anonymous users
if ( !self::get_user()->isRegistered() ) {
$user = self::get_user();
if (!self::is_registered()) {
$bar = array(
'navigation' => array(
array(
......@@ -121,7 +101,8 @@ class ConnectedHooks {
#Ajouter une feuille de style quand pas connecté
public static function add_NotLoggedIncss( OutputPage $out, Skin $skin ) {
if ( !self::get_user($out)->isRegistered() ) {
$user = self::get_user($out);
if (!self::is_registered()) {
$out->addStyle('/w/index.php?title=MediaWiki:NotLoggedIn.css&action=raw&ctype=text/css');
$out->addMeta( "logged", "no" );
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment