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 = array_flip( $params['prop'] );
00047
00048 $this->fld_ids = isset( $prop['ids'] );
00049 $this->fld_title = isset( $prop['title'] );
00050 $this->fld_type = isset( $prop['type'] );
00051 $this->fld_user = isset( $prop['user'] );
00052 $this->fld_timestamp = isset( $prop['timestamp'] );
00053 $this->fld_comment = isset( $prop['comment'] );
00054 $this->fld_parsedcomment = isset ( $prop['parsedcomment'] );
00055 $this->fld_details = isset( $prop['details'] );
00056 $this->fld_tags = isset( $prop['tags'] );
00057
00058 list( $tbl_logging, $tbl_page, $tbl_user ) = $db->tableNamesN( 'logging', 'page', 'user' );
00059
00060 $hideLogs = LogEventsList::getExcludeClause( $db );
00061 if ( $hideLogs !== false )
00062 $this->addWhere( $hideLogs );
00063
00064
00065 $this->addTables( array( 'logging', 'user', 'page' ) );
00066 $this->addOption( 'STRAIGHT_JOIN' );
00067 $this->addJoinConds( array(
00068 'user' => array( 'JOIN',
00069 'user_id=log_user' ),
00070 'page' => array( 'LEFT JOIN',
00071 array( 'log_namespace=page_namespace',
00072 'log_title=page_title' ) ) ) );
00073 $index = array( 'logging' => 'times' );
00074
00075 $this->addFields( array (
00076 'log_type',
00077 'log_action',
00078 'log_timestamp',
00079 'log_deleted',
00080 ) );
00081
00082 $this->addFieldsIf( 'log_id', $this->fld_ids );
00083 $this->addFieldsIf( 'page_id', $this->fld_ids );
00084 $this->addFieldsIf( 'log_user', $this->fld_user );
00085 $this->addFieldsIf( 'user_name', $this->fld_user );
00086 $this->addFieldsIf( 'log_namespace', $this->fld_title );
00087 $this->addFieldsIf( 'log_title', $this->fld_title );
00088 $this->addFieldsIf( 'log_comment', $this->fld_comment || $this->fld_parsedcomment );
00089 $this->addFieldsIf( 'log_params', $this->fld_details );
00090
00091 if ( $this->fld_tags ) {
00092 $this->addTables( 'tag_summary' );
00093 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', 'log_id=ts_log_id' ) ) );
00094 $this->addFields( 'ts_tags' );
00095 }
00096
00097 if ( !is_null( $params['tag'] ) ) {
00098 $this->addTables( 'change_tag' );
00099 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'log_id=ct_log_id' ) ) ) );
00100 $this->addWhereFld( 'ct_tag', $params['tag'] );
00101 global $wgOldChangeTagsIndex;
00102 $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
00103 }
00104
00105 if ( !is_null( $params['type'] ) ) {
00106 $this->addWhereFld( 'log_type', $params['type'] );
00107 $index['logging'] = 'type_time';
00108 }
00109
00110 $this->addWhereRange( 'log_timestamp', $params['dir'], $params['start'], $params['end'] );
00111
00112 $limit = $params['limit'];
00113 $this->addOption( 'LIMIT', $limit + 1 );
00114
00115 $user = $params['user'];
00116 if ( !is_null( $user ) ) {
00117 $userid = User::idFromName( $user );
00118 if ( !$userid )
00119 $this->dieUsage( "User name $user not found", 'param_user' );
00120 $this->addWhereFld( 'log_user', $userid );
00121 $index['logging'] = 'user_time';
00122 }
00123
00124 $title = $params['title'];
00125 if ( !is_null( $title ) ) {
00126 $titleObj = Title :: newFromText( $title );
00127 if ( is_null( $titleObj ) )
00128 $this->dieUsage( "Bad title value '$title'", 'param_title' );
00129 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
00130 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
00131
00132
00133 $index['logging'] = is_null( $user ) ? 'page_time' : array( 'page_time', 'user_time' );
00134 }
00135
00136 $this->addOption( 'USE INDEX', $index );
00137
00138
00139 if ( !is_null( $title ) ) {
00140 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0' );
00141 }
00142 if ( !is_null( $user ) ) {
00143 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0' );
00144 }
00145
00146 $count = 0;
00147 $res = $this->select( __METHOD__ );
00148 while ( $row = $db->fetchObject( $res ) ) {
00149 if ( ++ $count > $limit ) {
00150
00151 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) );
00152 break;
00153 }
00154
00155 $vals = $this->extractRowInfo( $row );
00156 if ( !$vals )
00157 continue;
00158 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
00159 if ( !$fit )
00160 {
00161 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) );
00162 break;
00163 }
00164 }
00165 $db->freeResult( $res );
00166
00167 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
00168 }
00169
00170 public static function addLogParams( $result, &$vals, $params, $type, $ts ) {
00171 $params = explode( "\n", $params );
00172 switch ( $type ) {
00173 case 'move':
00174 if ( isset ( $params[0] ) ) {
00175 $title = Title :: newFromText( $params[0] );
00176 if ( $title ) {
00177 $vals2 = array();
00178 ApiQueryBase :: addTitleInfo( $vals2, $title, "new_" );
00179 $vals[$type] = $vals2;
00180 }
00181 }
00182 if ( isset ( $params[1] ) && $params[1] ) {
00183 $vals[$type]['suppressedredirect'] = '';
00184 }
00185 $params = null;
00186 break;
00187 case 'patrol':
00188 $vals2 = array();
00189 list( $vals2['cur'], $vals2['prev'], $vals2['auto'] ) = $params;
00190 $vals[$type] = $vals2;
00191 $params = null;
00192 break;
00193 case 'rights':
00194 $vals2 = array();
00195 list( $vals2['old'], $vals2['new'] ) = $params;
00196 $vals[$type] = $vals2;
00197 $params = null;
00198 break;
00199 case 'block':
00200 $vals2 = array();
00201 list( $vals2['duration'], $vals2['flags'] ) = $params;
00202 $vals2['expiry'] = wfTimestamp( TS_ISO_8601,
00203 strtotime( $params[0], wfTimestamp( TS_UNIX, $ts ) ) );
00204 $vals[$type] = $vals2;
00205 $params = null;
00206 break;
00207 }
00208 if ( !is_null( $params ) ) {
00209 $result->setIndexedTagName( $params, 'param' );
00210 $vals = array_merge( $vals, $params );
00211 }
00212 return $vals;
00213 }
00214
00215 private function extractRowInfo( $row ) {
00216 $vals = array();
00217
00218 if ( $this->fld_ids ) {
00219 $vals['logid'] = intval( $row->log_id );
00220 $vals['pageid'] = intval( $row->page_id );
00221 }
00222
00223 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
00224
00225 if ( $this->fld_title ) {
00226 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
00227 $vals['actionhidden'] = '';
00228 } else {
00229 ApiQueryBase::addTitleInfo( $vals, $title );
00230 }
00231 }
00232
00233 if ( $this->fld_type ) {
00234 $vals['type'] = $row->log_type;
00235 $vals['action'] = $row->log_action;
00236 }
00237
00238 if ( $this->fld_details && $row->log_params !== '' ) {
00239 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
00240 $vals['actionhidden'] = '';
00241 } else {
00242 self::addLogParams( $this->getResult(), $vals,
00243 $row->log_params, $row->log_type,
00244 $row->log_timestamp );
00245 }
00246 }
00247
00248 if ( $this->fld_user ) {
00249 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
00250 $vals['userhidden'] = '';
00251 } else {
00252 $vals['user'] = $row->user_name;
00253 if ( !$row->log_user )
00254 $vals['anon'] = '';
00255 }
00256 }
00257 if ( $this->fld_timestamp ) {
00258 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->log_timestamp );
00259 }
00260
00261 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->log_comment ) ) {
00262 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
00263 $vals['commenthidden'] = '';
00264 } else {
00265 if ( $this->fld_comment )
00266 $vals['comment'] = $row->log_comment;
00267
00268 if ( $this->fld_parsedcomment ) {
00269 global $wgUser;
00270 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->log_comment, $title );
00271 }
00272 }
00273 }
00274
00275 if ( $this->fld_tags ) {
00276 if ( $row->ts_tags ) {
00277 $tags = explode( ',', $row->ts_tags );
00278 $this->getResult()->setIndexedTagName( $tags, 'tag' );
00279 $vals['tags'] = $tags;
00280 } else {
00281 $vals['tags'] = array();
00282 }
00283 }
00284
00285 return $vals;
00286 }
00287
00288 public function getCacheMode( $params ) {
00289 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
00290
00291 return 'anon-public-user-private';
00292 } else {
00293 return 'public';
00294 }
00295 }
00296
00297 public function getAllowedParams() {
00298 global $wgLogTypes;
00299 return array (
00300 'prop' => array (
00301 ApiBase :: PARAM_ISMULTI => true,
00302 ApiBase :: PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
00303 ApiBase :: PARAM_TYPE => array (
00304 'ids',
00305 'title',
00306 'type',
00307 'user',
00308 'timestamp',
00309 'comment',
00310 'parsedcomment',
00311 'details',
00312 'tags'
00313 )
00314 ),
00315 'type' => array (
00316 ApiBase :: PARAM_TYPE => $wgLogTypes
00317 ),
00318 'start' => array (
00319 ApiBase :: PARAM_TYPE => 'timestamp'
00320 ),
00321 'end' => array (
00322 ApiBase :: PARAM_TYPE => 'timestamp'
00323 ),
00324 'dir' => array (
00325 ApiBase :: PARAM_DFLT => 'older',
00326 ApiBase :: PARAM_TYPE => array (
00327 'newer',
00328 'older'
00329 )
00330 ),
00331 'user' => null,
00332 'title' => null,
00333 'tag' => null,
00334 'limit' => array (
00335 ApiBase :: PARAM_DFLT => 10,
00336 ApiBase :: PARAM_TYPE => 'limit',
00337 ApiBase :: PARAM_MIN => 1,
00338 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00339 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00340 )
00341 );
00342 }
00343
00344 public function getParamDescription() {
00345 return array (
00346 'prop' => 'Which properties to get',
00347 'type' => 'Filter log entries to only this type(s)',
00348 'start' => 'The timestamp to start enumerating from.',
00349 'end' => 'The timestamp to end enumerating.',
00350 'dir' => 'In which direction to enumerate.',
00351 'user' => 'Filter entries to those made by the given user.',
00352 'title' => 'Filter entries to those related to a page.',
00353 'limit' => 'How many total event entries to return.',
00354 'tag' => 'Only list event entries tagged with this tag.',
00355 );
00356 }
00357
00358 public function getDescription() {
00359 return 'Get events from logs.';
00360 }
00361
00362 public function getPossibleErrors() {
00363 return array_merge( parent::getPossibleErrors(), array(
00364 array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
00365 array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
00366 ) );
00367 }
00368
00369 protected function getExamples() {
00370 return array (
00371 'api.php?action=query&list=logevents'
00372 );
00373 }
00374
00375 public function getVersion() {
00376 return __CLASS__ . ': $Id: ApiQueryLogEvents.php 69932 2010-07-26 08:03:21Z tstarling $';
00377 }
00378 }