MediaWiki  1.34.0
ApiQueryPrefixSearch.php
Go to the documentation of this file.
1 <?php
2 
27  use SearchApi;
28 
30  private $allowedParams;
31 
32  public function __construct( $query, $moduleName ) {
33  parent::__construct( $query, $moduleName, 'ps' );
34  }
35 
36  public function execute() {
37  $this->run();
38  }
39 
40  public function executeGenerator( $resultPageSet ) {
41  $this->run( $resultPageSet );
42  }
43 
47  private function run( $resultPageSet = null ) {
48  $params = $this->extractRequestParams();
49  $search = $params['search'];
50  $limit = $params['limit'];
51  $offset = $params['offset'];
52 
53  $searchEngine = $this->buildSearchEngine( $params );
54  $suggestions = $searchEngine->completionSearchWithVariants( $search );
55  $titles = $searchEngine->extractTitles( $suggestions );
56 
57  if ( $suggestions->hasMoreResults() ) {
58  $this->setContinueEnumParameter( 'offset', $offset + $limit );
59  }
60 
61  if ( $resultPageSet ) {
62  $resultPageSet->setRedirectMergePolicy( function ( array $current, array $new ) {
63  if ( !isset( $current['index'] ) || $new['index'] < $current['index'] ) {
64  $current['index'] = $new['index'];
65  }
66  return $current;
67  } );
68  $resultPageSet->populateFromTitles( $titles );
69  foreach ( $titles as $index => $title ) {
70  $resultPageSet->setGeneratorData( $title, [ 'index' => $index + $offset + 1 ] );
71  }
72  } else {
73  $result = $this->getResult();
74  $count = 0;
75  foreach ( $titles as $title ) {
76  $vals = [
77  'ns' => (int)$title->getNamespace(),
78  'title' => $title->getPrefixedText(),
79  ];
80  if ( $title->isSpecialPage() ) {
81  $vals['special'] = true;
82  } else {
83  $vals['pageid'] = (int)$title->getArticleID();
84  }
85  $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
86  ++$count;
87  if ( !$fit ) {
88  $this->setContinueEnumParameter( 'offset', $offset + $count );
89  break;
90  }
91  }
92  $result->addIndexedTagName(
93  [ 'query', $this->getModuleName() ], $this->getModulePrefix()
94  );
95  }
96  }
97 
98  public function getCacheMode( $params ) {
99  return 'public';
100  }
101 
102  public function getAllowedParams() {
103  if ( $this->allowedParams !== null ) {
104  return $this->allowedParams;
105  }
106  $this->allowedParams = $this->buildCommonApiParams();
107 
108  return $this->allowedParams;
109  }
110 
111  public function getSearchProfileParams() {
112  return [
113  'profile' => [
114  'profile-type' => SearchEngine::COMPLETION_PROFILE_TYPE,
115  'help-message' => 'apihelp-query+prefixsearch-param-profile',
116  ],
117  ];
118  }
119 
120  protected function getExamplesMessages() {
121  return [
122  'action=query&list=prefixsearch&pssearch=meaning'
123  => 'apihelp-query+prefixsearch-example-simple',
124  ];
125  }
126 
127  public function getHelpUrls() {
128  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Prefixsearch';
129  }
130 }
ApiQueryPrefixSearch\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryPrefixSearch.php:98
ApiQueryPrefixSearch\$allowedParams
array $allowedParams
list of api allowed params
Definition: ApiQueryPrefixSearch.php:30
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:640
ApiQueryPrefixSearch\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryPrefixSearch.php:102
SearchEngine\COMPLETION_PROFILE_TYPE
const COMPLETION_PROFILE_TYPE
Profile type for completionSearch.
Definition: SearchEngine.php:63
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Definition: ApiQueryGeneratorBase.php:84
ApiQueryPrefixSearch\getSearchProfileParams
getSearchProfileParams()
Definition: ApiQueryPrefixSearch.php:111
buildSearchEngine
buildSearchEngine(array $params=null)
Build the search engine to use.
Definition: SearchApi.php:153
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
$title
$title
Definition: testCompression.php:34
ApiQueryPrefixSearch\executeGenerator
executeGenerator( $resultPageSet)
Execute this module as a generator.
Definition: ApiQueryPrefixSearch.php:40
ApiBase\getModulePrefix
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition: ApiBase.php:528
ApiQueryPrefixSearch\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryPrefixSearch.php:36
ApiQueryPrefixSearch
Definition: ApiQueryPrefixSearch.php:26
ApiQueryPrefixSearch\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryPrefixSearch.php:127
ApiQueryPrefixSearch\__construct
__construct( $query, $moduleName)
Definition: ApiQueryPrefixSearch.php:32
ApiQueryPrefixSearch\run
run( $resultPageSet=null)
Definition: ApiQueryPrefixSearch.php:47
ApiQueryGeneratorBase
Definition: ApiQueryGeneratorBase.php:26
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
SearchApi
trait SearchApi
Traits for API components that use a SearchEngine.
Definition: SearchApi.php:29
ApiQueryPrefixSearch\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryPrefixSearch.php:120
buildCommonApiParams
buildCommonApiParams( $isScrollable=true)
The set of api parameters that are shared between api calls that call the SearchEngine.
Definition: SearchApi.php:47