MediaWiki  REL1_20
ApiQueryLogEvents.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryLogEvents extends ApiQueryBase {
00033 
00034         public function __construct( $query, $moduleName ) {
00035                 parent::__construct( $query, $moduleName, 'le' );
00036         }
00037 
00038         private $fld_ids = false, $fld_title = false, $fld_type = false,
00039                 $fld_action = false, $fld_user = false, $fld_userid = false,
00040                 $fld_timestamp = false, $fld_comment = false, $fld_parsedcomment = false,
00041                 $fld_details = false, $fld_tags = false;
00042 
00043         public function execute() {
00044                 $params = $this->extractRequestParams();
00045                 $db = $this->getDB();
00046 
00047                 $prop = array_flip( $params['prop'] );
00048 
00049                 $this->fld_ids = isset( $prop['ids'] );
00050                 $this->fld_title = isset( $prop['title'] );
00051                 $this->fld_type = isset( $prop['type'] );
00052                 $this->fld_action = isset ( $prop['action'] );
00053                 $this->fld_user = isset( $prop['user'] );
00054                 $this->fld_userid = isset( $prop['userid'] );
00055                 $this->fld_timestamp = isset( $prop['timestamp'] );
00056                 $this->fld_comment = isset( $prop['comment'] );
00057                 $this->fld_parsedcomment = isset ( $prop['parsedcomment'] );
00058                 $this->fld_details = isset( $prop['details'] );
00059                 $this->fld_tags = isset( $prop['tags'] );
00060 
00061                 $hideLogs = LogEventsList::getExcludeClause( $db );
00062                 if ( $hideLogs !== false ) {
00063                         $this->addWhere( $hideLogs );
00064                 }
00065 
00066                 // Order is significant here
00067                 $this->addTables( array( 'logging', 'user', 'page' ) );
00068                 $this->addOption( 'STRAIGHT_JOIN' );
00069                 $this->addJoinConds( array(
00070                         'user' => array( 'JOIN',
00071                                 'user_id=log_user' ),
00072                         'page' => array( 'LEFT JOIN',
00073                                 array(  'log_namespace=page_namespace',
00074                                         'log_title=page_title' ) ) ) );
00075                 $index = array( 'logging' => 'times' ); // default, may change
00076 
00077                 $this->addFields( array(
00078                         'log_type',
00079                         'log_action',
00080                         'log_timestamp',
00081                         'log_deleted',
00082                 ) );
00083 
00084                 $this->addFieldsIf( array( 'log_id', 'page_id' ), $this->fld_ids );
00085                 $this->addFieldsIf( array( 'log_user', 'user_name' ), $this->fld_user );
00086                 $this->addFieldsIf( 'user_id', $this->fld_userid );
00087                 $this->addFieldsIf( array( 'log_namespace', 'log_title' ), $this->fld_title || $this->fld_parsedcomment );
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['action'] ) ) {
00106                         list( $type, $action ) = explode( '/', $params['action'] );
00107                         $this->addWhereFld( 'log_type', $type );
00108                         $this->addWhereFld( 'log_action', $action );
00109                 } elseif ( !is_null( $params['type'] ) ) {
00110                         $this->addWhereFld( 'log_type', $params['type'] );
00111                         $index['logging'] = 'type_time';
00112                 }
00113 
00114                 $this->addTimestampWhereRange( 'log_timestamp', $params['dir'], $params['start'], $params['end'] );
00115 
00116                 $limit = $params['limit'];
00117                 $this->addOption( 'LIMIT', $limit + 1 );
00118 
00119                 $user = $params['user'];
00120                 if ( !is_null( $user ) ) {
00121                         $userid = User::idFromName( $user );
00122                         if ( !$userid ) {
00123                                 $this->dieUsage( "User name $user not found", 'param_user' );
00124                         }
00125                         $this->addWhereFld( 'log_user', $userid );
00126                         $index['logging'] = 'user_time';
00127                 }
00128 
00129                 $title = $params['title'];
00130                 if ( !is_null( $title ) ) {
00131                         $titleObj = Title::newFromText( $title );
00132                         if ( is_null( $titleObj ) ) {
00133                                 $this->dieUsage( "Bad title value '$title'", 'param_title' );
00134                         }
00135                         $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
00136                         $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
00137 
00138                         // Use the title index in preference to the user index if there is a conflict
00139                         $index['logging'] = is_null( $user ) ? 'page_time' : array( 'page_time', 'user_time' );
00140                 }
00141 
00142                 $prefix = $params['prefix'];
00143 
00144                 if ( !is_null( $prefix ) ) {
00145                         global $wgMiserMode;
00146                         if ( $wgMiserMode ) {
00147                                 $this->dieUsage( 'Prefix search disabled in Miser Mode', 'prefixsearchdisabled' );
00148                         }
00149 
00150                         $title = Title::newFromText( $prefix );
00151                         if ( is_null( $title ) ) {
00152                                 $this->dieUsage( "Bad title value '$prefix'", 'param_prefix' );
00153                         }
00154                         $this->addWhereFld( 'log_namespace',  $title->getNamespace() );
00155                         $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
00156                 }
00157 
00158                 $this->addOption( 'USE INDEX', $index );
00159 
00160                 // Paranoia: avoid brute force searches (bug 17342)
00161                 if ( !is_null( $title ) ) {
00162                         $this->addWhere( $db->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0' );
00163                 }
00164                 if ( !is_null( $user ) ) {
00165                         $this->addWhere( $db->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0' );
00166                 }
00167 
00168                 $count = 0;
00169                 $res = $this->select( __METHOD__ );
00170                 $result = $this->getResult();
00171                 foreach ( $res as $row ) {
00172                         if ( ++ $count > $limit ) {
00173                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
00174                                 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) );
00175                                 break;
00176                         }
00177 
00178                         $vals = $this->extractRowInfo( $row );
00179                         if ( !$vals ) {
00180                                 continue;
00181                         }
00182                         $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
00183                         if ( !$fit ) {
00184                                 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) );
00185                                 break;
00186                         }
00187                 }
00188                 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
00189         }
00190 
00201         public static function addLogParams( $result, &$vals, $params, $type, $action, $ts, $legacy = false ) {
00202                 switch ( $type ) {
00203                         case 'move':
00204                                 if ( $legacy ){
00205                                         $targetKey = 0;
00206                                         $noredirKey = 1;
00207                                 } else {
00208                                         $targetKey = '4::target';
00209                                         $noredirKey = '5::noredir';
00210                                 }
00211 
00212                                 if ( isset( $params[ $targetKey ] ) ) {
00213                                         $title = Title::newFromText( $params[ $targetKey ] );
00214                                         if ( $title ) {
00215                                                 $vals2 = array();
00216                                                 ApiQueryBase::addTitleInfo( $vals2, $title, 'new_' );
00217                                                 $vals[$type] = $vals2;
00218                                         }
00219                                 }
00220                                 if ( isset( $params[ $noredirKey ] ) && $params[ $noredirKey ] ) {
00221                                         $vals[$type]['suppressedredirect'] = '';
00222                                 }
00223                                 $params = null;
00224                                 break;
00225                         case 'patrol':
00226                                 if ( $legacy ){
00227                                         $cur = 0;
00228                                         $prev = 1;
00229                                         $auto = 2;
00230                                 } else {
00231                                         $cur = '4::curid';
00232                                         $prev = '5::previd';
00233                                         $auto = '6::auto';
00234                                 }
00235                                 $vals2 = array();
00236                                 $vals2['cur'] = $params[$cur];
00237                                 $vals2['prev'] = $params[$prev];
00238                                 $vals2['auto'] = $params[$auto];
00239                                 $vals[$type] = $vals2;
00240                                 $params = null;
00241                                 break;
00242                         case 'rights':
00243                                 $vals2 = array();
00244                                 list( $vals2['old'], $vals2['new'] ) = $params;
00245                                 $vals[$type] = $vals2;
00246                                 $params = null;
00247                                 break;
00248                         case 'block':
00249                                 if ( $action == 'unblock' ) {
00250                                         break;
00251                                 }
00252                                 $vals2 = array();
00253                                 list( $vals2['duration'], $vals2['flags'] ) = $params;
00254 
00255                                 // Indefinite blocks have no expiry time
00256                                 if ( SpecialBlock::parseExpiryInput( $params[0] ) !== wfGetDB( DB_SLAVE )->getInfinity() ) {
00257                                         $vals2['expiry'] = wfTimestamp( TS_ISO_8601,
00258                                                 strtotime( $params[0], wfTimestamp( TS_UNIX, $ts ) ) );
00259                                 }
00260                                 $vals[$type] = $vals2;
00261                                 $params = null;
00262                                 break;
00263                 }
00264                 if ( !is_null( $params ) ) {
00265                         $result->setIndexedTagName( $params, 'param' );
00266                         $result->setIndexedTagName_recursive( $params, 'param' );
00267                         $vals = array_merge( $vals, $params );
00268                 }
00269                 return $vals;
00270         }
00271 
00272         private function extractRowInfo( $row ) {
00273                 $logEntry = DatabaseLogEntry::newFromRow( $row );
00274                 $vals = array();
00275 
00276                 if ( $this->fld_ids ) {
00277                         $vals['logid'] = intval( $row->log_id );
00278                         $vals['pageid'] = intval( $row->page_id );
00279                 }
00280 
00281                 if ( $this->fld_title || $this->fld_parsedcomment ) {
00282                         $title = Title::makeTitle( $row->log_namespace, $row->log_title );
00283                 }
00284 
00285                 if ( $this->fld_title ) {
00286                         if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
00287                                 $vals['actionhidden'] = '';
00288                         } else {
00289                                 ApiQueryBase::addTitleInfo( $vals, $title );
00290                         }
00291                 }
00292 
00293                 if ( $this->fld_type || $this->fld_action ) {
00294                         $vals['type'] = $row->log_type;
00295                         $vals['action'] = $row->log_action;
00296                 }
00297 
00298                 if ( $this->fld_details && $row->log_params !== '' ) {
00299                         if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
00300                                 $vals['actionhidden'] = '';
00301                         } else {
00302                                 self::addLogParams(
00303                                         $this->getResult(),
00304                                         $vals,
00305                                         $logEntry->getParameters(),
00306                                         $logEntry->getType(),
00307                                         $logEntry->getSubtype(),
00308                                         $logEntry->getTimestamp(),
00309                                         $logEntry->isLegacy()
00310                                 );
00311                         }
00312                 }
00313 
00314                 if ( $this->fld_user || $this->fld_userid ) {
00315                         if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
00316                                 $vals['userhidden'] = '';
00317                         } else {
00318                                 if ( $this->fld_user ) {
00319                                         $vals['user'] = $row->user_name;
00320                                 }
00321                                 if ( $this->fld_userid ) {
00322                                         $vals['userid'] = $row->user_id;
00323                                 }
00324 
00325                                 if ( !$row->log_user ) {
00326                                         $vals['anon'] = '';
00327                                 }
00328                         }
00329                 }
00330                 if ( $this->fld_timestamp ) {
00331                         $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->log_timestamp );
00332                 }
00333 
00334                 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->log_comment ) ) {
00335                         if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
00336                                 $vals['commenthidden'] = '';
00337                         } else {
00338                                 if ( $this->fld_comment ) {
00339                                         $vals['comment'] = $row->log_comment;
00340                                 }
00341 
00342                                 if ( $this->fld_parsedcomment ) {
00343                                         $vals['parsedcomment'] = Linker::formatComment( $row->log_comment, $title );
00344                                 }
00345                         }
00346                 }
00347 
00348                 if ( $this->fld_tags ) {
00349                         if ( $row->ts_tags ) {
00350                                 $tags = explode( ',', $row->ts_tags );
00351                                 $this->getResult()->setIndexedTagName( $tags, 'tag' );
00352                                 $vals['tags'] = $tags;
00353                         } else {
00354                                 $vals['tags'] = array();
00355                         }
00356                 }
00357 
00358                 return $vals;
00359         }
00360 
00361         public function getCacheMode( $params ) {
00362                 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
00363                         // formatComment() calls wfMessage() among other things
00364                         return 'anon-public-user-private';
00365                 } else {
00366                         return 'public';
00367                 }
00368         }
00369 
00370         public function getAllowedParams() {
00371                 global $wgLogTypes, $wgLogActions, $wgLogActionsHandlers;
00372                 return array(
00373                         'prop' => array(
00374                                 ApiBase::PARAM_ISMULTI => true,
00375                                 ApiBase::PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
00376                                 ApiBase::PARAM_TYPE => array(
00377                                         'ids',
00378                                         'title',
00379                                         'type',
00380                                         'user',
00381                                         'userid',
00382                                         'timestamp',
00383                                         'comment',
00384                                         'parsedcomment',
00385                                         'details',
00386                                         'tags'
00387                                 )
00388                         ),
00389                         'type' => array(
00390                                 ApiBase::PARAM_TYPE => $wgLogTypes
00391                         ),
00392                         'action' => array(
00393                                 ApiBase::PARAM_TYPE => array_keys( array_merge( $wgLogActions, $wgLogActionsHandlers ) )
00394                         ),
00395                         'start' => array(
00396                                 ApiBase::PARAM_TYPE => 'timestamp'
00397                         ),
00398                         'end' => array(
00399                                 ApiBase::PARAM_TYPE => 'timestamp'
00400                         ),
00401                         'dir' => array(
00402                                 ApiBase::PARAM_DFLT => 'older',
00403                                 ApiBase::PARAM_TYPE => array(
00404                                         'newer',
00405                                         'older'
00406                                 )
00407                         ),
00408                         'user' => null,
00409                         'title' => null,
00410                         'prefix' => null,
00411                         'tag' => null,
00412                         'limit' => array(
00413                                 ApiBase::PARAM_DFLT => 10,
00414                                 ApiBase::PARAM_TYPE => 'limit',
00415                                 ApiBase::PARAM_MIN => 1,
00416                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00417                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00418                         )
00419                 );
00420         }
00421 
00422         public function getParamDescription() {
00423                 $p = $this->getModulePrefix();
00424                 return array(
00425                         'prop' => array(
00426                                 'Which properties to get',
00427                                 ' ids            - Adds the ID of the log event',
00428                                 ' title          - Adds the title of the page for the log event',
00429                                 ' type           - Adds the type of log event',
00430                                 ' user           - Adds the user responsible for the log event',
00431                                 ' userid         - Adds the user ID who was responsible for the log event',
00432                                 ' timestamp      - Adds the timestamp for the event',
00433                                 ' comment        - Adds the comment of the event',
00434                                 ' parsedcomment  - Adds the parsed comment of the event',
00435                                 ' details        - Lists addtional details about the event',
00436                                 ' tags           - Lists tags for the event',
00437                         ),
00438                         'type' => 'Filter log entries to only this type',
00439                         'action' => "Filter log actions to only this type. Overrides {$p}type",
00440                         'start' => 'The timestamp to start enumerating from',
00441                         'end' => 'The timestamp to end enumerating',
00442                         'dir' => $this->getDirectionDescription( $p ),
00443                         'user' => 'Filter entries to those made by the given user',
00444                         'title' => 'Filter entries to those related to a page',
00445                         'prefix' => 'Filter entries that start with this prefix. Disabled in Miser Mode',
00446                         'limit' => 'How many total event entries to return',
00447                         'tag' => 'Only list event entries tagged with this tag',
00448                 );
00449         }
00450 
00451         public function getResultProperties() {
00452                 global $wgLogTypes;
00453                 return array(
00454                         'ids' => array(
00455                                 'logid' => 'integer',
00456                                 'pageid' => 'integer'
00457                         ),
00458                         'title' => array(
00459                                 'ns' => 'namespace',
00460                                 'title' => 'string'
00461                         ),
00462                         'type' => array(
00463                                 'type' => array(
00464                                         ApiBase::PROP_TYPE => $wgLogTypes
00465                                 ),
00466                                 'action' => 'string'
00467                         ),
00468                         'details' => array(
00469                                 'actionhidden' => 'boolean'
00470                         ),
00471                         'user' => array(
00472                                 'userhidden' => 'boolean',
00473                                 'user' => array(
00474                                         ApiBase::PROP_TYPE => 'string',
00475                                         ApiBase::PROP_NULLABLE => true
00476                                 ),
00477                                 'anon' => 'boolean'
00478                         ),
00479                         'userid' => array(
00480                                 'userhidden' => 'boolean',
00481                                 'userid' => array(
00482                                         ApiBase::PROP_TYPE => 'integer',
00483                                         ApiBase::PROP_NULLABLE => true
00484                                 ),
00485                                 'anon' => 'boolean'
00486                         ),
00487                         'timestamp' => array(
00488                                 'timestamp' => 'timestamp'
00489                         ),
00490                         'comment' => array(
00491                                 'commenthidden' => 'boolean',
00492                                 'comment' => array(
00493                                         ApiBase::PROP_TYPE => 'string',
00494                                         ApiBase::PROP_NULLABLE => true
00495                                 )
00496                         ),
00497                         'parsedcomment' => array(
00498                                 'commenthidden' => 'boolean',
00499                                 'parsedcomment' => array(
00500                                         ApiBase::PROP_TYPE => 'string',
00501                                         ApiBase::PROP_NULLABLE => true
00502                                 )
00503                         )
00504                 );
00505         }
00506 
00507         public function getDescription() {
00508                 return 'Get events from logs';
00509         }
00510 
00511         public function getPossibleErrors() {
00512                 return array_merge( parent::getPossibleErrors(), array(
00513                         array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
00514                         array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
00515                         array( 'code' => 'param_prefix', 'info' => 'Bad title value \'prefix\'' ),
00516                         array( 'code' => 'prefixsearchdisabled', 'info' => 'Prefix search disabled in Miser Mode' ),
00517                 ) );
00518         }
00519 
00520         public function getExamples() {
00521                 return array(
00522                         'api.php?action=query&list=logevents'
00523                 );
00524         }
00525 
00526         public function getHelpUrls() {
00527                 return 'https://www.mediawiki.org/wiki/API:Logevents';
00528         }
00529 
00530         public function getVersion() {
00531                 return __CLASS__ . ': $Id$';
00532         }
00533 }