MediaWiki master
ApiQueryPrefixSearch.php
Go to the documentation of this file.
1<?php
2
27 use SearchApi;
28
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
58 private function run( $resultPageSet = null ) {
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}
buildSearchEngine(array $params=null)
Build the search engine to use.
buildCommonApiParams( $isScrollable=true)
The set of api parameters that are shared between api calls that call the SearchEngine.
Definition SearchApi.php:65
array $params
The job parameters.
run()
Run the job.
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition ApiBase.php:541
getResult()
Get the result object.
Definition ApiBase.php:671
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:811
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:532
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
getCacheMode( $params)
Get the cache mode for the data generated by this module.
getHelpUrls()
Return links to more detailed help pages about the module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
__construct(ApiQuery $query, $moduleName, SearchEngineConfig $searchEngineConfig, SearchEngineFactory $searchEngineFactory)
executeGenerator( $resultPageSet)
Execute this module as a generator.
getExamplesMessages()
Returns usage examples for this module.
This is the main query class.
Definition ApiQuery.php:43
Configuration handling class for SearchEngine.
Factory class for SearchEngine.
trait SearchApi
Traits for API components that use a SearchEngine.
Definition SearchApi.php:31