Skip to content

Commit

Permalink
integrated SPARQL Query; addresses SPARQL-issue#1
Browse files Browse the repository at this point in the history
  • Loading branch information
waximabbax committed Nov 8, 2023
1 parent 0475683 commit 7e1ed8e
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 26 deletions.
24 changes: 14 additions & 10 deletions src/SparqlLua.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,31 @@

namespace ProfessionalWiki\SPARQL;

use InvalidArgumentException;
use MediaWiki\MediaWikiServices;
use Scribunto_LuaLibraryBase;

class SparqlLua extends Scribunto_LuaLibraryBase {

/**
* @return mixed[]
*/
public function register(): array {
public function register() {

Check failure on line 13 in src/SparqlLua.php

View workflow job for this annotation

GitHub Actions / PHPStan: MW REL1_39, PHP 8.1

Method ProfessionalWiki\SPARQL\SparqlLua::register() return type has no value type specified in iterable type array.

Check failure on line 13 in src/SparqlLua.php

View workflow job for this annotation

GitHub Actions / PHPStan: MW REL1_39, PHP 8.1

Method ProfessionalWiki\SPARQL\SparqlLua::register() return type has no value type specified in iterable type array.
return $this->getEngine()->registerInterface(
__DIR__ . '/Sparql.lua',
[
'runQuery' => fn( string $subjectId ): array => [ $this->runQuery( $subjectId ) ],
'runQuery' => fn ( string $subjectId ): array => [ $this->runSparqlQuery( $subjectId ) ],
]
);
}

/**
* @return mixed[]
*/
private function runQuery( string $sparql ): array {
return [ $sparql ]; // TODO
private function runSparqlQuery( string $sparql ): string|null {
$sparqlEndpoint = MediaWikiServices::getInstance()->getMainConfig()->get( 'SPARQLEndpoint' );

if ( !is_string( $sparqlEndpoint ) ) {
throw new InvalidArgumentException( 'SPARQLEndpoint must be configured properly' );
}

$requestFactory = MediaWikiServices::getInstance()->getHttpRequestFactory();
$queryRunner = new SparqlLuaQueryRunner( $requestFactory, $sparqlEndpoint );
return $queryRunner->runQuery( $sparql );
}

}
28 changes: 28 additions & 0 deletions src/SparqlLuaQueryRunner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare( strict_types = 1 );

namespace ProfessionalWiki\SPARQL;

use MediaWiki\Http\HttpRequestFactory;

class SparqlLuaQueryRunner {

public function __construct(
private readonly HttpRequestFactory $requestFactory,
private readonly string $sparqlEndpoint
) {
}

public function runQuery( string $sparql ): string|null {
$options = [
'postData' => [
'query' => $sparql,
'format' => 'json'
],
];

return $this->requestFactory->request( 'POST', $this->sparqlEndpoint, $options );
}

}
40 changes: 40 additions & 0 deletions tests/SparqlLuaQueryRunnerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare( strict_types = 1 );

namespace ProfessionalWiki\SPARQL\Tests;

use MediaWiki\Http\HttpRequestFactory;
use PHPUnit\Framework\TestCase;
use ProfessionalWiki\SPARQL\SparqlLuaQueryRunner;

/**
* @covers SparqlLuaQueryRunner::runQuery
*/
class SparqlLuaQueryRunnerTest extends TestCase {

public function testRunQuery() {
$SPARQLEndpoint = 'https://www.example.com';
$httpRequestFactory = $this->createMock( HttpRequestFactory::class );
$sparqlLua = new SparqlLuaQueryRunner( $httpRequestFactory, $SPARQLEndpoint );

$sparql = 'SELECT * WHERE { ?s ?p ?o }';
$expectedResponse = 'mocking response';

$httpRequestFactory
->expects( $this->once() )
->method( 'request' )
->with(
'POST',
$SPARQLEndpoint,
[
'postData' => [ 'query' => $sparql, 'format' => 'json' ]
]
)
->willReturn( $expectedResponse );

$response = $sparqlLua->runQuery( $sparql );
$this->assertSame( $expectedResponse, $response );
}

}
15 changes: 0 additions & 15 deletions tests/StubTest.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/parser/lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local sparql = require('SPARQL')

local p = {}
function p.runQuery(frame)
return sparql.runQuery(frame.args[1])[0]
return sparql.runQuery(frame.args[1])
end

return p
Expand Down

0 comments on commit 7e1ed8e

Please sign in to comment.