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

Change subject: Add API action=query&meta=babel module
......................................................................


Add API action=query&meta=babel module

Change-Id: I8d24f1f8b65ece1be75432421fcd5c870403178d
---
M Babel.class.php
M extension.json
M i18n/en.json
M i18n/qqq.json
A includes/ApiQueryBabel.php
M tests/phpunit/BabelTest.php
6 files changed, 136 insertions(+), 13 deletions(-)

Approvals:
  Nikerabbit: Checked; Looks good to me, approved
  jenkins-bot: Verified



diff --git a/Babel.class.php b/Babel.class.php
index d4fdd47..fd895c1 100644
--- a/Babel.class.php
+++ b/Babel.class.php
@@ -314,9 +314,28 @@
        }
 
        /**
-        * Gets the list of languages a user has set up with Babel
+        * Gets the language information a user has set up with Babel
         *
-        * TODO There could be an API module that returns the result of this 
function
+        * @param User $user
+        * @return string[] [ language code => level ]
+        */
+       public static function getUserLanguageInfo( User $user ) {
+               global $wgBabelMainCategory, $wgBabelUseDatabase;
+
+               if ( $wgBabelUseDatabase ) {
+                       $result = self::getUserLanguagesDB( $user );
+               } elseif ( $wgBabelMainCategory ) {
+                       $result = self::getUserLanguagesCat( $user );
+               } else {
+                       return [];
+               }
+
+               ksort( $result );
+               return $result;
+       }
+
+       /**
+        * Gets the list of languages a user has set up with Babel
         *
         * @param User $user
         * @param string $level Minimal level as given in $wgBabelCategoryNames
@@ -325,13 +344,8 @@
         * @since Version 1.9.0
         */
        public static function getUserLanguages( User $user, $level = null ) {
-               global $wgBabelMainCategory, $wgBabelUseDatabase;
-
-               if ( $wgBabelUseDatabase ) {
-                       $result = self::getUserLanguagesDB( $user );
-               } elseif ( $wgBabelMainCategory ) {
-                       $result = self::getUserLanguagesCat( $user );
-               } else {
+               $result = self::getUserLanguageInfo( $user );
+               if ( !$result ) {
                        return [];
                }
 
diff --git a/extension.json b/extension.json
index 0e20cc3..7dff55f 100644
--- a/extension.json
+++ b/extension.json
@@ -19,12 +19,16 @@
                "BabelLanguageCodes": "BabelLanguageCodes.class.php",
                "BabelStatic": "BabelStatic.class.php",
                "BabelAutoCreate": "BabelAutoCreate.class.php",
+               "MediaWiki\\Babel\\ApiQueryBabel": "includes/ApiQueryBabel.php",
                "MediaWiki\\Babel\\Database": "includes/Database.php",
                "MediaWiki\\Babel\\BabelBox\\BabelBox": "BabelBox/BabelBox.php",
                "MediaWiki\\Babel\\BabelBox\\LanguageBabelBox": 
"BabelBox/LanguageBabelBox.php",
                "MediaWiki\\Babel\\BabelBox\\NotBabelBox": 
"BabelBox/NotBabelBox.php",
                "MediaWiki\\Babel\\BabelBox\\NullBabelBox": 
"BabelBox/NullBabelBox.php"
        },
+       "APIMetaModules": {
+               "babel": "MediaWiki\\Babel\\ApiQueryBabel"
+       },
        "ResourceModules": {
                "ext.babel": {
                        "position": "top",
diff --git a/i18n/en.json b/i18n/en.json
index 01f0b9d..39fa59d 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -33,5 +33,8 @@
        "babel-cellpadding": "0",
        "babel-portal": "",
        "babel-template": "Template:User $1",
-       "babel-footer": "Users by language"
+       "babel-footer": "Users by language",
+       "apihelp-query+babel-description": "Get information about what 
languages the user knows",
+       "apihelp-query+babel-param-user": "User to get information about",
+       "apihelp-query+babel-example-1": "Get the Babel information for user 
<kbd>Example</kbd>"
 }
diff --git a/i18n/qqq.json b/i18n/qqq.json
index f3548dc..c2d6674 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -43,5 +43,8 @@
        "babel-cellpadding": "{{notranslate}}\nInserted as the value of the 
<code>cellpadding</code> attribute in the HTML tables of the inner boxes.",
        "babel-portal": "{{notranslate}}",
        "babel-template": "{{Optional}}{{doc-important|Do not translate 
\"Template:\".}}\nThis message contains the template schema for additional 
templates that can be used in Babel. Parameters:\n* $1 is the parameter value 
used in the <nowiki>#babel</nowiki> parser tag.",
-       "babel-footer": "This text is used in the last entry (footer) of the 
outer box. It is a link anchor, if {{msg-mw|babel-footer-url}} is not empty, 
otherwise, it is only text. No footer is generated, when this text is empty."
+       "babel-footer": "This text is used in the last entry (footer) of the 
outer box. It is a link anchor, if {{msg-mw|babel-footer-url}} is not empty, 
otherwise, it is only text. No footer is generated, when this text is empty.",
+       "apihelp-query+babel-description": 
"{{doc-apihelp-description|query+babel}}",
+       "apihelp-query+babel-param-user": 
"{{doc-apihelp-param|query+babel|user}}",
+       "apihelp-query+babel-example-1": "{{doc-apihelp-example|query+babel}}"
 }
diff --git a/includes/ApiQueryBabel.php b/includes/ApiQueryBabel.php
new file mode 100644
index 0000000..6947168
--- /dev/null
+++ b/includes/ApiQueryBabel.php
@@ -0,0 +1,74 @@
+<?php
+/**
+ * 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
+ *
+ * @file
+ */
+
+namespace MediaWiki\Babel;
+
+use ApiBase;
+use ApiQuery;
+use ApiQueryBase;
+use ApiResult;
+use Babel;
+use User;
+
+class ApiQueryBabel extends ApiQueryBase {
+       public function __construct( ApiQuery $queryModule, $moduleName ) {
+               parent::__construct( $queryModule, $moduleName, 'bab' );
+       }
+
+       public function execute() {
+               $params = $this->extractRequestParams();
+               $userName = $params['user'];
+               $user = User::newFromName( $userName );
+               if ( !$user || !$user->getId() ) {
+                       $this->dieUsage( 'User does not exist', 'baduser' );
+                       return;
+               }
+
+               $data = Babel::getUserLanguageInfo( $user );
+               // Force a JSON object
+               $data[ApiResult::META_TYPE] = 'assoc';
+
+               $this->getResult()->addValue(
+                       'query',
+                       $this->getModuleName(),
+                       $data
+               );
+       }
+
+       public function getAllowedParams( /* $flags = 0 */ ) {
+               return [
+                       'user' => [
+                               ApiBase::PARAM_REQUIRED => true,
+                               ApiBase::PARAM_TYPE => 'user',
+                       ]
+               ];
+       }
+
+       /**
+        * @see ApiBase::getExamplesMessages()
+        */
+       protected function getExamplesMessages() {
+               return [
+                       'action=query&meta=babel&babuser=Example'
+                               => 'apihelp-query+babel-example-1',
+               ];
+       }
+
+}
diff --git a/tests/phpunit/BabelTest.php b/tests/phpunit/BabelTest.php
index aeb3d52..10535fa 100644
--- a/tests/phpunit/BabelTest.php
+++ b/tests/phpunit/BabelTest.php
@@ -35,7 +35,7 @@
                $user = User::newFromName( 'User-1' );
                $user->addToDatabase();
                $title = $user->getUserPage();
-               $this->insertPage( $title->getPrefixedText(), '{{#babel:en-1}}' 
);
+               $this->insertPage( $title->getPrefixedText(), 
'{{#babel:en-1|es-2|de}}' );
                $page = WikiPage::factory( $title );
                // Force a run of LinksUpdate
                $updates = $page->getContent()->getSecondaryDataUpdates( $title 
);
@@ -219,9 +219,34 @@
        public function testGetUserLanguages( $settings ) {
                $this->setMwGlobals( $settings );
                $user = User::newFromName( 'User-1' );
-               $languages = Babel::getUserLanguages( $user );
                $this->assertSame( [
+                       'de',
                        'en',
+                       'es',
+               ], Babel::getUserLanguages( $user ) );
+
+               // Filter based on level
+               $this->assertSame( [
+                       'de',
+                       'es',
+               ], Babel::getUserLanguages( $user, '2' ) );
+
+               $this->assertSame( [
+                       'de',
+               ], Babel::getUserLanguages( $user, '3' ) );
+
+               // Non-numerical level
+               $this->assertSame( [
+                       'de',
+               ], Babel::getUserLanguages( $user, 'N' ) );
+       }
+       public function testGetUserLanguageInfo() {
+               $user = User::newFromName( 'User-1' );
+               $languages = Babel::getUserLanguageInfo( $user );
+               $this->assertSame( [
+                       'de' => 'N',
+                       'en' => '1',
+                       'es' => '2',
                ], $languages );
        }
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I8d24f1f8b65ece1be75432421fcd5c870403178d
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/extensions/Babel
Gerrit-Branch: master
Gerrit-Owner: Legoktm <legoktm.wikipe...@gmail.com>
Gerrit-Reviewer: Anomie <bjor...@wikimedia.org>
Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com>
Gerrit-Reviewer: Nikerabbit <niklas.laxst...@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