Hoo man has uploaded a new change for review. https://gerrit.wikimedia.org/r/214517
Change subject: Generalize the action=query&meta=wikibase API, expose siteid ...................................................................... Generalize the action=query&meta=wikibase API, expose siteid Changed it to be more generic and not just cover information about the Wikibase repository. The siteid is often needed and it's very convenient to get it via this api. Also simplified tests and changed to use a factory method. Change-Id: I0c4020703e57be8db086ef78177bef1204e6f762 --- M client/WikibaseClient.php M client/i18n/en.json M client/includes/api/ApiClientInfo.php M client/tests/phpunit/includes/api/ApiClientInfoTest.php 4 files changed, 75 insertions(+), 88 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/17/214517/1 diff --git a/client/WikibaseClient.php b/client/WikibaseClient.php index e9a8b45..245f5d8 100644 --- a/client/WikibaseClient.php +++ b/client/WikibaseClient.php @@ -123,7 +123,17 @@ $wgHooks['WikibaseDeleteData'][] = '\Wikibase\ClientHooks::onWikibaseDeleteData'; // api modules - $wgAPIMetaModules['wikibase'] = 'Wikibase\ApiClientInfo'; + $wgAPIMetaModules['wikibase'] = array( + 'class' => 'Wikibase\ApiClientInfo', + 'factory' => function( ApiQuery $apiQuery, $moduleName ) { + return new Wikibase\ApiClientInfo( + Wikibase\Client\WikibaseClient::getDefaultInstance()->getSettings(), + $apiQuery, + $moduleName + ); + } + ); + $wgAPIPropModules['pageterms'] = array( 'class' => 'Wikibase\Client\Api\PageTerms', 'factory' => function ( ApiQuery $query, $moduleName ) { diff --git a/client/i18n/en.json b/client/i18n/en.json index 33f1ba7..f3cc894 100644 --- a/client/i18n/en.json +++ b/client/i18n/en.json @@ -15,9 +15,9 @@ "apihelp-query+pageterms-example-simple": "Get all terms associated with the page 'London', in the user language.", "apihelp-query+pageterms-example-label-en": "Get labels and aliases associated with the page 'London', in English.", "apihelp-query+pageterms-param-terms": "The types of terms to get, e.g. 'description'. If not specified, all types are returned.", - "apihelp-query+wikibase-description": "Get information about the associated Wikibase repository.", - "apihelp-query+wikibase-example": "Get URL path and other information for the Wikibase repository.", - "apihelp-query+wikibase-param-prop": "Which properties to get:\n;<kbd>url</kbd>: Base URL, script path and article path.", + "apihelp-query+wikibase-description": "Get information about the the Wikibase installation and the associated Wikibase repository.", + "apihelp-query+wikibase-example": "Get URL path and other information about the Wikibase installation.", + "apihelp-query+wikibase-param-prop": "Which properties to get:\n;<kbd>url</kbd>: Base URL, script path and article path of the Wikibase repository.\n;<kbd>siteid</kbd>: The siteid of this site.", "wikibase-after-page-move": "You may also [$1 update] the associated {{WBREPONAME}} item to maintain language links on moved page.", "wikibase-after-page-move-queued": "The [$1 {{WBREPONAME}} item] associated with this page will be automatically updated soon.", "wikibase-after-page-delete": "You may also remove the link to this page from the associated [$1 {{WBREPONAME}} item] to maintain language links.", diff --git a/client/includes/api/ApiClientInfo.php b/client/includes/api/ApiClientInfo.php index 6b2364d..59c1608 100644 --- a/client/includes/api/ApiClientInfo.php +++ b/client/includes/api/ApiClientInfo.php @@ -5,7 +5,6 @@ use ApiBase; use ApiQuery; use ApiQueryBase; -use Wikibase\Client\WikibaseClient; /** * Provides url and path information for the associated Wikibase repo @@ -16,6 +15,7 @@ * * @licence GNU GPL v2+ * @author Katie Filbert < aude.w...@gmail.com > + * @author Marius Hoch < h...@online.de > */ class ApiClientInfo extends ApiQueryBase { @@ -27,14 +27,14 @@ /** * @since 0.4 * + * @param SettingsArray $settings * @param ApiQuery $apiQuery * @param string $moduleName */ - public function __construct( ApiQuery $apiQuery, $moduleName ) { + public function __construct( SettingsArray $settings, ApiQuery $apiQuery, $moduleName ) { parent::__construct( $apiQuery, $moduleName, 'wb' ); - // @todo inject this instead of using singleton here - $this->settings = WikibaseClient::getDefaultInstance()->getSettings(); + $this->settings = $settings; } /** @@ -45,44 +45,29 @@ public function execute() { $params = $this->extractRequestParams(); - $apiData = $this->getRepoInfo( $params ); + $apiData = $this->getInfo( $params ); $this->getResult()->addValue( 'query', 'wikibase', $apiData ); } /** - * Set settings for api module - * - * @since 0.4 - * - * @param SettingsArray $settings - */ - public function setSettings( SettingsArray $settings ) { - $this->settings = $settings; - } - - /** * Gets repo url info to inject into the api module * - * @since 0.4 - * - * @param array $params[] + * @param array $params * * @return array */ - public function getRepoInfo( array $params ) { - $data = array( 'repo' => array() ); - - $repoUrlArray = array( - 'base' => $this->settings->getSetting( 'repoUrl' ), - 'scriptpath' => $this->settings->getSetting( 'repoScriptPath' ), - 'articlepath' => $this->settings->getSetting( 'repoArticlePath' ), - ); + private function getInfo( array $params ) { + $data = array(); foreach ( $params['prop'] as $p ) { switch ( $p ) { case 'url': - $data['repo']['url'] = $repoUrlArray; + $data['repo'] = array(); + $data['repo']['url'] = $this->getRepoUrls(); + break; + case 'siteid': + $data['siteid'] = $this->settings->getSetting( 'siteGlobalID' ); break; default; break; @@ -93,15 +78,26 @@ } /** + * @return string[] + */ + private function getRepoUrls() { + return array( + 'base' => $this->settings->getSetting( 'repoUrl' ), + 'scriptpath' => $this->settings->getSetting( 'repoScriptPath' ), + 'articlepath' => $this->settings->getSetting( 'repoArticlePath' ), + ); + } + + /** * @see ApiBase::getAllowedParams */ protected function getAllowedParams() { return array( 'prop' => array( - ApiBase::PARAM_DFLT => 'url', + ApiBase::PARAM_DFLT => 'url|siteid', ApiBase::PARAM_ISMULTI => true, ApiBase::PARAM_TYPE => array( - 'url', + 'url', 'siteid' ) ), ); diff --git a/client/tests/phpunit/includes/api/ApiClientInfoTest.php b/client/tests/phpunit/includes/api/ApiClientInfoTest.php index b6dce87..11c35c2 100644 --- a/client/tests/phpunit/includes/api/ApiClientInfoTest.php +++ b/client/tests/phpunit/includes/api/ApiClientInfoTest.php @@ -38,11 +38,10 @@ /** * @param array $params - * @param SettingsArray $settings * * @return ApiClientInfo */ - public function getApiModule( array $params, SettingsArray $settings ) { + public function getApiModule( array $params ) { $request = new FauxRequest( $params, true ); $user = User::newFromName( 'zombie' ); @@ -51,8 +50,7 @@ $apiMain = new ApiMain( $context, true ); $apiQuery = new ApiQuery( $apiMain, 'wikibase' ); - $apiModule = new ApiClientInfo( $apiQuery, 'query' ); - $apiModule->setSettings( $settings ); + $apiModule = new ApiClientInfo( $this->getSettings(), $apiQuery, 'query' ); return $apiModule; } @@ -60,73 +58,55 @@ /** * @dataProvider executeProvider */ - public function testExecute( $params ) { - $settings = $this->getSettings(); - - $module = $this->getApiModule( $params, $settings ); + public function testExecute( array $expected, array $params ) { + $module = $this->getApiModule( $params ); $module->execute(); $result = $module->getResult()->getResultData(); - $this->assertInternalType( 'array', $result, 'top level element is an array' ); - - $this->assertArrayHasKey( 'query', $result, 'top level element has a query key' ); - $this->assertArrayHasKey( 'wikibase', $result['query'], 'second level element has a wikibase key' ); - } - - /** - * @dataProvider getRepoInfoProvider - */ - public function testGetRepoInfo( array $params, SettingsArray $settings ) { - $module = $this->getApiModule( $params, $settings ); - $reqParams = $module->extractRequestParams(); - $repoInfo = $module->getRepoInfo( $reqParams ); - - $this->assertArrayHasKey( 'repo', $repoInfo, 'top level element has repo key' ); - $urlInfo = $repoInfo['repo']['url']; - - $this->assertArrayHasKey( 'base', $urlInfo ); - $this->assertArrayHasKey( 'scriptpath', $urlInfo ); - $this->assertArrayHasKey( 'articlepath', $urlInfo ); - - $this->assertInternalType( 'string', $urlInfo['base'], - "The repo URL information for 'base' should be a string" ); - $this->assertInternalType( 'string', $urlInfo['scriptpath'], - "The repo URL information for 'scriptpath' should be a string" ); - $this->assertInternalType( 'string', $urlInfo['articlepath'], - "The repo URL information for 'articlepath' should be a string" ); - - $this->assertEquals( $settings->getSetting( 'repoUrl' ), $urlInfo['base'] ); - $this->assertEquals( $settings->getSetting( 'repoScriptPath' ), $urlInfo['scriptpath'] ); - $this->assertEquals( $settings->getSetting( 'repoArticlePath' ), $urlInfo['articlepath'] ); - + $this->assertEquals( $expected, $result['query']['wikibase'] ); } public function executeProvider() { - $params = $this->getApiRequestParams(); - - return array( - array( $params ) - ); - } - - public function getRepoInfoProvider() { - $params = $this->getApiRequestParams(); $settings = $this->getSettings(); + $repo = array( 'repo' => array( + 'url' => array( + 'base' => $settings->getSetting( 'repoUrl' ), + 'scriptpath' => $settings->getSetting( 'repoScriptPath' ), + 'articlepath' => $settings->getSetting( 'repoArticlePath' ), + ) + ) + ); + + $siteid = array( 'siteid' => $settings->getSetting( 'siteGlobalID' ) ); + return array( - array( $params, $settings ) + array( + $repo + $siteid, + $this->getApiRequestParams( 'url|siteid' ) + ), + array( + $repo, + $this->getApiRequestParams( 'url' ) + ), + array( + $siteid, + $this->getApiRequestParams( 'siteid' ) + ) ); } /** + * @param string $wbprop + * * @return array */ - protected function getApiRequestParams() { + private function getApiRequestParams( $wbprop ) { $params = array( 'action' => 'query', 'meta' => 'wikibase', - 'wbprop' => 'url' + 'wbprop' => $wbprop ); return $params; @@ -135,11 +115,12 @@ /** * @return SettingsArray */ - protected function getSettings() { + private function getSettings() { return new SettingsArray( array( 'repoUrl' => 'http://www.example.org', 'repoScriptPath' => '/w', - 'repoArticlePath' => '/wiki/$1' + 'repoArticlePath' => '/wiki/$1', + 'siteGlobalID' => 'somerandomwiki', ) ); } -- To view, visit https://gerrit.wikimedia.org/r/214517 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I0c4020703e57be8db086ef78177bef1204e6f762 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Hoo man <h...@online.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits