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
14 changes: 11 additions & 3 deletions maintenance/GenerateReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,29 @@ public function execute() {
);
}

// enforce that the query does not contain pipe arguments aside from print requests
if ( preg_match( '/\|[^?]+/', $query ) ) {
// enforce that the query does not contain pipe arguments aside from
// print requests. Print requests ("|?Prop"), their modifiers
// ("|+filter=..."), and value disjunctions ("[[A::x||y]]") are allowed;
// printer parameters ("|format=...") are not.
if ( preg_match( '/\|\s*[a-zA-Z][a-zA-Z0-9_-]*\s*=/', $query ) ) {
$this->fatalError(
'Query cannot contain pipe arguments aside from print requests, i.e. "|?Prop" is allowed,' .
' but "|format=test" is not'
);
}

$limit = $this->getOption( 'limit' );
if ( $limit !== null && !preg_match( '/^\d+$/', $limit ) ) {
$this->fatalError( 'Limit must be a non-negative integer' );
}

$result = $semanticReports->getReportData(
$query,
$format,
$this->getOption( 'mainlabel' ),
$this->getOption( 'sep' ),
$this->getOption( 'valuesep' ),
$this->getOption( 'limit' )
$limit !== null ? (int)$limit : null
);

if ( $result === false ) {
Expand Down
39 changes: 39 additions & 0 deletions tests/phpunit/integration/GenerateReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace MediaWiki\Extension\SemanticReports\Tests\Integration;

use MediaWiki\Extension\SemanticReports\Maintenance\GenerateReport;
use MediaWiki\Extension\SemanticReports\SemanticReports;
use MediaWiki\MediaWikiServices;
use MediaWiki\Tests\Maintenance\MaintenanceBaseTestCase;
use RuntimeException;

Expand Down Expand Up @@ -39,6 +41,43 @@ public static function provideInvalidArgs() {
'Query cannot contain pipe arguments aside from print requests, i.e. "|?Prop" is allowed,' .
' but "|format=test" is not',
];
yield 'Invalid query - pipe argument with space' => [
[ '--query', '[[Category:Books]]| format=json', '--format', 'csv' ],
'Query cannot contain pipe arguments aside from print requests, i.e. "|?Prop" is allowed,' .
' but "|format=test" is not',
];
yield 'Invalid limit - non numeric' => [
[ '--query', 'ignored', '--format', 'csv', '--limit', 'abc' ],
'Limit must be a non-negative integer',
];
yield 'Invalid limit - decimal' => [
[ '--query', 'ignored', '--format', 'csv', '--limit', '10.5' ],
'Limit must be a non-negative integer',
];
}

public static function provideValidQueries() {
yield 'Print request' => [ '[[Category:Books]]|?Author' ];
yield 'Value disjunction' => [ '[[Status::Active||Pending]]' ];
yield 'Printout modifier' => [ '[[Status::Active]]|?Status|+filter=A' ];
}

/** @dataProvider provideValidQueries */
public function testValidQueriesAreNotRejected( string $query ) {
$mock = $this->createMock( SemanticReports::class );
$mock->method( 'getReportData' )->willReturn( 'a,b' );

$this->overrideMwServices();
MediaWikiServices::getInstance()->redefineService(
'SemanticReports',
static function () use ( $mock ) {
return $mock;
}
);
$this->maintenance->loadWithArgv( [ '--query', $query, '--format', 'csv' ] );
// Reaching the end of execute() without a RuntimeException proves the
// guards accepted the query.
$this->maintenance->execute();
$this->expectNotToPerformAssertions();
}
}
Loading