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