Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,11 @@ Install the required PHP via composer:
```sh
composer install
```

### Tests

Run the ShEx parser tests with Node.js:

```sh
node --test tests/shex-parser.test.js
```
37 changes: 33 additions & 4 deletions public_html/vue_components/shex-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ <h3 tt='last_items_created'></h3>
<script>
'use strict';

function getDirectPropertyPrefixes( schemaText, directPropertyNamespace ) {
const prefixRegex = /^\s*PREFIX\s+([A-Za-z_][\w.-]*):\s*<([^>]+)>/gim;
const prefixes = new Set();
let match;

while ( ( match = prefixRegex.exec( schemaText ) ) !== null ) {
if ( match[2] === directPropertyNamespace ) {
prefixes.add( match[1] );
}
}

return prefixes;
}

function getDirectProperty( part, prefixes ) {
const match = part.match( /^([A-Za-z_][\w.-]*):(P\d+)\s*(.*)$/ );
if ( match === null || !prefixes.has( match[1] ) ) {
return null;
}

return { id: match[2], rest: match[3] };
}

let ShexPage = Vue.extend ( {
props : [ 'e' ] ,
data : function () { return { s:{} , data:{} , lads:["labels","aliases","descriptions"] , can_create_new_item:false , creation_data:{} , warning_messages:[] , created_items:[] , vue_key_num:0 , debugging:false , loading:true , j:{} } } ,
Expand Down Expand Up @@ -93,18 +116,24 @@ <h3 tt='last_items_created'></h3>

let s = {labels:{} , props:{}} ;
let to_load = [] ;
const regex1 = RegExp('<([^/<>]+)>(?: EXTRA .*?)?\w*\{(.+?)\}','gi'); // THIS DOES NOT DEAL WITH {num} OR {num,num}!!
const directPropertyNamespace = new URL( config.wikibase_api ).origin
+ '/prop/direct/';
const directPropertyPrefixes = getDirectPropertyPrefixes(
j.schemaText,
directPropertyNamespace
);
const regex1 = /<([^/<>]+)>(?:\s+EXTRA\s+.*?)?\s*\w*\s*\{(.+?)\}/gi; // THIS DOES NOT DEAL WITH {num} OR {num,num}!!
while ( (m = regex1.exec(t)) !== null ) {
if ( m[1] != start ) continue ;
let parts = m[2].split(';') ;
$.each ( parts , function ( dummy , part ) {
part = $.trim(part) ;
if ( part == '' ) return ;
let n = part.match(/^wdt:(P\d+)\s*(.*)$/) ;
let n = getDirectProperty( part, directPropertyPrefixes );
if ( n == null ) return ;

let prop = n[1] ;
let rest = n[2] ;
let prop = n.id ;
let rest = n.rest ;
to_load.push ( prop ) ;
//console.log(part,'||',prop,'||',rest);
s.props[prop] = { min:0 } ;
Expand Down
100 changes: 100 additions & 0 deletions tests/shex-parser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
'use strict';

const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const test = require('node:test');
const vm = require('node:vm');

const shexPage = fs.readFileSync(
path.join(__dirname, '../public_html/vue_components/shex-page.html'),
'utf8'
);
const regexDeclaration = shexPage.match(/const regex1 = ([^;]+);/);

assert.ok(regexDeclaration, 'ShEx shape regex declaration exists');

function getProductionFunction( name ) {
const declaration = shexPage.match(
new RegExp( `function ${name}\\([\\s\\S]*?\\n\\}` )
);

assert.ok( declaration, `${name} helper exists` );
return vm.runInNewContext( `(${declaration[0]})` );
}

function getStartShapeBody( schemaText ) {
const startMatch = schemaText.match( /\n *start\s*=\s*@<\s*(.+?)\s*>/ );
if ( startMatch === null ) {
return null;
}

const startShape = startMatch[1];
const flattenedText = schemaText
.replace( /(?<!\<)#.*(\n|$)/mg, '\n' )
.replace( /\n/mg, ' ' );
const regex = vm.runInNewContext( regexDeclaration[1] );
let match;

while ( ( match = regex.exec( flattenedText ) ) !== null ) {
if ( match[1] === startShape ) {
return match[2];
}
}

return null;
}

const prefixAndStart = 'PREFIX wdt: <http://example.test/prop/direct/>\n'
+ 'PREFIX wd: <http://example.test/entity/>\n'
+ 'start = @<human>\n';

test( 'matches the start shape when whitespace precedes its opening brace', () => {
const schema = prefixAndStart + '<human> {\n wdt:P1 [wd:Q1]\n}';
const body = getStartShapeBody( schema );

assert.ok( body, 'the start shape should be detected' );
assert.match( body, /wdt:P1/ );
} );

test( 'continues to match compact shape syntax', () => {
const schema = prefixAndStart + '<human>{\n wdt:P1 [wd:Q1]\n}';
const body = getStartShapeBody( schema );

assert.ok( body, 'the start shape should be detected' );
assert.match( body, /wdt:P1/ );
} );

test( 'matches shapes with an EXTRA declaration', () => {
const schema = prefixAndStart
+ '<human> EXTRA wdt:P31 {\n wdt:P1 [wd:Q1]\n}';
const body = getStartShapeBody( schema );

assert.ok( body, 'the start shape should be detected' );
assert.match( body, /wdt:P1/ );
} );

test( 'allows whitespace around EXTRA and before the opening brace', () => {
const schema = prefixAndStart
+ '<human> EXTRA wdt:P31 {\n wdt:P1 [wd:Q1]\n}';
const body = getStartShapeBody( schema );

assert.ok( body, 'the start shape should be detected' );
assert.match( body, /wdt:P1/ );
} );

test( 'resolves custom prefixes for the local direct-property namespace', () => {
const directPropertyNamespace = 'http://example.test/prop/direct/';
const schema = 'PREFIX wdt: <http://example.test/prop/direct/>\n'
+ 'PREFIX wfdt: <http://example.test/prop/direct/>\n'
+ 'PREFIX wdtForeign: <http://www.wikidata.org/prop/direct/>\n'
+ 'PREFIX ex: <http://example.test/prop/>\n';
const getDirectPropertyPrefixes = getProductionFunction( 'getDirectPropertyPrefixes' );
const getDirectProperty = getProductionFunction( 'getDirectProperty' );
const prefixes = getDirectPropertyPrefixes( schema, directPropertyNamespace );

assert.equal( getDirectProperty( 'wfdt:P1 [wd:Q1]', prefixes ).id, 'P1' );
assert.equal( getDirectProperty( 'wdt:P2', prefixes ).id, 'P2' );
assert.equal( getDirectProperty( 'wdtForeign:P3', prefixes ), null );
assert.equal( getDirectProperty( 'ex:P4', prefixes ), null );
} );
Loading