Kaldari has uploaded a new change for review.

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

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.

Also changing WikiDataApi::getOccupations to WikiDataApi::getLabels
since it's a more accurate description of what the function does.

Also making sure data.entities is defined before checking sub-value.

Bug: 71335
Change-Id: I93ceb1ec860e8e2e8087a0bd7b88958e504ac6e9
---
M javascripts/modules/wikigrok/WikiDataApi.js
M javascripts/modules/wikigrok/WikiGrokDialog.js
M javascripts/modules/wikigrok/wikigrokeval.js
3 files changed, 54 insertions(+), 79 deletions(-)


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

diff --git a/javascripts/modules/wikigrok/WikiDataApi.js 
b/javascripts/modules/wikigrok/WikiDataApi.js
index 18280b0..558c124 100644
--- a/javascripts/modules/wikigrok/WikiDataApi.js
+++ b/javascripts/modules/wikigrok/WikiDataApi.js
@@ -10,11 +10,20 @@
                initialize: function() {
                        Api.prototype.initialize.apply( this, arguments );
                },
-               getOccupations: function( occupationId ) {
+               /**
+                * Get labels for an item from Wikidata
+                * See: https://www.wikidata.org/wiki/Help:Label
+                *
+                * @param {int} ID for item in Wikidata
+                * @return {jQuery.Deferred} Object returned by ajax call
+                */
+               getLabels: function( itemId ) {
                        return this.ajax( {
                                        action: 'wbgetentities',
                                        props: 'labels',
-                                       ids: occupationId
+                                       // FIXME: change this to a parameter
+                                       languages: 'en',
+                                       ids: itemId
                                },
                                {
                                        url: this.apiUrl,
diff --git a/javascripts/modules/wikigrok/WikiGrokDialog.js 
b/javascripts/modules/wikigrok/WikiGrokDialog.js
index 7008540..b6cb830 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,45 +50,42 @@
                },
 
                askWikidataQuestion: function( options ) {
-                       var self = this;
+                       var self = this, occupationArray;
 
-                       // Get potential occupations for the person.
-                       this.apiWikiGrok.getPossibleOccupations( options.itemId 
).done( function( data ) {
-                               var 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 ( options.occupations !== undefined ) {
+                               occupationArray = options.occupations.split( 
',' );
+                               // Choose a random occupation from the list of 
possible occupations.
+                               options.occupationId = 'Q' + 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 ( data.occupations !== undefined ) {
-                                       occupationArray = 
data.occupations.split( ',' );
-                                       // Choose a random occupation from the 
list of possible occupations.
-                                       options.occupationId = 'Q' + 
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.getOccupations( 
options.occupationId ).done( function( data ) {
-                                               var vowels = [ 'a', 'e', 'i', 
'o', 'u' ];
-                                               if ( 
data.entities[options.occupationId].labels.en.value !== undefined ) {
-                                                       // Re-render with new 
content for 'Question' step
-                                                       options.beginQuestions 
= true;
-                                                       options.occupation = 
data.entities[options.occupationId].labels.en.value;
-                                                       // 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.getLabels( 
options.occupationId ).done( function( data ) {
+                                       var vowels = [ 'a', 'e', 'i', 'o', 'u' 
];
+                                       if ( data.entities !== undefined &&
+                                               
data.entities[options.occupationId].labels.en.value !== undefined
+                                       ) {
+                                               // Re-render with new content 
for 'Question' step
+                                               options.beginQuestions = true;
+                                               options.occupation = 
data.entities[options.occupationId].labels.en.value;
+                                               // 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 );
+                                       }
+                               } );
+                       }
                },
 
                // Record answer in temporary database for analysis.
diff --git a/javascripts/modules/wikigrok/wikigrokeval.js 
b/javascripts/modules/wikigrok/wikigrokeval.js
index e4460a3..b3c1c19 100644
--- a/javascripts/modules/wikigrok/wikigrokeval.js
+++ b/javascripts/modules/wikigrok/wikigrokeval.js
@@ -1,48 +1,17 @@
-( function( M, $ ) {
+( function( M ) {
        var wikidataID = mw.config.get( 'wgWikibaseItemId' ),
+               WikiGrokApi = M.require( 'modules/wikigrok/WikiGrokApi' ),
                WikiGrokDialog = M.require( 'modules/wikigrok/WikiGrokDialog' );
 
-       // 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' ) ) {
-               $.ajax( {
-                       type: 'get',
-                       url: 'https://www.wikidata.org/w/api.php',
-                       data: {
-                               'action': 'wbgetentities',
-                               'ids': wikidataID,
-                               'props': 'claims',
-                               'format': 'json'
-                       },
-                       // Using JSONP so we aren't restricted by cross-site 
rules. This isn't
-                       // strictly needed on the Wikimedia cluster since it 
has CORS exceptions
-                       // for requests from other Wikimedia sites, but this 
makes it easy to
-                       // test locally.
-                       dataType: 'jsonp',
-                       success: function( data ) {
-                               var instanceClaims,
-                                       loadWikiGrokDialog = false;
-
-                               // See if the page has any 'instance of' claims.
-                               if ( data.entities !== undefined && 
data.entities[wikidataID].claims.P31 !== undefined ) {
-                                       instanceClaims = 
data.entities[wikidataID].claims.P31;
-                                       $.each( instanceClaims, function( id, 
claim ) {
-                                               // See if any of the claims 
state that the topic is a human.
-                                               if ( 
claim.mainsnak.datavalue.value['numeric-id'] === 5 ) {
-                                                       // Make sure there are 
no existing occupation claims.
-                                                       if ( 
data.entities[wikidataID].claims.P106 === undefined ) {
-                                                               
loadWikiGrokDialog = true;
-                                                       }
-                                                       // Break each loop.
-                                                       return false;
-                                               }
-                                       } );
-                                       if ( loadWikiGrokDialog ) {
-                                               new WikiGrokDialog( { itemId: 
wikidataID } );
-                                       }
-                               }
+               this.apiWikiGrok = new WikiGrokApi();
+               this.apiWikiGrok.getPossibleOccupations( wikidataID ).done( 
function( data ) {
+                       if ( data.occupations ) {
+                               new WikiGrokDialog( { itemId: wikidataID, 
occupations: data.occupations } );
                        }
                } );
        }
 
-}( mw.mobileFrontend, jQuery ) );
+}( mw.mobileFrontend ) );

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

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

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

Reply via email to