Jeroen De Dauw has submitted this change and it was merged.

Change subject: Change client defaults if repo is on same wiki.
......................................................................


Change client defaults if repo is on same wiki.

If the client extension is active on the repo,
some client settings need to match the local
wiki's setup.

Change-Id: Id77d4e5a13682ccf00276de33dcb6a1982897703
---
M client/WikibaseClient.hooks.php
M client/WikibaseClient.php
M client/config/WikibaseClient.default.php
M client/config/WikibaseClient.example.php
A client/tests/phpunit/ClientHooksTest.php
M lib/includes/Settings.php
M repo/Wikibase.php
7 files changed, 226 insertions(+), 6 deletions(-)

Approvals:
  Jeroen De Dauw: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/client/WikibaseClient.hooks.php b/client/WikibaseClient.hooks.php
index c999dda..1e0577f 100644
--- a/client/WikibaseClient.hooks.php
+++ b/client/WikibaseClient.hooks.php
@@ -165,6 +165,47 @@
        }
 
        /**
+        * @since 0.4
+        *
+        * @param SettingsArray $settings
+        * @param array         $wg global settings (wgXXX).
+        * @param bool          $repoIsLocal whether this wiki is also our repo.
+        */
+       public static function applyMagicDefaultSettings( SettingsArray 
$settings, array $wg, $repoIsLocal ) {
+               // to the actual values for the local wiki.
+               if ( $repoIsLocal ) {
+                       if ( $settings->getSetting( 'repoUrl' ) === null ) {
+                               $settings->setSetting( 'repoUrl', 
$wg['wgServer'] );
+                       }
+
+                       if ( $settings->getSetting( 'repoArticlePath' ) === 
null ) {
+                               $settings->setSetting( 'repoArticlePath', 
$wg['wgArticlePath'] );
+                       }
+
+                       if ( $settings->getSetting( 'repoScriptPath' ) === null 
) {
+                               $settings->setSetting( 'repoScriptPath', 
$wg['wgScriptPath'] );
+                       }
+               }
+
+               if ( $settings->getSetting( 'siteGlobalID' ) === null ) {
+                       $settings->setSetting( 'siteGlobalID', $wg['wgDBName'] 
);
+               }
+       }
+
+       /**
+        * @since 0.4
+        *
+        * @return bool
+        */
+       public static function onSetupAfterCache() {
+               $settings = Settings::singleton();
+
+               self::applyMagicDefaultSettings( $settings, $GLOBALS, defined( 
'WB_VERSION' ) );
+
+               return true;
+       }
+
+       /**
         * Hook for injecting a message on [[Special:MovePage]]
         * @see 
https://www.mediawiki.org/wiki/Manual:Hooks/SpecialMovepageAfterMove
         *
diff --git a/client/WikibaseClient.php b/client/WikibaseClient.php
index a178cb1..4607e37 100644
--- a/client/WikibaseClient.php
+++ b/client/WikibaseClient.php
@@ -147,6 +147,7 @@
        // extension hooks
        $wgHooks['WikibaseDeleteData'][]                                    = 
'\Wikibase\ClientHooks::onWikibaseDeleteData';
        $wgHooks['WikibaseRebuildData'][]                                   = 
'\Wikibase\ClientHooks::onWikibaseRebuildData';
+       $wgHooks['SetupAfterCache'][]                                   = 
'\Wikibase\ClientHooks::onSetupAfterCache';
 
        // api modules
        $wgAPIMetaModules['wikibase'] = 'Wikibase\ApiClientInfo';
diff --git a/client/config/WikibaseClient.default.php 
b/client/config/WikibaseClient.default.php
index f0b167f..31ab62c 100644
--- a/client/config/WikibaseClient.default.php
+++ b/client/config/WikibaseClient.default.php
@@ -31,7 +31,7 @@
 
 
 return call_user_func( function() {
-       global $wgScriptPath, $wgArticlePath, $wgLanguageCode, $wgDBname;
+       global $wgLanguageCode, $wgDBname;
 
        $defaults = array(
                'namespaces' => array(), // by default, include all namespaces; 
deprecated as of 0.4
@@ -183,5 +183,16 @@
                ),
        );
 
+       // Repository is active on the local wiki, change defaults accordingly
+       if ( defined( 'WB_VERSION' ) ) {
+               $defaults['repoUrl'] = null; // initialized later
+               $defaults['repoArticlePath'] = null; // initialized later
+               $defaults['repoScriptPath'] = null; // initialized later
+
+               // use the local database for direct repo access
+               $defaults['repoDatabase'] = false;
+               $defaults['changesDatabase'] = false;
+       }
+
        return $defaults;
 } );
diff --git a/client/config/WikibaseClient.example.php 
b/client/config/WikibaseClient.example.php
index 9654b3a..30e9e82 100644
--- a/client/config/WikibaseClient.example.php
+++ b/client/config/WikibaseClient.example.php
@@ -34,6 +34,12 @@
        die( 'Not an entry point.' );
 }
 
+// NOTE: If this wiki also runs the Wikibase repo extension,
+//       stop and just use the defaults for settings.
+if ( defined( 'WB_VERSION' ) ) {
+       return;
+}
+
 // Base URL for building links to the repository.
 // Assumes your wiki is setup as "http://repo.example.org/wiki/";
 // This can be protocol relative, such as "//www.wikidata.org"
diff --git a/client/tests/phpunit/ClientHooksTest.php 
b/client/tests/phpunit/ClientHooksTest.php
new file mode 100644
index 0000000..a6c177b
--- /dev/null
+++ b/client/tests/phpunit/ClientHooksTest.php
@@ -0,0 +1,150 @@
+<?php
+ /**
+ *
+ * Copyright © 02.07.13 by the authors listed below.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @license GPL 2+
+ * @file
+ *
+ * @ingroup WikibaseClient
+ * @ingroup Test
+ *
+ * @group WikibaseClient
+ *
+ * @author Daniel Kinzler
+ */
+
+
+namespace Wikibase\Test;
+
+
+use Wikibase\ClientHooks;
+use Wikibase\SettingsArray;
+
+/**
+ * Class ClientHooksTest
+ * @covers Wikibase\ClientHooks
+ * @package Wikibase\Test
+ */
+class ClientHooksTest extends \PHPUnit_Framework_TestCase {
+
+       public static function provideOnSetupAfterCache() {
+               return array(
+                       array( // #0: no local repo, all values set
+                               array( // $settings
+                                       'repoUrl' => 'http://acme.com',
+                                       'repoArticlePath' => '/wiki',
+                                       'repoScriptPath' => '/w',
+                                       'siteGlobalID' => 'mywiki',
+                               ),
+                               array( // $wg
+                                       'wgServer' => 'http://www.acme.com',
+                                       'wgArticlePath' => '/mywiki',
+                                       'wgScriptPath' => '/mediawiki',
+                                       'wgDBName' => 'mw_mywiki',
+                               ),
+                               false, // $repoIsLocal
+                               array( // $expected
+                                       'repoUrl' => 'http://acme.com',
+                                       'repoArticlePath' => '/wiki',
+                                       'repoScriptPath' => '/w',
+                                       'siteGlobalID' => 'mywiki',
+                               )
+                       ),
+
+                       array( // #1: no local repo, no values set
+                               array( // $settings
+                                       'repoUrl' => null,
+                                       'repoArticlePath' => null,
+                                       'repoScriptPath' => null,
+                                       'siteGlobalID' => null,
+                               ),
+                               array( // $wg
+                                       'wgServer' => 'http://www.acme.com',
+                                       'wgArticlePath' => '/mywiki',
+                                       'wgScriptPath' => '/mediawiki',
+                                       'wgDBName' => 'mw_mywiki',
+                               ),
+                               false, // $repoIsLocal
+                               array( // $expected
+                                       'repoUrl' => null,
+                                       'repoArticlePath' => null,
+                                       'repoScriptPath' => null,
+                                       'siteGlobalID' => 'mw_mywiki',
+                               )
+                       ),
+
+                       array( // #2: local repo, all values set
+                               array( // $settings
+                                       'repoUrl' => 'http://acme.com',
+                                       'repoArticlePath' => '/wiki',
+                                       'repoScriptPath' => '/w',
+                                       'siteGlobalID' => 'mywiki',
+                               ),
+                               array( // $wg
+                                       'wgServer' => 'http://www.acme.com',
+                                       'wgArticlePath' => '/mywiki',
+                                       'wgScriptPath' => '/mediawiki',
+                                       'wgDBName' => 'mw_mywiki',
+                               ),
+                               true, // $repoIsLocal
+                               array( // $expected
+                                       'repoUrl' => 'http://acme.com',
+                                       'repoArticlePath' => '/wiki',
+                                       'repoScriptPath' => '/w',
+                                       'siteGlobalID' => 'mywiki',
+                               )
+                       ),
+
+                       array( // #3: local repo, no values set
+                               array( // $settings
+                                       'repoUrl' => null,
+                                       'repoArticlePath' => null,
+                                       'repoScriptPath' => null,
+                                       'siteGlobalID' => null,
+                               ),
+                               array( // $wg
+                                       'wgServer' => 'http://www.acme.com',
+                                       'wgArticlePath' => '/mywiki',
+                                       'wgScriptPath' => '/mediawiki',
+                                       'wgDBName' => 'mw_mywiki',
+                               ),
+                               true, // $repoIsLocal
+                               array( // $expected
+                                       'repoUrl' => 'http://www.acme.com',
+                                       'repoArticlePath' => '/mywiki',
+                                       'repoScriptPath' => '/mediawiki',
+                                       'siteGlobalID' => 'mw_mywiki',
+                               )
+                       ),
+               );
+       }
+
+       /**
+        * @dataProvider provideOnSetupAfterCache
+        */
+       public function testOnSetupAfterCache( array $settings, array $wg, 
$repoIsLocal, $expected ) {
+               $settings = new SettingsArray( $settings );
+               ClientHooks::applyMagicDefaultSettings( $settings, $wg, 
$repoIsLocal );
+
+               foreach ( $expected as $key => $value ) {
+                       $this->assertSame( $value, $settings->getSetting( $key 
), "Setting $key" );
+               }
+       }
+
+}
\ No newline at end of file
diff --git a/lib/includes/Settings.php b/lib/includes/Settings.php
index 1959c64..c719dce 100644
--- a/lib/includes/Settings.php
+++ b/lib/includes/Settings.php
@@ -62,15 +62,17 @@
        public function initFromGlobals() {
                $settings = array();
 
-               // merge appropriate settings -------------------
-               if ( defined( 'WB_VERSION' ) ) {
-                       $settings = array_merge( $settings, 
$GLOBALS['wgWBRepoSettings'] );
-               }
+               //NOTE: Repo overrides client. This is important especially for
+               //      settings initialized by WikibaseLib.
 
                if ( defined( 'WBC_VERSION' ) ) {
                        $settings = array_merge( $settings, 
$GLOBALS['wgWBClientSettings'] );
                }
 
+               if ( defined( 'WB_VERSION' ) ) {
+                       $settings = array_merge( $settings, 
$GLOBALS['wgWBRepoSettings'] );
+               }
+
                // store
                foreach ( $settings as $key => $value ) {
                        $this[$key] = $value;
diff --git a/repo/Wikibase.php b/repo/Wikibase.php
index b541afb..2233718 100644
--- a/repo/Wikibase.php
+++ b/repo/Wikibase.php
@@ -40,7 +40,16 @@
 }
 
 if ( version_compare( $GLOBALS['wgVersion'], '1.20c', '<' ) ) { // Needs to be 
1.20c because version_compare() works in confusing ways.
-       die( '<b>Error:</b> Wikibase requires MediaWiki 1.20 or above.' );
+       die( "<b>Error:</b> Wikibase requires MediaWiki 1.20 or above.\n" );
+}
+
+if ( defined( 'WBC_VERSION' ) ) {
+       // TODO: needed because WikibaseClient.defaults.php depends on 
WB_VERSION to know whether the
+       // repo is configured. Should be removed as soon as we change Settings 
to apply defaults
+       // "late", that is, when merging settings, after setup is complete.
+
+       throw new Exception( "Bad initialization order: When running the 
Wikibase repository extension and the "
+               . "WikibaseClient extension on the same wiki, WikibaseClient 
has to be included AFTER the repository." );
 }
 
 // Include the WikibaseLib extension if that hasn't been done yet, since it's 
required for Wikibase to work.

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Id77d4e5a13682ccf00276de33dcb6a1982897703
Gerrit-PatchSet: 9
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Denny Vrandecic <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to