Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 71 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
CargoQueryAutocompleteAPI | |
0.00% |
0 / 71 |
|
0.00% |
0 / 9 |
552 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
getAllowedParams | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
getParamDescription | |
0.00% |
0 / 4 |
|
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 | |||
getVersion | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTables | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
20 | |||
getFields | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
90 |
1 | <?php |
2 | /** |
3 | * Adds and handles the 'cargoqueryautocomplete' action to the MediaWiki API. |
4 | * |
5 | * @ingroup Cargo |
6 | * |
7 | * @author Ankita Mandal |
8 | */ |
9 | |
10 | class CargoQueryAutocompleteAPI extends ApiBase { |
11 | |
12 | public function __construct( $query, $moduleName ) { |
13 | parent::__construct( $query, $moduleName ); |
14 | } |
15 | |
16 | public function execute() { |
17 | $params = $this->extractRequestParams(); |
18 | $substr = $params['search']; |
19 | $tables = $params['tables']; |
20 | |
21 | // Call appropriate method as per the parameters passed |
22 | if ( $tables === null ) { |
23 | $data = self::getTables( $substr ); |
24 | } else { |
25 | $data = self::getFields( $tables, $substr ); |
26 | } |
27 | |
28 | // If we got back an error message, exit with that message. |
29 | if ( !is_array( $data ) ) { |
30 | if ( !$data instanceof Message ) { |
31 | $data = ApiMessage::create( new RawMessage( '$1', [ $data ] ), 'unknownerror' ); |
32 | } |
33 | $this->dieWithError( $data ); |
34 | } |
35 | |
36 | // Set top-level elements. |
37 | $result = $this->getResult(); |
38 | $result->setIndexedTagName( $data, 'p' ); |
39 | $result->addValue( null, $this->getModuleName(), $data ); |
40 | } |
41 | |
42 | protected function getAllowedParams() { |
43 | return [ |
44 | 'limit' => [ |
45 | ApiBase::PARAM_TYPE => 'limit', |
46 | ApiBase::PARAM_DFLT => 10, |
47 | ApiBase::PARAM_MIN => 1, |
48 | ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, |
49 | ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
50 | ], |
51 | 'search' => null, |
52 | 'tables' => null, |
53 | ]; |
54 | } |
55 | |
56 | protected function getParamDescription() { |
57 | return [ |
58 | 'search' => 'Search substring', |
59 | 'tables' => 'Array of selected cargo table(s)' |
60 | ]; |
61 | } |
62 | |
63 | protected function getDescription() { |
64 | return 'Autocompletion call used by the Cargo extension (https://www.mediawiki.org/Extension:Cargo) on the CargoQuery page.'; |
65 | } |
66 | |
67 | protected function getExamples() { |
68 | return [ |
69 | 'api.php?action=cargoqueryautocomplete&format=json&search=Em', |
70 | 'api.php?action=cargoqueryautocomplete&format=json&tables=Employee&search=Skil', |
71 | ]; |
72 | } |
73 | |
74 | public function getVersion() { |
75 | return __CLASS__ . ': $Id$'; |
76 | } |
77 | |
78 | public function getTables( $substr ) { |
79 | $dbr = CargoUtils::getMainDBForRead(); |
80 | $tables = []; |
81 | $conds = [ |
82 | 'main_table NOT' . $dbr->buildLike( $dbr->anyString(), '__NEXT' ), |
83 | ]; |
84 | if ( $substr !== null && $substr !== '' ) { |
85 | $conds[] = 'main_table' . $dbr->buildLike( $dbr->anyString(), $substr, $dbr->anyString() ); |
86 | } |
87 | $res = $dbr->select( |
88 | 'cargo_tables', |
89 | [ 'main_table' ], |
90 | $conds, |
91 | __METHOD__ |
92 | ); |
93 | foreach ( $res as $row ) { |
94 | array_push( $tables, $row ); |
95 | } |
96 | |
97 | return $tables; |
98 | } |
99 | |
100 | public function getFields( $tableNames, $substr ) { |
101 | $dbr = CargoUtils::getMainDBForRead(); |
102 | $tables = explode( ",", $tableNames ); |
103 | $fields = []; |
104 | foreach ( $tables as $table ) { |
105 | $tableSchemas = []; |
106 | $res = $dbr->select( 'cargo_tables', [ 'main_table', 'table_schema' ], |
107 | [ 'main_table' => $table ], __METHOD__ ); |
108 | foreach ( $res as $row ) { |
109 | $tableName = $row->main_table; |
110 | $tableSchemaString = $row->table_schema; |
111 | $tableSchemas[$tableName] = CargoTableSchema::newFromDBString( $tableSchemaString ); |
112 | $mFieldDescriptions = array_column( $tableSchemas, 'mFieldDescriptions' ); |
113 | $tempfields = array_keys( call_user_func_array( 'array_merge', $mFieldDescriptions ) ); |
114 | array_push( $tempfields, "_pageName", "_pageTitle", "_pageNamespace", "_pageID", "_ID" ); |
115 | foreach ( $tempfields as $value ) { |
116 | if ( ( $substr === null || $substr == '' || stripos( $tableName, $substr ) === 0 ) || |
117 | stristr( $value, $substr ) || |
118 | stristr( $tableName . '.' . $value, $substr ) |
119 | ) { |
120 | array_push( $fields, $tableName . '.' . $value ); |
121 | } |
122 | } |
123 | } |
124 | } |
125 | return $fields; |
126 | } |
127 | } |