Jdlrobson has uploaded a new change for review.

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

Change subject: Hygiene: Abstract more of the nastiness of the wikidata api
......................................................................

Hygiene: Abstract more of the nastiness of the wikidata api

Return a simple object with information about the wikidata item.

Change-Id: Ib680afae6e3b864057230619eb9c9e4f30e0b5ea
---
M javascripts/modules/wikigrok/WikiDataApi.js
M javascripts/modules/wikigrok/wikigrokeval.js
2 files changed, 28 insertions(+), 31 deletions(-)


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

diff --git a/javascripts/modules/wikigrok/WikiDataApi.js 
b/javascripts/modules/wikigrok/WikiDataApi.js
index 567dce6..0362a0b 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
@@ -13,11 +13,28 @@
                        Api.prototype.initialize.apply( this, arguments );
                },
                getClaims: function() {
+                       var id = this.id;
                        return this.ajax( {
                                action: 'wbgetentities',
                                ids: this.id,
                                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;
+                               }
                        } );
                },
                getOccupations: function( occupationId ) {
@@ -31,4 +48,4 @@
 
        M.define( 'modules/wikigrok/WikiDataApi', WikiDataApi );
 
-}( mw.mobileFrontend ) );
+}( mw.mobileFrontend, jQuery ) );
diff --git a/javascripts/modules/wikigrok/wikigrokeval.js 
b/javascripts/modules/wikigrok/wikigrokeval.js
index 50372e5..47536a3 100644
--- a/javascripts/modules/wikigrok/wikigrokeval.js
+++ b/javascripts/modules/wikigrok/wikigrokeval.js
@@ -9,36 +9,16 @@
        if ( !M.settings.getUserSetting( 'mfHideWikiGrok' ) ) {
                api = new WikiDataApi( { id: wikidataID });
                api.getClaims().done(
-                       function( data ) {
-                               var instanceClaims,
-                                       dialog,
-                                       loadWikiGrokDialog = false;
+                       function( claims ) {
+                               var dialog;
 
-                               // 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 ) {
-                                               dialog = new WikiGrokDialog( { 
itemId: wikidataID } );
-                                               // Insert the dialog into the 
page
-                                               $( function() {
-                                                       // If there is a table 
of contents, insert before it.
-                                                       if ( $( '.toc-mobile' 
).length ) {
-                                                               
dialog.insertBefore( '.toc-mobile' );
-                                                       } else {
-                                                               
dialog.appendTo( M.getLeadSection() );
-                                                       }
-                                               } );
+                               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() );
                                        }
                                }
                        }

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

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

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

Reply via email to