Bmansurov has uploaded a new change for review.

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

Change subject: WIP: WikiGrok: show suggestions that are already on the page.
......................................................................

WIP: WikiGrok: show suggestions that are already on the page.

Question types include: author, actor, and album

Change-Id: I2bf8c94703f7e5d49c3a60cf8885f7a1194e7726
---
M includes/Resources.php
A javascripts/modules/wikigrok/WikiGrokCampaign.js
M javascripts/modules/wikigrok/WikiGrokDialog.js
3 files changed, 176 insertions(+), 52 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/34/175934/1

diff --git a/includes/Resources.php b/includes/Resources.php
index a7f1a42..628c002 100644
--- a/includes/Resources.php
+++ b/includes/Resources.php
@@ -942,6 +942,7 @@
                        'javascripts/modules/wikigrok/WikiDataApi.js',
                        'javascripts/modules/wikigrok/WikiGrokSuggestionApi.js',
                        'javascripts/modules/wikigrok/WikiGrokResponseApi.js',
+                       'javascripts/modules/wikigrok/wikiGrokCampaign.js',
                ),
        ),
 
diff --git a/javascripts/modules/wikigrok/WikiGrokCampaign.js 
b/javascripts/modules/wikigrok/WikiGrokCampaign.js
new file mode 100644
index 0000000..041a12f
--- /dev/null
+++ b/javascripts/modules/wikigrok/WikiGrokCampaign.js
@@ -0,0 +1,52 @@
+( function ( $, mw, M ) {
+       var wikiGrokCampaign = {
+               /**
+                * Randomly pick a property
+                * @param object
+                * @returns {String} object's property
+                */
+               getRandomProperty: function ( object ) {
+                       var result,
+                               property,
+                               count = 0;
+
+                       for ( property in object ) {
+                               if ( object.hasOwnProperty( property ) ) {
+                                       if ( Math.random() <= 1 / ++count ) {
+                                               result = property;
+                                       }
+                               }
+                       }
+                       return result;
+               },
+
+               /**
+                * Get WikiGrok campaigns that are present on the page
+                * @returns {Object/null} campaigns
+                */
+               getCampaigns: function () {
+                       return mw.config.get( 'wgWikiGrokCampaigns' );
+               },
+
+               /**
+                * Randomly pick a campaign name
+                * @returns {String} campaign name
+                */
+               getRandomCampaignName: function () {
+                       return this.getRandomProperty( this.getCampaigns() );
+               },
+
+               /**
+                * Randomly pick a claimId
+                * @param campaign
+                * @returns {String} claimId
+                */
+               getRandomClaimId: function ( campaign ) {
+                       return this.getRandomProperty( 
this.getCampaigns()[campaign].questions );
+               }
+
+       };
+
+       M.define( 'modules/wikigrok/wikiGrokCampaign', wikiGrokCampaign );
+
+} ( jQuery, mw, mw.mobileFrontend ) );
diff --git a/javascripts/modules/wikigrok/WikiGrokDialog.js 
b/javascripts/modules/wikigrok/WikiGrokDialog.js
index c12a29b..1c5f78d 100644
--- a/javascripts/modules/wikigrok/WikiGrokDialog.js
+++ b/javascripts/modules/wikigrok/WikiGrokDialog.js
@@ -5,6 +5,7 @@
                WikiGrokSuggestionApi = M.require( 
'modules/wikigrok/WikiGrokSuggestionApi' ),
                WikiGrokResponseApi = M.require( 
'modules/wikigrok/WikiGrokResponseApi' ),
                WikiDataApi = M.require( 'modules/wikigrok/WikiDataApi' ),
+               wikiGrokCampaign = M.require( 
'modules/wikigrok/wikiGrokCampaign' ),
                schema = M.require( 'loggingSchemas/mobileWebWikiGrok' ),
                errorSchema = M.require( 
'loggingSchemas/mobileWebWikiGrokError' ),
                WikiGrokDialog,
@@ -50,13 +51,20 @@
                initialize: function ( options ) {
                        var self = this;
 
+                       self.campaigns = wikiGrokCampaign.getCampaigns();
+                       self.vowels = [ 'a', 'e', 'i', 'o', 'u' ];
+
                        // Remove any disambiguation parentheticals from the 
title.
                        options.name = options.title.replace( / \(.+\)$/, '' );
-                       this.apiWikiGrokSuggestion = new WikiGrokSuggestionApi( 
{
-                               itemId: options.itemId,
-                               subject: options.name,
-                               version: this.version
-                       } );
+
+                       // only load suggestions from the API if it is not 
available on page load
+                       if ( !self.campaigns ) {
+                               this.apiWikiGrokSuggestion = new 
WikiGrokSuggestionApi( {
+                                       itemId: options.itemId,
+                                       subject: options.name,
+                                       version: this.version
+                               } );
+                       }
                        this.apiWikiGrokResponse = new WikiGrokResponseApi( {
                                itemId: options.itemId,
                                subject: options.name,
@@ -155,15 +163,81 @@
                },
 
                /**
+                * Shows a question with a yes, no and not sure answer.
+                * @param options
+                */
+               askQuestion: function ( options ) {
+                       // Re-render with new content for 'Question' step
+                       options.beginQuestions = true;
+                       options.buttons = [
+                               {
+                                       classes: 'yes inline mw-ui-button 
mw-ui-progressive',
+                                       label: 'Yes'
+                               },
+                               {
+                                       classes: 'not-sure inline mw-ui-button',
+                                       label: 'Not Sure'
+                               },
+                               {
+                                       classes: 'no inline mw-ui-button 
mw-ui-progressive',
+                                       label: 'No'
+                               }
+                       ];
+                       options.noticeMsg = 'All submissions are <a 
class="wg-notice-link" href="#/wikigrok/about">released freely</a>';
+                       this.render( options );
+               },
+
+               /**
+                * Creates a question with a yes, no and not sure answer.
+                * Makes API request to Wikidata to retrieve labels and uses 
campaigns for that.
+                * FIXME: No i18n
+                * @method
+                * @param {Object} options needed to render.
+                */
+               askCampaignQuestion: function ( options ) {
+                       var self = this,
+                               campaignName = 
wikiGrokCampaign.getRandomCampaignName();
+
+                       options.claimId = wikiGrokCampaign.getRandomClaimId( 
campaignName );
+                       options.suggestion = {
+                               id: self.campaigns[campaignName].property,
+                               name: campaignName
+                       };
+
+                       self.apiWikiData.getLabels( [ options.claimId ] ).done( 
function ( labels ) {
+                               options.claimLabel = labels[ options.claimId ];
+                               if ( options.claimLabel ) {
+                                       if ( campaignName === 'author' ) {
+                                               // Hack for English prototype
+                                               if ( $.inArray( 
options.claimLabel.charAt( 0 ), self.vowels ) === -1 ) {
+                                                       options.contentMsg = 
'Is ' + options.name + ' a ' + options.claimLabel + '?';
+                                               } else {
+                                                       options.contentMsg = 
'Is ' + options.name + ' an ' + options.claimLabel + '?';
+                                               }
+                                       } else if ( campaignName === 'actor' ) {
+                                               // FIXME: Is $Person a 
[film/television] actor?
+                                       } else if ( campaignName === 'album' ) {
+                                               // FIXME: Is this a 
[studio/live] album?
+                                       }
+
+                                       self.askQuestion( options );
+                               } else {
+                                       self.showError( options, 'There was an 
error retrieving tag labels.' );
+                               }
+                       } ).fail( function () {
+                               self.logError( 
'no-impression-cannot-fetch-labels' );
+                       } );
+               },
+
+               /**
                 * Creates a question with a yes, no and not sure answer
-                * Makes API request to Wikidata to retrieve labels.
+                * Makes API request to Wikidata to retrieve labels and uses 
tools labs suggestions for that.
                 * FIXME: No i18n
                 * @method
                 * @param {Object} options needed to render.
                 */
                askWikidataQuestion: function ( options ) {
                        var self = this,
-                               vowels = [ 'a', 'e', 'i', 'o', 'u' ],
                                theCountries = [ 'United States', 'United 
Kingdom', 'Philippines',
                                        'Marshall Islands', 'Central African 
Republic' ];
 
@@ -181,7 +255,7 @@
                                                // FIXME: add support for DOB 
and DOD
                                                if ( options.suggestion.name 
=== 'occupations' ) {
                                                        // Hack for English 
prototype
-                                                       if ( $.inArray( 
options.claimLabel.charAt( 0 ), vowels ) === -1 ) {
+                                                       if ( $.inArray( 
options.claimLabel.charAt( 0 ), self.vowels ) === -1 ) {
                                                                
options.contentMsg = 'Was ' + options.name + ' a ' + options.claimLabel + '?';
                                                        } else {
                                                                
options.contentMsg = 'Was ' + options.name + ' an ' + options.claimLabel + '?';
@@ -196,24 +270,7 @@
                                                        options.contentMsg = 
'Did ' + options.name + ' attend ' + options.claimLabel + '?';
                                                }
 
-                                               // Re-render with new content 
for 'Question' step
-                                               options.beginQuestions = true;
-                                               options.buttons = [
-                                                       {
-                                                               classes: 'yes 
inline mw-ui-button mw-ui-progressive',
-                                                               label: 'Yes'
-                                                       },
-                                                       {
-                                                               classes: 
'not-sure inline mw-ui-button',
-                                                               label: 'Not 
Sure'
-                                                       },
-                                                       {
-                                                               classes: 'no 
inline mw-ui-button mw-ui-progressive',
-                                                               label: 'No'
-                                                       }
-                                               ];
-                                               options.noticeMsg = 'All 
submissions are <a class="wg-notice-link" href="#/wikigrok/about">released 
freely</a>';
-                                               self.render( options );
+                                               self.askQuestion( options );
                                        } else {
                                                self.showError( options, 'There 
was an error retrieving tag labels.' );
                                        }
@@ -246,12 +303,18 @@
                                };
 
                        // FIXME: add support for DOB and DOD
-                       if ( options.suggestion.name === 'occupations' ) {
+                       if (
+                               options.suggestion.name === 'occupations' ||
+                               options.suggestion.name === 'author' ||
+                               options.suggestion.name === 'actor'
+                       ) {
                                claim.prop = 'occupation';
                        } else if ( options.suggestion.name === 'nationality' ) 
{
                                claim.prop = 'nationality';
                        } else if ( options.suggestion.name === 'schools' ) {
                                claim.prop = 'alma mater';
+                       } else if ( options.suggestion.name === 'album' ) {
+                               claim.prop = 'instance of';
                        }
 
                        this.apiWikiGrokResponse.recordClaims( [ claim ] 
).always( function () {
@@ -398,7 +461,11 @@
                                this.$( '.wg-buttons .proceed' ).on( 'click', 
function () {
                                        self.log( 'widget-click-accept' );
                                        // Proceed with asking the user a 
metadata question.
-                                       self.askWikidataQuestion( options );
+                                       if ( self.campaigns ) {
+                                               self.askCampaignQuestion( 
options );
+                                       } else {
+                                               self.askWikidataQuestion( 
options );
+                                       }
                                } );
                                // Log more info clicks
                                this.$( '.wg-notice-link' ).on( 'click', 
function () {
@@ -422,30 +489,34 @@
                                self.show();
                        } else {
                                options.suggestions = [];
-                               self.apiWikiData.getClaims().done( function ( 
claims ) {
-                                       if ( claims.isHuman ) {
-                                               
self.apiWikiGrokSuggestion.getSuggestions().fail( function () {
-                                                       self.logError( 
'no-impression-cannot-fetch-suggestions' );
-                                               } ).done( function ( 
suggestions ) {
-                                                       // FIXME: add support 
for DOB and DOD
-                                                       if ( 
suggestions.occupations && suggestions.occupations.list.length ) {
-                                                               
options.suggestions.push( suggestions.occupations );
-                                                       }
-                                                       if ( 
suggestions.nationalities && suggestions.nationalities.list.length ) {
-                                                               
options.suggestions.push( suggestions.nationalities );
-                                                       }
-                                                       if ( 
suggestions.schools && suggestions.schools.list.length ) {
-                                                               
options.suggestions.push( suggestions.schools );
-                                                       }
-                                                       if ( 
options.suggestions.length ) {
-                                                               self.show();
-                                                       } else {
-                                                               // FIXME: 
remove this before deploying to stable
-                                                               self.logError( 
'no-impression-not-enough-suggestions' );
-                                                       }
-                                               } );
-                                       }
-                               } );
+                               if ( self.campaigns ) {
+                                       self.show();
+                               } else {
+                                       self.apiWikiData.getClaims().done( 
function ( claims ) {
+                                               if ( claims.isHuman ) {
+                                                       
self.apiWikiGrokSuggestion.getSuggestions().fail( function () {
+                                                               self.logError( 
'no-impression-cannot-fetch-suggestions' );
+                                                       } ).done( function ( 
suggestions ) {
+                                                               // FIXME: add 
support for DOB and DOD
+                                                               if ( 
suggestions.occupations && suggestions.occupations.list.length ) {
+                                                                       
options.suggestions.push( suggestions.occupations );
+                                                               }
+                                                               if ( 
suggestions.nationalities && suggestions.nationalities.list.length ) {
+                                                                       
options.suggestions.push( suggestions.nationalities );
+                                                               }
+                                                               if ( 
suggestions.schools && suggestions.schools.list.length ) {
+                                                                       
options.suggestions.push( suggestions.schools );
+                                                               }
+                                                               if ( 
options.suggestions.length ) {
+                                                                       
self.show();
+                                                               } else {
+                                                                       // 
FIXME: remove this before deploying to stable
+                                                                       
self.logError( 'no-impression-not-enough-suggestions' );
+                                                               }
+                                                       } );
+                                               }
+                                       } );
+                               }
                        }
                }
        } );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2bf8c94703f7e5d49c3a60cf8885f7a1194e7726
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Bmansurov <[email protected]>

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

Reply via email to