Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integrated SPARQL Query; addresses SPARQL-issue-1 #3

Merged
merged 1 commit into from
Nov 8, 2023
Merged
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
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
ignoreErrors:
-
message: "#^Method ProfessionalWiki\\\\SPARQL\\\\SparqlLua\\:\\:register\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/SparqlLua.php
22 changes: 13 additions & 9 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 {
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