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

Change subject: Add an option to load a specific survey
......................................................................


Add an option to load a specific survey

This will help with testing.

The query string 'quicksurvey' is used to load a specific survey. If its
value is:
  * 'true', then load a random survey
  * 'internal-survey-XXX', then load the internal survey with the name 'XXX'
  * 'external-survey-XXX', then load the external survey with the name 'XXX'

In all of the above cases the bucketing is bypassed.

Bug: T110225
Change-Id: I88bf88f436730f4d9ab8e10fd7d69ca9c6821b3c
---
M includes/ExternalSurvey.php
M includes/InternalSurvey.php
M resources/ext.quicksurveys.init/init.js
3 files changed, 65 insertions(+), 5 deletions(-)

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



diff --git a/includes/ExternalSurvey.php b/includes/ExternalSurvey.php
index 568de8f..46875ac 100644
--- a/includes/ExternalSurvey.php
+++ b/includes/ExternalSurvey.php
@@ -5,6 +5,11 @@
 class ExternalSurvey extends Survey
 {
        /**
+        * @var string The name of the external survey.
+        */
+       private $name;
+
+       /**
         * @var string The URL of the external survey.
         */
        private $link;
@@ -25,6 +30,7 @@
        ) {
                parent::__construct( $name, $question, $description, 
$isEnabled, $coverage );
 
+               $this->name = $name;
                $this->link = $link;
                $this->privacyPolicy = $privacyPolicy;
        }
@@ -35,6 +41,7 @@
 
        public function toArray() {
                return parent::toArray() + array(
+                       'name' => $this->name,
                        'type' => 'external',
                        'link' => $this->link,
                        'privacyPolicy' => $this->privacyPolicy,
diff --git a/includes/InternalSurvey.php b/includes/InternalSurvey.php
index 5ce74d6..6bcd135 100644
--- a/includes/InternalSurvey.php
+++ b/includes/InternalSurvey.php
@@ -5,6 +5,10 @@
 class InternalSurvey extends Survey
 {
        /**
+        * @var string The name of the internal survey.
+        */
+       private $name;
+       /**
         * @var array A map of internal key, e.g. "positive", to a i18n message 
key
         */
        private $answers;
@@ -19,6 +23,7 @@
        ) {
                parent::__construct( $name, $question, $description, 
$isEnabled, $coverage );
 
+               $this->name = $name;
                $this->answers = $answers;
        }
 
@@ -28,6 +33,7 @@
 
        public function toArray() {
                return parent::toArray() + array(
+                       'name' => $this->name,
                        'type' => 'internal',
                        'answers' => $this->answers,
                );
diff --git a/resources/ext.quicksurveys.init/init.js 
b/resources/ext.quicksurveys.init/init.js
index e800293..ee2eee8 100644
--- a/resources/ext.quicksurveys.init/init.js
+++ b/resources/ext.quicksurveys.init/init.js
@@ -18,8 +18,15 @@
        $( enabledSurveys ).each( function ( i, enabledSurvey ) {
                // Setting the quicksurvey param makes every enabled survey 
available
                if ( mw.util.getParamValue( 'quicksurvey' ) ) {
-                       availableSurveys.push( enabledSurvey );
-                       return;
+                       // Setting the param quicksurvey bypasses the bucketing
+                       enabledSurvey = getSurveyFromQueryString(
+                               mw.util.getParamValue( 'quicksurvey' ) || '',
+                               enabledSurveys
+                       );
+                       if ( enabledSurvey ) {
+                               availableSurveys.push( enabledSurvey );
+                       }
+                       return false;
                } else if (
                        getSurveyToken( enabledSurvey ) !== '~' &&
                        getBucketForSurvey( enabledSurvey ) === 'A'
@@ -30,7 +37,7 @@
 
        if ( availableSurveys.length ) {
                // Get a random available survey
-               survey = availableSurveys[Math.floor( Math.random() * 
availableSurveys.length )];
+               survey = availableSurveys[ Math.floor( Math.random() * 
availableSurveys.length ) ];
                $bodyContent = $( '.mw-content-ltr, .mw-content-rtl' );
                $place = $bodyContent.find( '> .thumb, > h1, > h2, > h3, > h4, 
> h5, > h6' ).eq( 0 );
 
@@ -66,7 +73,7 @@
         * Get the bucket for the given survey.
         * Initializes the survey storage with a token
         *
-        * @returns {String} The bucket
+        * @return {String} The bucket
         */
        function getBucketForSurvey( survey ) {
                var control = 1 - survey.coverage,
@@ -91,7 +98,7 @@
        /**
         * Get the storage key for the given survey.
 
-        * @returns {String} The survey localstorage key
+        * @return {String} The survey localStorage key
         */
        function getSurveyStorageKey( survey ) {
                return 'ext-quicksurvey-' + survey.name.replace( / /g, '-' );
@@ -106,4 +113,44 @@
                return mw.storage.get( getSurveyStorageKey( survey ) );
        }
 
+       /**
+        * Return a survey from the available surveys given the survey query 
string.
+        * If the query string is 'true' return a random survey.
+        * If the query string is either 'internal-survey-XXX' or 
'external-survey-XXX' where
+        * 'XXX' is the survey name, then return the survey with the name XXX.
+        * Return null in the remaining cases.
+        *
+        * @param {String} queryString query string
+        * @param {Array} availableSurveys array of survey objects
+        * @return {Object|null} Survey object or null
+        */
+       function getSurveyFromQueryString( queryString, availableSurveys ) {
+               var surveyIndex,
+                       surveyType,
+                       surveyName,
+                       survey = null;
+
+               // true returns a random survey
+               if ( queryString === 'true' ) {
+                       surveyIndex = Math.floor( Math.random() * 
availableSurveys.length );
+                       survey = availableSurveys[ surveyIndex ];
+               } else if ( queryString.indexOf( 'internal-survey-' ) === 0 ) {
+                       surveyType = 'internal';
+               } else if ( queryString.indexOf( 'external-survey-' ) === 0 ) {
+                       surveyType = 'external';
+               }
+
+               if ( surveyType ) {
+                       surveyName = queryString.split( '-' ).slice( 2 ).join( 
'-' );
+                       availableSurveys = $.grep( availableSurveys, function ( 
survey ) {
+                               return survey.name === surveyName && 
survey.type === surveyType;
+                       } );
+                       if ( availableSurveys.length ) {
+                               survey = availableSurveys[ 0 ];
+                       }
+               }
+
+               return survey;
+       }
+
 }( jQuery ) );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I88bf88f436730f4d9ab8e10fd7d69ca9c6821b3c
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/extensions/QuickSurveys
Gerrit-Branch: dev
Gerrit-Owner: Bmansurov <[email protected]>
Gerrit-Reviewer: Bmansurov <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to