Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
68.52% |
37 / 54 |
|
55.56% |
5 / 9 |
CRAP | |
0.00% |
0 / 1 |
ApiQueryPrefixSearch | |
68.52% |
37 / 54 |
|
55.56% |
5 / 9 |
26.02 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
executeGenerator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
run | |
69.44% |
25 / 36 |
|
0.00% |
0 / 1 |
11.31 | |||
getCacheMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAllowedParams | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSearchProfileParams | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
getExamplesMessages | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * http://www.gnu.org/copyleft/gpl.html |
18 | * |
19 | * @file |
20 | * @since 1.23 |
21 | */ |
22 | |
23 | /** |
24 | * @ingroup API |
25 | */ |
26 | class ApiQueryPrefixSearch extends ApiQueryGeneratorBase { |
27 | use SearchApi; |
28 | |
29 | /** |
30 | * @param ApiQuery $query |
31 | * @param string $moduleName |
32 | * @param SearchEngineConfig $searchEngineConfig |
33 | * @param SearchEngineFactory $searchEngineFactory |
34 | */ |
35 | public function __construct( |
36 | ApiQuery $query, |
37 | $moduleName, |
38 | SearchEngineConfig $searchEngineConfig, |
39 | SearchEngineFactory $searchEngineFactory |
40 | ) { |
41 | parent::__construct( $query, $moduleName, 'ps' ); |
42 | // Services needed in SearchApi trait |
43 | $this->searchEngineConfig = $searchEngineConfig; |
44 | $this->searchEngineFactory = $searchEngineFactory; |
45 | } |
46 | |
47 | public function execute() { |
48 | $this->run(); |
49 | } |
50 | |
51 | public function executeGenerator( $resultPageSet ) { |
52 | $this->run( $resultPageSet ); |
53 | } |
54 | |
55 | /** |
56 | * @param ApiPageSet|null $resultPageSet |
57 | */ |
58 | private function run( $resultPageSet = null ) { |
59 | $params = $this->extractRequestParams(); |
60 | $search = $params['search']; |
61 | $limit = $params['limit']; |
62 | $offset = $params['offset']; |
63 | |
64 | $searchEngine = $this->buildSearchEngine( $params ); |
65 | $suggestions = $searchEngine->completionSearchWithVariants( $search ); |
66 | $titles = $searchEngine->extractTitles( $suggestions ); |
67 | |
68 | if ( $suggestions->hasMoreResults() ) { |
69 | $this->setContinueEnumParameter( 'offset', $offset + $limit ); |
70 | } |
71 | |
72 | if ( $resultPageSet ) { |
73 | $resultPageSet->setRedirectMergePolicy( static function ( array $current, array $new ) { |
74 | if ( !isset( $current['index'] ) || $new['index'] < $current['index'] ) { |
75 | $current['index'] = $new['index']; |
76 | } |
77 | return $current; |
78 | } ); |
79 | $resultPageSet->populateFromTitles( $titles ); |
80 | foreach ( $titles as $index => $title ) { |
81 | $resultPageSet->setGeneratorData( $title, [ 'index' => $index + $offset + 1 ] ); |
82 | } |
83 | } else { |
84 | $result = $this->getResult(); |
85 | $count = 0; |
86 | foreach ( $titles as $title ) { |
87 | $vals = [ |
88 | 'ns' => $title->getNamespace(), |
89 | 'title' => $title->getPrefixedText(), |
90 | ]; |
91 | if ( $title->isSpecialPage() ) { |
92 | $vals['special'] = true; |
93 | } else { |
94 | $vals['pageid'] = (int)$title->getArticleID(); |
95 | } |
96 | $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals ); |
97 | ++$count; |
98 | if ( !$fit ) { |
99 | $this->setContinueEnumParameter( 'offset', $offset + $count ); |
100 | break; |
101 | } |
102 | } |
103 | $result->addIndexedTagName( |
104 | [ 'query', $this->getModuleName() ], $this->getModulePrefix() |
105 | ); |
106 | } |
107 | } |
108 | |
109 | public function getCacheMode( $params ) { |
110 | return 'public'; |
111 | } |
112 | |
113 | public function getAllowedParams() { |
114 | return $this->buildCommonApiParams(); |
115 | } |
116 | |
117 | public function getSearchProfileParams() { |
118 | return [ |
119 | 'profile' => [ |
120 | 'profile-type' => SearchEngine::COMPLETION_PROFILE_TYPE, |
121 | 'help-message' => 'apihelp-query+prefixsearch-param-profile', |
122 | ], |
123 | ]; |
124 | } |
125 | |
126 | protected function getExamplesMessages() { |
127 | return [ |
128 | 'action=query&list=prefixsearch&pssearch=meaning' |
129 | => 'apihelp-query+prefixsearch-example-simple', |
130 | ]; |
131 | } |
132 | |
133 | public function getHelpUrls() { |
134 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Prefixsearch'; |
135 | } |
136 | } |