MediaWiki  master
ApiQueryPrefixSearch.php
Go to the documentation of this file.
1 <?php
2 
27  use SearchApi;
28 
30  private $allowedParams;
31 
38  public function __construct(
39  ApiQuery $query,
40  $moduleName,
41  SearchEngineConfig $searchEngineConfig,
42  SearchEngineFactory $searchEngineFactory
43  ) {
44  parent::__construct( $query, $moduleName, 'ps' );
45  // Services needed in SearchApi trait
46  $this->searchEngineConfig = $searchEngineConfig;
47  $this->searchEngineFactory = $searchEngineFactory;
48  }
49 
50  public function execute() {
51  $this->run();
52  }
53 
54  public function executeGenerator( $resultPageSet ) {
55  $this->run( $resultPageSet );
56  }
57 
61  private function run( $resultPageSet = null ) {
62  $params = $this->extractRequestParams();
63  $search = $params['search'];
64  $limit = $params['limit'];
65  $offset = $params['offset'];
66 
67  $searchEngine = $this->buildSearchEngine( $params );
68  $suggestions = $searchEngine->completionSearchWithVariants( $search );
69  $titles = $searchEngine->extractTitles( $suggestions );
70 
71  if ( $suggestions->hasMoreResults() ) {
72  $this->setContinueEnumParameter( 'offset', $offset + $limit );
73  }
74 
75  if ( $resultPageSet ) {
76  $resultPageSet->setRedirectMergePolicy( static function ( array $current, array $new ) {
77  if ( !isset( $current['index'] ) || $new['index'] < $current['index'] ) {
78  $current['index'] = $new['index'];
79  }
80  return $current;
81  } );
82  $resultPageSet->populateFromTitles( $titles );
83  foreach ( $titles as $index => $title ) {
84  $resultPageSet->setGeneratorData( $title, [ 'index' => $index + $offset + 1 ] );
85  }
86  } else {
87  $result = $this->getResult();
88  $count = 0;
89  foreach ( $titles as $title ) {
90  $vals = [
91  'ns' => $title->getNamespace(),
92  'title' => $title->getPrefixedText(),
93  ];
94  if ( $title->isSpecialPage() ) {
95  $vals['special'] = true;
96  } else {
97  $vals['pageid'] = (int)$title->getArticleID();
98  }
99  $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
100  ++$count;
101  if ( !$fit ) {
102  $this->setContinueEnumParameter( 'offset', $offset + $count );
103  break;
104  }
105  }
106  $result->addIndexedTagName(
107  [ 'query', $this->getModuleName() ], $this->getModulePrefix()
108  );
109  }
110  }
111 
112  public function getCacheMode( $params ) {
113  return 'public';
114  }
115 
116  public function getAllowedParams() {
117  if ( $this->allowedParams !== null ) {
118  return $this->allowedParams;
119  }
120  $this->allowedParams = $this->buildCommonApiParams();
121 
122  return $this->allowedParams;
123  }
124 
125  public function getSearchProfileParams() {
126  return [
127  'profile' => [
128  'profile-type' => SearchEngine::COMPLETION_PROFILE_TYPE,
129  'help-message' => 'apihelp-query+prefixsearch-param-profile',
130  ],
131  ];
132  }
133 
134  protected function getExamplesMessages() {
135  return [
136  'action=query&list=prefixsearch&pssearch=meaning'
137  => 'apihelp-query+prefixsearch-example-simple',
138  ];
139  }
140 
141  public function getHelpUrls() {
142  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Prefixsearch';
143  }
144 }
buildSearchEngine(array $params=null)
Build the search engine to use.
Definition: SearchApi.php:177
buildCommonApiParams( $isScrollable=true)
The set of api parameters that are shared between api calls that call the SearchEngine.
Definition: SearchApi.php:66
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition: ApiBase.php:506
getResult()
Get the result object.
Definition: ApiBase.php:629
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:765
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:498
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:41
Configuration handling class for SearchEngine.
Factory class for SearchEngine.
const COMPLETION_PROFILE_TYPE
Profile type for completionSearch.
trait SearchApi
Traits for API components that use a SearchEngine.
Definition: SearchApi.php:30