Skip to content

Commit

Permalink
fix: cleaning prefixes removes all instances instead of retaining wan…
Browse files Browse the repository at this point in the history
…ted ones
  • Loading branch information
m90 committed Jan 3, 2024
1 parent ca78c10 commit bdccc8e
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions wikibase/queryService/ui/queryHelper/QueryHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,22 @@ wikibase.queryService.ui.queryHelper.QueryHelper = ( function( $, wikibase, _ )
* @private
*/
SELF.prototype._cleanQueryPrefixes = function( query ) {
var prefixRegex = /PREFIX ([a-z]+): <(.*)>/gi,
m,
prefixes = {},
cleanQuery = query.replace( prefixRegex, '' ).replace( /\n+/g, '\n' );

while ( ( m = prefixRegex.exec( query ) ) ) {
var prefix = m[1];
var uri = m[2].replace( /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&' );

var newQuery = cleanQuery.replace( new RegExp( '<' + uri + '([^/>#]+?)>', 'gi' ),
prefix + ':$1' );

if ( cleanQuery !== newQuery ) {
cleanQuery = newQuery;
if ( !wikibase.queryService.RdfNamespaces.STANDARD_PREFIXES[prefix] ) {
prefixes[m[0]] = true;
}
}
}

cleanQuery = Object.keys( prefixes ).join( '\n' ) + '\n\n' + cleanQuery.trim();
return cleanQuery;
var prefixRe = /^\s*PREFIX\s+([a-z]+):\s+<.*>\s*$/;
return query.split( '\n' )
.map(function ( line ) {
if ( !prefixRe.test( line ) ) {
return line;
}
line = line.trim();
var match = line.match( prefixRe );
var prefix = match[1];
if ( prefix in wikibase.queryService.RdfNamespaces.STANDARD_PREFIXES ) {
return null;
}
return line;
})
.filter( Boolean )
.join( '\n' );
};

/**
Expand Down

0 comments on commit bdccc8e

Please sign in to comment.