-
Notifications
You must be signed in to change notification settings - Fork 0
Add cypher_raw parser function #471
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| This page demonstrates the <code>cypher_raw</code> parser function for executing Cypher queries against the Neo4j graph database. | ||
|
|
||
| This is a '''demo feature''' for the NeoWiki proof of concept demo. It will not be present as-is in a production version. | ||
|
|
||
| == Query Companies == | ||
|
|
||
| <syntaxhighlight lang="cypher"> | ||
| MATCH (n:Company) RETURN n.name, n.id | ||
| </syntaxhighlight> | ||
|
|
||
| {{#cypher_raw: MATCH (n:Company) RETURN n.name, n.id}} | ||
|
|
||
| == Query Products == | ||
|
|
||
| <syntaxhighlight lang="cypher"> | ||
| MATCH (n:Product) RETURN n.name, n.id | ||
| </syntaxhighlight> | ||
|
|
||
| {{#cypher_raw: MATCH (n:Product) RETURN n.name, n.id}} | ||
|
|
||
| == Query Relations == | ||
|
|
||
| <syntaxhighlight lang="cypher"> | ||
| MATCH (source:Company)-[r]->(target) | ||
| RETURN source.name, target.name, r.id | ||
| LIMIT 10 | ||
| </syntaxhighlight> | ||
|
|
||
| {{#cypher_raw: MATCH (source:Company)-[r]->(target) RETURN source.name, target.name, r.id LIMIT 10}} | ||
|
|
||
| == Notes == | ||
|
|
||
| * Only read-only queries are allowed. Write operations like <code>CREATE</code>, <code>SET</code>, <code>DELETE</code> are rejected. | ||
| * Function calls are currently not supported. | ||
| * Query results are displayed as formatted JSON. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <?php | ||
|
|
||
| $magicWords = []; | ||
|
|
||
| $magicWords['en'] = [ | ||
| 'cypher_raw' => [ 0, 'cypher_raw' ], | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| <?php | ||
|
|
||
| declare( strict_types = 1 ); | ||
|
|
||
| namespace ProfessionalWiki\NeoWiki\EntryPoints; | ||
|
|
||
| use Exception; | ||
| use MediaWiki\Parser\Parser; | ||
| use ProfessionalWiki\NeoWiki\CypherQueryFilter; | ||
| use ProfessionalWiki\NeoWiki\Persistence\QueryEngine; | ||
| use RuntimeException; | ||
|
|
||
| class CypherRawParserFunction { | ||
|
|
||
| public function __construct( | ||
| private readonly QueryEngine $queryEngine, | ||
| private readonly CypherQueryFilter $queryFilter | ||
| ) { | ||
| } | ||
|
|
||
| public function handle( Parser $parser, string $cypherQuery ): string { | ||
| $cypherQuery = trim( $cypherQuery ); | ||
|
|
||
| if ( $cypherQuery === '' ) { | ||
| return $this->formatError( wfMessage( 'neowiki-cypher-raw-error-empty-query' )->text() ); | ||
| } | ||
|
|
||
| if ( !$this->queryFilter->isReadQuery( $cypherQuery ) ) { | ||
| return $this->formatError( wfMessage( 'neowiki-cypher-raw-error-write-query' )->text() ); | ||
| } | ||
|
|
||
| try { | ||
| $result = $this->queryEngine->runReadQuery( $cypherQuery ); | ||
| $jsonOutput = json_encode( $result->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ); | ||
|
|
||
| if ( $jsonOutput === false ) { | ||
| throw new RuntimeException( wfMessage( 'neowiki-cypher-raw-error-json-encode' )->text() ); | ||
| } | ||
|
|
||
| return $this->formatCodeBlock( $jsonOutput ); | ||
| } catch ( Exception $e ) { | ||
| return $this->formatError( wfMessage( 'neowiki-cypher-raw-error-query-failed', $e->getMessage() )->text() ); | ||
| } | ||
| } | ||
|
|
||
| private function formatCodeBlock( string $content ): string { | ||
| return '<pre><code class="json">' . "\n" . htmlspecialchars( $content ) . "\n" . '</code></pre>'; | ||
| } | ||
|
|
||
| private function formatError( string $message ): string { | ||
| return '<div class="error">' . htmlspecialchars( $message ) . '</div>'; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
tests/phpunit/EntryPoints/CypherRawParserFunctionTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| <?php | ||
|
|
||
| declare( strict_types = 1 ); | ||
|
|
||
| namespace ProfessionalWiki\NeoWiki\Tests\EntryPoints; | ||
|
|
||
| use Exception; | ||
| use Laudis\Neo4j\Databags\SummarizedResult; | ||
| use Laudis\Neo4j\Types\CypherMap; | ||
| use MediaWiki\Parser\Parser; | ||
| use PHPUnit\Framework\TestCase; | ||
| use ProfessionalWiki\NeoWiki\CypherQueryFilter; | ||
| use ProfessionalWiki\NeoWiki\EntryPoints\CypherRawParserFunction; | ||
| use ProfessionalWiki\NeoWiki\Persistence\QueryEngine; | ||
|
|
||
| /** | ||
| * @covers \ProfessionalWiki\NeoWiki\EntryPoints\CypherRawParserFunction | ||
| */ | ||
| class CypherRawParserFunctionTest extends TestCase { | ||
|
|
||
| private function createMockParser(): Parser { | ||
| return $this->createMock( Parser::class ); | ||
| } | ||
|
|
||
| private function createDummyQueryEngine(): QueryEngine { | ||
| // Create a simple mock that won't be called | ||
| return $this->createMock( QueryEngine::class ); | ||
| } | ||
|
|
||
| private function createQueryEngineWithData( array $returnData ): QueryEngine { | ||
| $queryEngine = $this->createMock( QueryEngine::class ); | ||
|
|
||
| $cypherMaps = array_map( | ||
| fn( array $row ) => new CypherMap( $row ), | ||
| $returnData | ||
| ); | ||
| $summary = null; | ||
| $result = new SummarizedResult( $summary, $cypherMaps ); | ||
|
|
||
| $queryEngine | ||
| ->method( 'runReadQuery' ) | ||
| ->willReturn( $result ); | ||
|
|
||
| return $queryEngine; | ||
| } | ||
|
|
||
| private function createQueryEngineWithException( Exception $exception ): QueryEngine { | ||
| $queryEngine = $this->createMock( QueryEngine::class ); | ||
| $queryEngine | ||
| ->method( 'runReadQuery' ) | ||
| ->willThrowException( $exception ); | ||
| return $queryEngine; | ||
| } | ||
|
|
||
| public function testEmptyQueryReturnsError(): void { | ||
| $parserFunction = new CypherRawParserFunction( | ||
| $this->createDummyQueryEngine(), | ||
| new CypherQueryFilter() | ||
| ); | ||
|
|
||
| $result = $parserFunction->handle( $this->createMockParser(), '' ); | ||
|
|
||
| $this->assertStringContainsString( 'error', $result ); | ||
| } | ||
|
|
||
| public function testWriteQueryIsRejected(): void { | ||
| $parserFunction = new CypherRawParserFunction( | ||
| $this->createDummyQueryEngine(), | ||
| new CypherQueryFilter() | ||
| ); | ||
|
|
||
| $result = $parserFunction->handle( $this->createMockParser(), "CREATE (n:Person {name: 'Alice'})" ); | ||
|
|
||
| $this->assertStringContainsString( 'error', $result ); | ||
| } | ||
|
|
||
| public function testValidReadQueryReturnsFormattedResult(): void { | ||
| $testData = [ | ||
| [ 'name' => 'Alice', 'age' => 30 ], | ||
| [ 'name' => 'Bob', 'age' => 25 ] | ||
| ]; | ||
|
|
||
| $parserFunction = new CypherRawParserFunction( | ||
| $this->createQueryEngineWithData( $testData ), | ||
| new CypherQueryFilter() | ||
| ); | ||
|
|
||
| $result = $parserFunction->handle( $this->createMockParser(), 'MATCH (n:Person) RETURN n' ); | ||
|
|
||
| $this->assertStringContainsString( '<pre><code class="json">', $result ); | ||
| $this->assertStringContainsString( 'Alice', $result ); | ||
| $this->assertStringContainsString( 'Bob', $result ); | ||
| } | ||
|
|
||
| public function testQueryExecutionExceptionReturnsError(): void { | ||
| $parserFunction = new CypherRawParserFunction( | ||
| $this->createQueryEngineWithException( new Exception( 'Connection failed' ) ), | ||
| new CypherQueryFilter() | ||
| ); | ||
|
|
||
| $result = $parserFunction->handle( $this->createMockParser(), 'MATCH (n) RETURN n' ); | ||
|
|
||
| $this->assertStringContainsString( 'error', $result ); | ||
| } | ||
|
|
||
| public function testTrimWhitespaceFromQuery(): void { | ||
| $parserFunction = new CypherRawParserFunction( | ||
| $this->createQueryEngineWithData( [] ), | ||
| new CypherQueryFilter() | ||
| ); | ||
|
|
||
| $result = $parserFunction->handle( $this->createMockParser(), ' MATCH (n) RETURN n ' ); | ||
|
|
||
| $this->assertStringContainsString( '<pre><code class="json">', $result ); | ||
| } | ||
|
|
||
| public function testOutputIsHTMLEscaped(): void { | ||
| $testData = [ | ||
| [ 'name' => '<script>alert("xss")</script>' ] | ||
| ]; | ||
|
|
||
| $parserFunction = new CypherRawParserFunction( | ||
| $this->createQueryEngineWithData( $testData ), | ||
| new CypherQueryFilter() | ||
| ); | ||
|
|
||
| $result = $parserFunction->handle( $this->createMockParser(), 'MATCH (n) RETURN n' ); | ||
|
|
||
| $this->assertStringNotContainsString( '<script>alert', $result ); | ||
| $this->assertStringContainsString( '<script>', $result ); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
Cypher, orCypher.wikitextshould be renamed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch
I did not realize this came from a commit in this PR so the fix landed in the follow up at