MediaWiki master
ApiQueryPrefixSearch.php
Go to the documentation of this file.
1<?php
2
9namespace MediaWiki\Api;
10
14
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
38 public function executeGenerator( $resultPageSet ) {
39 $this->run( $resultPageSet );
40 }
41
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
97 public function getCacheMode( $params ) {
98 return 'public';
99 }
100
102 public function getAllowedParams() {
103 return $this->buildCommonApiParams();
104 }
105
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
117 protected function getExamplesMessages() {
118 return [
119 'action=query&list=prefixsearch&pssearch=meaning'
120 => 'apihelp-query+prefixsearch-example-simple',
121 ];
122 }
123
125 public function getHelpUrls() {
126 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Prefixsearch';
127 }
128}
129
131class_alias( ApiQueryPrefixSearch::class, 'ApiQueryPrefixSearch' );
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition ApiBase.php:552
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:543
getResult()
Get the result object.
Definition ApiBase.php:682
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
executeGenerator( $resultPageSet)
Execute this module as a generator.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
__construct(ApiQuery $query, string $moduleName, SearchEngineConfig $searchEngineConfig, SearchEngineFactory $searchEngineFactory)
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getCacheMode( $params)
Get the cache mode for the data generated by this module.Override this in the module subclass....
This is the main query class.
Definition ApiQuery.php:36
Configuration handling class for SearchEngine.
Factory class for SearchEngine.
Contain a class for special pages.
buildCommonApiParams( $isScrollable=true)
The set of api parameters that are shared between api calls that call the SearchEngine.
Definition SearchApi.php:57
buildSearchEngine(?array $params=null)
Build the search engine to use.