-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateReport.php
More file actions
136 lines (118 loc) · 3.96 KB
/
Copy pathGenerateReport.php
File metadata and controls
136 lines (118 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace MediaWiki\Extension\SemanticReports\Maintenance;
use MediaWiki\Extension\SemanticReports\SemanticReports;
use MediaWiki\MediaWikiServices;
use RuntimeException;
$IP = getenv( 'MW_INSTALL_PATH' );
if ( $IP === false ) {
$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";
/**
* @method \MediaWiki\MediaWikiServices getServiceContainer() available in 1.40+
*/
class GenerateReport extends \Maintenance {
/**
* SemanticReportsReport constructor.
*/
public function __construct() {
parent::__construct();
$this->addDescription( 'Generates a report based on the semantic query' );
$this->addOption( 'query', 'Query to run: "{{#ask: ...}}"', true, true, 'q' );
$this->addOption( 'format', 'Output format: csv', true, true, 'f' );
$this->addOption( 'output', 'Output file', false, true, 'o' );
$this->addOption( 'mainlabel', 'Override title label', false, true, 'm' );
$this->addOption( 'sep', 'Override columns separator', false, true, 's' );
$this->addOption( 'valuesep', 'Override values separator', false, true, 'v' );
$this->addOption( 'limit', 'Override the results limit', false, true, 'l' );
$this->requireExtension( 'SemanticReports' );
}
/**
* @return null
*/
public function execute() {
// REL1_39 compat
if ( !method_exists( $this, 'getServiceContainer' ) ) {
/** @var SemanticReports $semanticReports */
$semanticReports = MediaWikiServices::getInstance()->get( 'SemanticReports' );
} else {
/** @var SemanticReports $semanticReports */
$semanticReports = $this->getServiceContainer()->get( 'SemanticReports' );
}
$query = $this->getOption( 'query' );
$format = $this->getOption( 'format' );
if ( $format !== 'csv' ) {
$this->fatalError( 'Only CSV output is supported' );
}
// enforce that the query does not contain curly braces
// (str_contains() is PHP 8+; this extension still supports 1.39/PHP 7.4)
if ( strpos( $query, '{' ) !== false ) {
$this->fatalError(
'Query cannot contain curly braces, i.e. use "[[Category:Test]] [[Property:Test]]", ' .
'not "{{#ask: [[Category:Test]] ... }}"'
);
}
// enforce that the query does not contain pipe arguments aside from print requests
if ( preg_match( '/\|[^?]+/', $query ) ) {
$this->fatalError(
'Query cannot contain pipe arguments aside from print requests, i.e. "|?Prop" is allowed,' .
' but "|format=test" is not'
);
}
$result = $semanticReports->getReportData(
$query,
$format,
$this->getOption( 'mainlabel' ),
$this->getOption( 'sep' ),
$this->getOption( 'valuesep' ),
$this->getOption( 'limit' )
);
if ( $result === false ) {
$this->fatalError( 'Error generating report' );
}
$filename = $this->getOption( 'output' );
if ( $filename ) {
// output to the file
if ( !file_put_contents( $filename, $result ) ) {
$this->fatalError( "Error saving report to $filename" );
}
$this->outputChanneled( "Report saved to $filename" );
} else {
// output to the stdout
$this->output( $result );
}
}
/**
* @codeCoverageIgnore
* @inheritDoc
* @return never
*/
protected function fatalError( $msg, $exitCode = 1 ) {
// Until 1.43 fatalError() would call exit() unconditionally, making it
// impossible to test fatalError() calls, see T272241
// In tests always use RuntimeException so that we support both 1.39 and
// 1.43
if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
parent::fatalError( $msg, $exitCode );
} else {
throw new RuntimeException( "FATAL ERROR: $msg (exit code = $exitCode)" );
}
}
/**
* @codeCoverageIgnore
* @inheritDoc
*/
protected function error( $err, $die = 0 ) {
// Parent method will use
// `fwrite( STDERR, $err . "\n" );` outside of tests
// but `print $err;` in tests, add an extra line ending for readability
// in tests
if ( defined( 'MW_PHPUNIT_TEST' ) ) {
print( $err . "\n" );
} else {
parent::error( $err, $die );
}
}
}
$maintClass = GenerateReport::class;
require_once RUN_MAINTENANCE_IF_MAIN;