Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
67.27% covered (warning)
67.27%
37 / 55
55.56% covered (warning)
55.56%
5 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryPrefixSearch
68.52% covered (warning)
68.52%
37 / 54
55.56% covered (warning)
55.56%
5 / 9
26.02
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 executeGenerator
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 run
69.44% covered (warning)
69.44%
25 / 36
0.00% covered (danger)
0.00%
0 / 1
11.31
 getCacheMode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAllowedParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSearchProfileParams
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * @license GPL-2.0-or-later
5 * @file
6 * @since 1.23
7 */
8
9namespace MediaWiki\Api;
10
11use MediaWiki\Search\SearchEngine;
12use MediaWiki\Search\SearchEngineConfig;
13use MediaWiki\Search\SearchEngineFactory;
14
15/**
16 * @ingroup API
17 */
18class ApiQueryPrefixSearch extends ApiQueryGeneratorBase {
19    use \MediaWiki\Api\SearchApi;
20
21    public function __construct(
22        ApiQuery $query,
23        string $moduleName,
24        SearchEngineConfig $searchEngineConfig,
25        SearchEngineFactory $searchEngineFactory
26    ) {
27        parent::__construct( $query, $moduleName, 'ps' );
28        // Services needed in SearchApi trait
29        $this->searchEngineConfig = $searchEngineConfig;
30        $this->searchEngineFactory = $searchEngineFactory;
31    }
32
33    public function execute() {
34        $this->run();
35    }
36
37    /** @inheritDoc */
38    public function executeGenerator( $resultPageSet ) {
39        $this->run( $resultPageSet );
40    }
41
42    /**
43     * @param ApiPageSet|null $resultPageSet
44     */
45    private function run( $resultPageSet = null ) {
46        $params = $this->extractRequestParams();
47        $search = $params['search'];
48        $limit = $params['limit'];
49        $offset = $params['offset'];
50
51        $searchEngine = $this->buildSearchEngine( $params );
52        $suggestions = $searchEngine->completionSearchWithVariants( $search );
53        $titles = $searchEngine->extractTitles( $suggestions );
54
55        if ( $suggestions->hasMoreResults() ) {
56            $this->setContinueEnumParameter( 'offset', $offset + $limit );
57        }
58
59        if ( $resultPageSet ) {
60            $resultPageSet->setRedirectMergePolicy( static function ( array $current, array $new ) {
61                if ( !isset( $current['index'] ) || $new['index'] < $current['index'] ) {
62                    $current['index'] = $new['index'];
63                }
64                return $current;
65            } );
66            $resultPageSet->populateFromTitles( $titles );
67            foreach ( $titles as $index => $title ) {
68                $resultPageSet->setGeneratorData( $title, [ 'index' => $index + $offset + 1 ] );
69            }
70        } else {
71            $result = $this->getResult();
72            $count = 0;
73            foreach ( $titles as $title ) {
74                $vals = [
75                    'ns' => $title->getNamespace(),
76                    'title' => $title->getPrefixedText(),
77                ];
78                if ( $title->isSpecialPage() ) {
79                    $vals['special'] = true;
80                } else {
81                    $vals['pageid'] = (int)$title->getArticleID();
82                }
83                $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
84                ++$count;
85                if ( !$fit ) {
86                    $this->setContinueEnumParameter( 'offset', $offset + $count );
87                    break;
88                }
89            }
90            $result->addIndexedTagName(
91                [ 'query', $this->getModuleName() ], $this->getModulePrefix()
92            );
93        }
94    }
95
96    /** @inheritDoc */
97    public function getCacheMode( $params ) {
98        return 'public';
99    }
100
101    /** @inheritDoc */
102    public function getAllowedParams() {
103        return $this->buildCommonApiParams();
104    }
105
106    /** @inheritDoc */
107    public function getSearchProfileParams() {
108        return [
109            'profile' => [
110                'profile-type' => SearchEngine::COMPLETION_PROFILE_TYPE,
111                'help-message' => 'apihelp-query+prefixsearch-param-profile',
112            ],
113        ];
114    }
115
116    /** @inheritDoc */
117    protected function getExamplesMessages() {
118        return [
119            'action=query&list=prefixsearch&pssearch=meaning'
120                => 'apihelp-query+prefixsearch-example-simple',
121        ];
122    }
123
124    /** @inheritDoc */
125    public function getHelpUrls() {
126        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Prefixsearch';
127    }
128}
129
130/** @deprecated class alias since 1.43 */
131class_alias( ApiQueryPrefixSearch::class, 'ApiQueryPrefixSearch' );