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,
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 }
ApiQuery
This is the main query class.
Definition: ApiQuery.php:37
ApiQueryPrefixSearch\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryPrefixSearch.php:112
ApiQueryPrefixSearch\$allowedParams
array $allowedParams
list of api allowed params
Definition: ApiQueryPrefixSearch.php:30
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:628
$searchEngineFactory
SearchEngineFactory null $searchEngineFactory
Definition: SearchApi.php:33
SearchEngineFactory
Factory class for SearchEngine.
Definition: SearchEngineFactory.php:12
ApiQueryPrefixSearch\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryPrefixSearch.php:116
SearchEngine\COMPLETION_PROFILE_TYPE
const COMPLETION_PROFILE_TYPE
Profile type for completionSearch.
Definition: SearchEngine.php:72
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Definition: ApiQueryGeneratorBase.php:83
ApiQueryPrefixSearch\getSearchProfileParams
getSearchProfileParams()
Definition: ApiQueryPrefixSearch.php:125
buildSearchEngine
buildSearchEngine(array $params=null)
Build the search engine to use.
Definition: SearchApi.php:174
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:764
$title
$title
Definition: testCompression.php:38
ApiQueryPrefixSearch\executeGenerator
executeGenerator( $resultPageSet)
Execute this module as a generator.
Definition: ApiQueryPrefixSearch.php:54
ApiBase\getModulePrefix
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition: ApiBase.php:505
ApiQueryPrefixSearch\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryPrefixSearch.php:50
ApiQueryPrefixSearch
Definition: ApiQueryPrefixSearch.php:26
ApiQueryPrefixSearch\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryPrefixSearch.php:141
ApiQueryPrefixSearch\run
run( $resultPageSet=null)
Definition: ApiQueryPrefixSearch.php:61
ApiQueryGeneratorBase
Definition: ApiQueryGeneratorBase.php:28
ApiQueryPrefixSearch\__construct
__construct(ApiQuery $query, $moduleName, SearchEngineConfig $searchEngineConfig, SearchEngineFactory $searchEngineFactory)
Definition: ApiQueryPrefixSearch.php:38
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:497
SearchEngineConfig
Configuration handling class for SearchEngine.
Definition: SearchEngineConfig.php:12
SearchApi
trait SearchApi
Traits for API components that use a SearchEngine.
Definition: SearchApi.php:27
ApiQueryPrefixSearch\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryPrefixSearch.php:134
buildCommonApiParams
buildCommonApiParams( $isScrollable=true)
The set of api parameters that are shared between api calls that call the SearchEngine.
Definition: SearchApi.php:63