* API: Overall query-related 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 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 else
52 if ($apfilterredir === 'nonredirects')
53 $where['page_is_redirect'] = 0;
54
55 $res = $db->select('page', array (
56 'page_id',
57 'page_namespace',
58 'page_title'
59 ), $where, __CLASS__ . '::' . __METHOD__, array (
60 'USE INDEX' => 'name_title',
61 'LIMIT' => $aplimit +1,
62 'ORDER BY' => 'page_namespace, page_title'
63 ));
64
65 $data = array ();
66 $data['_element'] = 'p';
67 $count = 0;
68 while ($row = $db->fetchObject($res)) {
69 if (++ $count > $aplimit) {
70 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
71 $msg = array (
72 'continue' => 'apfrom=' . ApiQueryBase :: KeyToTitle($row->page_title
73 ));
74 $this->GetResult()->AddMessage('query-status', 'allpages', $msg);
75 break;
76 }
77
78 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
79 // skip any pages that user has no rights to read
80 if ($title->userCanRead()) {
81
82 $id = intval($row->page_id);
83 $pagedata = array ();
84 $pagedata['id'] = $id;
85 if ($title->getNamespace() !== 0)
86 $pagedata['ns'] = $title->getNamespace();
87 $pagedata['title'] = $title->getPrefixedText();
88 $pagedata['*'] = '';
89
90 $data[$id] = $pagedata;
91 }
92 }
93 $db->freeResult($res);
94 $this->GetResult()->AddMessage('query', 'allpages', $data);
95 }
96
97 protected function GetAllowedParams() {
98
99 global $wgContLang;
100 $validNamespaces = array ();
101 foreach (array_keys($wgContLang->getNamespaces()) as $ns) {
102 if ($ns >= 0)
103 $validNamespaces[] = $ns; // strval($ns);
104 }
105
106 return array (
107 'apfrom' => null,
108 'apnamespace' => array (
109 GN_ENUM_DFLT => 0,
110 GN_ENUM_TYPE => $validNamespaces
111 ),
112 'apfilterredir' => array (
113 GN_ENUM_DFLT => 'all',
114 GN_ENUM_TYPE => array (
115 'all',
116 'redirects',
117 'nonredirects'
118 )
119 ),
120 'aplimit' => array (
121 GN_ENUM_DFLT => 10,
122 GN_ENUM_TYPE => 'limit',
123 GN_ENUM_MIN => 1,
124 GN_ENUM_MAX1 => 500,
125 GN_ENUM_MAX2 => 5000
126 )
127 );
128 }
129
130 protected function GetParamDescription() {
131 return array ();
132 }
133
134 protected function GetDescription() {
135 return 'Enumerate all pages sequentially in a given namespace';
136 }
137
138 protected function GetExamples() {
139 return array (
140 'api.php?action=query&list=allpages',
141 'api.php?action=query&list=allpages&apfrom=B&aplimit=5'
142 );
143 }
144 public function GetCanGenerate() {
145 return true;
146 }
147 }
148 ?>