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