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

Change subject: Using same API data for wikigrokeval and askWikidataQuestion
......................................................................


Using same API data for wikigrokeval and askWikidataQuestion

This saves us an API request and also makes sure that we only load
the WikiGrok interface when there are potential occupations.

Bug: 71335
Change-Id: I93ceb1ec860e8e2e8087a0bd7b88958e504ac6e9
---
M javascripts/modules/wikigrok/WikiDataApi.js
M javascripts/modules/wikigrok/WikiGrokApi.js
M javascripts/modules/wikigrok/WikiGrokDialog.js
M javascripts/modules/wikigrok/wikigrokeval.js
4 files changed, 66 insertions(+), 85 deletions(-)

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



diff --git a/javascripts/modules/wikigrok/WikiDataApi.js 
b/javascripts/modules/wikigrok/WikiDataApi.js
index 8dad673..79f491f 100644
--- a/javascripts/modules/wikigrok/WikiDataApi.js
+++ b/javascripts/modules/wikigrok/WikiDataApi.js
@@ -1,4 +1,4 @@
-( function( M, $ ) {
+( function( M ) {
        var Api = M.require( 'api' ).Api, WikiDataApi;
        /**
         * @class EditorApi
@@ -9,33 +9,8 @@
                useJsonp: true,
 
                initialize: function( options ) {
-                       this.itemId = options.itemId;
+                       this.subjectId = options.itemId;
                        Api.prototype.initialize.apply( this, arguments );
-               },
-               getClaims: function() {
-                       var id = this.itemId;
-                       return this.ajax( {
-                               action: 'wbgetentities',
-                               ids: this.itemId,
-                               props: 'claims',
-                               format: 'json'
-                       } ).then( function( data ) {
-                               var instanceClaims,
-                                       claims = {};
-                               // See if the page has any 'instance of' claims.
-                               if ( data.entities !== undefined && 
data.entities[id].claims.P31 !== undefined ) {
-                                       instanceClaims = 
data.entities[id].claims.P31;
-
-                                       // Examine claims closely
-                                       $.each( instanceClaims, function( i, 
claim ) {
-                                               claims.isHuman = 
claim.mainsnak.datavalue.value['numeric-id'] === 5 ? true : false;
-                                               claims.hasOccupation = 
data.entities[id].claims.P106 ? true : false;
-                                       } );
-                                       return claims;
-                               } else {
-                                       return false;
-                               }
-                       } );
                },
                /**
                 * Get labels for an item from Wikidata
@@ -63,4 +38,4 @@
 
        M.define( 'modules/wikigrok/WikiDataApi', WikiDataApi );
 
-}( mw.mobileFrontend, jQuery ) );
+}( mw.mobileFrontend ) );
diff --git a/javascripts/modules/wikigrok/WikiGrokApi.js 
b/javascripts/modules/wikigrok/WikiGrokApi.js
index cc43f2d..273cb68 100644
--- a/javascripts/modules/wikigrok/WikiGrokApi.js
+++ b/javascripts/modules/wikigrok/WikiGrokApi.js
@@ -9,13 +9,13 @@
                useJsonp: true,
 
                initialize: function( options ) {
-                       this.itemId = options.itemId;
+                       this.subjectId = options.itemId;
                        Api.prototype.initialize.apply( this, arguments );
                },
                recordOccupation: function( subject, occupationId, occupation, 
claimIsCorrect ) {
                        return this.ajax( {
                                        action: 'record_answer',
-                                       subject_id: this.itemId,
+                                       subject_id: this.subjectId,
                                        subject: subject,
                                        occupation_id: occupationId,
                                        occupation: occupation,
@@ -29,9 +29,9 @@
                        return this.ajax( {
                                        action: 'get_potential_occupations',
                                        // Strip the Q out of the Wikibase item 
ID
-                                       item: this.itemId.replace( 'Q' , '' )
+                                       item: this.subjectId.replace( 'Q' , '' )
                                } ).then( function( data ) {
-                                       if ( data.occupations !== undefined ) {
+                                       if ( data.occupations !== undefined && 
data.occupations ) {
                                                return data.occupations.split( 
',' ).map( function( item ) {
                                                        return 'Q' + item;
                                                } );
diff --git a/javascripts/modules/wikigrok/WikiGrokDialog.js 
b/javascripts/modules/wikigrok/WikiGrokDialog.js
index f69127a..57ad917 100644
--- a/javascripts/modules/wikigrok/WikiGrokDialog.js
+++ b/javascripts/modules/wikigrok/WikiGrokDialog.js
@@ -9,7 +9,7 @@
 
        /**
         * @class WikiGrokDialog
-        * @extends InlineDialog
+        * @extends Panel
         * THIS IS AN EXPERIMENTAL FEATURE THAT MAY BE MOVED TO A SEPARATE 
EXTENSION.
         * This creates the dialog at the bottom of the lead section that 
appears when a user
         * scrolls past the lead. It asks the user to confirm metadata 
information for use
@@ -50,41 +50,47 @@
                },
 
                askWikidataQuestion: function( options ) {
-                       var self = this;
+                       var self = this,
+                               occupationArray = options.occupations;
 
-                       this.apiWikiGrok.getPossibleOccupations().done( 
function( occupationArray ) {
-                               // If there are potential occupations for this 
person, select one at
-                               // random and ask if it is a correct occupation 
for the person.
-                               if ( occupationArray.length ) {
-                                       // Choose a random occupation from the 
list of possible occupations.
-                                       options.occupationId = occupationArray[ 
Math.floor( Math.random() * occupationArray.length ) ];
-                                       // Remove any disambiguation 
parentheticals from the title.
-                                       options.name = mw.config.get( 'wgTitle' 
).replace( / \(.+\)$/, '' );
+                       // If there are potential occupations for this person, 
select one at
+                       // random and ask if it is a correct occupation for the 
person.
+                       if ( occupationArray.length ) {
+                               // Choose a random occupation from the list of 
possible occupations.
+                               options.occupationId = occupationArray[ 
Math.floor( Math.random() * occupationArray.length ) ];
+                               // Remove any disambiguation parentheticals 
from the title.
+                               options.name = mw.config.get( 'wgTitle' 
).replace( / \(.+\)$/, '' );
 
-                                       // Get the name of the occupation from 
Wikidata.
-                                       self.apiWikiData.getLabel( 
options.occupationId ).done( function( label ) {
-                                               var vowels = [ 'a', 'e', 'i', 
'o', 'u' ];
-                                               if ( label ) {
-                                                       // Re-render with new 
content for 'Question' step
-                                                       options.beginQuestions 
= true;
-                                                       options.occupation = 
label;
-                                                       // Hack for English 
prototype
-                                                       if ( $.inArray( 
options.occupation.charAt(0), vowels ) === -1 ) {
-                                                               
options.contentMsg = 'Was ' + options.name + ' a ' + options.occupation + '?';
-                                                       } else {
-                                                               
options.contentMsg = 'Was ' + options.name + ' an ' + options.occupation + '?';
-                                                       }
-                                                       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 );
+                               // Get the name of the occupation from Wikidata.
+                               self.apiWikiData.getLabel( options.occupationId 
).done( function( label ) {
+                                       var vowels = [ 'a', 'e', 'i', 'o', 'u' 
];
+                                       if ( label ) {
+                                               // Re-render with new content 
for 'Question' step
+                                               options.beginQuestions = true;
+                                               options.occupation = label;
+                                               // Hack for English prototype
+                                               if ( $.inArray( 
options.occupation.charAt(0), vowels ) === -1 ) {
+                                                       options.contentMsg = 
'Was ' + options.name + ' a ' + options.occupation + '?';
+                                               } else {
+                                                       options.contentMsg = 
'Was ' + options.name + ' an ' + options.occupation + '?';
                                                }
-                                       } );
-                               }
-                       } );
+                                               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 );
+                                       }
+                                       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 );
+                               } );
+                       }
                },
 
                // Record answer in temporary database for analysis.
@@ -120,6 +126,8 @@
                postRender: function( options ) {
                        var self = this;
 
+                       // If you're wondering where the DOM insertion happens, 
look in wikigrokeval.js.
+
                        // Initialize all the buttons and links
                        // ...for final 'Thanks' step
                        if ( options.thankUser ) {
diff --git a/javascripts/modules/wikigrok/wikigrokeval.js 
b/javascripts/modules/wikigrok/wikigrokeval.js
index 23198ce..66f56f7 100644
--- a/javascripts/modules/wikigrok/wikigrokeval.js
+++ b/javascripts/modules/wikigrok/wikigrokeval.js
@@ -1,28 +1,26 @@
 ( function( M, $ ) {
-       var api,
-               wikidataID = mw.config.get( 'wgWikibaseItemId' ),
-               WikiDataApi = M.require( 'modules/wikigrok/WikiDataApi' ),
-               WikiGrokDialog = M.require( 'modules/wikigrok/WikiGrokDialog' );
+       var wikidataID = mw.config.get( 'wgWikibaseItemId' ),
+               WikiGrokApi = M.require( 'modules/wikigrok/WikiGrokApi' ),
+               WikiGrokDialog = M.require( 'modules/wikigrok/WikiGrokDialog' ),
+               apiWikiGrok;
 
-       // Get existing Wikidata claims about this page so we can decide if 
it's appropriate
-       // to display the WikiGrok interface.
+       // See if there are potential occupation claims about this person so we 
can decide if
+       // it's appropriate to display the WikiGrok interface.
        if ( !M.settings.getUserSetting( 'mfHideWikiGrok' ) ) {
-               api = new WikiDataApi( { itemId: wikidataID });
-               api.getClaims().done(
-                       function( claims ) {
-                               var dialog;
-
-                               if ( claims && !claims.hasOccupation ) {
-                                       dialog = new WikiGrokDialog( { itemId: 
wikidataID } );
-                                       // If there is a table of contents, 
insert before it.
-                                       if ( $( '.toc-mobile' ).length ) {
-                                               dialog.insertBefore( 
'.toc-mobile' );
-                                       } else {
-                                               dialog.appendTo( 
M.getLeadSection() );
-                                       }
+               apiWikiGrok = new WikiGrokApi( { itemId: wikidataID } );
+               // FIXME: This fires an API request on every page load. We may 
need to do
+               // something different if this is promoted to stable.
+               apiWikiGrok.getPossibleOccupations().done( function( 
occupations ) {
+                       var dialog;
+                       if ( occupations.length ) {
+                               dialog = new WikiGrokDialog( { itemId: 
wikidataID, occupations: occupations } );
+                               if ( $( '.toc-mobile' ).length ) {
+                                       dialog.insertBefore( '.toc-mobile' );
+                               } else {
+                                       dialog.appendTo( M.getLeadSection() );
                                }
                        }
-               );
+               } );
        }
 
 }( mw.mobileFrontend, jQuery ) );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I93ceb1ec860e8e2e8087a0bd7b88958e504ac6e9
Gerrit-PatchSet: 12
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Kaldari <[email protected]>
Gerrit-Reviewer: Awjrichards <[email protected]>
Gerrit-Reviewer: JGonera <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: Kaldari <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to