* remove end of line whitespace
[lhc/web/wiklou.git] / includes / api / ApiQuerySearch.php
1 <?php
2
3 /*
4 * Created on July 30, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
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.
14 *
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.
19 *
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
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
29 }
30
31 /**
32 * Query module to perform full text search within wiki titles and content
33 *
34 * @addtogroup API
35 */
36 class ApiQuerySearch extends ApiQueryGeneratorBase {
37
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'sr');
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function executeGenerator($resultPageSet) {
47 $this->run($resultPageSet);
48 }
49
50 private function run($resultPageSet = null) {
51
52 $params = $this->extractRequestParams();
53
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');
58
59 $search = SearchEngine::create();
60 $search->setLimitOffset( $limit+1, $params['offset'] );
61 $search->setNamespaces( $params['namespace'] );
62 $search->showRedirects = $params['redirects'];
63
64 if ($params['what'] == 'text')
65 $matches = $search->searchText( $query );
66 else
67 $matches = $search->searchTitle( $query );
68 if (is_null($matches))
69 $this->dieUsage("{$params['what']} search is disabled",
70 "search-{$params['what']}-disabled");
71
72 $data = array ();
73 $count = 0;
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']);
78 break;
79 }
80
81 $title = $result->getTitle();
82 if (is_null($resultPageSet)) {
83 $data[] = array(
84 'ns' => intval($title->getNamespace()),
85 'title' => $title->getPrefixedText());
86 } else {
87 $data[] = $title;
88 }
89 }
90
91 if (is_null($resultPageSet)) {
92 $result = $this->getResult();
93 $result->setIndexedTagName($data, 'p');
94 $result->addValue('query', $this->getModuleName(), $data);
95 } else {
96 $resultPageSet->populateFromTitles($data);
97 }
98 }
99
100 public function getAllowedParams() {
101 return array (
102 'search' => null,
103 'namespace' => array (
104 ApiBase :: PARAM_DFLT => 0,
105 ApiBase :: PARAM_TYPE => 'namespace',
106 ApiBase :: PARAM_ISMULTI => true,
107 ),
108 'what' => array (
109 ApiBase :: PARAM_DFLT => 'title',
110 ApiBase :: PARAM_TYPE => array (
111 'title',
112 'text',
113 )
114 ),
115 'redirects' => false,
116 'offset' => 0,
117 'limit' => array (
118 ApiBase :: PARAM_DFLT => 10,
119 ApiBase :: PARAM_TYPE => 'limit',
120 ApiBase :: PARAM_MIN => 1,
121 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
122 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
123 )
124 );
125 }
126
127 public function getParamDescription() {
128 return array (
129 'search' => 'Search for all page titles (or content) that has this value.',
130 'namespace' => 'The namespace(s) to enumerate.',
131 'what' => 'Search inside the text or titles.',
132 'redirects' => 'Include redirect pages in the search.',
133 'offset' => 'Use this value to continue paging (return by query)',
134 'limit' => 'How many total pages to return.'
135 );
136 }
137
138 public function getDescription() {
139 return 'Perform a full text search';
140 }
141
142 protected function getExamples() {
143 return array (
144 'api.php?action=query&list=search&srsearch=meaning',
145 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
146 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
147 );
148 }
149
150 public function getVersion() {
151 return __CLASS__ . ': $Id$';
152 }
153 }