API: documentation and cleanup.
[lhc/web/wiklou.git] / includes / api / ApiQueryRecentChanges.php
1 <?php
2
3 /*
4 * Created on Oct 19, 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 * A query action to enumerate the recent changes that were done to the wiki.
33 * Various filters are supported.
34 *
35 * @addtogroup API
36 */
37 class ApiQueryRecentChanges extends ApiQueryBase {
38
39 public function __construct($query, $moduleName) {
40 parent :: __construct($query, $moduleName, 'rc');
41 }
42
43 public function execute() {
44 $limit = $prop = $namespace = $show = $dir = $start = $end = null;
45 extract($this->extractRequestParams());
46
47 $this->addTables('recentchanges');
48 $this->addWhereRange('rc_timestamp', $dir, $start, $end);
49 $this->addWhereFld('rc_namespace', $namespace);
50
51 if (!is_null($show)) {
52 $show = array_flip($show);
53 if ((isset ($show['minor']) && isset ($show['!minor'])) || (isset ($show['bot']) && isset ($show['!bot'])) || (isset ($show['anon']) && isset ($show['!anon'])))
54 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
55
56 $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
57 $this->addWhereIf('rc_minor != 0', isset ($show['minor']));
58 $this->addWhereIf('rc_bot = 0', isset ($show['!bot']));
59 $this->addWhereIf('rc_bot != 0', isset ($show['bot']));
60 $this->addWhereIf('rc_user = 0', isset ($show['anon']));
61 $this->addWhereIf('rc_user != 0', isset ($show['!anon']));
62 }
63
64 $this->addFields(array (
65 'rc_timestamp',
66 'rc_namespace',
67 'rc_title',
68 'rc_cur_id',
69 'rc_this_oldid',
70 'rc_last_oldid',
71 'rc_type',
72 'rc_moved_to_ns',
73 'rc_moved_to_title'
74 ));
75
76 if (!is_null($prop)) {
77 $prop = array_flip($prop);
78 $this->addFieldsIf('rc_comment', isset ($prop['comment']));
79 if (isset ($prop['user'])) {
80 $this->addFields('rc_user');
81 $this->addFields('rc_user_text');
82 }
83 if (isset ($prop['flags'])) {
84 $this->addFields('rc_minor');
85 $this->addFields('rc_bot');
86 $this->addFields('rc_new');
87 }
88 }
89
90 $this->addOption('LIMIT', $limit +1);
91 $this->addOption('USE INDEX', 'rc_timestamp');
92
93 $data = array ();
94 $count = 0;
95 $db = $this->getDB();
96 $res = $this->select(__METHOD__);
97 while ($row = $db->fetchObject($res)) {
98 if (++ $count > $limit) {
99 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
100 $this->setContinueEnumParameter('start', $row->rc_timestamp);
101 break;
102 }
103
104 $vals = $this->addRowInfo('rc', $row);
105 if ($vals)
106 $data[] = $vals;
107 }
108 $db->freeResult($res);
109
110 $result = $this->getResult();
111 $result->setIndexedTagName($data, 'rc');
112 $result->addValue('query', $this->getModuleName(), $data);
113 }
114
115 protected function getAllowedParams() {
116 return array (
117 'start' => array (
118 ApiBase :: PARAM_TYPE => 'timestamp'
119 ),
120 'end' => array (
121 ApiBase :: PARAM_TYPE => 'timestamp'
122 ),
123 'dir' => array (
124 ApiBase :: PARAM_DFLT => 'older',
125 ApiBase :: PARAM_TYPE => array (
126 'newer',
127 'older'
128 )
129 ),
130 'namespace' => array (
131 ApiBase :: PARAM_ISMULTI => true,
132 ApiBase :: PARAM_TYPE => 'namespace'
133 ),
134 'prop' => array (
135 ApiBase :: PARAM_ISMULTI => true,
136 ApiBase :: PARAM_TYPE => array (
137 'user',
138 'comment',
139 'flags'
140 )
141 ),
142 'show' => array (
143 ApiBase :: PARAM_ISMULTI => true,
144 ApiBase :: PARAM_TYPE => array (
145 'minor',
146 '!minor',
147 'bot',
148 '!bot',
149 'anon',
150 '!anon'
151 )
152 ),
153 'limit' => array (
154 ApiBase :: PARAM_DFLT => 10,
155 ApiBase :: PARAM_TYPE => 'limit',
156 ApiBase :: PARAM_MIN => 1,
157 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
158 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
159 )
160 );
161 }
162
163 protected function getParamDescription() {
164 return array (
165 'start' => 'The timestamp to start enumerating from.',
166 'end' => 'The timestamp to end enumerating.',
167 'dir' => 'In which direction to enumerate.',
168 'namespace' => 'Filter log entries to only this namespace(s)',
169 'prop' => 'Include additional pieces of information',
170 'show' => array (
171 'Show only items that meet this criteria.',
172 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
173 ),
174 'limit' => 'How many total pages to return.'
175 );
176 }
177
178 protected function getDescription() {
179 return 'Enumerate recent changes';
180 }
181
182 protected function getExamples() {
183 return array (
184 'api.php?action=query&list=recentchanges'
185 );
186 }
187
188 public function getVersion() {
189 return __CLASS__ . ': $Id$';
190 }
191 }
192 ?>