Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 67 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
CargoAutocompleteAPI | |
0.00% |
0 / 67 |
|
0.00% |
0 / 7 |
272 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
30 | |||
getAllowedParams | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
2 | |||
getParamDescription | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getDescription | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getExamples | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
getAllValues | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
42 |
1 | <?php |
2 | /** |
3 | * @file |
4 | * @ingroup Cargo |
5 | */ |
6 | |
7 | /** |
8 | * Adds the 'cargoautocomplete' action to the MediaWiki API. |
9 | * |
10 | * @ingroup Cargo |
11 | * |
12 | * @author Yaron Koren |
13 | */ |
14 | class CargoAutocompleteAPI extends ApiBase { |
15 | |
16 | public function __construct( $query, $moduleName ) { |
17 | parent::__construct( $query, $moduleName ); |
18 | } |
19 | |
20 | public function execute() { |
21 | $params = $this->extractRequestParams(); |
22 | $substr = $params['substr']; |
23 | $table = $params['table']; |
24 | $field = $params['field']; |
25 | $where = $params['where']; |
26 | |
27 | if ( $table == '' ) { |
28 | $this->dieWithError( '"table" value must be specified', 'param_table' ); |
29 | } |
30 | |
31 | if ( $field == '' ) { |
32 | $this->dieWithError( '"field" value must be specified', 'param_field' ); |
33 | } |
34 | |
35 | $data = self::getAllValues( $table, $field, $where, $substr ); |
36 | |
37 | // If we got back an error message, exit with that message. |
38 | if ( !is_array( $data ) ) { |
39 | if ( !$data instanceof Message ) { |
40 | $data = ApiMessage::create( new RawMessage( '$1', [ $data ] ), 'unknownerror' ); |
41 | } |
42 | $this->dieWithError( $data ); |
43 | } |
44 | |
45 | // Set top-level elements. |
46 | $result = $this->getResult(); |
47 | $result->setIndexedTagName( $data, 'p' ); |
48 | $result->addValue( null, $this->getModuleName(), $data ); |
49 | } |
50 | |
51 | protected function getAllowedParams() { |
52 | return [ |
53 | 'limit' => [ |
54 | ApiBase::PARAM_TYPE => 'limit', |
55 | ApiBase::PARAM_DFLT => 10, |
56 | ApiBase::PARAM_MIN => 1, |
57 | ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, |
58 | ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
59 | ], |
60 | 'substr' => null, |
61 | 'table' => null, |
62 | 'field' => null, |
63 | 'where' => null, |
64 | ]; |
65 | } |
66 | |
67 | protected function getParamDescription() { |
68 | return [ |
69 | 'substr' => 'Search substring', |
70 | 'table' => 'Cargo table for which to search values', |
71 | 'field' => 'Cargo field for which to search values', |
72 | 'where' => 'The "where" clause for the query, based on the other filters specified', |
73 | ]; |
74 | } |
75 | |
76 | protected function getDescription() { |
77 | return 'Autocompletion call used by Special:Drilldown, defined by the Cargo extension (https://www.mediawiki.org/Extension:Cargo)'; |
78 | } |
79 | |
80 | protected function getExamples() { |
81 | return [ |
82 | 'api.php?action=cargoautocomplete&table=Books&field=Author&substr=jo', |
83 | 'api.php?action=cargoautocomplete&table=Books&field=Author&where=Genre=comedy+AND+Year_published=1986&substr=jo', |
84 | ]; |
85 | } |
86 | |
87 | private static function getAllValues( $table, $field, $where, $substr ) { |
88 | $values = []; |
89 | |
90 | if ( $substr !== null ) { |
91 | if ( $where != '' ) { |
92 | $where .= " AND "; |
93 | } |
94 | // There's no such global variable at the moment - |
95 | // perhaps there will be in the future. |
96 | // if ( $wgCargoDrilldownAutocompleteOnAllChars ) { |
97 | // $where .= "($field LIKE \"%$substr%\")"; |
98 | // } else { |
99 | $where .= "($field LIKE \"$substr%\" OR $field LIKE \"% $substr%\")"; |
100 | // } |
101 | } |
102 | |
103 | $sqlQuery = CargoSQLQuery::newFromValues( |
104 | $table, |
105 | $field, |
106 | $where, |
107 | $joinOn = null, |
108 | $field, |
109 | $having = null, |
110 | $field, |
111 | 20, |
112 | $offset = null |
113 | ); |
114 | if ( $field[0] != '_' ) { |
115 | $fieldAlias = str_replace( '_', ' ', $field ); |
116 | } else { |
117 | $fieldAlias = $field; |
118 | } |
119 | $queryResults = $sqlQuery->run(); |
120 | |
121 | foreach ( $queryResults as $row ) { |
122 | // @TODO - this check should not be necessary. |
123 | $value = $row[$fieldAlias]; |
124 | if ( $value != '' ) { |
125 | $values[] = $value; |
126 | } |
127 | } |
128 | |
129 | return $values; |
130 | } |
131 | |
132 | } |