]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQuerySearch.php
MediaWiki 1.11.0
[autoinstalls/mediawiki.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
69                 $data = array ();
70                 $count = 0;
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']);
75                                 break;
76                         }
77
78                         $title = $result->getTitle();
79                         if (is_null($resultPageSet)) {
80                                 $data[] = array(
81                                         'ns' => intval($title->getNamespace()),
82                                         'title' => $title->getPrefixedText());
83                         } else {
84                                 $data[] = $title;
85                         }
86                 }
87
88                 if (is_null($resultPageSet)) {
89                         $result = $this->getResult();
90                         $result->setIndexedTagName($data, 'p');
91                         $result->addValue('query', $this->getModuleName(), $data);
92                 } else {
93                         $resultPageSet->populateFromTitles($data);
94                 }
95         }
96
97         protected function getAllowedParams() {
98                 return array (
99                         'search' => null,
100                         'namespace' => array (
101                                 ApiBase :: PARAM_DFLT => 0,
102                                 ApiBase :: PARAM_TYPE => 'namespace',
103                                 ApiBase :: PARAM_ISMULTI => true, 
104                         ),
105                         'what' => array (
106                                 ApiBase :: PARAM_DFLT => 'title',
107                                 ApiBase :: PARAM_TYPE => array (
108                                         'title',
109                                         'text',
110                                 )
111                         ),
112                         'redirects' => false,
113                         'offset' => 0,
114                         'limit' => array (
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
120                         )
121                 );
122         }
123
124         protected function getParamDescription() {
125                 return array (
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.'
132                 );
133         }
134
135         protected function getDescription() {
136                 return 'Perform a full text search';
137         }
138
139         protected function getExamples() {
140                 return array (
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',
144                 );
145         }
146
147         public function getVersion() {
148                 return __CLASS__ . ': $Id: ApiQuerySearch.php 24453 2007-07-30 08:09:15Z yurik $';
149         }
150 }
151