Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
58.46% covered (warning)
58.46%
38 / 65
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
CargoQueryAPI
58.46% covered (warning)
58.46%
38 / 65
33.33% covered (danger)
33.33%
2 / 6
19.67
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
61.54% covered (warning)
61.54%
16 / 26
0.00% covered (danger)
0.00%
0 / 1
8.05
 getAllowedParams
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
1
 getParamDescription
0.00% covered (danger)
0.00%
0 / 11
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 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Adds and handles the 'cargoquery' action to the MediaWiki API.
4 *
5 * @ingroup Cargo
6 * @author Yaron Koren
7 */
8
9class CargoQueryAPI extends ApiBase {
10
11    public function __construct( $query, $moduleName ) {
12        parent::__construct( $query, $moduleName );
13    }
14
15    public function execute() {
16        $params = $this->extractRequestParams();
17        $tablesStr = $params['tables'];
18        $fieldsStr = $params['fields'];
19        $whereStr = $params['where'];
20        $joinOnStr = $params['join_on'];
21        $orderByStr = $params['order_by'];
22        $groupByStr = $params['group_by'];
23        $havingStr = $params['having'];
24        $limitStr = $params['limit'];
25        $offsetStr = $params['offset'];
26
27        if ( $tablesStr == '' ) {
28            $this->dieWithError( 'The tables must be specified', 'param_substr' );
29        }
30
31        $sqlQuery = CargoSQLQuery::newFromValues( $tablesStr, $fieldsStr, $whereStr, $joinOnStr,
32                $groupByStr, $havingStr, $orderByStr, $limitStr, $offsetStr );
33
34        foreach ( $sqlQuery->mFieldStringAliases as $fieldStrAlias ) {
35            if ( $fieldStrAlias[0] == '_' ) {
36                $this->dieWithError( [ 'apierror-cargoquery-invalidfieldalias', $fieldStrAlias ] );
37            }
38        }
39
40        try {
41            $queryResults = $sqlQuery->run();
42        } catch ( Exception $e ) {
43            $this->dieWithError( $e, 'db_error' );
44        }
45
46        // Format data as the API requires it.
47        $formattedData = [];
48        foreach ( $queryResults as $row ) {
49            $formattedData[] = [ 'title' => $row ];
50        }
51
52        // Set top-level elements.
53        $result = $this->getResult();
54        $result->setIndexedTagName( $formattedData, 'p' );
55        $result->addValue( null, $this->getModuleName(), $formattedData );
56    }
57
58    protected function getAllowedParams() {
59        return [
60            'limit' => [
61                ApiBase::PARAM_TYPE => 'limit',
62                ApiBase::PARAM_DFLT => 50,
63                ApiBase::PARAM_MIN => 1,
64                ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
65                ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
66            ],
67            'tables' => null,
68            'fields' => null,
69            'where' => null,
70            'join_on' => null,
71            'group_by' => null,
72            'having' => null,
73            'order_by' => null,
74            'offset' => [
75                ApiBase::PARAM_TYPE => 'integer',
76                ApiBase::PARAM_DFLT => 0,
77                ApiBase::PARAM_MIN => 0,
78            ],
79        ];
80    }
81
82    protected function getParamDescription() {
83        return [
84            'tables' => 'The Cargo database table or tables on which to search',
85            'fields' => 'The table field(s) to retrieve',
86            'where' => 'The conditions for the query, corresponding to an SQL WHERE clause',
87            'join_on' => 'Conditions for joining multiple tables, corresponding to an SQL JOIN ON clause',
88            'order_by' => 'The order of results, corresponding to an SQL ORDER BY clause',
89            'group_by' => 'Field(s) on which to group results, corresponding to an SQL GROUP BY clause',
90            'having' => 'Conditions for grouped values, corresponding to an SQL HAVING clause',
91            'limit' => 'A limit on the number of results returned',
92            'offset' => 'The query offset number',
93        ];
94    }
95
96    protected function getDescription() {
97        return 'An SQL-style query used for data tables, provided by the Cargo extension '
98            . '(https://www.mediawiki.org/Extension:Cargo)';
99    }
100
101    protected function getExamples() {
102        return [
103            'api.php?action=cargoquery&tables=Items&fields=_pageName=Item,Source,Date=Publication_date'
104            . '&where=Source+LIKE+\'%New%\'&order_by=Date&limit=100'
105        ];
106    }
107
108}