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

Change subject: Add the centralnoticebannerchoicedata API module
......................................................................


Add the centralnoticebannerchoicedata API module

Creates a Web API access point for banner choice data, for use when
direct inter-wiki DB queries are not possible.

This is part of a series of changes to shift banner selection partly
to the client. This change itself does not modify CN functionality
when serving banners.

Change-Id: Ia5abd44f92ecc3e378a55f566a78d075f3d1726b
---
M CentralNotice.hooks.php
A api/ApiCentralNoticeAllocationBase.php
M api/ApiCentralNoticeAllocations.php
A api/ApiCentralNoticeBannerChoiceData.php
M i18n/en.json
M i18n/qqq.json
6 files changed, 146 insertions(+), 44 deletions(-)

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



diff --git a/CentralNotice.hooks.php b/CentralNotice.hooks.php
index df9dce8..87a1c59 100644
--- a/CentralNotice.hooks.php
+++ b/CentralNotice.hooks.php
@@ -82,11 +82,14 @@
 
        $wgAutoloadClasses[ 'CNDatabasePatcher' ] = $dir . 
'patches/CNDatabasePatcher.php';
 
+       $wgAutoloadClasses[ 'ApiCentralNoticeAllocationBase' ] = $apiDir . 
'ApiCentralNoticeAllocationBase.php';
        $wgAutoloadClasses[ 'ApiCentralNoticeAllocations' ] = $apiDir . 
'ApiCentralNoticeAllocations.php';
+       $wgAutoloadClasses[ 'ApiCentralNoticeBannerChoiceData' ] = $apiDir . 
'ApiCentralNoticeBannerChoiceData.php';
        $wgAutoloadClasses[ 'ApiCentralNoticeQueryCampaign' ] = $apiDir . 
'ApiCentralNoticeQueryCampaign.php';
        $wgAutoloadClasses[ 'ApiCentralNoticeLogs' ] = $apiDir . 
'ApiCentralNoticeLogs.php';
 
        $wgAutoloadClasses[ 'CNDatabase' ] = $includeDir . 'CNDatabase.php';
+       $wgAPIModules[ 'centralnoticebannerchoicedata' ] = 
'ApiCentralNoticeBannerChoiceData';
        $wgAPIModules[ 'centralnoticeallocations' ] = 
'ApiCentralNoticeAllocations';
        $wgAPIModules[ 'centralnoticequerycampaign' ] = 
'ApiCentralNoticeQueryCampaign';
        $wgAPIListModules[ 'centralnoticelogs' ] = 'ApiCentralNoticeLogs';
diff --git a/api/ApiCentralNoticeAllocationBase.php 
b/api/ApiCentralNoticeAllocationBase.php
new file mode 100644
index 0000000..b5f914a
--- /dev/null
+++ b/api/ApiCentralNoticeAllocationBase.php
@@ -0,0 +1,31 @@
+<?php
+
+/**
+ * Base class for CentralNotice APIs that provide allocation data.
+ */
+abstract class ApiCentralNoticeAllocationBase extends ApiBase {
+
+       const LANG_FILTER = '/[a-zA-Z0-9\-]+/';
+
+       const PROJECT_FILTER = '/[a-zA-Z0-9_\-]+/';
+
+       /**
+        * @static Obtains the parameter $param, sanitizes by returning the 
first match to $regex or
+        * $default if there was no match.
+        *
+        * @param string    $param    Name of GET/POST parameter
+        * @param string    $regex    Sanitization regular expression
+        * @param string    $default  Default value to return on error
+        *
+        * @return string The sanitized value
+        */
+       protected static function sanitizeText( $param, $regex, $default = null 
) {
+               $matches = array();
+
+               if ( preg_match( $regex, $param, $matches ) ) {
+                       return $matches[ 0 ];
+               } else {
+                       return $default;
+               }
+       }
+}
diff --git a/api/ApiCentralNoticeAllocations.php 
b/api/ApiCentralNoticeAllocations.php
index acd7366..f0ee913 100644
--- a/api/ApiCentralNoticeAllocations.php
+++ b/api/ApiCentralNoticeAllocations.php
@@ -7,7 +7,7 @@
  * @todo: This needs some major cleanup to work more like the rest of the API,
  * starting with "this is a JSON only call".
  */
-class ApiCentralNoticeAllocations extends ApiBase {
+class ApiCentralNoticeAllocations extends ApiCentralNoticeAllocationBase {
 
        const DEFAULT_PROJECT = 'wikipedia';
        const DEFAULT_COUNTRY = 'XX';
@@ -17,21 +17,9 @@
        const DEFAULT_BUCKET = null;
 
        /**
-        * @var string Pattern for alphanum w/ -
-        */
-       const LANG_FILTER = '/[a-zA-Z0-9\-]+/';
-       /**
-        * @var string Pattern for alphanum w/ _ & -
-        */
-       const PROJECT_FILTER = '/[a-zA-Z0-9_\-]+/';
-       /**
         * @var string Pattern for 2 alphas
         */
        const LOCATION_FILTER = '/[a-zA-Z][a-zA-Z0-9]/';
-       /**
-        * @var string Pattern for bool
-        */
-       const ANONYMOUS_FILTER = '/true|false/';
        /**
         * @var string Pattern for a the requesting device type name, ie: 
desktop, iphone
         */
@@ -40,6 +28,12 @@
         * @var string Pattern for int
         */
        const BUCKET_FILTER = '/[0-9]+/';
+
+       // TODO make the anon param a boolean and remove this?
+       /**
+        * @var string Pattern for bool
+        */
+       const ANONYMOUS_FILTER = '/true|false/';
 
        public function execute() {
                // Obtain the ApiResults object from the base
@@ -158,38 +152,38 @@
         * @return array
         */
        public static function getBannerAllocation( $project, $country, 
$language, $anonymous, $device, $bucket = null ) {
-               $project = ApiCentralNoticeAllocations::sanitizeText(
+               $project = parent::sanitizeText(
                        $project,
-                       self::PROJECT_FILTER,
+                       parent::PROJECT_FILTER,
                        self::DEFAULT_PROJECT
                );
 
-               $country = ApiCentralNoticeAllocations::sanitizeText(
+               $country = parent::sanitizeText(
                        $country,
                        self::LOCATION_FILTER,
                        self::DEFAULT_COUNTRY
                );
 
-               $language = ApiCentralNoticeAllocations::sanitizeText(
+               $language = parent::sanitizeText(
                        $language,
-                       self::LANG_FILTER,
+                       parent::LANG_FILTER,
                        self::DEFAULT_LANGUAGE
                );
 
-               $anonymous = ApiCentralNoticeAllocations::sanitizeText(
+               $anonymous = parent::sanitizeText(
                        $anonymous,
                        self::ANONYMOUS_FILTER,
                        self::DEFAULT_ANONYMOUS
                );
                $anonymous = ( $anonymous == 'true' );
 
-               $device = ApiCentralNoticeAllocations::sanitizeText(
+               $device = parent::sanitizeText(
                        $device,
-                       static::DEVICE_NAME_FILTER,
-                       static::DEFAULT_DEVICE_NAME
+                       self::DEVICE_NAME_FILTER,
+                       self::DEFAULT_DEVICE_NAME
                );
 
-               $bucket = ApiCentralNoticeAllocations::sanitizeText(
+               $bucket = parent::sanitizeText(
                        $bucket,
                        self::BUCKET_FILTER,
                        self::DEFAULT_BUCKET
@@ -201,26 +195,5 @@
                $banners = $chooser->getBanners();
 
                return $banners;
-       }
-
-       /**
-        * @static Obtains the parameter $param, sanitizes by returning the 
first match to $regex or
-        * $default if there was no match.
-        *
-        * @params array    $params   Array of parameters to extract data from
-        * @param string    $param    Name of GET/POST parameter
-        * @param string    $regex    Sanitization regular expression
-        * @param string    $default  Default value to return on error
-        *
-        * @return string The sanitized value
-        */
-       private static function sanitizeText( $param, $regex, $default = null ) 
{
-               $matches = array();
-
-               if ( preg_match( $regex, $param, $matches ) ) {
-                       return $matches[ 0 ];
-               } else {
-                       return $default;
-               }
        }
 }
diff --git a/api/ApiCentralNoticeBannerChoiceData.php 
b/api/ApiCentralNoticeBannerChoiceData.php
new file mode 100644
index 0000000..d9402d7
--- /dev/null
+++ b/api/ApiCentralNoticeBannerChoiceData.php
@@ -0,0 +1,85 @@
+<?php
+
+/***
+ * Module for the centralnoticebannerchoicedata Web API.
+ *
+ * This is provided as a fallback mechanism for getting banner choice data
+ * from an infrastructure wiki, for cases in which direct cross-wiki DB
+ * queries are not possible.
+ */
+class ApiCentralNoticeBannerChoiceData extends ApiCentralNoticeAllocationBase {
+
+       /**
+        * Regex for filtering values of the status parameter
+        */
+       const STATUS_FILTER = '/loggedin|anonymous/';
+
+       public function execute() {
+
+               // Extract, sanitize and munge the parameters
+               $params = $this->extractRequestParams();
+
+               $project = parent::sanitizeText(
+                               $params['project'],
+                               parent::PROJECT_FILTER
+               );
+
+               $lang = parent::sanitizeText(
+                               $params['language'],
+                               parent::LANG_FILTER
+               );
+
+               $status = parent::sanitizeText(
+                               $params['status'],
+                               self::STATUS_FILTER
+               );
+
+               if ( $status === 'loggedin' ) {
+                       $status = BannerChoiceDataProvider::LOGGED_IN;
+               } else if ( $status === 'anonymous' ) {
+                       $status = BannerChoiceDataProvider::ANONYMOUS;
+               } else {
+                       $this->dieUsage(
+                               'Invalid status value: must be "loggedin" or 
"anonymous".',
+                               'invalid-status');
+               }
+
+               $choicesProvider = new BannerChoiceDataProvider(
+                       $project, $lang, $status, 
BannerChoiceDataProvider::USE_DEFAULT_DB );
+
+               $choices = $choicesProvider->getChoices();
+
+               // Get the result object for creating the output
+               $apiResult = $this->getResult();
+
+               $apiResult->addValue(
+                       null,
+                       'choices',
+                       $choices
+                );
+       }
+
+       public function getAllowedParams() {
+               return array(
+                       'project' => array(
+                                       ApiBase::PARAM_TYPE => 'string',
+                                       ApiBase::PARAM_REQUIRED => true
+                       ),
+                       'language' => array(
+                                       ApiBase::PARAM_TYPE => 'string',
+                                       ApiBase::PARAM_REQUIRED => true
+                       ),
+                       'status' => array(
+                                       ApiBase::PARAM_TYPE => 'string',
+                                       ApiBase::PARAM_REQUIRED => true
+                       )
+               );
+       }
+
+       protected function getExamplesMessages() {
+               return array(
+                       
'action=centralnoticebannerchoicedata&project=wikpedia&language=en&status=anonymous'
+                       => 'apihelp-centralnoticebannerchoicedata-example-1'
+               );
+       }
+}
\ No newline at end of file
diff --git a/i18n/en.json b/i18n/en.json
index fcf4188..0d6447b 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -242,6 +242,11 @@
        "apihelp-centralnoticequerycampaign-description": "Get all 
configuration settings for a campaign.",
        "apihelp-centralnoticequerycampaign-param-campaign": "Campaign name. 
Separate multiple values with a \"|\" (vertical bar).",
        "apihelp-centralnoticequerycampaign-example-1": "Show campaign 
\"Plea_US\"",
+       "apihelp-centralnoticebannerchoicedata-description": "Get data needed 
to choose a banner for a given project and language",
+       "apihelp-centralnoticebannerchoicedata-param-project": "The project to 
get banner choice data for.",
+       "apihelp-centralnoticebannerchoicedata-param-language": "The language 
to get banner choice data for.",
+       "apihelp-centralnoticebannerchoicedata-param-status": "The logged-in 
status to get banner choice data for (loggedin|anonymous).",
+       "apihelp-centralnoticebannerchoicedata-example-1": "Get the data for 
choosing a banner for English, Wikipedia, and anonymous users.",
        "apihelp-query+centralnoticelogs-description": "Get a log of campaign 
configuration changes.",
        "apihelp-query+centralnoticelogs-param-campaign": "Campaign name 
(optional). Separate multiple values with a \"|\" (vertical bar).",
        "apihelp-query+centralnoticelogs-param-user": "Username (optional).",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index cddf6bb..3ac2d03 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -262,6 +262,11 @@
        "apihelp-centralnoticeallocations-param-device": 
"{{doc-apihelp-param|centralnoticeallocations|device}}",
        "apihelp-centralnoticeallocations-param-bucket": 
"{{doc-apihelp-param|centralnoticeallocations|bucket}}",
        "apihelp-centralnoticeallocations-example-1": 
"{{doc-apihelp-example|centralnoticeallocations}}",
+       "apihelp-centralnoticebannerchoicedata-description": 
"{{doc-apihelp-description|centralnoticebannerchoicedata}}",
+       "apihelp-centralnoticebannerchoicedata-param-project": 
"{{doc-apihelp-param|centralnoticebannerchoicedata|project}}",
+       "apihelp-centralnoticebannerchoicedata-param-language": 
"{{doc-apihelp-param|centralnoticebannerchoicedata|language}}",
+       "apihelp-centralnoticebannerchoicedata-param-status": 
"{{doc-apihelp-param|centralnoticebannerchoicedata|status}}",
+       "apihelp-centralnoticebannerchoicedata-example-1": 
"{{doc-apihelp-example|centralnoticebannerchoicedata}}",
        "apihelp-centralnoticequerycampaign-description": 
"{{doc-apihelp-description|centralnoticequerycampaign}}",
        "apihelp-centralnoticequerycampaign-param-campaign": 
"{{doc-apihelp-param|centralnoticequerycampaign|campaign}}",
        "apihelp-centralnoticequerycampaign-example-1": 
"{{doc-apihelp-example|centralnoticequerycampaign}}",

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ia5abd44f92ecc3e378a55f566a78d075f3d1726b
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/CentralNotice
Gerrit-Branch: master
Gerrit-Owner: AndyRussG <[email protected]>
Gerrit-Reviewer: Awight <[email protected]>
Gerrit-Reviewer: Ejegg <[email protected]>
Gerrit-Reviewer: Mwalker <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: Ssmith <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to