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 );
68 if (is_null($matches))
69 $this->dieUsage("{$params['what']} search is disabled",
70 "search-{$params['what']}-disabled");
74 while( $result = $matches->next() ) {
75 if (++
$count > $limit) {
76 // We've reached the one extra which shows that there are additional items to be had. Stop here...
77 $this->setContinueEnumParameter('offset', $params['offset'] +
$params['limit']);
81 // Silently skip broken titles
82 if ($result->isBrokenTitle()) continue;
84 $title = $result->getTitle();
85 if (is_null($resultPageSet)) {
87 'ns' => intval($title->getNamespace()),
88 'title' => $title->getPrefixedText());
94 if (is_null($resultPageSet)) {
95 $result = $this->getResult();
96 $result->setIndexedTagName($data, 'p');
97 $result->addValue('query', $this->getModuleName(), $data);
99 $resultPageSet->populateFromTitles($data);
103 public function getAllowedParams() {
106 'namespace' => array (
107 ApiBase
:: PARAM_DFLT
=> 0,
108 ApiBase
:: PARAM_TYPE
=> 'namespace',
109 ApiBase
:: PARAM_ISMULTI
=> true,
112 ApiBase
:: PARAM_DFLT
=> 'title',
113 ApiBase
:: PARAM_TYPE
=> array (
118 'redirects' => false,
121 ApiBase
:: PARAM_DFLT
=> 10,
122 ApiBase
:: PARAM_TYPE
=> 'limit',
123 ApiBase
:: PARAM_MIN
=> 1,
124 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
125 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
130 public function getParamDescription() {
132 'search' => 'Search for all page titles (or content) that has this value.',
133 'namespace' => 'The namespace(s) to enumerate.',
134 'what' => 'Search inside the text or titles.',
135 'redirects' => 'Include redirect pages in the search.',
136 'offset' => 'Use this value to continue paging (return by query)',
137 'limit' => 'How many total pages to return.'
141 public function getDescription() {
142 return 'Perform a full text search';
145 protected function getExamples() {
147 'api.php?action=query&list=search&srsearch=meaning',
148 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
149 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
153 public function getVersion() {
154 return __CLASS__
. ': $Id$';