]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryLogEvents.php
MediaWiki 1.15.5
[autoinstalls/mediawiki.git] / includes / api / ApiQueryLogEvents.php
1 <?php
2
3 /*
4  * Created on Oct 16, 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 action to List the log events, with optional filtering by various parameters.
33  *
34  * @ingroup API
35  */
36 class ApiQueryLogEvents extends ApiQueryBase {
37
38         public function __construct($query, $moduleName) {
39                 parent :: __construct($query, $moduleName, 'le');
40         }
41
42         public function execute() {
43                 $params = $this->extractRequestParams();
44                 $db = $this->getDB();
45
46                 $prop = $params['prop'];
47                 $this->fld_ids = in_array('ids', $prop);
48                 $this->fld_title = in_array('title', $prop);
49                 $this->fld_type = in_array('type', $prop);
50                 $this->fld_user = in_array('user', $prop);
51                 $this->fld_timestamp = in_array('timestamp', $prop);
52                 $this->fld_comment = in_array('comment', $prop);
53                 $this->fld_details = in_array('details', $prop);
54
55                 list($tbl_logging, $tbl_page, $tbl_user) = $db->tableNamesN('logging', 'page', 'user');
56
57                 $hideLogs = LogEventsList::getExcludeClause($db);
58                 if($hideLogs !== false)
59                         $this->addWhere($hideLogs);
60
61                 // Order is significant here
62                 $this->addTables(array('logging', 'user', 'page'));
63                 $this->addOption('STRAIGHT_JOIN');
64                 $this->addJoinConds(array(
65                         'user' => array('JOIN',
66                                 'user_id=log_user'),
67                         'page' => array('LEFT JOIN',
68                                 array(  'log_namespace=page_namespace',
69                                         'log_title=page_title'))));
70                 $index = 'times'; // default, may change
71
72                 $this->addFields(array (
73                         'log_type',
74                         'log_action',
75                         'log_timestamp',
76                         'log_deleted',
77                 ));
78
79                 $this->addFieldsIf('log_id', $this->fld_ids);
80                 $this->addFieldsIf('page_id', $this->fld_ids);
81                 $this->addFieldsIf('log_user', $this->fld_user);
82                 $this->addFieldsIf('user_name', $this->fld_user);
83                 $this->addFieldsIf('log_namespace', $this->fld_title);
84                 $this->addFieldsIf('log_title', $this->fld_title);
85                 $this->addFieldsIf('log_comment', $this->fld_comment);
86                 $this->addFieldsIf('log_params', $this->fld_details);
87                 
88                 if( !is_null($params['type']) ) {
89                         $this->addWhereFld('log_type', $params['type']);
90                         $index = 'type_time';
91                 }
92                 
93                 $this->addWhereRange('log_timestamp', $params['dir'], $params['start'], $params['end']);
94
95                 $limit = $params['limit'];
96                 $this->addOption('LIMIT', $limit +1);
97
98                 $user = $params['user'];
99                 if (!is_null($user)) {
100                         $userid = User::idFromName($user);
101                         if (!$userid)
102                                 $this->dieUsage("User name $user not found", 'param_user');
103                         $this->addWhereFld('log_user', $userid);
104                         $index = 'user_time';
105                 }
106
107                 $title = $params['title'];
108                 if (!is_null($title)) {
109                         $titleObj = Title :: newFromText($title);
110                         if (is_null($titleObj))
111                                 $this->dieUsage("Bad title value '$title'", 'param_title');
112                         $this->addWhereFld('log_namespace', $titleObj->getNamespace());
113                         $this->addWhereFld('log_title', $titleObj->getDBkey());
114
115                         // Use the title index in preference to the user index if there is a conflict
116                         $index = is_null($user) ? 'page_time' : array('page_time','user_time');
117                 }
118
119                 $this->addOption( 'USE INDEX', array( 'logging' => $index ) );
120
121                 // Paranoia: avoid brute force searches (bug 17342)
122                 if (!is_null($title)) {
123                         $this->addWhere('log_deleted & ' . LogPage::DELETED_ACTION . ' = 0');
124                 }
125                 if (!is_null($user)) {
126                         $this->addWhere('log_deleted & ' . LogPage::DELETED_USER . ' = 0');
127                 }
128
129                 $count = 0;
130                 $res = $this->select(__METHOD__);
131                 while ($row = $db->fetchObject($res)) {
132                         if (++ $count > $limit) {
133                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
134                                 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->log_timestamp));
135                                 break;
136                         }
137
138                         $vals = $this->extractRowInfo($row);
139                         if(!$vals)
140                                 continue;
141                         $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
142                         if(!$fit)
143                         {
144                                 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->log_timestamp));
145                                 break;
146                         }
147                 }
148                 $db->freeResult($res);
149
150                 $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'item');
151         }
152         
153         public static function addLogParams($result, &$vals, $params, $type, $ts) {
154                 $params = explode("\n", $params);
155                 switch ($type) {
156                         case 'move':
157                                 if (isset ($params[0])) {
158                                         $title = Title :: newFromText($params[0]);
159                                         if ($title) {
160                                                 $vals2 = array();
161                                                 ApiQueryBase :: addTitleInfo($vals2, $title, "new_");
162                                                 $vals[$type] = $vals2;
163                                         }
164                                 }
165                                 if (isset ($params[1]) && $params[1]) {
166                                         $vals[$type]['suppressedredirect'] = '';
167                                 } 
168                                 $params = null;
169                                 break;
170                         case 'patrol':
171                                 $vals2 = array();
172                                 list( $vals2['cur'], $vals2['prev'], $vals2['auto'] ) = $params;
173                                 $vals[$type] = $vals2;
174                                 $params = null;
175                                 break;
176                         case 'rights':
177                                 $vals2 = array();
178                                 list( $vals2['old'], $vals2['new'] ) = $params;
179                                 $vals[$type] = $vals2;
180                                 $params = null;
181                                 break;
182                         case 'block':
183                                 $vals2 = array();
184                                 list( $vals2['duration'], $vals2['flags'] ) = $params;
185                                 $vals2['expiry'] = wfTimestamp(TS_ISO_8601,
186                                                 strtotime($params[0], wfTimestamp(TS_UNIX, $ts)));
187                                 $vals[$type] = $vals2;
188                                 $params = null;
189                                 break;
190                 }
191                 if (!is_null($params)) {
192                         $result->setIndexedTagName($params, 'param');
193                         $vals = array_merge($vals, $params);
194                 }
195                 return $vals;
196         }
197
198         private function extractRowInfo($row) {
199                 $vals = array();
200
201                 if ($this->fld_ids) {
202                         $vals['logid'] = intval($row->log_id);
203                         $vals['pageid'] = intval($row->page_id);
204                 }
205
206                 if ($this->fld_title) {
207                         if (LogEventsList::isDeleted($row, LogPage::DELETED_ACTION)) {
208                                 $vals['actionhidden'] = '';
209                         } else {
210                                 $title = Title :: makeTitle($row->log_namespace, $row->log_title);
211                                 ApiQueryBase :: addTitleInfo($vals, $title);
212                         }
213                 }
214
215                 if ($this->fld_type) {
216                         $vals['type'] = $row->log_type;
217                         $vals['action'] = $row->log_action;
218                 }
219
220                 if ($this->fld_details && $row->log_params !== '') {
221                         if (LogEventsList::isDeleted($row, LogPage::DELETED_ACTION)) {
222                                 $vals['actionhidden'] = '';
223                         } else {
224                                 self::addLogParams($this->getResult(), $vals,
225                                         $row->log_params, $row->log_type,
226                                         $row->log_timestamp);
227                         }
228                 }
229
230                 if ($this->fld_user) {
231                         if (LogEventsList::isDeleted($row, LogPage::DELETED_USER)) {
232                                 $vals['userhidden'] = '';
233                         } else {
234                                 $vals['user'] = $row->user_name;
235                                 if(!$row->log_user)
236                                         $vals['anon'] = '';
237                         }
238                 }
239                 if ($this->fld_timestamp) {
240                         $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->log_timestamp);
241                 }
242                 if ($this->fld_comment && isset($row->log_comment)) {
243                         if (LogEventsList::isDeleted($row, LogPage::DELETED_COMMENT)) {
244                                 $vals['commenthidden'] = '';
245                         } else {
246                                 $vals['comment'] = $row->log_comment;
247                         }
248                 }
249
250                 return $vals;
251         }
252
253
254         public function getCacheMode( $params ) {
255                 return 'public';
256         }
257
258         public function getAllowedParams() {
259                 global $wgLogTypes;
260                 return array (
261                         'prop' => array (
262                                 ApiBase :: PARAM_ISMULTI => true,
263                                 ApiBase :: PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
264                                 ApiBase :: PARAM_TYPE => array (
265                                         'ids',
266                                         'title',
267                                         'type',
268                                         'user',
269                                         'timestamp',
270                                         'comment',
271                                         'details',
272                                 )
273                         ),
274                         'type' => array (
275                                 ApiBase :: PARAM_TYPE => $wgLogTypes
276                         ),
277                         'start' => array (
278                                 ApiBase :: PARAM_TYPE => 'timestamp'
279                         ),
280                         'end' => array (
281                                 ApiBase :: PARAM_TYPE => 'timestamp'
282                         ),
283                         'dir' => array (
284                                 ApiBase :: PARAM_DFLT => 'older',
285                                 ApiBase :: PARAM_TYPE => array (
286                                         'newer',
287                                         'older'
288                                 )
289                         ),
290                         'user' => null,
291                         'title' => null,
292                         'limit' => array (
293                                 ApiBase :: PARAM_DFLT => 10,
294                                 ApiBase :: PARAM_TYPE => 'limit',
295                                 ApiBase :: PARAM_MIN => 1,
296                                 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
297                                 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
298                         )
299                 );
300         }
301
302         public function getParamDescription() {
303                 return array (
304                         'prop' => 'Which properties to get',
305                         'type' => 'Filter log entries to only this type(s)',
306                         'start' => 'The timestamp to start enumerating from.',
307                         'end' => 'The timestamp to end enumerating.',
308                         'dir' => 'In which direction to enumerate.',
309                         'user' => 'Filter entries to those made by the given user.',
310                         'title' => 'Filter entries to those related to a page.',
311                         'limit' => 'How many total event entries to return.'
312                 );
313         }
314
315         public function getDescription() {
316                 return 'Get events from logs.';
317         }
318
319         protected function getExamples() {
320                 return array (
321                         'api.php?action=query&list=logevents'
322                 );
323         }
324
325         public function getVersion() {
326                 return __CLASS__ . ': $Id: ApiQueryLogEvents.php 69986 2010-07-27 03:57:39Z tstarling $';
327         }
328 }