4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
26 class ApiQueryPrefixSearch
extends ApiQueryGeneratorBase
{
29 /** @var array list of api allowed params */
30 private $allowedParams;
32 public function __construct( $query, $moduleName ) {
33 parent
::__construct( $query, $moduleName, 'ps' );
36 public function execute() {
40 public function executeGenerator( $resultPageSet ) {
41 $this->run( $resultPageSet );
45 * @param ApiPageSet $resultPageSet
47 private function run( $resultPageSet = null ) {
48 $params = $this->extractRequestParams();
49 $search = $params['search'];
50 $limit = $params['limit'];
51 $offset = $params['offset'];
53 $searchEngine = $this->buildSearchEngine( $params );
54 $suggestions = $searchEngine->completionSearchWithVariants( $search );
55 $titles = $searchEngine->extractTitles( $suggestions );
57 if ( $suggestions->hasMoreResults() ) {
58 $this->setContinueEnumParameter( 'offset', $offset +
$limit );
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'];
68 $resultPageSet->populateFromTitles( $titles );
69 foreach ( $titles as $index => $title ) {
70 $resultPageSet->setGeneratorData( $title, [ 'index' => $index +
$offset +
1 ] );
73 $result = $this->getResult();
75 foreach ( $titles as $title ) {
77 'ns' => (int)$title->getNamespace(),
78 'title' => $title->getPrefixedText(),
80 if ( $title->isSpecialPage() ) {
81 $vals['special'] = true;
83 $vals['pageid'] = (int)$title->getArticleID();
85 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
88 $this->setContinueEnumParameter( 'offset', $offset +
$count );
92 $result->addIndexedTagName(
93 [ 'query', $this->getModuleName() ], $this->getModulePrefix()
98 public function getCacheMode( $params ) {
102 public function getAllowedParams() {
103 if ( $this->allowedParams
!== null ) {
104 return $this->allowedParams
;
106 $this->allowedParams
= $this->buildCommonApiParams();
108 return $this->allowedParams
;
111 public function getSearchProfileParams() {
114 'profile-type' => SearchEngine
::COMPLETION_PROFILE_TYPE
,
115 'help-message' => 'apihelp-query+prefixsearch-param-profile',
120 protected function getExamplesMessages() {
122 'action=query&list=prefixsearch&pssearch=meaning'
123 => 'apihelp-query+prefixsearch-example-simple',
127 public function getHelpUrls() {
128 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Prefixsearch';