Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 72 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
CargoRecreateDataAPI | |
0.00% |
0 / 72 |
|
0.00% |
0 / 8 |
420 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 43 |
|
0.00% |
0 / 1 |
182 | |||
getAllowedParams | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
2 | |||
getParamDescription | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getDescription | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getExamples | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
mustBePosted | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
needsToken | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Adds and handles the 'cargorecreatedata' action to the MediaWiki API. |
4 | * |
5 | * @ingroup Cargo |
6 | * @author Yaron Koren |
7 | */ |
8 | |
9 | use MediaWiki\MediaWikiServices; |
10 | use MediaWiki\Title\Title; |
11 | |
12 | class CargoRecreateDataAPI extends ApiBase { |
13 | |
14 | public function __construct( $query, $moduleName ) { |
15 | parent::__construct( $query, $moduleName ); |
16 | } |
17 | |
18 | public function execute() { |
19 | $user = $this->getUser(); |
20 | |
21 | if ( !$user->isAllowed( 'recreatecargodata' ) || $user->getBlock() !== null ) { |
22 | $this->dieWithError( [ 'badaccess-groups' ] ); |
23 | } |
24 | |
25 | $params = $this->extractRequestParams(); |
26 | $templateStr = $params['template']; |
27 | $tableStr = $params['table']; |
28 | |
29 | if ( $templateStr == '' ) { |
30 | $specialTableNames = CargoUtils::specialTableNames(); |
31 | if ( !in_array( $tableStr, $specialTableNames ) ) { |
32 | $this->dieWithError( 'The template must be specified', 'param_substr' ); |
33 | } |
34 | } |
35 | |
36 | if ( $tableStr == '' ) { |
37 | $this->dieWithError( 'The table must be specified', 'param_substr' ); |
38 | } |
39 | |
40 | // Create the jobs. |
41 | $jobParams = [ |
42 | 'dbTableName' => $tableStr, |
43 | 'replaceOldRows' => $params['replaceOldRows'] |
44 | ]; |
45 | $jobs = []; |
46 | if ( $templateStr != '' ) { |
47 | $templateTitle = Title::makeTitleSafe( NS_TEMPLATE, $templateStr ); |
48 | $titlesToStore = $templateTitle->getTemplateLinksTo( [ |
49 | 'LIMIT' => 500, 'OFFSET' => $params['offset'] ] ); |
50 | } else { |
51 | if ( $tableStr == '_pageData' ) { |
52 | $conds = null; |
53 | } elseif ( $tableStr == '_fileData' ) { |
54 | $conds = 'page_namespace = ' . NS_FILE; |
55 | } elseif ( $tableStr == '_bpmnData' ) { |
56 | $conds = 'page_namespace = ' . FD_NS_BPMN; |
57 | } else { // if ( $tableStr == '_ganttData' ) { |
58 | $conds = 'page_namespace = ' . FD_NS_GANTT; |
59 | } |
60 | $dbr = CargoUtils::getMainDBForRead(); |
61 | $pages = $dbr->select( |
62 | 'page', [ 'page_id' ], $conds, __METHOD__, |
63 | [ 'LIMIT' => 500, 'OFFSET' => $params['offset'] ] |
64 | ); |
65 | foreach ( $pages as $page ) { |
66 | $title = Title::newFromID( $page->page_id ); |
67 | if ( $title == null ) { |
68 | continue; |
69 | } |
70 | $titlesToStore[] = $title; |
71 | } |
72 | } |
73 | |
74 | foreach ( $titlesToStore as $titleToStore ) { |
75 | $jobs[] = new CargoPopulateTableJob( $titleToStore, $jobParams ); |
76 | } |
77 | MediaWikiServices::getInstance()->getJobQueueGroup()->push( $jobs ); |
78 | |
79 | // Set top-level elements. |
80 | $result = $this->getResult(); |
81 | $result->addValue( null, 'success', true ); |
82 | } |
83 | |
84 | protected function getAllowedParams() { |
85 | return [ |
86 | 'template' => [ |
87 | ApiBase::PARAM_TYPE => 'string', |
88 | ], |
89 | 'table' => [ |
90 | ApiBase::PARAM_TYPE => 'string', |
91 | ], |
92 | 'offset' => [ |
93 | ApiBase::PARAM_TYPE => 'integer', |
94 | ApiBase::PARAM_DFLT => 0, |
95 | ], |
96 | 'replaceOldRows' => [ |
97 | ApiBase::PARAM_TYPE => 'boolean', |
98 | ], |
99 | ]; |
100 | } |
101 | |
102 | protected function getParamDescription() { |
103 | return [ |
104 | 'template' => 'The template whose data to use', |
105 | 'table' => 'The Cargo database table to repopulate', |
106 | 'offset' => 'Of the pages that call this template, the number at which to start querying', |
107 | 'replaceOldRows' => 'Whether to replace old rows for each page while repopulating the table', |
108 | ]; |
109 | } |
110 | |
111 | protected function getDescription() { |
112 | return 'An API module to recreate data for the Cargo extension ' |
113 | . '(https://www.mediawiki.org/Extension:Cargo)'; |
114 | } |
115 | |
116 | protected function getExamples() { |
117 | return [ |
118 | 'api.php?action=cargorecreatedata&template=City&table=Cities' |
119 | ]; |
120 | } |
121 | |
122 | public function mustBePosted() { |
123 | return true; |
124 | } |
125 | |
126 | public function needsToken() { |
127 | return 'csrf'; |
128 | } |
129 | |
130 | } |