Page MenuHomePhabricator

Allow developers to call showSurvey() without unintentionally showing another survey
Closed, ResolvedPublic3 Estimated Story Points

Description

Background

If, for example, you want to show a survey after a user has completed an action (including outside of mainspace), then you have to use QuickSurveys' showSurvey() method:

const require = await mw.loader.using( 'ext.quicksurveys.lib' );

require( 'ext.quicksurveys.lib' ).showSurvey( 'foo' );

The ext.quicksurveys.lib module loads the configuration for all enabled surveys. Further, the ext.quicksurveys.lib module also calls showSurvey() unconditionally in order to show a survey to the user on page load. Unfortunately, this means that an unrelated survey may be shown when using the showSurvey() as above.

Solution

Move the unconditional showSurvey() call into the ext.quicksurveys.init.

The ext.quicksurveys.init module is one that has no assets and no dependencies when no surveys are configured and depends on the ext.quicksurveys.lib module when one or more surveys are configured. We could move the showSurvey() call to this module, i.e.

includes/ResourceLoader/QuickSurveysInitModule.php
<?php

namespace QuickSurveys\ResourceLoader;

use MediaWiki\MediaWikiServices;
use ResourceLoaderContext;

class InitModule extends \ResourceLoaderFileModule {
  public function getScript( ResourceLoaderContext $context ) {
    if ( !$this->hasEnabledSurveys() ) {
      return '';
    }

    return <<<JS
var FORCED_SURVEY = mw.util.getParamValue( 'quicksurvey' );

require( 'ext.quicksurveys.lib' ).showSurvey( FORCED_SURVEY );
JS;
  }

  public function getDependencies( ResourceLoaderContext $context = null ) { /* ... */  }

  private function hasEnabledSurveys(): bool { /* ... */ }
}

Event Timeline

phuedx renamed this task from Allow developers to call showSurvey() without side effects to Allow developers to call showSurvey() without unintentionally showing another survey.May 9 2022, 7:02 PM

Change 820784 had a related patch set uploaded (by Tchanders; author: Tchanders):

[mediawiki/extensions/QuickSurveys@master] Move unconditional call to showSurvey to init module from lib module

https://gerrit.wikimedia.org/r/820784

@phuedx We've made a patch for this since we now have a task depending on it. Would you have any time to review it?

Tchanders set the point value for this task to 3.Aug 11 2022, 2:36 PM

Change 820784 merged by jenkins-bot:

[mediawiki/extensions/QuickSurveys@master] Move unconditional call to showSurvey to init module from lib module

https://gerrit.wikimedia.org/r/820784

dom_walden subscribed.

I am not sure I understand this change, but I followed these instructions in the patch. The correct survey was shown when I ran step 9 (assuming I entered the correct value for showSurvey()).

I have also been testing T313415, which depends on this work, and I haven't found any issues yet.

Test environment: local docker QuickSurveys 1.4.0 (1d4eed3) 20:13, 11 August 2022.