00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if (!defined('MEDIAWIKI')) {
00027
00028 require_once ('ApiQueryBase.php');
00029 }
00030
00036 class ApiQueryLogEvents extends ApiQueryBase {
00037
00038 public function __construct($query, $moduleName) {
00039 parent :: __construct($query, $moduleName, 'le');
00040 }
00041
00042 public function execute() {
00043 $params = $this->extractRequestParams();
00044 $db = $this->getDB();
00045
00046 $prop = $params['prop'];
00047 $this->fld_ids = in_array('ids', $prop);
00048 $this->fld_title = in_array('title', $prop);
00049 $this->fld_type = in_array('type', $prop);
00050 $this->fld_user = in_array('user', $prop);
00051 $this->fld_timestamp = in_array('timestamp', $prop);
00052 $this->fld_comment = in_array('comment', $prop);
00053 $this->fld_details = in_array('details', $prop);
00054
00055 list($tbl_logging, $tbl_page, $tbl_user) = $db->tableNamesN('logging', 'page', 'user');
00056
00057 $hideLogs = LogEventsList::getExcludeClause($db);
00058 if($hideLogs !== false)
00059 $this->addWhere($hideLogs);
00060
00061
00062 $this->addTables(array('logging', 'user', 'page'));
00063 $this->addOption('STRAIGHT_JOIN');
00064 $this->addJoinConds(array(
00065 'user' => array('JOIN',
00066 'user_id=log_user'),
00067 'page' => array('LEFT JOIN',
00068 array( 'log_namespace=page_namespace',
00069 'log_title=page_title'))));
00070 $index = 'times';
00071
00072 $this->addFields(array (
00073 'log_type',
00074 'log_action',
00075 'log_timestamp',
00076 'log_deleted',
00077 ));
00078
00079 $this->addFieldsIf('log_id', $this->fld_ids);
00080 $this->addFieldsIf('page_id', $this->fld_ids);
00081 $this->addFieldsIf('log_user', $this->fld_user);
00082 $this->addFieldsIf('user_name', $this->fld_user);
00083 $this->addFieldsIf('log_namespace', $this->fld_title);
00084 $this->addFieldsIf('log_title', $this->fld_title);
00085 $this->addFieldsIf('log_comment', $this->fld_comment);
00086 $this->addFieldsIf('log_params', $this->fld_details);
00087
00088 if( !is_null($params['type']) ) {
00089 $this->addWhereFld('log_type', $params['type']);
00090 $index = 'type_time';
00091 }
00092
00093 $this->addWhereRange('log_timestamp', $params['dir'], $params['start'], $params['end']);
00094
00095 $limit = $params['limit'];
00096 $this->addOption('LIMIT', $limit +1);
00097
00098 $user = $params['user'];
00099 if (!is_null($user)) {
00100 $userid = User::idFromName($user);
00101 if (!$userid)
00102 $this->dieUsage("User name $user not found", 'param_user');
00103 $this->addWhereFld('log_user', $userid);
00104 $index = 'user_time';
00105 }
00106
00107 $title = $params['title'];
00108 if (!is_null($title)) {
00109 $titleObj = Title :: newFromText($title);
00110 if (is_null($titleObj))
00111 $this->dieUsage("Bad title value '$title'", 'param_title');
00112 $this->addWhereFld('log_namespace', $titleObj->getNamespace());
00113 $this->addWhereFld('log_title', $titleObj->getDBkey());
00114
00115
00116 $index = is_null($user) ? 'page_time' : array('page_time','user_time');
00117 }
00118
00119 $this->addOption( 'USE INDEX', array( 'logging' => $index ) );
00120
00121
00122 if (!is_null($title)) {
00123 $this->addWhere('log_deleted & ' . LogPage::DELETED_ACTION . ' = 0');
00124 }
00125 if (!is_null($user)) {
00126 $this->addWhere('log_deleted & ' . LogPage::DELETED_USER . ' = 0');
00127 }
00128
00129 $count = 0;
00130 $res = $this->select(__METHOD__);
00131 while ($row = $db->fetchObject($res)) {
00132 if (++ $count > $limit) {
00133
00134 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->log_timestamp));
00135 break;
00136 }
00137
00138 $vals = $this->extractRowInfo($row);
00139 if(!$vals)
00140 continue;
00141 $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
00142 if(!$fit)
00143 {
00144 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->log_timestamp));
00145 break;
00146 }
00147 }
00148 $db->freeResult($res);
00149
00150 $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'item');
00151 }
00152
00153 public static function addLogParams($result, &$vals, $params, $type, $ts) {
00154 $params = explode("\n", $params);
00155 switch ($type) {
00156 case 'move':
00157 if (isset ($params[0])) {
00158 $title = Title :: newFromText($params[0]);
00159 if ($title) {
00160 $vals2 = array();
00161 ApiQueryBase :: addTitleInfo($vals2, $title, "new_");
00162 $vals[$type] = $vals2;
00163 }
00164 }
00165 if (isset ($params[1]) && $params[1]) {
00166 $vals[$type]['suppressedredirect'] = '';
00167 }
00168 $params = null;
00169 break;
00170 case 'patrol':
00171 $vals2 = array();
00172 list( $vals2['cur'], $vals2['prev'], $vals2['auto'] ) = $params;
00173 $vals[$type] = $vals2;
00174 $params = null;
00175 break;
00176 case 'rights':
00177 $vals2 = array();
00178 list( $vals2['old'], $vals2['new'] ) = $params;
00179 $vals[$type] = $vals2;
00180 $params = null;
00181 break;
00182 case 'block':
00183 $vals2 = array();
00184 list( $vals2['duration'], $vals2['flags'] ) = $params;
00185 $vals2['expiry'] = wfTimestamp(TS_ISO_8601,
00186 strtotime($params[0], wfTimestamp(TS_UNIX, $ts)));
00187 $vals[$type] = $vals2;
00188 $params = null;
00189 break;
00190 }
00191 if (!is_null($params)) {
00192 $result->setIndexedTagName($params, 'param');
00193 $vals = array_merge($vals, $params);
00194 }
00195 return $vals;
00196 }
00197
00198 private function extractRowInfo($row) {
00199 $vals = array();
00200
00201 if ($this->fld_ids) {
00202 $vals['logid'] = intval($row->log_id);
00203 $vals['pageid'] = intval($row->page_id);
00204 }
00205
00206 if ($this->fld_title) {
00207 if (LogEventsList::isDeleted($row, LogPage::DELETED_ACTION)) {
00208 $vals['actionhidden'] = '';
00209 } else {
00210 $title = Title :: makeTitle($row->log_namespace, $row->log_title);
00211 ApiQueryBase :: addTitleInfo($vals, $title);
00212 }
00213 }
00214
00215 if ($this->fld_type) {
00216 $vals['type'] = $row->log_type;
00217 $vals['action'] = $row->log_action;
00218 }
00219
00220 if ($this->fld_details && $row->log_params !== '') {
00221 if (LogEventsList::isDeleted($row, LogPage::DELETED_ACTION)) {
00222 $vals['actionhidden'] = '';
00223 } else {
00224 self::addLogParams($this->getResult(), $vals,
00225 $row->log_params, $row->log_type,
00226 $row->log_timestamp);
00227 }
00228 }
00229
00230 if ($this->fld_user) {
00231 if (LogEventsList::isDeleted($row, LogPage::DELETED_USER)) {
00232 $vals['userhidden'] = '';
00233 } else {
00234 $vals['user'] = $row->user_name;
00235 if(!$row->log_user)
00236 $vals['anon'] = '';
00237 }
00238 }
00239 if ($this->fld_timestamp) {
00240 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->log_timestamp);
00241 }
00242 if ($this->fld_comment && isset($row->log_comment)) {
00243 if (LogEventsList::isDeleted($row, LogPage::DELETED_COMMENT)) {
00244 $vals['commenthidden'] = '';
00245 } else {
00246 $vals['comment'] = $row->log_comment;
00247 }
00248 }
00249
00250 return $vals;
00251 }
00252
00253
00254 public function getAllowedParams() {
00255 global $wgLogTypes;
00256 return array (
00257 'prop' => array (
00258 ApiBase :: PARAM_ISMULTI => true,
00259 ApiBase :: PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
00260 ApiBase :: PARAM_TYPE => array (
00261 'ids',
00262 'title',
00263 'type',
00264 'user',
00265 'timestamp',
00266 'comment',
00267 'details',
00268 )
00269 ),
00270 'type' => array (
00271 ApiBase :: PARAM_TYPE => $wgLogTypes
00272 ),
00273 'start' => array (
00274 ApiBase :: PARAM_TYPE => 'timestamp'
00275 ),
00276 'end' => array (
00277 ApiBase :: PARAM_TYPE => 'timestamp'
00278 ),
00279 'dir' => array (
00280 ApiBase :: PARAM_DFLT => 'older',
00281 ApiBase :: PARAM_TYPE => array (
00282 'newer',
00283 'older'
00284 )
00285 ),
00286 'user' => null,
00287 'title' => null,
00288 'limit' => array (
00289 ApiBase :: PARAM_DFLT => 10,
00290 ApiBase :: PARAM_TYPE => 'limit',
00291 ApiBase :: PARAM_MIN => 1,
00292 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00293 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00294 )
00295 );
00296 }
00297
00298 public function getParamDescription() {
00299 return array (
00300 'prop' => 'Which properties to get',
00301 'type' => 'Filter log entries to only this type(s)',
00302 'start' => 'The timestamp to start enumerating from.',
00303 'end' => 'The timestamp to end enumerating.',
00304 'dir' => 'In which direction to enumerate.',
00305 'user' => 'Filter entries to those made by the given user.',
00306 'title' => 'Filter entries to those related to a page.',
00307 'limit' => 'How many total event entries to return.'
00308 );
00309 }
00310
00311 public function getDescription() {
00312 return 'Get events from logs.';
00313 }
00314
00315 protected function getExamples() {
00316 return array (
00317 'api.php?action=query&list=logevents'
00318 );
00319 }
00320
00321 public function getVersion() {
00322 return __CLASS__ . ': $Id: ApiQueryLogEvents.php 47904 2009-03-01 11:02:49Z catrope $';
00323 }
00324 }