Yurik has uploaded a new change for review. https://gerrit.wikimedia.org/r/320069
Change subject: (WIP) jsondata api ...................................................................... (WIP) jsondata api Change-Id: I6b5f189690b52fc3b523a4087ba8d1e48755a879 --- M extension.json M i18n/en.json M i18n/qqq.json A includes/JCDataApi.php M includes/JCSingleton.php 5 files changed, 120 insertions(+), 2 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/JsonConfig refs/changes/69/320069/1 diff --git a/extension.json b/extension.json index 17e57c0..233daeb 100644 --- a/extension.json +++ b/extension.json @@ -48,6 +48,7 @@ "JsonConfig\\JCContent": "includes/JCContent.php", "JsonConfig\\JCContentHandler": "includes/JCContentHandler.php", "JsonConfig\\JCContentView": "includes/JCContentView.php", + "JsonConfig\\JCDataApi": "includes/JCDataApi.php", "JsonConfig\\JCDataContent": "includes/JCDataContent.php", "JsonConfig\\JCDefaultContentView": "includes/JCDefaultContentView.php", "JsonConfig\\JCDefaultObjContentView": "includes/JCDefaultObjContentView.php", @@ -80,6 +81,9 @@ "AbortMove": [ "JsonConfig\\JCSingleton::onAbortMove" ], + "ApiMain::moduleManager": [ + "JsonConfig\\JCSingleton::onApiMainModuleManager" + ], "ArticleDeleteComplete": [ "JsonConfig\\JCSingleton::onArticleDeleteComplete" ], diff --git a/i18n/en.json b/i18n/en.json index f793339..f5352f4 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -48,5 +48,10 @@ "apihelp-jsonconfig-param-content": "For $1command=reload, use this content instead.", "apihelp-jsonconfig-example-1": "Show configuration", "apihelp-jsonconfig-example-2": "Reset [[Zero:TEST]]", - "apihelp-jsonconfig-example-3": "Reload [[Zero:TEST]]" + "apihelp-jsonconfig-example-3": "Reload [[Zero:TEST]]", + "apihelp-jsondata-description": "Retrieve localized JSON data from the data storing wiki. This API only supports format=json and formatversion=2 or later.", + "apihelp-jsondata-param-namespace": "Namespace number of the title to get. If namespace \"Data\" (486) is defined, this parameter is optional.", + "apihelp-jsondata-param-title": "Title to get without namespace prefix. By default assumes namespace to be \"Data\"", + "apihelp-jsondata-example-1": "Get JSON content of the Sample.tab page, localized to user's language", + "apihelp-jsondata-example-2": "Get JSON content of the Sample.tab page localized to French" } diff --git a/i18n/qqq.json b/i18n/qqq.json index 808707a..d62132a 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -56,5 +56,10 @@ "apihelp-jsonconfig-param-content": "{{doc-apihelp-param|jsonconfig|content}}", "apihelp-jsonconfig-example-1": "{{doc-apihelp-example|jsonconfig}}", "apihelp-jsonconfig-example-2": "{{doc-apihelp-example|jsonconfig}}", - "apihelp-jsonconfig-example-3": "{{doc-apihelp-example|jsonconfig}}" + "apihelp-jsonconfig-example-3": "{{doc-apihelp-example|jsonconfig}}", + "apihelp-jsondata-description": "{{doc-apihelp-description|jsondata}}", + "apihelp-jsondata-param-namespace": "{{doc-apihelp-param|jsondata|namespace}}", + "apihelp-jsondata-param-title": "{{doc-apihelp-param|jsondata|title}}", + "apihelp-jsondata-example-1": "{{doc-apihelp-example|jsondata}}", + "apihelp-jsondata-example-2": "{{doc-apihelp-example|jsondata}}" } diff --git a/includes/JCDataApi.php b/includes/JCDataApi.php new file mode 100644 index 0000000..5b609d6 --- /dev/null +++ b/includes/JCDataApi.php @@ -0,0 +1,80 @@ +<?php +namespace JsonConfig; + +use ApiBase; +use ApiFormatJson; + +/** + * Allows users to get json + */ +class JCDataApi extends ApiBase { + + public function execute() { + $printerParams = $this->getMain()->getPrinter()->extractRequestParams(); + if ( !( $this->getMain()->getPrinter() instanceof ApiFormatJson ) || + !isset( $printerParams['formatversion'] ) + ) { + $this->dieUsage( 'This module only supports format=json and format=jsonfm', + 'invalidparammix' ); + } + if ( $printerParams['formatversion'] == 1 ) { + $this->dieUsage( 'This module only supports formatversion=2 or later', + 'invalidparammix' ); + } + + $params = $this->extractRequestParams(); + $jct = JCSingleton::parseTitle( $params['title'], intval( $params['namespace'] ) ); + if ( !$jct ) { + $this->dieUsageMsg( [ 'invalidtitle', $params['title'] ] ); + } + + $data = JCSingleton::getContent( $jct ); + if ( !$data ) { + $this->dieUsageMsg( [ 'invalidtitle', $jct ] ); + } elseif ( !method_exists( $data, 'getLocalizedData' ) ) { + $data = $data->getData(); + } else { + /** @var JCDataContent $data */ + $data = $data->getLocalizedData( $this->getLanguage() ); + } + + $this->getResult()->addValue( null, 'titleMap', $data ); + + $this->getMain()->setCacheMaxAge( 24 * 60 * 60 ); // seconds + $this->getMain()->setCacheMode( 'public' ); + } + + public function getAllowedParams( $flags = 0 ) { + $allowedNamespaces = JCSingleton::getSupportedNamespaces(); + if ( $flags & ApiBase::GET_VALUES_FOR_HELP ) { + sort( $allowedNamespaces, SORT_NUMERIC ); + } + + $namespaceParams = [ + ApiBase::PARAM_TYPE => $allowedNamespaces, + ]; + // Only set default if NS_DATA is allowed. Otherwise require namespace param + if ( in_array( NS_DATA, $allowedNamespaces ) ) { + $namespaceParams[ ApiBase::PARAM_DFLT ] = NS_DATA; + } else { + $namespaceParams[ ApiBase::PARAM_REQUIRED ] = true; + } + + return [ + 'namespace' => $namespaceParams, + 'title' => [ + ApiBase::PARAM_TYPE => 'string', + ApiBase::PARAM_REQUIRED => true, + ], + ]; + } + + protected function getExamplesMessages() { + return [ + 'api.php?action=jsondata&formatversion=2&format=jsonfm&title=Sample.tab' + => 'apihelp-jsondata-example-1', + 'api.php?action=jsondata&formatversion=2&format=jsonfm&title=Sample.tab&uselang=fr' + => 'apihelp-jsondata-example-2', + ]; + } +} diff --git a/includes/JCSingleton.php b/includes/JCSingleton.php index 72ed8c4..94cc730 100644 --- a/includes/JCSingleton.php +++ b/includes/JCSingleton.php @@ -1,6 +1,7 @@ <?php namespace JsonConfig; +use ApiModuleManager; use ContentHandler; use Exception; use GenderCache; @@ -382,6 +383,15 @@ return self::$titleMap; } + /** + * Get the list of namespaces supported by JsonConfig + * @return int[] list of known namespaces + */ + public static function getSupportedNamespaces() { + self::init(); + return array_keys( self::$titleMap ); + } + public static function getContentClass( $modelId ) { global $wgJsonConfigModels; $configModels = array_replace_recursive( \ExtensionRegistry::getInstance()->getAttribute( 'JsonConfigModels' ), $wgJsonConfigModels ); @@ -692,6 +702,20 @@ return true; } + /** + * Conditionally load API module 'jsondata' depending on whether or not + * this wiki stores any jsonconfig data + * + * @param ApiModuleManager $moduleManager Module manager instance + * @return bool + */ + public static function onApiMainModuleManager( ApiModuleManager $moduleManager ) { + if ( self::jsonConfigIsStorage() ) { + $moduleManager->addModule( 'jsondata', 'action', 'JsonConfig\\JCDataApi' ); + } + return true; + } + public static function onPageContentSaveComplete( /** @noinspection PhpUnusedParameterInspection */ $article, $user, $content, $summary, $isMinor, $isWatch, $section, $flags, $revision, $status, $baseRevId ) { return self::onArticleChangeComplete( $article, $content ); -- To view, visit https://gerrit.wikimedia.org/r/320069 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I6b5f189690b52fc3b523a4087ba8d1e48755a879 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/JsonConfig Gerrit-Branch: master Gerrit-Owner: Yurik <yu...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits