Addshore has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/311428

Change subject: Add CognateStore to MediawikiServices
......................................................................

Add CognateStore to MediawikiServices

Change-Id: I05001f16a56a1cc6af66b3d4f70cde9c0d2ae210
---
M extension.json
M src/CognateHooks.php
A src/ServiceWiring.php
A tests/phpunit/ServiceWiringTest.php
4 files changed, 55 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Cognate 
refs/changes/28/311428/1

diff --git a/extension.json b/extension.json
index c563b37..94e789e 100644
--- a/extension.json
+++ b/extension.json
@@ -28,5 +28,6 @@
       "i18n"
     ]
   },
+  "callback": "CognateHooks::onRegistration",
   "manifest_version": 1
 }
\ No newline at end of file
diff --git a/src/CognateHooks.php b/src/CognateHooks.php
index 419743d..bc06317 100644
--- a/src/CognateHooks.php
+++ b/src/CognateHooks.php
@@ -2,6 +2,8 @@
 
 // TODO Make this class testable by creating non-static public methods for 
each hook and using an instance in the static methods, see 
https://git.wikimedia.org/blob/mediawiki%2Fextensions%2FWikibase/master/client%2FWikibaseClient.hooks.php
 // TODO Split into two hook handler classes, one handling page-related stuff, 
the other handling database update and tests
+use MediaWiki\MediaWikiServices;
+
 class CognateHooks {
 
        /**
@@ -35,13 +37,13 @@
                Status $status,
                $baseRevId
        ) {
-               global $wgCognateWiki, $wgCognateNamespaces, $wgLanguageCode;
+               global $wgCognateNamespaces, $wgLanguageCode;
 
                $title = $article->getTitle();
                if ( !in_array( $title->getNamespace(), $wgCognateNamespaces ) 
) {
                        return true;
                }
-               $interlanguage = new CognateStore( wfGetLB(), $wgCognateWiki );
+               $interlanguage = MediaWikiServices::getInstance()->getService( 
'CognateStore' );
                $interlanguage->savePage( $wgLanguageCode, 
$article->getTitle()->getDBkey() );
 
                return true;
@@ -65,13 +67,13 @@
         * @return bool
         */
        public static function onLanguageLinks( $title, &$links, &$linkFlags ) {
-               global $wgCognateWiki, $wgCognateNamespaces, $wgLanguageCode;
+               global $wgCognateNamespaces, $wgLanguageCode;
 
                if ( !in_array( $title->getNamespace(), $wgCognateNamespaces ) 
) {
                        return true;
                }
 
-               $interlanguage = new CognateStore( wfGetLB(), $wgCognateWiki );
+               $interlanguage = MediaWikiServices::getInstance()->getService( 
'CognateStore' );
                $dbKey = $title->getDBkey();
                $languages = $interlanguage->getTranslationsForPage( 
$wgLanguageCode, $dbKey );
 
@@ -115,4 +117,9 @@
                return true;
        }
 
+       public static function onRegistration() {
+               global $wgServiceWiringFiles;
+               $wgServiceWiringFiles[] = __DIR__ . "/ServiceWiring.php";
+       }
+
 }
diff --git a/src/ServiceWiring.php b/src/ServiceWiring.php
new file mode 100644
index 0000000..694a3ad
--- /dev/null
+++ b/src/ServiceWiring.php
@@ -0,0 +1,15 @@
+<?php
+/**
+ * Cognate wiring for MediaWiki services.
+ */
+
+use MediaWiki\MediaWikiServices;
+
+return [
+       'CognateStore' => function( MediaWikiServices $services ) {
+               return new CognateStore(
+                       $services->getDBLoadBalancer(),
+                       $services->getMainConfig()->get( 'CognateWiki' )
+               );
+       },
+];
diff --git a/tests/phpunit/ServiceWiringTest.php 
b/tests/phpunit/ServiceWiringTest.php
new file mode 100644
index 0000000..afbe911
--- /dev/null
+++ b/tests/phpunit/ServiceWiringTest.php
@@ -0,0 +1,28 @@
+<?php
+use MediaWiki\MediaWikiServices;
+
+/**
+ * @license GNU GPL v2+
+ * @author Addshore
+ */
+class ServiceWiringTest extends MediaWikiTestCase {
+
+       public function provideServices() {
+               return [
+                       [ 'CognateStore', CognateStore::class ]
+               ];
+       }
+
+       /**
+        * @dataProvider provideServices
+        */
+       public function testServiceWiring( $serviceName, $expectedClass ) {
+               $service1 = MediaWikiServices::getInstance()->getService( 
$serviceName );
+               $service2 = MediaWikiServices::getInstance()->getService( 
$serviceName );
+
+               $this->assertInstanceOf( $expectedClass, $service1 );
+               $this->assertInstanceOf( $expectedClass, $service2 );
+               $this->assertSame( $service1, $service2 );
+       }
+
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I05001f16a56a1cc6af66b3d4f70cde9c0d2ae210
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Cognate
Gerrit-Branch: master
Gerrit-Owner: Addshore <addshorew...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to