-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCargoQueryAPI.php
92 lines (79 loc) · 2.67 KB
/
CargoQueryAPI.php
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
<?php
/**
* Adds and handles the 'cargoquery' action to the MediaWiki API.
*
* @ingroup Cargo
* @author Yaron Koren
*/
class CargoQueryAPI extends ApiBase {
public function __construct( $query, $moduleName ) {
parent::__construct( $query, $moduleName );
}
public function execute() {
$params = $this->extractRequestParams();
$tablesStr = $params['tables'];
$fieldsStr = $params['fields'];
$whereStr = $params['where'];
$joinOnStr = $params['join_on'];
$orderByStr = $params['order_by'];
$groupByStr = $params['group_by'];
$limitStr = $params['limit'];
if ( $tablesStr == '' ) {
$this->dieUsage( 'The tables must be specified', 'param_substr' );
}
$sqlQuery = CargoSQLQuery::newFromValues( $tablesStr, $fieldsStr, $whereStr, $joinOnStr, $groupByStr, $orderByStr, $limitStr );
try {
$queryResults = $sqlQuery->run();
} catch ( Exception $e ) {
$this->dieUsage( $e );
}
// Format data as the API requires it.
$formattedData = array();
foreach ( $queryResults as $row ) {
$formattedData[] = array( 'title' => $row );
}
// Set top-level elements.
$result = $this->getResult();
$result->setIndexedTagName( $formattedData, 'p' );
$result->addValue( null, $this->getModuleName(), $formattedData );
}
protected function getAllowedParams() {
return array (
'limit' => array (
ApiBase::PARAM_TYPE => 'limit',
ApiBase::PARAM_DFLT => 50,
ApiBase::PARAM_MIN => 1,
ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
),
'tables' => null,
'fields' => null,
'where' => null,
'join_on' => null,
'order_by' => null,
'group_by' => null,
);
}
protected function getParamDescription() {
return array (
'tables' => 'The Cargo database table or tables on which to search',
'fields' => 'The table field(s) to retrieve',
'where' => 'The conditions for the query, corresponding to an SQL WHERE clause',
'join_on' => 'Conditions for joining multiple tables, corresponding to an SQL JOIN ON clause',
'order_by' => 'The order of results, corresponding to an SQL ORDER BY clause',
'group_by' => 'Field(s) on which to group results, corresponding to an SQL GROUP BY clause',
'limit' => 'Limit how many entries to return',
);
}
protected function getDescription() {
return 'An SQL-style query used for data tables, provided by the Cargo extension (http://www.mediawiki.org/Extension:Cargo)';
}
protected function getExamples() {
return array (
'api.php?action=cargoquery&tables=Items&fields=_pageName=Item,Source,Date=Publication_date&where=Source+LIKE+\'%New%\'&order_by=Date&limit=100'
);
}
public function getVersion() {
return __CLASS__ . ': $Id$';
}
}