Jonas Kress (WMDE) has uploaded a new change for review. https://gerrit.wikimedia.org/r/283180
Change subject: Introduce Wikibase API and replace every other usage with it ...................................................................... Introduce Wikibase API and replace every other usage with it Change-Id: I2f9a3021ad5c0e838fa7acd9911774126b98415f --- M index.html M wikibase/config.js M wikibase/init.js A wikibase/queryService/api/Wikibase.js M wikibase/queryService/ui/App.js M wikibase/queryService/ui/editor/Editor.js M wikibase/queryService/ui/editor/hint/Rdf.js M wikibase/queryService/ui/editor/hint/Sparql.js R wikibase/queryService/ui/editor/tooltip/Rdf.js M wikibase/queryService/ui/visualEditor/SelectorBox.js M wikibase/queryService/ui/visualEditor/VisualEditor.js 11 files changed, 211 insertions(+), 97 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/wikidata/query/gui refs/changes/80/283180/1 diff --git a/index.html b/index.html index 4f42e16..d90ad19 100644 --- a/index.html +++ b/index.html @@ -207,11 +207,11 @@ <script src="vendor/d3/d3.min.js"></script> <script src="vendor/select2/dist/js/select2.min.js"></script> - <script src="wikibase/codemirror/addon/tooltip/WikibaseRDFTooltip.js"></script> <script src="wikibase/queryService/ui/App.js"></script> <script src="wikibase/queryService/ui/editor/hint/Sparql.js"></script> <script src="wikibase/queryService/ui/editor/hint/Rdf.js"></script> + <script src="wikibase/queryService/ui/editor/tooltip/Rdf.js"></script> <script src="wikibase/queryService/ui/editor/Editor.js"></script> <script src="wikibase/queryService/ui/visualEditor/VisualEditor.js"></script> <script src="wikibase/queryService/ui/visualEditor/SelectorBox.js"></script> @@ -226,6 +226,7 @@ <script src="wikibase/queryService/ui/resultBrowser/BubbleChartResultBrowser.js"></script> <script src="wikibase/queryService/api/Sparql.js"></script> <script src="wikibase/queryService/api/QuerySamples.js"></script> + <script src="wikibase/queryService/api/Wikibase.js"></script> <script src="wikibase/queryService/RdfNamespaces.js"></script> <script src="wikibase/config.js"></script> <script src="wikibase/init.js"></script> diff --git a/wikibase/config.js b/wikibase/config.js index 7c2eb82..8de6159 100644 --- a/wikibase/config.js +++ b/wikibase/config.js @@ -6,10 +6,10 @@ api : { sparql : { uri : '/bigdata/namespace/wdq/sparql' + }, + wikibase: { + uri : 'https://www.wikidata.org/w/api.php' } - }, - visualEditor: { - entitySearchEndpointPath : 'https://www.wikidata.org/w/' } }; diff --git a/wikibase/init.js b/wikibase/init.js index bda4d5c..5650686 100644 --- a/wikibase/init.js +++ b/wikibase/init.js @@ -2,10 +2,14 @@ 'use strict'; $( document ).ready( function () { + var wikibaseApi = new wikibase.queryService.api.Wikibase( config.api.wikibase.uri ); + var rdfHint = new wikibase.queryService.ui.editor.hint.Rdf( wikibaseApi ); + var rdfTooltip = new wikibase.queryService.ui.editor.tooltip.Rdf( wikibaseApi ); + new wikibase.queryService.ui.App( $( '.wikibase-queryservice ' ), - null, - new wikibase.queryService.ui.visualEditor.VisualEditor( config.visualEditor.entitySearchEndpointPath ), + new wikibase.queryService.ui.editor.Editor( rdfHint, null, rdfTooltip ), + new wikibase.queryService.ui.visualEditor.VisualEditor( wikibaseApi ), new wikibase.queryService.api.Sparql( config.api.sparql.uri ) ); } ); diff --git a/wikibase/queryService/api/Wikibase.js b/wikibase/queryService/api/Wikibase.js new file mode 100644 index 0000000..fbf0d03 --- /dev/null +++ b/wikibase/queryService/api/Wikibase.js @@ -0,0 +1,73 @@ +var wikibase = wikibase || {}; +wikibase.queryService = wikibase.queryService || {}; +wikibase.queryService.api = wikibase.queryService.api || {}; + +wikibase.queryService.api.Wikibase = ( function( $ ) { + 'use strict'; + + var API_ENDPOINT = 'https://www.wikidata.org/w/api.php'; + var LANGUAGE = 'en'; + + var SEARCH_ENTITES = { + action: 'wbsearchentities', + format: 'json', + contiunue: 0, + language: LANGUAGE, + uselang: LANGUAGE + }; + + + /** + * API for the Wikibase API + * + * @class wikibase.queryService.api.Wikibase + * @license GNU GPL v2+ + * + * @author Jonas Kress + * @constructor + * @param {string} endpoint default: 'https://www.wikidata.org/w/api.php' + */ + function SELF( endpoint, defaultLanguage ) { + this._endpoint = API_ENDPOINT; + + if( endpoint ){ + this._endpoint = endpoint; + } + } + + /** + * @property {string} + * @private + */ + SELF.prototype._endpoint= null; + + /** + * Search an entity with using wbsearchentities + * + * @param {string} term search string + * @param {string} type entity type to search for + * @param {string} language of search string default:en + * + * @return {jQuery.Deferred} + */ + SELF.prototype.searchEntities = function( term, type, language ) { + var query = SEARCH_ENTITES; + query.search = term; + + if( type ){ + query.type = type; + } + + if( language ){ + query.type = type; + } + + return $.ajax( { + url: this._endpoint + '?' + jQuery.param( query ), + dataType: 'jsonp' + } ); + }; + + return SELF; + +}( jQuery ) ); diff --git a/wikibase/queryService/ui/App.js b/wikibase/queryService/ui/App.js index f8164c0..2a05495 100644 --- a/wikibase/queryService/ui/App.js +++ b/wikibase/queryService/ui/App.js @@ -19,8 +19,8 @@ * @constructor * * @param {jQuery} $element - * @param {wikibase.queryService.ui.editor.Editor} - * @param {wikibase.queryService.api.Sparql} + * @param {wikibase.queryService.ui.editor.Editor} editor + * @param {wikibase.queryService.api.Sparql} visualEditor */ function SELF( $element, editor, visualEditor, sparqlApi, querySamplesApi ) { diff --git a/wikibase/queryService/ui/editor/Editor.js b/wikibase/queryService/ui/editor/Editor.js index 70e4b00..cf1a118 100644 --- a/wikibase/queryService/ui/editor/Editor.js +++ b/wikibase/queryService/ui/editor/Editor.js @@ -3,7 +3,7 @@ wikibase.queryService.ui = wikibase.queryService.ui || {}; wikibase.queryService.ui.editor = wikibase.queryService.ui.editor || {}; -wikibase.queryService.ui.editor.Editor = ( function( $, CodeMirror, WikibaseRDFTooltip, localStorage ) { +wikibase.queryService.ui.editor.Editor = ( function( $, wikibase, CodeMirror, WikibaseRDFTooltip, localStorage ) { "use strict"; var CODEMIRROR_DEFAULTS = { @@ -28,8 +28,24 @@ * @author Stanislav Malyshev * @author Jonas Kress * @constructor + * @param {wikibase.queryService.ui.editor.hint.Rdf} rdfHint + * @param {wikibase.queryService.ui.editor.hint.Sparql} sparqlHint + * @param {wikibase.queryService.ui.editor.tooltip.Rdf} rdfTooltip */ - function SELF() { + function SELF( rdfHint, sparqlHint, rdfTooltip ) { + this._rdfHint = rdfHint; + this._sparqlHint = sparqlHint; + this._rdfTooltip = rdfTooltip; + + if( !this._sparqlHint ){ + this._sparqlHint = new wikibase.queryService.ui.editor.hint.Sparql(); + } + if( !this._rdfHint ){ + this._rdfHint = new wikibase.queryService.ui.editor.hint.Rdf(); + } + if ( !this._rdfTooltip ){ + this._rdfTooltip = new wikibase.queryService.ui.editor.tooltip.Rdf(); + } } /** @@ -52,6 +68,12 @@ SELF.prototype._rdfHint = null; /** + * @property {wikibase.queryService.ui.editor.tooltip.Rdf} + * @private + **/ + SELF.prototype._rdfTooltip = null; + + /** * Construct an this._editor on the given textarea DOM element * * @param {Element} element @@ -65,12 +87,12 @@ self.clearError(); if( changeObj.text[0] === '?' || changeObj.text[0] === '#' ){ - editor.showHint({closeCharacters: /[\s]/}); + editor.showHint( {closeCharacters: /[\s]/} ); } } ); this._editor.focus(); - new WikibaseRDFTooltip(this._editor); + this._rdfTooltip.setEditor( this._editor ); this._registerHints(); }; @@ -98,12 +120,6 @@ SELF.prototype._getHints = function( editorContent, lineContent, lineNum, cursorPos ) { var deferred = new $.Deferred(), self = this; - if( !this._sparqlHint ){ - this._sparqlHint = new wikibase.queryService.ui.editor.hint.Sparql(); - } - if( !this._rdfHint ){ - this._rdfHint = new wikibase.queryService.ui.editor.hint.Rdf(); - } this._rdfHint.getHint( editorContent, lineContent, lineNum, cursorPos ).done( function( hint ){ hint.from = CodeMirror.Pos( hint.from.line, hint.from.char ); @@ -238,4 +254,4 @@ return SELF; -}( jQuery, CodeMirror, WikibaseRDFTooltip, window.localStorage ) ); +}( jQuery, wikibase, CodeMirror, window.localStorage ) ); diff --git a/wikibase/queryService/ui/editor/hint/Rdf.js b/wikibase/queryService/ui/editor/hint/Rdf.js index 4547984..0039a48 100755 --- a/wikibase/queryService/ui/editor/hint/Rdf.js +++ b/wikibase/queryService/ui/editor/hint/Rdf.js @@ -21,22 +21,27 @@ 'http://www.wikidata.org/prop/reference/value/': 'property', 'http://www.wikidata.org/wiki/Special:EntityData/': 'item', 'http://www.wikidata.org/entity/': 'item' - }, - ENTITY_SEARCH_API_ENDPOINT = 'https://www.wikidata.org/w/api.php?action=wbsearchentities&' + - 'search={term}&format=json&language=en&uselang=en&type={entityType}&continue=0'; + }; /** * Code completion for Wikibase entities RDF prefixes in SPARQL * completes SPARQL keywords and ?variables * + * @class wikibase.queryService.ui.editor.hint.Rdf * licence GNU GPL v2+ * * @author Jonas Kress + * @param {wikibase.queryService.api.Wikibase} api * @param {wikibase.queryService.RdfNamespace} rdfNamespace * @constructor */ - var SELF = MODULE.Rdf = function( rdfNamespaces ) { + var SELF = MODULE.Rdf = function( api, rdfNamespaces ) { + this._api = api; this._rdfNamespaces = rdfNamespaces; + + if( !this._api ){ + this._api = new wikibase.queryService.api.Wikibase(); + } if( !this._rdfNamespaces ){ this._rdfNamespaces = wikibase.queryService.RdfNamespaces; @@ -48,6 +53,12 @@ * @private **/ SELF.prototype._rdfNamespaces = null; + + /** + * @property {wikibase.queryService.api.Wikibase} + * @private + **/ + SELF.prototype._api = null; /** * Get list of hints @@ -111,10 +122,7 @@ var entityList = [], deferred = $.Deferred(); - $.ajax( { - url: ENTITY_SEARCH_API_ENDPOINT.replace( '{term}', term ).replace( '{entityType}', type ), - dataType: 'jsonp' - } ).done( function ( data ) { + this._api.searchEntities( term, type ).done( function ( data ) { $.each( data.search, function ( key, value ) { entityList.push( { className: 'wikibase-rdf-hint', diff --git a/wikibase/queryService/ui/editor/hint/Sparql.js b/wikibase/queryService/ui/editor/hint/Sparql.js index 0cd4d23..c52fa55 100755 --- a/wikibase/queryService/ui/editor/hint/Sparql.js +++ b/wikibase/queryService/ui/editor/hint/Sparql.js @@ -48,6 +48,7 @@ * Code completion for Wikibase entities RDF prefixes in SPARQL * completes SPARQL keywords and ?variables * + * @class wikibase.queryService.ui.editor.hint.Sparql * licence GNU GPL v2+ * * @author Jonas Kress diff --git a/wikibase/codemirror/addon/tooltip/WikibaseRDFTooltip.js b/wikibase/queryService/ui/editor/tooltip/Rdf.js similarity index 74% rename from wikibase/codemirror/addon/tooltip/WikibaseRDFTooltip.js rename to wikibase/queryService/ui/editor/tooltip/Rdf.js index 64ce007..4f900d2 100644 --- a/wikibase/codemirror/addon/tooltip/WikibaseRDFTooltip.js +++ b/wikibase/queryService/ui/editor/tooltip/Rdf.js @@ -1,17 +1,28 @@ -var WikibaseRDFTooltip = ( function ( CodeMirror, $ ) { +var wikibase = wikibase || {}; +wikibase.queryService = wikibase.queryService || {}; +wikibase.queryService.ui = wikibase.queryService.ui || {}; +wikibase.queryService.ui.editor = wikibase.queryService.ui.editor || {}; +wikibase.queryService.ui.editor.tooltip = wikibase.queryService.ui.editor.tooltip || {}; + +wikibase.queryService.ui.editor.tooltip.Rdf = ( function ( CodeMirror, $ ) { 'use strict'; /** * Wikibase RDF tooltip for codemirror editor * - * @class WikibaseRdfTooltip * @license GNU GPL v2+ + * @class wikibase.queryService.ui.editor.tooltip.Rdf + * * @author Jonas Kress * @constructor + * @param {wikibase.queryService.api.Wikibase} api */ - function SELF( editor ) { - this.editor = editor; - this._registerHandler(); + function SELF( api ) { + this._api = api; + + if ( !this._api ){ + this._api = new wikibase.queryService.api.Wikibase(); + } } SELF.prototype.editor = null; @@ -31,11 +42,19 @@ 'http://www.wikidata.org/entity/': 'item' }; - var ENTITY_SEARCH_API_ENDPOINT = 'https://www.wikidata.org/w/api.php?action=wbsearchentities&search={term}&format=json&language=en&uselang=en&type={entityType}&continue=0'; + /** + * Set the editor the onmouseover callback is registered to + * + * @param {wikibase.queryService.ui.editor.Editor} editor + */ + SELF.prototype.setEditor = function ( editor ) { + this.editor = editor; + this._registerHandler(); + }; SELF.prototype._registerHandler = function () { CodeMirror.on( this.editor.getWrapperElement(), 'mouseover', $.proxy( this._triggerTooltip, this ) ); - }; + };//TODO: Remove CodeMirror dependency SELF.prototype._triggerTooltip = function ( e ) { clearTimeout( this.tooltipTimeoutHandler ); @@ -99,7 +118,7 @@ $.each( lines, function ( index, line ) { // PREFIX wd: <http://www.wikidata.org/entity/> - if ( matches = line.match( /(PREFIX) (\S+): <([^>]+)>/ ) ) { + if ( ( matches = line.match( /(PREFIX) (\S+): <([^>]+)>/ ) ) ) { if ( ENTITY_TYPES[ matches[ 3 ] ] ) { prefixes[ matches[ 2 ] ] = ENTITY_TYPES[ matches[ 3 ] ]; } @@ -113,14 +132,11 @@ var entityList = [], deferred = $.Deferred(); - $.ajax( { - url: ENTITY_SEARCH_API_ENDPOINT.replace( '{term}', term ).replace( '{entityType}', type ), - dataType: 'jsonp' - } ).done( function ( data ) { + this._api.searchEntities( term, type ).done( function ( data ) { $.each( data.search, function ( key, value ) { - entityList.push( value.label + ' (' + value.id + ')\n' - + (value.description?value.description:'') ); + entityList.push( value.label + ' (' + value.id + ')\n' + + ( value.description ? value.description: '' ) ); } ); deferred.resolve( entityList ); diff --git a/wikibase/queryService/ui/visualEditor/SelectorBox.js b/wikibase/queryService/ui/visualEditor/SelectorBox.js index 86449f2..80f5c7e 100644 --- a/wikibase/queryService/ui/visualEditor/SelectorBox.js +++ b/wikibase/queryService/ui/visualEditor/SelectorBox.js @@ -3,13 +3,8 @@ wikibase.queryService.ui = wikibase.queryService.ui || {}; wikibase.queryService.ui.visualEditor = wikibase.queryService.ui.visualEditor || {}; -wikibase.queryService.ui.visualEditor.SelectorBox = ( function( $ ) { +wikibase.queryService.ui.visualEditor.SelectorBox = ( function( $, wikibase ) { "use strict"; - - var ENTITY_SEARCH_API_ENDPOINT_REQUEST = 'api.php?action=wbsearchentities&search={term}&format=json&language=en&uselang=en&type={entityType}&continue=0'; - - var ENTITY_SEARCH_API_ENDPOINT_PATH = 'https://www.wikidata.org/w/'; - /** * A selector box for selecting and changing properties and items @@ -20,13 +15,25 @@ * @author Jonas Kress * @constructor * @param {jQuery} $element + * @param {wikibase.queryService.api.Wikibase} api */ - function SELF( $element ) { - this._entitySearchEndpoint = ENTITY_SEARCH_API_ENDPOINT_PATH + ENTITY_SEARCH_API_ENDPOINT_REQUEST; + function SELF( $element, api ) { this._$element = $element; + + if ( api ){ + this._api = api; + }else{ + this._api = new wikibase.queryService.api.Wikibase(); + } this._create(); } + + /** + * @property {wikibase.queryService.api.Wikibase} + * @private + */ + SELF.prototype._api = null; /** * @property {function} @@ -134,35 +141,31 @@ '</span><br/><small>' + item.data.description + '</small>' ); }; + var transport = function( params, success, failure ) { + self._api.searchEntities( params.data.term, type ).done( function( data ){ + var r = data.search.map( function( d ) { + return { + id : d.id, + text : d.label, + data : d + }; + } ); + success( { results:r } ); + }).fail( failure ); + }; + $select.select2({ - placeholder: 'Select an option', - width: 'auto', - minimumInputLength: 1, - templateResult: formatter, - ajax: {//TODO: implement inversion of control - url: self._entitySearchEndpoint.replace( '&search={term}', '' ).replace( '{entityType}', type ), - dataType: 'jsonp', - delay: 250, - data: function (params) { - return { - search: params.term, // search term - }; - }, - processResults: function (data, params) { - return { - results: data.search.map( function( d ){ - return { - id: d.id, - text: d.label, - data: d - }; - } ), - }; - }, - cache: true - } + placeholder : 'Select an option', + width : 'auto', + minimumInputLength : 1, + templateResult : formatter, + ajax : { + delay: 250, + transport : transport + }, + cache : true }); }; return SELF; -}( jQuery) ); +}( jQuery, wikibase ) ); diff --git a/wikibase/queryService/ui/visualEditor/VisualEditor.js b/wikibase/queryService/ui/visualEditor/VisualEditor.js index c89069c..95a67b5 100644 --- a/wikibase/queryService/ui/visualEditor/VisualEditor.js +++ b/wikibase/queryService/ui/visualEditor/VisualEditor.js @@ -3,12 +3,8 @@ wikibase.queryService.ui = wikibase.queryService.ui || {}; wikibase.queryService.ui.visualEditor = wikibase.queryService.ui.visualEditor || {}; -wikibase.queryService.ui.visualEditor.VisualEditor = ( function( $ ) { +wikibase.queryService.ui.visualEditor.VisualEditor = ( function( $, wikibase ) { "use strict"; - - var ENTITY_SEARCH_API_ENDPOINT_REQUEST = 'api.php?action=wbsearchentities&search={term}&format=json&language=en&uselang=en&type={entityType}&continue=0'; - - var ENTITY_SEARCH_API_ENDPOINT_PATH = 'https://www.wikidata.org/w/'; var FILTER_PREDICATES = { 'http://www.w3.org/2000/01/rdf-schema#label': true, @@ -25,21 +21,21 @@ * * @author Jonas Kress * @constructor - * @param {string} entitySearchEndpoint + * @param {wikibase.queryService.api.Wikibase} api */ - function SELF( entitySearchEndpointPath ) { - if ( entitySearchEndpointPath ){ - this._entitySearchEndpoint = entitySearchEndpointPath + ENTITY_SEARCH_API_ENDPOINT_REQUEST; - }else{ - this._entitySearchEndpoint = ENTITY_SEARCH_API_ENDPOINT_PATH + ENTITY_SEARCH_API_ENDPOINT_REQUEST; + function SELF( api ) { + this._api = api; + + if ( !this._api ){ + this._api = new wikibase.queryService.api.Wikibase(); } } /** - * @property {string} + * @property {wikibase.queryService.api.Wikibase} * @private */ - SELF.prototype._entitySearchEndpoint = null; + SELF.prototype._api = null; /** * @property {function} @@ -384,11 +380,9 @@ }; /** - * TODO: Implement inversion of control * @private */ SELF.prototype._getLabel = function( url ) { - var self = this; var deferred = $.Deferred(); var entity = url.match(/(Q|P)([0-9]+)/);//TODO: make use of Rdf namespaces @@ -400,10 +394,7 @@ type = type[entity[1]]; var term = entity[0]; - $.ajax( { - url: self._entitySearchEndpoint.replace( '{term}', term ).replace( '{entityType}', type ), - dataType: 'jsonp' - } ).done( function ( data ) { + this._api.searchEntities( term, type ).done( function ( data ) { $.each( data.search, function ( key, value ) { deferred.resolve( value.label, value.id, value.description, type ); return false; @@ -420,7 +411,8 @@ SELF.prototype._valuleChanger = function( $element ) { var deferred = $.Deferred(); - var $selector = new wikibase.queryService.ui.visualEditor.SelectorBox( $element ); + //TODO Use only one instance and make that instance injectable + var $selector = new wikibase.queryService.ui.visualEditor.SelectorBox( $element, this._api ); $selector.setEntitySearchEndpoint( this._entitySearchEndpoint ); $selector.setChangeListener( function( id ){ @@ -431,4 +423,4 @@ }; return SELF; -}( jQuery) ); +}( jQuery, wikibase ) ); -- To view, visit https://gerrit.wikimedia.org/r/283180 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I2f9a3021ad5c0e838fa7acd9911774126b98415f Gerrit-PatchSet: 1 Gerrit-Project: wikidata/query/gui Gerrit-Branch: master Gerrit-Owner: Jonas Kress (WMDE) <jonas.kr...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits