e02b459cc915d4d46d6c81e14cbeb1e7cfa385ab
[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 ($filterredir === 'redirects') {
64 $where['page_is_redirect'] = 1;
65 }
66 elseif ($filterredir === 'nonredirects') {
67 $where['page_is_redirect'] = 0;
68 }
69
70 if (is_null($resultPageSet)) {
71 $fields = array (
72 'page_id',
73 'page_namespace',
74 'page_title'
75 );
76 } else {
77 $fields = $resultPageSet->getPageTableFields();
78 }
79
80 $options = array (
81 'USE INDEX' => 'name_title',
82 'LIMIT' => $limit +1,
83 'ORDER BY' => 'page_namespace, page_title'
84 );
85
86 $this->profileDBIn();
87 $res = $db->select('page', $fields, $where, __METHOD__, $options);
88 $this->profileDBOut();
89
90 $data = array ();
91 $count = 0;
92 while ($row = $db->fetchObject($res)) {
93 if (++ $count > $limit) {
94 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
95 $msg = array (
96 'continue' => $this->encodeParamName('from'
97 ) . '=' . ApiQueryBase :: keyToTitle($row->page_title));
98 $this->getResult()->addValue('query-status', 'allpages', $msg);
99 break;
100 }
101
102 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
103 // skip any pages that user has no rights to read
104 if ($title->userCanRead()) {
105
106 if (is_null($resultPageSet)) {
107 $id = intval($row->page_id);
108 $data[$id] = array (
109 'id' => $id,
110 'ns' => $title->getNamespace(),
111 'title' => $title->getPrefixedText());
112 } else {
113 $resultPageSet->processDbRow($row);
114 }
115 }
116 }
117 $db->freeResult($res);
118
119 if (is_null($resultPageSet)) {
120 ApiResult :: setIndexedTagName($data, 'p');
121 $this->getResult()->addValue('query', 'allpages', $data);
122 }
123 }
124
125 protected function getAllowedParams() {
126
127 return array (
128 'from' => null,
129 'namespace' => array (
130 ApiBase :: PARAM_DFLT => 0,
131 ApiBase :: PARAM_TYPE => $this->getQuery()->getValidNamespaces()),
132 'filterredir' => array (
133 ApiBase :: PARAM_DFLT => 'all',
134 ApiBase :: PARAM_TYPE => array (
135 'all',
136 'redirects',
137 'nonredirects'
138 )),
139 'limit' => array (
140 ApiBase :: PARAM_DFLT => 10,
141 ApiBase :: PARAM_TYPE => 'limit',
142 ApiBase :: PARAM_MIN => 1,
143 ApiBase :: PARAM_MAX1 => 500,
144 ApiBase :: PARAM_MAX2 => 5000
145 ));
146 }
147
148 protected function getParamDescription() {
149 return array (
150 'from' => 'The page title to start enumerating from.',
151 'namespace' => 'The namespace to enumerate. Default 0 (Main).',
152 'filterredir' => 'Which pages to list: "all" (default), "redirects", or "nonredirects"',
153 'limit' => 'How many total pages to return'
154 );
155 }
156
157 protected function getDescription() {
158 return 'Enumerate all pages sequentially in a given namespace';
159 }
160
161 protected function getExamples() {
162 return array (
163 'Simple Use',
164 ' Show a list of pages starting at the letter "B"',
165 ' api.php?action=query&list=allpages&apfrom=B',
166 'Using as Generator',
167 ' Show info about 4 pages starting at the letter "T"',
168 ' api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
169 ' Show content of first 2 non-redirect pages begining at "Re"',
170 ' api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
171 );
172 }
173
174 public function getVersion() {
175 return __CLASS__ . ': $Id$';
176 }
177 }
178 ?>