[MediaWiki-commits] [Gerrit] mediawiki...Babel[master]: Add API action=query&meta=babel module
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 Example" } 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 cellpadding 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 #babel 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, o
[MediaWiki-commits] [Gerrit] mediawiki...Babel[master]: Add API action=query&meta=babel module
Legoktm has uploaded a new change for review. https://gerrit.wikimedia.org/r/309948 Change subject: Add API action=query&meta=babel module .. Add API action=query&meta=babel module Change-Id: I8d24f1f8b65ece1be75432421fcd5c870403178d --- M extension.json M i18n/en.json M i18n/qqq.json A includes/ApiQueryBabel.php 4 files changed, 86 insertions(+), 2 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Babel refs/changes/48/309948/1 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..4228770 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", + "action=query&meta=babel&babuser=Example": "Get the Babel information for user:Example" } diff --git a/i18n/qqq.json b/i18n/qqq.json index f3548dc..43d45ad 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -43,5 +43,8 @@ "babel-cellpadding": "{{notranslate}}\nInserted as the value of the cellpadding 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 #babel 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}}", + "action=query&meta=babel&babuser=Example": "{{doc-apihelp-example|query+babel}}" } diff --git a/includes/ApiQueryBabel.php b/includes/ApiQueryBabel.php new file mode 100644 index 000..5c988c0 --- /dev/null +++ b/includes/ApiQueryBabel.php @@ -0,0 +1,74 @@ +http://www.gnu.org/copyleft/gpl.html + * + * @file + */ + +namespace MediaWiki\Babel; + +use ApiBase; +use ApiQuery; +use ApiQueryBase; +use ApiResult; +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; + } + + $babelDB = new Database(); + $data = $babelDB->getForUser( $user->getId() ); + // 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, +