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