jenkins-bot has submitted this change and it was merged.

Change subject: Added jsondata api for localized json from Data ns
......................................................................


Added jsondata api for localized json from Data ns

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, 103 insertions(+), 2 deletions(-)

Approvals:
  MaxSem: Looks good to me, approved
  jenkins-bot: Verified



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..e327c20 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -48,5 +48,9 @@
        "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. This API 
only supports format=json and formatversion=2 or later.",
+       "apihelp-jsondata-param-title": "Title to get. 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..62edca8 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -56,5 +56,9 @@
        "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-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..5a1d79b
--- /dev/null
+++ b/includes/JCDataApi.php
@@ -0,0 +1,68 @@
+<?php
+namespace JsonConfig;
+
+use ApiBase;
+use ApiFormatJson;
+
+/**
+ * Get localized json data, similar to Lua's mw.data.get() function
+ */
+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'], NS_DATA );
+               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, $this->getModuleName(), 
$data );
+
+               $this->getMain()->setCacheMaxAge( 24 * 60 * 60 ); // seconds
+               $this->getMain()->setCacheMode( 'public' );
+       }
+
+       public function getAllowedParams() {
+               return [
+                       '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',
+               ];
+       }
+
+       public function isInternal() {
+               return true;
+       }
+}
diff --git a/includes/JCSingleton.php b/includes/JCSingleton.php
index 72ed8c4..edf8d19 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,11 @@
                return self::$titleMap;
        }
 
+       /**
+        * Get the name of the class for a given content model
+        * @param string $modelId
+        * @return null|string
+        */
        public static function getContentClass( $modelId ) {
                global $wgJsonConfigModels;
                $configModels = array_replace_recursive( 
\ExtensionRegistry::getInstance()->getAttribute( 'JsonConfigModels' ), 
$wgJsonConfigModels );
@@ -692,6 +698,21 @@
                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 ) {
+               global $wgJsonConfigEnableLuaSupport;
+               if ( $wgJsonConfigEnableLuaSupport ) {
+                       $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: merged
Gerrit-Change-Id: I6b5f189690b52fc3b523a4087ba8d1e48755a879
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/JsonConfig
Gerrit-Branch: master
Gerrit-Owner: Yurik <yu...@wikimedia.org>
Gerrit-Reviewer: MaxSem <maxsem.w...@gmail.com>
Gerrit-Reviewer: Siebrand <siebr...@kitano.nl>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to