Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
CargoRecreateSpecialTableAPI
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 8
272
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 / 27
0.00% covered (danger)
0.00%
0 / 1
90
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getParamDescription
0.00% covered (danger)
0.00%
0 / 4
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 'cargorecreatespecialtable' action to the MediaWiki API.
4 *
5 * @ingroup Cargo
6 * @author Yaron Koren
7 */
8
9class CargoRecreateSpecialTableAPI extends ApiBase {
10
11    public function __construct( $query, $moduleName ) {
12        parent::__construct( $query, $moduleName );
13    }
14
15    public function execute() {
16        $user = $this->getUser();
17
18        if ( !$user->isAllowed( 'recreatecargodata' ) || $user->getBlock() !== null ) {
19            $this->dieWithError( [ 'badaccess-groups' ] );
20        }
21
22        $params = $this->extractRequestParams();
23        $tableStr = $params['table'];
24        if ( $tableStr == '' ) {
25            $this->dieWithError( 'The table must be specified', 'param_substr' );
26        }
27        $createReplacement = $params['createReplacement'];
28
29        $tableName = $createReplacement ? $tableStr . '__NEXT' : $tableStr;
30        if ( $tableStr == '_pageData' ) {
31                $tableSchema = CargoPageData::getTableSchema();
32        } elseif ( $tableStr == '_fileData' ) {
33                $tableSchema = CargoFileData::getTableSchema();
34        } elseif ( $tableStr == '_bpmnData' ) {
35                $tableSchema = CargoBPMNData::getTableSchema();
36        } elseif ( $tableStr == '_ganttData' ) {
37                $tableSchema = CargoGanttData::getTableSchema();
38        } else {
39            $this->dieWithError( 'Invalid table name', 'param_substr' );
40        }
41        $tableSchemaString = $tableSchema->toDBString();
42        $cdb = CargoUtils::getDB();
43        $dbw = CargoUtils::getMainDBForWrite();
44        CargoUtils::createCargoTableOrTables(
45            $cdb, $dbw, $tableName, $tableSchema, $tableSchemaString, -1
46        );
47
48        // Set top-level elements.
49        $result = $this->getResult();
50        $result->addValue( null, 'success', true );
51
52        CargoUtils::logTableAction( 'recreatetable', $tableStr, $user );
53    }
54
55    protected function getAllowedParams() {
56        return [
57            'table' => [
58                ApiBase::PARAM_TYPE => 'string',
59            ],
60            'createReplacement' => [
61                ApiBase::PARAM_TYPE => 'boolean',
62            ],
63        ];
64    }
65
66    protected function getParamDescription() {
67        return [
68            'table' => 'The special table to be recreated',
69            'createReplacement' => 'Whether to put data into a replacement table',
70        ];
71    }
72
73    protected function getDescription() {
74        return 'An API module to recreate tables for the Cargo extension '
75            . '(https://www.mediawiki.org/Extension:Cargo)';
76    }
77
78    protected function getExamples() {
79        return [
80            'api.php?action=cargorecreatespecialtable&table=_pageData'
81        ];
82    }
83
84    public function mustBePosted() {
85        return true;
86    }
87
88    public function needsToken() {
89        return 'csrf';
90    }
91
92}