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