* API: Watchlist feed allows 'hours' parameter of how many hours to go back
[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 <FirstnameLastname@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 * @addtogroup API
33 */
34 class ApiQueryAllpages extends ApiQueryGeneratorBase {
35
36 public function __construct($query, $moduleName) {
37 parent :: __construct($query, $moduleName, 'ap');
38 }
39
40 public function execute() {
41 $this->run();
42 }
43
44 public function executeGenerator($resultPageSet) {
45 if ($resultPageSet->isResolvingRedirects())
46 $this->dieUsage('Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params');
47
48 $this->run($resultPageSet);
49 }
50
51 private function run($resultPageSet = null) {
52
53 $db = $this->getDB();
54
55 $limit = $from = $namespace = $filterredir = $prefix = null;
56 extract($this->extractRequestParams());
57
58 $this->addTables('page');
59 if (!$this->addWhereIf('page_is_redirect = 1', $filterredir === 'redirects'))
60 $this->addWhereIf('page_is_redirect = 0', $filterredir === 'nonredirects');
61 $this->addWhereFld('page_namespace', $namespace);
62 if (isset ($from))
63 $this->addWhere('page_title>=' . $db->addQuotes(ApiQueryBase :: titleToKey($from)));
64 if (isset ($prefix))
65 $this->addWhere("page_title LIKE '{$db->strencode(ApiQueryBase :: titleToKey($prefix))}%'");
66
67 if (is_null($resultPageSet)) {
68 $this->addFields(array (
69 'page_id',
70 'page_namespace',
71 'page_title'
72 ));
73 } else {
74 $this->addFields($resultPageSet->getPageTableFields());
75 }
76
77 $this->addOption('USE INDEX', 'name_title');
78 $this->addOption('LIMIT', $limit +1);
79 $this->addOption('ORDER BY', 'page_namespace, page_title');
80
81 $res = $this->select(__METHOD__);
82
83 $data = array ();
84 $count = 0;
85 while ($row = $db->fetchObject($res)) {
86 if (++ $count > $limit) {
87 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
88 $this->setContinueEnumParameter('from', ApiQueryBase :: keyToTitle($row->page_title));
89 break;
90 }
91
92 if (is_null($resultPageSet)) {
93 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
94 if ($title->userCanRead()) {
95 $data[intval($row->page_id)] = array(
96 'pageid' => $row->page_id,
97 'ns' => $title->getNamespace(),
98 'title' => $title->getPrefixedText());
99 }
100 } else {
101 $resultPageSet->processDbRow($row);
102 }
103 }
104 $db->freeResult($res);
105
106 if (is_null($resultPageSet)) {
107 $result = $this->getResult();
108 $result->setIndexedTagName($data, 'p');
109 $result->addValue('query', $this->getModuleName(), $data);
110 }
111 }
112
113 protected function getAllowedParams() {
114 return array (
115 'from' => null,
116 'prefix' => null,
117 'namespace' => array (
118 ApiBase :: PARAM_DFLT => 0,
119 ApiBase :: PARAM_TYPE => 'namespace'
120 ),
121 'filterredir' => array (
122 ApiBase :: PARAM_DFLT => 'all',
123 ApiBase :: PARAM_TYPE => array (
124 'all',
125 'redirects',
126 'nonredirects'
127 )
128 ),
129 'limit' => array (
130 ApiBase :: PARAM_DFLT => 10,
131 ApiBase :: PARAM_TYPE => 'limit',
132 ApiBase :: PARAM_MIN => 1,
133 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
134 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
135 )
136 );
137 }
138
139 protected function getParamDescription() {
140 return array (
141 'from' => 'The page title to start enumerating from.',
142 'prefix' => 'Search for all page titles that begin with this value.',
143 'namespace' => 'The namespace to enumerate.',
144 'filterredir' => 'Which pages to list.',
145 'limit' => 'How many total pages to return.'
146 );
147 }
148
149 protected function getDescription() {
150 return 'Enumerate all pages sequentially in a given namespace';
151 }
152
153 protected function getExamples() {
154 return array (
155 'Simple Use',
156 ' Show a list of pages starting at the letter "B"',
157 ' api.php?action=query&list=allpages&apfrom=B',
158 'Using as Generator',
159 ' Show info about 4 pages starting at the letter "T"',
160 ' api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
161 ' Show content of first 2 non-redirect pages begining at "Re"',
162 ' api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
163 );
164 }
165
166 public function getVersion() {
167 return __CLASS__ . ': $Id$';
168 }
169 }
170 ?>