Daniel Kinzler has uploaded a new change for review.

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


Change subject: Re-use top level registries everywhere.
......................................................................

Re-use top level registries everywhere.

By re-using the top level registries (WikibaseRepo and
WikibaseClient) we allow the service objects themselves
to be re-used. This way, local (in-process/in-memory)
caches can be used effectively.

This should help to reduce the number of (redundant)
requests to memcached.

Change-Id: Ifa80245c93ce19d746f6af6efcf013ea859a3b9f
---
M client/includes/WikibaseClient.php
M client/includes/parserhooks/PropertyParserFunction.php
M client/tests/phpunit/includes/WikibaseClientTest.php
M client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php
M lib/includes/LibRegistry.php
M lib/resources/Resources.php
M repo/includes/EntityContentDiffView.php
M repo/includes/WikibaseRepo.php
M repo/includes/actions/EditEntityAction.php
M repo/includes/api/SetClaim.php
M repo/includes/content/EntityContentFactory.php
M repo/tests/phpunit/includes/WikibaseRepoTest.php
12 files changed, 43 insertions(+), 26 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/98/71598/1

diff --git a/client/includes/WikibaseClient.php 
b/client/includes/WikibaseClient.php
index a7767a2..894a5fc 100644
--- a/client/includes/WikibaseClient.php
+++ b/client/includes/WikibaseClient.php
@@ -279,7 +279,7 @@
         *
         * @return WikibaseClient
         */
-       public static function newInstance() {
+       protected static function newInstance() {
                global $wgContLang;
 
                return new self(
diff --git a/client/includes/parserhooks/PropertyParserFunction.php 
b/client/includes/parserhooks/PropertyParserFunction.php
index 4d09ab9..1301e70 100644
--- a/client/includes/parserhooks/PropertyParserFunction.php
+++ b/client/includes/parserhooks/PropertyParserFunction.php
@@ -182,7 +182,7 @@
                $targetLanguage = $parser->getTargetLanguage();
                $errorFormatter = new ParserErrorMessageFormatter( 
$targetLanguage );
 
-               $wikibaseClient = WikibaseClient::newInstance();
+               $wikibaseClient = WikibaseClient::getDefaultInstance();
 
                $entityLookup = $wikibaseClient->getStore()->getEntityLookup();
                $propertyLabelResolver = 
$wikibaseClient->getStore()->getPropertyLabelResolver();
diff --git a/client/tests/phpunit/includes/WikibaseClientTest.php 
b/client/tests/phpunit/includes/WikibaseClientTest.php
index 1f23815..f7003c5 100644
--- a/client/tests/phpunit/includes/WikibaseClientTest.php
+++ b/client/tests/phpunit/includes/WikibaseClientTest.php
@@ -40,32 +40,32 @@
        /**
         * @return WikibaseClient
         */
-       private function newInstance() {
-               return WikibaseClient::newInstance();
+       private function getDefaultInstance() {
+               return WikibaseClient::getDefaultInstance();
        }
 
        public function testGetSettingsReturnType() {
-               $returnValue = $this->newInstance()->getSettings();
+               $returnValue = $this->getDefaultInstance()->getSettings();
                $this->assertInstanceOf( 'Wikibase\SettingsArray', $returnValue 
);
        }
 
        public function testGetStoreReturnType() {
-               $returnValue = $this->newInstance()->getStore();
+               $returnValue = $this->getDefaultInstance()->getStore();
                $this->assertInstanceOf( 'Wikibase\ClientStore', $returnValue );
        }
 
        public function testGetDataTypeFactoryReturnType() {
-               $returnValue = $this->newInstance()->getDataTypeFactory();
+               $returnValue = 
$this->getDefaultInstance()->getDataTypeFactory();
                $this->assertInstanceOf( 'DataTypes\DataTypeFactory', 
$returnValue );
        }
 
        public function testGetEntityIdParserReturnType() {
-               $returnValue = $this->newInstance()->getEntityIdParser();
+               $returnValue = $this->getDefaultInstance()->getEntityIdParser();
                $this->assertInstanceOf( 'Wikibase\Lib\EntityIdParser', 
$returnValue );
        }
 
        public function testEntityIdLabelFormatterReturnType() {
-               $returnValue = $this->newInstance()->newEntityIdLabelFormatter( 
'en' );
+               $returnValue = 
$this->getDefaultInstance()->newEntityIdLabelFormatter( 'en' );
                $this->assertInstanceOf( 'Wikibase\Lib\EntityIdLabelFormatter', 
$returnValue );
        }
 
diff --git 
a/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php 
b/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php
index adfc272..450247b 100644
--- a/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php
+++ b/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php
@@ -49,8 +49,8 @@
  */
 class PropertyParserFunctionTest extends \PHPUnit_Framework_TestCase {
 
-       private function newInstance() {
-               $wikibaseClient = WikibaseClient::newInstance();
+       private function getDefaultInstance() {
+               $wikibaseClient = WikibaseClient::getDefaultInstance();
 
                $targetLanguage = \Language::factory( 'en' );
                $errorFormatter = new ParserErrorMessageFormatter( 
$targetLanguage );
@@ -120,7 +120,7 @@
         * @dataProvider provideRenderForEntityId
         */
        public function testRenderForEntityId( $name, $expected, $info ) {
-               $parserFunction = $this->newInstance();
+               $parserFunction = $this->getDefaultInstance();
 
                $status = $parserFunction->renderForEntityId(
                        new EntityId( Item::ENTITY_TYPE, 42 ),
diff --git a/lib/includes/LibRegistry.php b/lib/includes/LibRegistry.php
index bd4650b..2f32b70 100644
--- a/lib/includes/LibRegistry.php
+++ b/lib/includes/LibRegistry.php
@@ -116,14 +116,31 @@
 
        /**
         * Returns a new instance constructed from global settings.
+        *
+        * @since 0.4
+        *
+        * @return LibRegistry
+        */
+       protected static function newInstance() {
+               return new self( Settings::singleton() );
+       }
+
+       /**
+        * Returns a default instance constructed from global settings.
         * IMPORTANT: Use only when it is not feasible to inject an instance 
properly.
         *
         * @since 0.4
         *
         * @return LibRegistry
         */
-       public static function newInstance() {
-               return new self( Settings::singleton() );
+       public static function getDefaultInstance() {
+               static $instance = null;
+
+               if ( $instance === null ) {
+                       $instance = self::newInstance();
+               }
+
+               return $instance;
        }
 
        // Do not add new stuff here without reading the notice at the top 
first.
diff --git a/lib/resources/Resources.php b/lib/resources/Resources.php
index 1e250de..d5223f0 100644
--- a/lib/resources/Resources.php
+++ b/lib/resources/Resources.php
@@ -81,7 +81,7 @@
                'mw.config.values.wbDataTypes' => $moduleTemplate + array(
                        'class' => 'DataTypes\DataTypesModule',
                        'datatypefactory' => function() {
-                               return 
LibRegistry::newInstance()->getDataTypeFactory();
+                               return 
LibRegistry::getDefaultInstance()->getDataTypeFactory();
                        },
                        'datatypesconfigvarname' => 'wbDataTypes',
                ),
diff --git a/repo/includes/EntityContentDiffView.php 
b/repo/includes/EntityContentDiffView.php
index 61fbdc6..e459a96 100644
--- a/repo/includes/EntityContentDiffView.php
+++ b/repo/includes/EntityContentDiffView.php
@@ -147,7 +147,7 @@
                        new ClaimDifferenceVisualizer(
                                new WikiPageEntityLookup(),
                                $langCode,
-                               WikibaseRepo::newInstance()->getIdFormatter()
+                               
WikibaseRepo::getDefaultInstance()->getIdFormatter()
                        )
                );
 
diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php
index 3b87e11..9a7e179 100644
--- a/repo/includes/WikibaseRepo.php
+++ b/repo/includes/WikibaseRepo.php
@@ -232,7 +232,7 @@
         *
         * @return WikibaseRepo
         */
-       public static function newInstance() {
+       protected static function newInstance() {
                return new self(
                        Settings::singleton(),
                        StoreFactory::getStore()
diff --git a/repo/includes/actions/EditEntityAction.php 
b/repo/includes/actions/EditEntityAction.php
index 1312945..21b4832 100644
--- a/repo/includes/actions/EditEntityAction.php
+++ b/repo/includes/actions/EditEntityAction.php
@@ -443,7 +443,7 @@
                        new ClaimDifferenceVisualizer(
                                new WikiPageEntityLookup(),
                                $langCode,
-                               WikibaseRepo::newInstance()->getIdFormatter()
+                               
WikibaseRepo::getDefaultInstance()->getIdFormatter()
                        )
                );
 
diff --git a/repo/includes/api/SetClaim.php b/repo/includes/api/SetClaim.php
index bd4f131..050be43 100644
--- a/repo/includes/api/SetClaim.php
+++ b/repo/includes/api/SetClaim.php
@@ -97,7 +97,7 @@
                $claimSummaryBuilder = new ClaimSummaryBuilder(
                        $this->getModuleName(),
                        $claimDiffer,
-                       WikibaseRepo::newInstance()->getIdFormatter()
+                       WikibaseRepo::getDefaultInstance()->getIdFormatter()
                );
                $claimSaver = new ClaimSaver();
 
diff --git a/repo/includes/content/EntityContentFactory.php 
b/repo/includes/content/EntityContentFactory.php
index c2cc2f9..cad6027 100644
--- a/repo/includes/content/EntityContentFactory.php
+++ b/repo/includes/content/EntityContentFactory.php
@@ -48,7 +48,7 @@
                static $instance = false;
 
                if ( $instance === false ) {
-                       $instance = 
WikibaseRepo::newInstance()->getEntityContentFactory();
+                       $instance = 
WikibaseRepo::getDefaultInstance()->getEntityContentFactory();
                }
 
                return $instance;
diff --git a/repo/tests/phpunit/includes/WikibaseRepoTest.php 
b/repo/tests/phpunit/includes/WikibaseRepoTest.php
index 6c2a426..04ed56e 100644
--- a/repo/tests/phpunit/includes/WikibaseRepoTest.php
+++ b/repo/tests/phpunit/includes/WikibaseRepoTest.php
@@ -41,22 +41,22 @@
        /**
         * @return WikibaseRepo
         */
-       private function newInstance() {
-               return WikibaseRepo::newInstance();
+       private function getDefaultInstance() {
+               return WikibaseRepo::getDefaultInstance();
        }
 
        public function testGetSettingsReturnType() {
-               $returnValue = $this->newInstance()->getSettings();
+               $returnValue = $this->getDefaultInstance()->getSettings();
                $this->assertInstanceOf( 'Wikibase\SettingsArray', $returnValue 
);
        }
 
        public function testGetDataTypeFactoryReturnType() {
-               $returnValue = $this->newInstance()->getDataTypeFactory();
+               $returnValue = 
$this->getDefaultInstance()->getDataTypeFactory();
                $this->assertInstanceOf( 'DataTypes\DataTypeFactory', 
$returnValue );
        }
 
        public function testGetEntityIdParserReturnType() {
-               $returnValue = $this->newInstance()->getEntityIdParser();
+               $returnValue = $this->getDefaultInstance()->getEntityIdParser();
                $this->assertInstanceOf( 'Wikibase\Lib\EntityIdParser', 
$returnValue );
        }
 
@@ -74,7 +74,7 @@
        public function testGetRdfBaseURI( $server, $expected ) {
                $this->setMwGlobals( 'wgServer', $server );
 
-               $returnValue = $this->newInstance()->getRdfBaseURI();
+               $returnValue = $this->getDefaultInstance()->getRdfBaseURI();
                $this->assertEquals( $expected, $returnValue );
        }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifa80245c93ce19d746f6af6efcf013ea859a3b9f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de>

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

Reply via email to