5e01222b84fb9f3423411221d500d13f593b89b7
[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 $this->setContinueEnumParameter('from', ApiQueryBase :: keyToTitle($row->page_title));
100 break;
101 }
102
103 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
104 // skip any pages that user has no rights to read
105 if ($title->userCanRead()) {
106
107 if (is_null($resultPageSet)) {
108 $id = intval($row->page_id);
109 $data[$id] = array (
110 'id' => $id,
111 'ns' => $title->getNamespace(),
112 'title' => $title->getPrefixedText());
113 } else {
114 $resultPageSet->processDbRow($row);
115 }
116 }
117 }
118 $db->freeResult($res);
119
120 if (is_null($resultPageSet)) {
121 ApiResult :: setIndexedTagName($data, 'p');
122 $this->getResult()->addValue('query', $this->getModuleName(), $data);
123 }
124 }
125
126 protected function getAllowedParams() {
127
128 return array (
129 'from' => null,
130 'prefix' => null,
131 'namespace' => array (
132 ApiBase :: PARAM_DFLT => 0,
133 ApiBase :: PARAM_TYPE => $this->getQuery()->getValidNamespaces()),
134 'filterredir' => array (
135 ApiBase :: PARAM_DFLT => 'all',
136 ApiBase :: PARAM_TYPE => array (
137 'all',
138 'redirects',
139 'nonredirects'
140 )),
141 'limit' => array (
142 ApiBase :: PARAM_DFLT => 10,
143 ApiBase :: PARAM_TYPE => 'limit',
144 ApiBase :: PARAM_MIN => 1,
145 ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1,
146 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
147 ));
148 }
149
150 protected function getParamDescription() {
151 return array (
152 'from' => 'The page title to start enumerating from.',
153 'prefix' => 'Search for all page titles that begin with this value.',
154 'namespace' => 'The namespace to enumerate.',
155 'filterredir' => 'Which pages to list.',
156 'limit' => 'How many total pages to return.'
157 );
158 }
159
160 protected function getDescription() {
161 return 'Enumerate all pages sequentially in a given namespace';
162 }
163
164 protected function getExamples() {
165 return array (
166 'Simple Use',
167 ' Show a list of pages starting at the letter "B"',
168 ' api.php?action=query&list=allpages&apfrom=B',
169 'Using as Generator',
170 ' Show info about 4 pages starting at the letter "T"',
171 ' api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
172 ' Show content of first 2 non-redirect pages begining at "Re"',
173 ' api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
174 );
175 }
176
177 public function getVersion() {
178 return __CLASS__ . ': $Id$';
179 }
180 }
181 ?>