* API: Restructured to allow internal usage. Error handling cleanup.
[lhc/web/wiklou.git] / includes / api / ApiQueryAllpages.php
1 <?php
2
3
4 /*
5 * Created on Sep 25, 2006
6 *
7 * API for MediaWiki 1.8+
8 *
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 */
26
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiQueryBase.php');
30 }
31
32 class ApiQueryAllpages extends ApiQueryGeneratorBase {
33
34 public function __construct($query, $moduleName) {
35 parent :: __construct($query, $moduleName, 'ap');
36 }
37
38 public function execute() {
39 $this->run();
40 }
41
42 public function executeGenerator($resultPageSet) {
43 if ($resultPageSet->isResolvingRedirects())
44 $this->dieUsage('Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params');
45
46 $this->run($resultPageSet);
47 }
48
49 private function run($resultPageSet = null) {
50 $limit = $from = $namespace = $filterredir = null;
51 extract($this->extractRequestParams());
52
53 $db = $this->getDB();
54
55 $where = array (
56 'page_namespace' => $namespace
57 );
58
59 if (isset ($from)) {
60 $where[] = 'page_title>=' . $db->addQuotes(ApiQueryBase :: titleToKey($from));
61 }
62
63 if (isset ($prefix)) {
64 $where[] = "page_title LIKE '{$db->strencode(ApiQueryBase :: titleToKey($prefix))}%'";
65 }
66
67 if ($filterredir === 'redirects') {
68 $where['page_is_redirect'] = 1;
69 }
70 elseif ($filterredir === 'nonredirects') {
71 $where['page_is_redirect'] = 0;
72 }
73
74 if (is_null($resultPageSet)) {
75 $fields = array (
76 'page_id',
77 'page_namespace',
78 'page_title'
79 );
80 } else {
81 $fields = $resultPageSet->getPageTableFields();
82 }
83
84 $options = array (
85 'USE INDEX' => 'name_title',
86 'LIMIT' => $limit +1,
87 'ORDER BY' => 'page_namespace, page_title'
88 );
89
90 $this->profileDBIn();
91 $res = $db->select('page', $fields, $where, __METHOD__, $options);
92 $this->profileDBOut();
93
94 $data = array ();
95 $count = 0;
96 while ($row = $db->fetchObject($res)) {
97 if (++ $count > $limit) {
98 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
99 $msg = array (
100 'continue' => $this->encodeParamName('from'
101 ) . '=' . ApiQueryBase :: keyToTitle($row->page_title));
102 $this->getResult()->addValue('query-status', 'allpages', $msg);
103 break;
104 }
105
106 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
107 // skip any pages that user has no rights to read
108 if ($title->userCanRead()) {
109
110 if (is_null($resultPageSet)) {
111 $id = intval($row->page_id);
112 $data[$id] = array (
113 'id' => $id,
114 'ns' => $title->getNamespace(),
115 'title' => $title->getPrefixedText());
116 } else {
117 $resultPageSet->processDbRow($row);
118 }
119 }
120 }
121 $db->freeResult($res);
122
123 if (is_null($resultPageSet)) {
124 ApiResult :: setIndexedTagName($data, 'p');
125 $this->getResult()->addValue('query', 'allpages', $data);
126 }
127 }
128
129 protected function getAllowedParams() {
130
131 return array (
132 'from' => null,
133 'prefix' => null,
134 'namespace' => array (
135 ApiBase :: PARAM_DFLT => 0,
136 ApiBase :: PARAM_TYPE => $this->getQuery()->getValidNamespaces()),
137 'filterredir' => array (
138 ApiBase :: PARAM_DFLT => 'all',
139 ApiBase :: PARAM_TYPE => array (
140 'all',
141 'redirects',
142 'nonredirects'
143 )),
144 'limit' => array (
145 ApiBase :: PARAM_DFLT => 10,
146 ApiBase :: PARAM_TYPE => 'limit',
147 ApiBase :: PARAM_MIN => 1,
148 ApiBase :: PARAM_MAX1 => 500,
149 ApiBase :: PARAM_MAX2 => 5000
150 ));
151 }
152
153 protected function getParamDescription() {
154 return array (
155 'from' => 'The page title to start enumerating from.',
156 'prefix' => 'Search for all page titles that begin with this value.',
157 'namespace' => 'The namespace to enumerate. Default 0 (Main).',
158 'filterredir' => 'Which pages to list: "all" (default), "redirects", or "nonredirects"',
159 'limit' => 'How many total pages to return'
160 );
161 }
162
163 protected function getDescription() {
164 return 'Enumerate all pages sequentially in a given namespace';
165 }
166
167 protected function getExamples() {
168 return array (
169 'Simple Use',
170 ' Show a list of pages starting at the letter "B"',
171 ' api.php?action=query&list=allpages&apfrom=B',
172 'Using as Generator',
173 ' Show info about 4 pages starting at the letter "T"',
174 ' api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
175 ' Show content of first 2 non-redirect pages begining at "Re"',
176 ' api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
177 );
178 }
179
180 public function getVersion() {
181 return __CLASS__ . ': $Id$';
182 }
183 }
184 ?>