User:Kaldari/wikigrokdialog.js

( function( $ ) {	var $wikigrok,		options = {};

$wikigrok = $(' ') .append( ' ' ) .dialog( {			width: 400,			autoOpen: false,			title: 'Improve Wikidata information about this article',			modal: false,			buttons: [				{					text: 'Not Sure',					click: function { $( this ).dialog( 'close' ) },					class : 'mw-ui-button'				},				{					text: 'No',					click: function {						options.claimIsCorrect = 0;						fakeRecordClaim;						$( this ).dialog( 'close' );					},					class : 'mw-ui-button mw-ui-progressive'				},				{					text: 'Yes',					click: function {						options.claimIsCorrect = 1;						fakeRecordClaim;						$( this ).dialog( 'close' );					},					class : 'mw-ui-button mw-ui-progressive'				},			]		} );

function askWikidataQuestion { // Get potential occupations for the person. // FIXME: Create a client-side API class for interacting with the WikiGrok API $.ajax( {			type: 'get',			// https://github.com/kaldari/WikiGrokAPI			url: 'https://tools.wmflabs.org/wikigrok/api.php',			data: {				'action': 'get_potential_occupations',				// Strip the Q out of the Wikibase item ID				'item': options.itemId.replace( 'Q', '' )			},			dataType: 'jsonp',			success: 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 ( 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. $.ajax( {						type: 'get',						url: 'https://www.wikidata.org/w/api.php',						data: {							'action': 'wbgetentities',							'props': 'labels',							'ids': options.occupationId,							'format': 'json'						},						dataType: 'jsonp',						success: 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 + '?';								}								$wikigrok.dialog( 'open' ); $( '.wg-content' ).text( options.contentMsg ); }						}					} );				}			}		} );	}

// Record answer in temporary database for analysis. // Eventually answers will be recorded directly to Wikidata. function fakeRecordClaim { $.ajax( {			type: 'get',			url: 'https://tools.wmflabs.org/wikigrok/api.php',			data: {				'action': 'record_answer',				'subject_id': options.itemId,				'subject': options.name,				'occupation_id': options.occupationId,				'occupation': options.occupation,				'page_name': mw.config.get( 'wgPageName' ),				'correct': options.claimIsCorrect,				'user_id': mw.config.get( 'wgUserId' ),				'source': 'desktop A'			},			dataType: 'jsonp',			success: function {				console.debug( 'claim recorded (not really)' );			}		} ); }

// Record answer in temporary database for analysis. // Eventually answers will be recorded directly to Wikidata. function recordClaim { $.ajax( {			type: 'get',			url: 'https://www.wikidata.org/w/api.php',			data: {				'action': 'query',				'meta': 'tokens',				'format': 'json'			},			dataType: 'json',			success: function( data ) {				if ( data.query.tokens.csrftoken !== undefined ) {					$.ajax( { type: 'post', url: 'https://www.wikidata.org/w/api.php', data: { 'action': 'wbcreateclaim', 'entity': options.itemId, 'snaktype': 'value', 'property': P106, 'value': options.occupationId, 'summary': 'recording occupation claim via WikiGrok', 'token': data.query.tokens.csrftoken, 'format': 'json' },						dataType: 'jsonp', success: function( data ) { console.debug( 'claim recorded' ); }					} );				}			}		} );	}

options.itemId = mw.config.get( 'wgWikibaseItemId' ); askWikidataQuestion; }( jQuery ) );