jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/358934 )

Change subject: Move comment handling from VisualEditor to SparqlQuery
......................................................................


Move comment handling from VisualEditor to SparqlQuery

SparqlQuery already had a _queryComments member, but never used it –
VisualEditor had its own _queryComments and managed them independently
of the SparqlQuery. This commit moves the comments handling to
SparqlQuery: they are extracted in parse() and added back in
getQueryString(). SparqlQuery also gains a function to extract the
content of a “marker” comment, like the #TEMPLATE= comments.

VisualEditor._cleanQueryPrefixes() needs a slight adjustment because its
query argument now already includes the comments, before the prefixes;
as a consequence, after replacing the prefix lines with empty strings,
the resulting blank lines are no longer at the beginning of the string
and therefore no longer removed by trim().

Tests are added to check that getQueryString() includes the comments,
and that getCommentContent() works.

Change-Id: I624172e1b44033766fa41d165f8f713187c7194e
---
M wikibase/queryService/ui/visualEditor/SparqlQuery.js
M wikibase/queryService/ui/visualEditor/VisualEditor.js
M wikibase/tests/queryService/ui/visualEditor/SparqlQuery.test.js
3 files changed, 58 insertions(+), 24 deletions(-)

Approvals:
  Jonas Kress (WMDE): Looks good to me, approved
  jenkins-bot: Verified



diff --git a/wikibase/queryService/ui/visualEditor/SparqlQuery.js 
b/wikibase/queryService/ui/visualEditor/SparqlQuery.js
index 2e50f54..d942112 100644
--- a/wikibase/queryService/ui/visualEditor/SparqlQuery.js
+++ b/wikibase/queryService/ui/visualEditor/SparqlQuery.js
@@ -38,8 +38,15 @@
         * @param {String} query SPARQL query string
         */
        SELF.prototype.parse = function( query, prefixes ) {
-               var parser = new sparqljs.Parser( prefixes );
+               var parser = new sparqljs.Parser( prefixes ),
+                       queryComments = [];
                this._query = parser.parse( query );
+               $.each( query.split( '\n' ), function( index, line ) {
+                       if ( line.indexOf( '#' ) === 0 ) {
+                               queryComments.push( line );
+                       }
+               } );
+               this._queryComments = queryComments;
        };
 
        /**
@@ -49,7 +56,14 @@
         */
        SELF.prototype.getQueryString = function() {
                try {
-                       return new sparqljs.Generator().stringify( this._query 
);
+                       var sparql = new sparqljs.Generator().stringify( 
this._query ),
+                               comments = this._queryComments.join( '\n' 
).trim();
+
+                       if ( comments !== '' ) {
+                               return comments + '\n' + sparql;
+                       } else {
+                               return sparql;
+                       }
                } catch ( e ) {
                        return null;
                }
@@ -280,5 +294,26 @@
                return Object.keys( variables );
        };
 
+       /**
+        * Get the content of a content beginning with start.
+        *
+        * For example, on a query with '#foo=bar',
+        * getCommentContent( 'foo=' ) will return 'bar'.
+        *
+        * @param {string} start The beginning of the comment, *without* the 
comment mark ('#').
+        *
+        * @return {?string}
+        */
+       SELF.prototype.getCommentContent = function( start ) {
+               var i, comment;
+               for ( i = 0; i < this._queryComments.length; i++ ) {
+                       comment = this._queryComments[ i ];
+                       if ( comment.startsWith( '#' + start ) ) {
+                               return comment.substring( 1 + start.length );
+                       }
+               }
+               return null;
+       };
+
        return SELF;
 }( jQuery, wikibase, sparqljs ) );
diff --git a/wikibase/queryService/ui/visualEditor/VisualEditor.js 
b/wikibase/queryService/ui/visualEditor/VisualEditor.js
index 531da3a..e7c1a21 100644
--- a/wikibase/queryService/ui/visualEditor/VisualEditor.js
+++ b/wikibase/queryService/ui/visualEditor/VisualEditor.js
@@ -59,12 +59,6 @@
        SELF.prototype._query = null;
 
        /**
-        * @property {string[]}
-        * @private
-        */
-       SELF.prototype._queryComments = null;
-
-       /**
         * @property {Object}
         * @private
         */
@@ -97,14 +91,6 @@
         * @param {string} query SPARQL query string
         */
        SELF.prototype.setQuery = function( query ) {
-               var self = this;
-               this._queryComments = [];
-               $.each( query.split( '\n' ), function( k, v ) {
-                       if ( v.indexOf( '#' ) === 0 ) {
-                               self._queryComments.push( v );
-                       }
-               } );
-
                var prefixes = wikibase.queryService.RdfNamespaces.ALL_PREFIXES;
                this._query.parse( query, prefixes );
        };
@@ -118,7 +104,6 @@
                try {
                        var q = this._query.getQueryString();
                        q = this._cleanQueryPrefixes( q ).trim();
-                       q = this._queryComments.join( '\n' ) + '\n' + q;
                        return q.trim();
                } catch ( e ) {
                        return null;
@@ -134,7 +119,7 @@
                var prefixRegex = /PREFIX ([a-z]+): <(.*)>/gi,
                        m,
                        prefixes = {},
-                       cleanQuery = query.replace( prefixRegex, '' ).trim();
+                       cleanQuery = query.replace( prefixRegex, '' ).replace( 
/\n+/g, '\n' );
 
                while ( ( m = prefixRegex.exec( query ) ) ) {
                        var prefix = m[1];
@@ -190,15 +175,14 @@
         * @return {object}|null
         */
        SELF.prototype._getQueryTemplateDefinition = function() {
-               var definition = '#TEMPLATE=',
+               var templateComment = null,
                        template = null;
 
                try {
-                       $.each( this._queryComments, function( key, comment ) {
-                               if ( comment.startsWith( definition ) ) {
-                                       template = JSON.parse(  
comment.replace( definition, '' ) );
-                               }
-                       } );
+                       templateComment = this._query.getCommentContent( 
'TEMPLATE=' );
+                       if ( templateComment ) {
+                               template = JSON.parse( templateComment );
+                       }
                } catch ( e ) {
                }
 
diff --git a/wikibase/tests/queryService/ui/visualEditor/SparqlQuery.test.js 
b/wikibase/tests/queryService/ui/visualEditor/SparqlQuery.test.js
index f9c313c..e4efc7c 100644
--- a/wikibase/tests/queryService/ui/visualEditor/SparqlQuery.test.js
+++ b/wikibase/tests/queryService/ui/visualEditor/SparqlQuery.test.js
@@ -12,6 +12,7 @@
                TRIPLES: 'SELECT ?x1 ?x2 ?x3 WHERE { <S> <P> <O>.  OPTIONAL{ 
<S1> <P1> <O1> }  <S2> <P2> <O2>.}',
                SUBQUERIES: 'SELECT * WHERE {  {SELECT * WHERE { {SELECT * 
WHERE {}} }} }',
                BOUND: 'SELECT * WHERE { ?bound <P> <O>.  OPTIONAL{ <S1> ?x 
?bound2 }  <S2> <P2> <O2>.}',
+               COMMENTS: '#foo:bar\n#6*9=42\nSELECT * WHERE {  }',
        };
 
        QUnit.test( 'When instantiating new SparqlQuery then', function( assert 
) {
@@ -239,4 +240,18 @@
                ], 'bound subject variables must be ?bound and ?bound2' );
        } );
 
+       QUnit.test( 'When query is \'' + QUERY.COMMENTS + '\'', function( 
assert ) {
+               assert.expect( 3 );
+
+               var q = new PACKAGE.SparqlQuery();
+               q.parse( QUERY.COMMENTS );
+
+               assert.strictEqual( q.getQueryString(), QUERY.COMMENTS,
+                       'formatted query must be identical' );
+               assert.strictEqual( q.getCommentContent( 'foo:' ), 'bar',
+                       'content of #foo: comment must be bar' );
+               assert.strictEqual( q.getCommentContent( '6*9=' ), '42',
+                       'six times nine must be forty-two' );
+       } );
+
 }( jQuery, QUnit, sinon, wikibase ) );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I624172e1b44033766fa41d165f8f713187c7194e
Gerrit-PatchSet: 1
Gerrit-Project: wikidata/query/gui
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <lucas.werkmeis...@wikimedia.de>
Gerrit-Reviewer: Jonas Kress (WMDE) <jonas.kr...@wikimedia.de>
Gerrit-Reviewer: Smalyshev <smalys...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to