API:
[lhc/web/wiklou.git] / includes / api / ApiQueryAllpages.php
1 <?php
2
3 /*
4 * Created on Sep 25, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
29 }
30
31 /**
32 * Query module to enumerate all available pages.
33 *
34 * @addtogroup API
35 */
36 class ApiQueryAllpages extends ApiQueryGeneratorBase {
37
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'ap');
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function executeGenerator($resultPageSet) {
47 if ($resultPageSet->isResolvingRedirects())
48 $this->dieUsage('Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params');
49
50 $this->run($resultPageSet);
51 }
52
53 private function run($resultPageSet = null) {
54
55 $db = $this->getDB();
56
57 $params = $this->extractRequestParams();
58
59 $this->addTables('page');
60 if (!$this->addWhereIf('page_is_redirect = 1', $params['filterredir'] === 'redirects'))
61 $this->addWhereIf('page_is_redirect = 0', $params['filterredir'] === 'nonredirects');
62 $this->addWhereFld('page_namespace', $params['namespace']);
63 if (!is_null($params['from']))
64 $this->addWhere('page_title>=' . $db->addQuotes(ApiQueryBase :: titleToKey($params['from'])));
65 if (isset ($params['prefix']))
66 $this->addWhere("page_title LIKE '" . $db->strencode(ApiQueryBase :: titleToKey($params['prefix'])) . "%'");
67
68 if (is_null($resultPageSet)) {
69 $this->addFields(array (
70 'page_id',
71 'page_namespace',
72 'page_title'
73 ));
74 } else {
75 $this->addFields($resultPageSet->getPageTableFields());
76 }
77
78 $this->addOption('USE INDEX', 'name_title');
79 $limit = $params['limit'];
80 $this->addOption('LIMIT', $limit+1);
81 $this->addOption('ORDER BY', 'page_namespace, page_title');
82
83 $res = $this->select(__METHOD__);
84
85 $data = array ();
86 $count = 0;
87 while ($row = $db->fetchObject($res)) {
88 if (++ $count > $limit) {
89 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
90 // TODO: Security issue - if the user has no right to view next title, it will still be shown
91 $this->setContinueEnumParameter('from', ApiQueryBase :: keyToTitle($row->page_title));
92 break;
93 }
94
95 if (is_null($resultPageSet)) {
96 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
97 if ($title->userCanRead()) {
98 $data[] = array(
99 'pageid' => intval($row->page_id),
100 'ns' => intval($title->getNamespace()),
101 'title' => $title->getPrefixedText());
102 }
103 } else {
104 $resultPageSet->processDbRow($row);
105 }
106 }
107 $db->freeResult($res);
108
109 if (is_null($resultPageSet)) {
110 $result = $this->getResult();
111 $result->setIndexedTagName($data, 'p');
112 $result->addValue('query', $this->getModuleName(), $data);
113 }
114 }
115
116 protected function getAllowedParams() {
117 return array (
118 'from' => null,
119 'prefix' => null,
120 'namespace' => array (
121 ApiBase :: PARAM_DFLT => 0,
122 ApiBase :: PARAM_TYPE => 'namespace'
123 ),
124 'filterredir' => array (
125 ApiBase :: PARAM_DFLT => 'all',
126 ApiBase :: PARAM_TYPE => array (
127 'all',
128 'redirects',
129 'nonredirects'
130 )
131 ),
132 'limit' => array (
133 ApiBase :: PARAM_DFLT => 10,
134 ApiBase :: PARAM_TYPE => 'limit',
135 ApiBase :: PARAM_MIN => 1,
136 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
137 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
138 )
139 );
140 }
141
142 protected function getParamDescription() {
143 return array (
144 'from' => 'The page title to start enumerating from.',
145 'prefix' => 'Search for all page titles that begin with this value.',
146 'namespace' => 'The namespace to enumerate.',
147 'filterredir' => 'Which pages to list.',
148 'limit' => 'How many total pages to return.'
149 );
150 }
151
152 protected function getDescription() {
153 return 'Enumerate all pages sequentially in a given namespace';
154 }
155
156 protected function getExamples() {
157 return array (
158 'Simple Use',
159 ' Show a list of pages starting at the letter "B"',
160 ' api.php?action=query&list=allpages&apfrom=B',
161 'Using as Generator',
162 ' Show info about 4 pages starting at the letter "T"',
163 ' api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
164 ' Show content of first 2 non-redirect pages begining at "Re"',
165 ' api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
166 );
167 }
168
169 public function getVersion() {
170 return __CLASS__ . ': $Id$';
171 }
172 }
173