4 * Created on July 30, 2007
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
32 * Query module to perform full text search within wiki titles and content
36 class ApiQuerySearch
extends ApiQueryGeneratorBase
{
38 public function __construct($query, $moduleName) {
39 parent
:: __construct($query, $moduleName, 'sr');
42 public function execute() {
46 public function executeGenerator($resultPageSet) {
47 $this->run($resultPageSet);
50 private function run($resultPageSet = null) {
52 $params = $this->extractRequestParams();
54 $limit = $params['limit'];
55 $query = $params['search'];
56 if (is_null($query) ||
empty($query))
57 $this->dieUsage("empty search string is not allowed", 'param-search');
59 $search = SearchEngine
::create();
60 $search->setLimitOffset( $limit+
1, $params['offset'] );
61 $search->setNamespaces( $params['namespace'] );
62 $search->showRedirects
= $params['redirects'];
64 if ($params['what'] == 'text')
65 $matches = $search->searchText( $query );
67 $matches = $search->searchTitle( $query );
71 while( $result = $matches->next() ) {
72 if (++
$count > $limit) {
73 // We've reached the one extra which shows that there are additional items to be had. Stop here...
74 $this->setContinueEnumParameter('offset', $params['offset'] +
$params['limit']);
78 $title = $result->getTitle();
79 if (is_null($resultPageSet)) {
81 'ns' => intval($title->getNamespace()),
82 'title' => $title->getPrefixedText());
88 if (is_null($resultPageSet)) {
89 $result = $this->getResult();
90 $result->setIndexedTagName($data, 'p');
91 $result->addValue('query', $this->getModuleName(), $data);
93 $resultPageSet->populateFromTitles($data);
97 protected function getAllowedParams() {
100 'namespace' => array (
101 ApiBase
:: PARAM_DFLT
=> 0,
102 ApiBase
:: PARAM_TYPE
=> 'namespace',
103 ApiBase
:: PARAM_ISMULTI
=> true,
106 ApiBase
:: PARAM_DFLT
=> 'title',
107 ApiBase
:: PARAM_TYPE
=> array (
112 'redirects' => false,
115 ApiBase
:: PARAM_DFLT
=> 10,
116 ApiBase
:: PARAM_TYPE
=> 'limit',
117 ApiBase
:: PARAM_MIN
=> 1,
118 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
119 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
124 protected function getParamDescription() {
126 'search' => 'Search for all page titles (or content) that has this value.',
127 'namespace' => 'The namespace(s) to enumerate.',
128 'what' => 'Search inside the text or titles.',
129 'redirects' => 'Include redirect pages in the search.',
130 'offset' => 'Use this value to continue paging (return by query)',
131 'limit' => 'How many total pages to return.'
135 protected function getDescription() {
136 return 'Perform a full text search';
139 protected function getExamples() {
141 'api.php?action=query&list=search&srsearch=meaning',
142 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
143 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
147 public function getVersion() {
148 return __CLASS__
. ': $Id$';