* API: Refactored per brion's suggestions
[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 $this->profileDBIn();
56 $res = $db->select('page', array (
57 'page_id',
58 'page_namespace',
59 'page_title'
60 ), $where, __CLASS__ . '::' . __METHOD__, array (
61 'USE INDEX' => 'name_title',
62 'LIMIT' => $aplimit +1,
63 'ORDER BY' => 'page_namespace, page_title'
64 ));
65 $this->profileDBOut();
66
67 $data = array ();
68 $data['_element'] = 'p';
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' => 'apfrom=' . ApiQueryBase :: keyToTitle($row->page_title
75 ));
76 $this->getResult()->addMessage('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
84 $id = intval($row->page_id);
85 $pagedata = array ();
86 $pagedata['id'] = $id;
87 if ($title->getNamespace() !== 0)
88 $pagedata['ns'] = $title->getNamespace();
89 $pagedata['title'] = $title->getPrefixedText();
90 $pagedata['*'] = '';
91
92 $data[$id] = $pagedata;
93 }
94 }
95 $db->freeResult($res);
96 $this->getResult()->addMessage('query', 'allpages', $data);
97 }
98
99 protected function getAllowedParams() {
100
101 global $wgContLang;
102 $validNamespaces = array ();
103 foreach (array_keys($wgContLang->getNamespaces()) as $ns) {
104 if ($ns >= 0)
105 $validNamespaces[] = $ns; // strval($ns);
106 }
107
108 return array (
109 'apfrom' => null,
110 'apnamespace' => array (
111 GN_ENUM_DFLT => 0,
112 GN_ENUM_TYPE => $validNamespaces
113 ),
114 'apfilterredir' => array (
115 GN_ENUM_DFLT => 'all',
116 GN_ENUM_TYPE => array (
117 'all',
118 'redirects',
119 'nonredirects'
120 )
121 ),
122 'aplimit' => array (
123 GN_ENUM_DFLT => 10,
124 GN_ENUM_TYPE => 'limit',
125 GN_ENUM_MIN => 1,
126 GN_ENUM_MAX1 => 500,
127 GN_ENUM_MAX2 => 5000
128 )
129 );
130 }
131
132 protected function getParamDescription() {
133 return array ();
134 }
135
136 protected function getDescription() {
137 return 'Enumerate all pages sequentially in a given namespace';
138 }
139
140 protected function getExamples() {
141 return array (
142 'api.php?action=query&list=allpages',
143 'api.php?action=query&list=allpages&apfrom=B&aplimit=5'
144 );
145 }
146 public function getCanGenerate() {
147 return true;
148 }
149 }
150 ?>