Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 70
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
CargoQueryAutocompleteAPI
0.00% covered (danger)
0.00%
0 / 70
0.00% covered (danger)
0.00%
0 / 9
552
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
20
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 getParamDescription
0.00% covered (danger)
0.00%
0 / 4
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
 getVersion
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTables
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
20
 getFields
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
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
10class 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        );
92        foreach ( $res as $row ) {
93            array_push( $tables, $row );
94        }
95
96        return $tables;
97    }
98
99    public function getFields( $tableNames, $substr ) {
100        $dbr = CargoUtils::getMainDBForRead();
101        $tables = explode( ",", $tableNames );
102        $fields = [];
103        foreach ( $tables as $table ) {
104            $tableSchemas = [];
105            $res = $dbr->select( 'cargo_tables', [ 'main_table', 'table_schema' ],
106                [ 'main_table' => $table ] );
107            foreach ( $res as $row ) {
108                $tableName = $row->main_table;
109                $tableSchemaString = $row->table_schema;
110                $tableSchemas[$tableName] = CargoTableSchema::newFromDBString( $tableSchemaString );
111                $mFieldDescriptions = array_column( $tableSchemas, 'mFieldDescriptions' );
112                $tempfields = array_keys( call_user_func_array( 'array_merge', $mFieldDescriptions ) );
113                array_push( $tempfields, "_pageName", "_pageTitle", "_pageNamespace", "_pageID", "_ID" );
114                foreach ( $tempfields as $value ) {
115                    if ( ( $substr === null || $substr == '' || stripos( $tableName, $substr ) === 0 ) ||
116                        stristr( $value, $substr ) ||
117                        stristr( $tableName . '.' . $value, $substr )
118                    ) {
119                        array_push( $fields, $tableName . '.' . $value );
120                    }
121                }
122            }
123        }
124        return $fields;
125    }
126}