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

Not working version

parent c63c9cdd
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,9 @@ ...@@ -6,6 +6,9 @@
"license-name": "GPL-2.0-or-later", "license-name": "GPL-2.0-or-later",
"descriptionmsg": "connected-desc", "descriptionmsg": "connected-desc",
"type": "parserhook", "type": "parserhook",
"requires": {
"MediaWiki": ">= 1.34.0"
},
"MessagesDirs": { "MessagesDirs": {
"Connected": [ "Connected": [
"i18n" "i18n"
...@@ -19,6 +22,8 @@ ...@@ -19,6 +22,8 @@
}, },
"Hooks": { "Hooks": {
"ParserFirstCallInit": "ConnectedHooks::onParserFirstCallInit", "ParserFirstCallInit": "ConnectedHooks::onParserFirstCallInit",
"ParserOptionsRegister": "ConnectedHooks::onParserOptionsRegister",
"onPageRenderingHash": "ConnectedHooks::setParserOptions",
"ParserGetVariableValueSwitch": "ConnectedHooks::wfConnectedAssignValue", "ParserGetVariableValueSwitch": "ConnectedHooks::wfConnectedAssignValue",
"MagicWordwgVariableIDs": "ConnectedHooks::connectedDeclareVarIds", "MagicWordwgVariableIDs": "ConnectedHooks::connectedDeclareVarIds",
"SkinBuildSidebar": "ConnectedHooks::lfHideSidebar", "SkinBuildSidebar": "ConnectedHooks::lfHideSidebar",
......
...@@ -12,51 +12,65 @@ class ConnectedHooks { ...@@ -12,51 +12,65 @@ class ConnectedHooks {
public static function onParserFirstCallInit( Parser $parser ) { public static function onParserFirstCallInit( Parser $parser ) {
// Create a function hook associating the "example" magic word with renderExample() // Create a function hook associating the "example" magic word with renderExample()
$parser->setFunctionHook( 'iflogged', [ self::class, 'ifConnectedRender' ] ); $parser->setFunctionHook( 'iflogged', [ self::class, 'ifConnectedRender' ] );
// TODO: Le getuser ne marche pas car $parser est null ne marche pas ici à voir comment on fait
//cf. https://github.com/wikimedia/mediawiki-extensions-WikiTextLoggedInOut/blob/d764bd3972ed6b84a4af3ed423a3a5fd9b82c493/includes/WikiTextLoggedInOut.php#L39
if ( method_exists( $parser, 'getUserIdentity' ) ) {
// MW 1.36+
$res = $parser->getUserIdentity()->isRegistered();
} else {
if (method_exists( $parser, 'getUser' )){
$res = $parser->getUser()->isRegistered();
}
else{
$res = '';
}
}
$parser->getOptions()->setOption( 'connected', string($res) );
}
public static function onParserOptionsRegister( &$defaults, &$inCacheKey, &$lazyOptions ) {
$inCacheKey['connected'] = true;
$defaults['connected'] = '';
return true;
} }
// Render the output of {{#ifloggedin:}} i.e. iflogged function. // Render the output of {{#ifloggedin:}} i.e. iflogged function.
public static function ifConnectedRender( Parser $parser, $ifLoggedIn = '', $ifNot = '') { public static function ifConnectedRender( Parser $parser, $ifLoggedIn = '', $ifNot = '') {
// The input parameters are wikitext with templates expanded. // The input parameters are wikitext with templates expanded.
// The output should be wikitext too. // The output should be wikitext too.
global $wgUser; if ( $parser->getOptions()->getOption('connected') == string(true)) {
if($wgUser->isSafeToLoad()){ $output = $ifLoggedIn;
if ( $wgUser->isLoggedIn() ) { }
$output = $ifLoggedIn; else{
} $output = $ifNot;
else{
$output = $ifNot;
}
} }
return [ $output, 'noparse' => false ]; return [ $output, 'noparse' => false ];
} }
//calcul des variables //calcul des variables
public static function wfConnectedAssignValue( Parser $parser, &$cache, $magicWordId, &$ret, $frame) { public static function wfConnectedAssignValue( Parser $parser, &$cache, $magicWordId, &$ret, $frame) {
global $wgUser;
$result = false; $result = false;
if($wgUser->isSafeToLoad()){ switch($magicWordId){
switch($magicWordId){ case 'connected' :
case 'connected' : if ( $parser->getOptions()->getOption('connected') == string(false)) {
if ( !$wgUser->isLoggedIn() ) { $ret = 0;
$ret = 0; $result = true;
$result = true; }
} else{
else{ $ret = 1;
$ret = 1; $result = true;
$result = true; }
} break;
break; case 'u_name':
case 'u_name': if ( $parser->getOptions()->getOption('connected') == string(false)) {
if ( !$wgUser->isLoggedIn() ) { $ret = $wgUser->getName();
$ret = $wgUser->getName(); $result = true;
$result = true; }
} else{
else{ $ret = $wgUser->getName();
$ret = $wgUser->getName(); $result = true;
$result = true; }
} break;
break;
}
} }
return $result; return $result;
} }
...@@ -73,9 +87,8 @@ class ConnectedHooks { ...@@ -73,9 +87,8 @@ class ConnectedHooks {
//https://www.mediawiki.org/wiki/Manual:Interface/Sidebar#Change_sidebar_content_when_logged_in_(PHP) //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 //https://www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
public static function lfHideSidebar( $skin, &$bar ) { public static function lfHideSidebar( $skin, &$bar ) {
global $wgUser;
// Hide sidebar for anonymous users // Hide sidebar for anonymous users
if ( !$wgUser->isLoggedIn() ) { if ( $parser->getOptions()->getOption('connected') == string(false)) {
$bar = array( $bar = array(
'navigation' => array( 'navigation' => array(
array( array(
...@@ -92,8 +105,7 @@ class ConnectedHooks { ...@@ -92,8 +105,7 @@ class ConnectedHooks {
#Ajouter une feuille de style quand pas connecté #Ajouter une feuille de style quand pas connecté
public static function add_NotLoggedIncss( OutputPage $out, Skin $skin ) { public static function add_NotLoggedIncss( OutputPage $out, Skin $skin ) {
global $wgUser; if ( $parser->getOptions()->getOption('connected') == string(false)) {
if ( !$wgUser->isLoggedIn() ) {
$out->addStyle('/w/index.php?title=MediaWiki:NotLoggedIn.css&action=raw&ctype=text/css'); $out->addStyle('/w/index.php?title=MediaWiki:NotLoggedIn.css&action=raw&ctype=text/css');
$out->addMeta( "logged", "no" ); $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