Jonas Kress (WMDE) has uploaded a new change for review.

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

Change subject: SPARQL auto completion for keywords and ?variables
......................................................................

SPARQL auto completion for keywords and ?variables

Auto completion support for SPARQL keywords like SELECT, OPTIONAL, WHERE
{ } and SERVICE.
And also for defined ?variables within the document.

Bug: T116080
Change-Id: I40bf67ae766f8700ed07a1daf550c3b378387100
---
A gui/addon/hint/wikibase-sparql-hint.js
M gui/index.html
2 files changed, 129 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikidata/query/rdf 
refs/changes/90/248690/1

diff --git a/gui/addon/hint/wikibase-sparql-hint.js 
b/gui/addon/hint/wikibase-sparql-hint.js
new file mode 100755
index 0000000..303e473
--- /dev/null
+++ b/gui/addon/hint/wikibase-sparql-hint.js
@@ -0,0 +1,128 @@
+/**
+ * Code completion for Wikibase entities RDF prefixes in SPARQL
+ *
+ * Determines entity type from list of prefixes and completes input
+ * based on search results from wikidata.org entities search API
+ *
+ * licence GNU GPL v2+
+ *
+ * @author Jens Ohlig <jens.oh...@wikimedia.de>
+ * @author Jan Zerebecki
+ * @author Jonas Kress
+ */
+
+( function ( mod ) {
+       if ( typeof exports == 'object' && typeof module == 'object' ) {// 
CommonJS
+               mod( require( '../../lib/codemirror' ) );
+       } else if ( typeof define == 'function' && define.amd ) {// AMD
+               define( [ '../../lib/codemirror' ], mod );
+       } else {// Plain browser env
+               mod( CodeMirror );
+       }
+} )( function ( CodeMirror ) {
+       'use strict';
+
+       var SPARQL_KEYWORDS = [ 'SELECT', 'OPTIONAL', 'WHERE', 'ORDER', 'ORDER 
BY', 'DISTINCT', 'WHERE {\n\n}', 'SERVICE', 'SERVICE wikibase:label {\n 
bd:serviceParam wikibase:language "en" .\n}',
+                               'BASE', 'PREFIX', 'REDUCED', 'FROM', 'LIMIT', 
'OFFSET', 'HAVING', 'UNION'];
+
+       CodeMirror.registerHelper( 'hint', 'sparql', function ( editor, 
callback, options ) {
+               var currentWord = getCurrentWord( getCurrentLine( editor ), 
getCurrentCurserPosition( editor ) ),
+                       hintList = [];
+
+
+               if ( currentWord.word.indexOf('?') === 0) {
+                       hintList = hintList.concat( getVariableHints( 
currentWord.word, getDefinedVariables( editor.doc.getValue() ) ) );
+               }
+
+               hintList = hintList.concat( getSPARQLHints( currentWord.word ) 
);
+
+               callback( getHintCompletion( editor, currentWord, hintList ) );
+
+       } );
+
+       CodeMirror.hint.sparql.async = true;
+       CodeMirror.defaults.hintOptions = {};
+       CodeMirror.defaults.hintOptions.closeCharacters = /[]/;
+       CodeMirror.defaults.hintOptions.completeSingle = false;
+
+       function getSPARQLHints( term ){
+               var list = [];
+
+               $.each(SPARQL_KEYWORDS, function( key, keyword ){
+                       if( keyword.toLowerCase().indexOf( term.toLowerCase() ) 
=== 0){
+                               list.push(keyword);
+                       }
+               });
+
+               return list;
+       }
+
+       function getDefinedVariables( text ) {
+               var variables = [];
+
+               $.each(text.split(' '), function( key, word ){
+                       if( word.indexOf('?') === 0){
+                               variables.push(word);
+                       }
+               });
+
+               return $.unique( variables );
+       }
+
+       function getVariableHints( term, variables ) {
+
+               var list = [];
+
+               $.each(variables, function( key, variable ){
+                       if( variable.toLowerCase().indexOf( term.toLowerCase() 
) === 0){
+                               list.push(variable);
+                       }
+               });
+
+               return list;
+       }
+
+       function getHintCompletion( editor, currentWord , list) {
+
+               var completion = { list: [] };
+               completion.from = CodeMirror.Pos( editor.getCursor().line, 
currentWord.start );
+               completion.to = CodeMirror.Pos( editor.getCursor().line, 
currentWord.end );
+               completion.list = list;
+
+               return completion;
+       }
+
+
+       function getCurrentWord(line, position) {
+               var words = line.split(' '), matchedWord = "", scannedPostion = 
0;
+
+               $.each(words, function(key, word) {
+
+                       scannedPostion += word.length;
+
+                       if (key > 0) {// add spaces to position
+                               scannedPostion++;
+                       }
+
+                       if (scannedPostion >= position) {
+                               matchedWord = word;
+                               return;
+                       }
+               });
+
+               return {
+                       word : matchedWord,
+                       start : scannedPostion - matchedWord.length,
+                       end : scannedPostion
+               };
+       }
+
+       function getCurrentLine(editor) {
+               return editor.getLine(editor.getCursor().line);
+       }
+
+       function getCurrentCurserPosition(editor) {
+               return editor.getCursor().ch;
+       }
+
+} );
diff --git a/gui/index.html b/gui/index.html
index ce6bcc5..85c6df0 100644
--- a/gui/index.html
+++ b/gui/index.html
@@ -8,6 +8,7 @@
 <script src="sparql.js"></script>
 <script src="addon/hint/show-hint.js"></script>
 <script src="addon/hint/wikibase-rdf-hint.js"></script>
+<script src="addon/hint/wikibase-sparql-hint.js"></script>
 <script src="wdqs.js"></script>
 <script src="wdqs-explorer.js"></script>
 <script src="vis.js"></script>

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I40bf67ae766f8700ed07a1daf550c3b378387100
Gerrit-PatchSet: 1
Gerrit-Project: wikidata/query/rdf
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

Reply via email to