Robert Vogel has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/349924 )

Change subject: [PoC] PSR-4 autoloader and hook handler classes
......................................................................

[PoC] PSR-4 autoloader and hook handler classes

This change is only to show how a new way of implementing hook handlers in
BlueSpice could look like.

TODO:
* Add examples for unit tests
* Profile performance impact

Change-Id: I6b75430ee4bdd5aa9b225dafa7dd46bedef61f40
---
M composer.json
M extension.json
M includes/CoreHooks.php
A src/GlobalVarConfig.php
A src/Hooks/Hook.php
A src/Hooks/LinkEnd.php
A src/Hooks/LinkEnd/AddDataTitle.php
A src/Hooks/LinkEnd/AddDataUserName.php
A src/Hooks/SoftwareInfo.php
A src/Hooks/SoftwareInfo/AddBlueSpice.php
10 files changed, 249 insertions(+), 47 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/BlueSpiceFoundation 
refs/changes/24/349924/1

diff --git a/composer.json b/composer.json
index 0a6f1f4..5c60740 100644
--- a/composer.json
+++ b/composer.json
@@ -29,5 +29,11 @@
        "support": {
                "issues": "https://sourceforge.net/projects/bluespice/support";,
                "wiki": "https://help.bluespice.com";
+       },
+       "autoload" : {
+               "psr-4": {
+                       "BlueSpice\\Tests\\" : "tests/phpunit",
+                       "BlueSpice\\" : "src"
+               }
        }
 }
diff --git a/extension.json b/extension.json
index 12e9ff0..f03a5dc 100644
--- a/extension.json
+++ b/extension.json
@@ -391,9 +391,12 @@
        },
        "Hooks": {
                "SetupAfterCache": "BsCoreHooks::onSetupAfterCache",
-               "SoftwareInfo": "BsCoreHooks::onSoftwareInfo",
+               "SoftwareInfo": 
"BlueSpice\\Hooks\\SoftwareInfo\\AddBlueSpice::handle",
                "BeforePageDisplay": "BsCoreHooks::onBeforePageDisplay",
-               "LinkEnd": "BsCoreHooks::onLinkEnd",
+               "LinkEnd": [
+                       "BlueSpice\\Hooks\\LinkEnd\\AddDataTitle::handle",
+                       "BlueSpice\\Hooks\\LinkEnd\\AddDataUserName::handle"
+               ],
                "LinkerMakeMediaLinkFile": 
"BsCoreHooks::onLinkerMakeMediaLinkFile",
                "MakeGlobalVariablesScript": 
"BsCoreHooks::onMakeGlobalVariablesScript",
                "LoadExtensionSchemaUpdates": 
"BsCoreHooks::onLoadExtensionSchemaUpdates",
@@ -424,6 +427,9 @@
                },
                "ConfigFiles": []
        },
+       "ConfigRegistry": {
+               "bsg": "BlueSpice\\GlobalVarConfig::newInstance"
+       },
        "AutoloadClasses": {
                "BsCore": "includes/Core.class.php",
                "BsCoreHooks": "includes/CoreHooks.php",
diff --git a/includes/CoreHooks.php b/includes/CoreHooks.php
index 26a4f6b..305b2ef 100755
--- a/includes/CoreHooks.php
+++ b/includes/CoreHooks.php
@@ -53,16 +53,6 @@
                return true;
        }
 
-       /**
-        * Called by Special:Version for returning information about the 
software
-        * @param Array $aSoftware: The array of software in format 'name' => 
'version'.
-        */
-       public static function onSoftwareInfo( &$aSoftware ) {
-               global $bsgBlueSpiceExtInfo;
-               $aSoftware['[http://bluespice.com/ ' . 
$bsgBlueSpiceExtInfo['name'] . '] ([' . SpecialPage::getTitleFor( 
'SpecialCredits' )->getFullURL() . ' Credits])'] = 
$bsgBlueSpiceExtInfo['version'];
-               return true;
-       }
-
        public static function setup() {
                HTMLForm::$typeMappings['staticimage'] = 
'HTMLStaticImageFieldOverride';
                HTMLForm::$typeMappings['link'] = 'HTMLInfoFieldOverride';
@@ -314,41 +304,6 @@
                }
                return true;
        }
-
-       /**
-        * Adds additional data to links generated by the framework. This 
allows us
-        * to add more functionality to the UI.
-        * @param SkinTemplate $skin
-        * @param Title $target
-        * @param array $options
-        * @param string $html
-        * @param array $attribs
-        * @param string $ret
-        * @return boolean Always true to keep hook running
-        */
-       public static function onLinkEnd( $skin, $target, $options, &$html, 
&$attribs, &$ret ) {
-               //We add the original title to a link. This may be the same 
content as
-               //"title" attribute, but it doesn't have to. I.e. in red links
-               $attribs['data-bs-title'] = $target->getPrefixedText();
-
-               if( $target->getNamespace() == NS_USER && $target->isSubpage() 
=== false ) {
-                       //Linker::userLink adds class "mw-userlink" by default
-                       /*if( !isset($attribs['class']) ) {
-                               $attribs['class'] = '';
-                       }
-                       $attribs['class'] .= ' user';*/
-                       if( $target->getText() == $html ) {
-                               $html = htmlspecialchars(
-                                       BsCore::getUserDisplayName(
-                                               
User::newFromName($target->getText())
-                                       )
-                               );
-                               $attribs['data-bs-username'] = 
$target->getText();
-                       }
-               }
-               return true;
-       }
-
 
        /**
         * Adds data attributes to media link tags
diff --git a/src/GlobalVarConfig.php b/src/GlobalVarConfig.php
new file mode 100644
index 0000000..b1fd94e
--- /dev/null
+++ b/src/GlobalVarConfig.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace BlueSpice;
+
+class GlobalVarConfig extends \GlobalVarConfig {
+       public static function newInstance() {
+               return new self( 'bsg' );
+       }
+}
diff --git a/src/Hooks/Hook.php b/src/Hooks/Hook.php
new file mode 100644
index 0000000..5687bda
--- /dev/null
+++ b/src/Hooks/Hook.php
@@ -0,0 +1,54 @@
+<?php
+namespace BlueSpice\Hooks;
+
+abstract class Hook {
+
+       /**
+        *
+        * @var \IContextSource
+        */
+       protected $context = null;
+
+       /**
+        *
+        * @var \Config
+        */
+       protected $config = null;
+
+
+       /**
+        *
+        * @param \IContextSource $context
+        * @param \Config $config
+        */
+       public function __construct( $context, $config ) {
+               $this->context = $context;
+               $this->config = $config;
+       }
+
+       /**
+        *
+        * @return \IContextSource
+        */
+       protected static function makeDefaultContext() {
+               return \RequestContext::getMain();
+       }
+
+       /**
+        *
+        * @var string
+        */
+       protected static $configName = 'main';
+
+       /**
+        * 
+        * @return \Config
+        */
+       protected static function makeDefaultConfig() {
+               return \ConfigFactory::getDefaultInstance()->makeConfig( 
static::$configName );
+       }
+
+       /* abstract static function handle(); Can not be abstract because hook 
signatures differ */
+
+       abstract public function process();
+}
\ No newline at end of file
diff --git a/src/Hooks/LinkEnd.php b/src/Hooks/LinkEnd.php
new file mode 100644
index 0000000..9dbcd23
--- /dev/null
+++ b/src/Hooks/LinkEnd.php
@@ -0,0 +1,90 @@
+<?php
+
+namespace BlueSpice\Hooks;
+
+abstract class LinkEnd extends Hook {
+
+       /**
+        *
+        * @var \DummyLinker
+        */
+       protected $dummy = null;
+
+       /**
+        *
+        * @var \Title
+        */
+       protected $target = null;
+
+       /**
+        *
+        * @var array
+        */
+       protected $options = [];
+
+       /**
+        *
+        * @var string
+        */
+       protected $html = '';
+
+       /**
+        *
+        * @var array
+        */
+       protected $attribs = [];
+
+       /**
+        *
+        * @var string
+        */
+       protected $ret  = '';
+
+       /**
+        * Adds additional data to links generated by the framework. This 
allows us
+        * to add more functionality to the UI.
+        * @param \DummyLinker $dummy
+        * @param \Title $target
+        * @param array $options
+        * @param string $html
+        * @param array $attribs
+        * @param string $ret
+        * @return boolean Always true to keep hook running
+        */
+       public static function handle( \DummyLinker $dummy, \Title $target, 
$options, &$html, &$attribs, &$ret  ) {
+               $className = static::class;
+               $hookHandler = new $className(
+                       static::makeDefaultContext(),
+                       static::makeDefaultConfig(),
+                       $dummy,
+                       $target,
+                       $options,
+                       $html,
+                       $attribs,
+                       $ret
+               );
+               return $hookHandler->process();
+       }
+
+       /**
+        *
+        * @param \IContextSource $context
+        * @param \Config $config
+        * @param \DummyLinker $dummy
+        * @param \Title $target
+        * @param array $options
+        * @param string $html
+        * @param array $attribs
+        * @param string $ret
+        */
+       public function __construct( $context, $config, \DummyLinker $dummy, 
\Title $target, $options, &$html, &$attribs, &$ret ) {
+               parent::__construct( $context, $config );
+
+               $this->dummy =& $dummy;
+               $this->target = $target;
+               $this->options = $options;
+               $this->html =& $html;
+               $this->attribs =& $attribs;
+               $this->ret =& $ret;
+       }
+}
\ No newline at end of file
diff --git a/src/Hooks/LinkEnd/AddDataTitle.php 
b/src/Hooks/LinkEnd/AddDataTitle.php
new file mode 100644
index 0000000..4e49be9
--- /dev/null
+++ b/src/Hooks/LinkEnd/AddDataTitle.php
@@ -0,0 +1,12 @@
+<?php
+
+namespace BlueSpice\Hooks\LinkEnd;
+
+class AddDataTitle extends \BlueSpice\Hooks\LinkEnd {
+       public function process() {
+               //We add the original title to a link. This may be the same 
content as
+               //"title" attribute, but it doesn't have to. I.e. in red links
+               $this->attribs['data-bs-title'] = 
$this->target->getPrefixedText();
+               return true;
+       }
+}
\ No newline at end of file
diff --git a/src/Hooks/LinkEnd/AddDataUserName.php 
b/src/Hooks/LinkEnd/AddDataUserName.php
new file mode 100644
index 0000000..849ed9d
--- /dev/null
+++ b/src/Hooks/LinkEnd/AddDataUserName.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace BlueSpice\Hooks\LinkEnd;
+
+class AddDataUserName extends \BlueSpice\Hooks\LinkEnd {
+       public function process() {
+               if( $this->target->getNamespace() !== NS_USER || 
$this->target->isSubpage() ) {
+                       return true;
+               }
+
+               if( $this->target->getText() == $this->html ) {
+                       $this->html = htmlspecialchars(
+                               \BsCore::getUserDisplayName(
+                                       \User::newFromName( 
$this->target->getText() )
+                               )
+                       );
+                       $this->attribs['data-bs-username'] = 
$this->target->getText();
+               }
+       }
+}
\ No newline at end of file
diff --git a/src/Hooks/SoftwareInfo.php b/src/Hooks/SoftwareInfo.php
new file mode 100644
index 0000000..78d73b3
--- /dev/null
+++ b/src/Hooks/SoftwareInfo.php
@@ -0,0 +1,34 @@
+<?php
+
+namespace BlueSpice\Hooks;
+
+abstract class SoftwareInfo extends Hook {
+
+       protected $softwareInfo = [];
+
+       /**
+        * Called by Special:Version for returning information about the 
software
+        * @param array $software
+        * @return boolean
+        */
+       public static function handle( &$software ) {
+               $className = static::class;
+               $hookHandler = new $className( 
+                       static::makeDefaultContext(),
+                       static::makeDefaultConfig(),
+                       $software
+               );
+               return $hookHandler->process();
+       }
+
+       /**
+        * @param \IContextSource $context
+        * @param \Config $config
+        * @param array $software
+        */
+       public function __construct( \IContextSource $context, \Config $config, 
&$software ) {
+               parent::__construct( $context, $config );
+
+               $this->softwareInfo =& $software;
+       }
+}
\ No newline at end of file
diff --git a/src/Hooks/SoftwareInfo/AddBlueSpice.php 
b/src/Hooks/SoftwareInfo/AddBlueSpice.php
new file mode 100644
index 0000000..0a5de72
--- /dev/null
+++ b/src/Hooks/SoftwareInfo/AddBlueSpice.php
@@ -0,0 +1,16 @@
+<?php
+
+namespace BlueSpice\Hooks\SoftwareInfo;
+
+class AddBlueSpice extends \BlueSpice\Hooks\SoftwareInfo {
+
+       protected static $configName = 'bsg';
+
+       public function process() {
+               $extInfo = $this->config->get( 'BlueSpiceExtInfo' );
+
+               $this->softwareInfo['[http://bluespice.com/ ' .  
$extInfo['name'] . '] ([' . \SpecialPage::getTitleFor( 'SpecialCredits' 
)->getFullURL() . ' Credits])'] = $extInfo['version'];
+               return true;
+       }
+}
+

-- 
To view, visit https://gerrit.wikimedia.org/r/349924
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6b75430ee4bdd5aa9b225dafa7dd46bedef61f40
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/BlueSpiceFoundation
Gerrit-Branch: master
Gerrit-Owner: Robert Vogel <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to