55b5bdb6c6c57a9ac3b1bd77e92695e00d54c024
[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 $this->run($resultPageSet);
44 }
45
46 private function run($resultPageSet = null) {
47 $limit = $from = $namespace = $filterredir = null;
48 extract($this->extractRequestParams());
49
50 $db = $this->getDB();
51
52 $where = array (
53 'page_namespace' => $namespace
54 );
55 if (isset ($from)) {
56 $where[] = 'page_title>=' . $db->addQuotes(ApiQueryBase :: titleToKey($from));
57 }
58 if ($filterredir === 'redirects') {
59 $where['page_is_redirect'] = 1;
60 }
61 elseif ($filterredir === 'nonredirects') {
62 $where['page_is_redirect'] = 0;
63 }
64
65 $this->profileDBIn();
66 $res = $db->select('page', array (
67 'page_id',
68 'page_namespace',
69 'page_title'
70 ), $where, __CLASS__ . '::' . __METHOD__, array (
71 'USE INDEX' => 'name_title',
72 'LIMIT' => $limit +1,
73 'ORDER BY' => 'page_namespace, page_title'
74 ));
75 $this->profileDBOut();
76
77 $data = array ();
78 $count = 0;
79 while ($row = $db->fetchObject($res)) {
80 if (++ $count > $limit) {
81 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
82 $msg = array (
83 'continue' => $this->encodeParamName('from') . '='. ApiQueryBase :: keyToTitle($row->page_title));
84 $this->getResult()->addValue('query-status', 'allpages', $msg);
85 break;
86 }
87
88 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
89 // skip any pages that user has no rights to read
90 if ($title->userCanRead()) {
91 $id = intval($row->page_id);
92
93 if (is_null($resultPageSet)) {
94 $pagedata = array ();
95 $pagedata['id'] = $id;
96 if ($title->getNamespace() !== 0)
97 $pagedata['ns'] = $title->getNamespace();
98 $pagedata['title'] = $title->getPrefixedText();
99
100 $data[$id] = $pagedata;
101 } else {
102 $data[] = $id; // in generator mode, just assemble a list of page IDs.
103 }
104 }
105 }
106 $db->freeResult($res);
107
108 if (is_null($resultPageSet)) {
109 ApiResult :: setIndexedTagName($data, 'p');
110 $this->getResult()->addValue('query', 'allpages', $data);
111 } else {
112 $resultPageSet->executeForPageIDs($data);
113 }
114 }
115
116 protected function getAllowedParams() {
117
118 global $wgContLang;
119 $validNamespaces = array ();
120 foreach (array_keys($wgContLang->getNamespaces()) as $ns) {
121 if ($ns >= 0)
122 $validNamespaces[] = $ns; // strval($ns);
123 }
124
125 return array (
126 'from' => null,
127 'namespace' => array (
128 ApiBase :: PARAM_DFLT => 0,
129 ApiBase :: PARAM_TYPE => $validNamespaces
130 ),
131 'filterredir' => array (
132 ApiBase :: PARAM_DFLT => 'all',
133 ApiBase :: PARAM_TYPE => array (
134 'all',
135 'redirects',
136 'nonredirects'
137 )
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
149 protected function getParamDescription() {
150 return array (
151 'from' => 'The page title to start enumerating from.',
152 'namespace' => 'The namespace to enumerate. Default 0 (Main).',
153 'filterredir' => 'Which pages to list: "all" (default), "redirects", or "nonredirects"',
154 'limit' => 'How many total pages to return'
155 );
156 }
157
158 protected function getDescription() {
159 return 'Enumerate all pages sequentially in a given namespace';
160 }
161
162 protected function getExamples() {
163 return array (
164 'api.php?action=query&list=allpages',
165 'api.php?action=query&list=allpages&apfrom=B&aplimit=5',
166 'api.php?action=query&generator=allpages&gaplimit=4&prop=info (generator)'
167 );
168 }
169
170 public function getVersion() {
171 return __CLASS__ . ': $Id$';
172 }
173 }
174 ?>