MediaWiki  REL1_21
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, 'user', $this->getUser() );
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                                 if( $legacy ) {
00245                                         list( $vals2['old'], $vals2['new'] ) = $params;
00246                                 } else {
00247                                         $vals2['new'] = implode( ', ', $params['5::newgroups'] );
00248                                         $vals2['old'] = implode( ', ', $params['4::oldgroups'] );
00249                                 }
00250                                 $vals[$type] = $vals2;
00251                                 $params = null;
00252                                 break;
00253                         case 'block':
00254                                 if ( $action == 'unblock' ) {
00255                                         break;
00256                                 }
00257                                 $vals2 = array();
00258                                 list( $vals2['duration'], $vals2['flags'] ) = $params;
00259 
00260                                 // Indefinite blocks have no expiry time
00261                                 if ( SpecialBlock::parseExpiryInput( $params[0] ) !== wfGetDB( DB_SLAVE )->getInfinity() ) {
00262                                         $vals2['expiry'] = wfTimestamp( TS_ISO_8601,
00263                                                 strtotime( $params[0], wfTimestamp( TS_UNIX, $ts ) ) );
00264                                 }
00265                                 $vals[$type] = $vals2;
00266                                 $params = null;
00267                                 break;
00268                 }
00269                 if ( !is_null( $params ) ) {
00270                         $logParams = array();
00271                         // Keys like "4::paramname" can't be used for output so we change them to "paramname"
00272                         foreach ( $params as $key => $value ) {
00273                                 if ( strpos( $key, ':' ) === false ) {
00274                                         $logParams[$key] = $value;
00275                                         continue;
00276                                 }
00277                                 $logParam = explode( ':', $key, 3 );
00278                                 $logParams[$logParam[2]] = $value;
00279                         }
00280                         $result->setIndexedTagName( $logParams, 'param' );
00281                         $result->setIndexedTagName_recursive( $logParams, 'param' );
00282                         $vals = array_merge( $vals, $logParams );
00283                 }
00284                 return $vals;
00285         }
00286 
00287         private function extractRowInfo( $row ) {
00288                 $logEntry = DatabaseLogEntry::newFromRow( $row );
00289                 $vals = array();
00290 
00291                 if ( $this->fld_ids ) {
00292                         $vals['logid'] = intval( $row->log_id );
00293                 }
00294 
00295                 if ( $this->fld_title || $this->fld_parsedcomment ) {
00296                         $title = Title::makeTitle( $row->log_namespace, $row->log_title );
00297                 }
00298 
00299                 if ( $this->fld_title || $this->fld_ids ) {
00300                         if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
00301                                 $vals['actionhidden'] = '';
00302                         } else {
00303                                 if ( $this->fld_title ) {
00304                                         ApiQueryBase::addTitleInfo( $vals, $title );
00305                                 }
00306                                 if ( $this->fld_ids ) {
00307                                         $vals['pageid'] = intval( $row->page_id );
00308                                 }
00309                         }
00310                 }
00311 
00312                 if ( $this->fld_type || $this->fld_action ) {
00313                         $vals['type'] = $row->log_type;
00314                         $vals['action'] = $row->log_action;
00315                 }
00316 
00317                 if ( $this->fld_details && $row->log_params !== '' ) {
00318                         if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
00319                                 $vals['actionhidden'] = '';
00320                         } else {
00321                                 self::addLogParams(
00322                                         $this->getResult(),
00323                                         $vals,
00324                                         $logEntry->getParameters(),
00325                                         $logEntry->getType(),
00326                                         $logEntry->getSubtype(),
00327                                         $logEntry->getTimestamp(),
00328                                         $logEntry->isLegacy()
00329                                 );
00330                         }
00331                 }
00332 
00333                 if ( $this->fld_user || $this->fld_userid ) {
00334                         if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
00335                                 $vals['userhidden'] = '';
00336                         } else {
00337                                 if ( $this->fld_user ) {
00338                                         $vals['user'] = $row->user_name;
00339                                 }
00340                                 if ( $this->fld_userid ) {
00341                                         $vals['userid'] = $row->user_id;
00342                                 }
00343 
00344                                 if ( !$row->log_user ) {
00345                                         $vals['anon'] = '';
00346                                 }
00347                         }
00348                 }
00349                 if ( $this->fld_timestamp ) {
00350                         $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->log_timestamp );
00351                 }
00352 
00353                 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->log_comment ) ) {
00354                         if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
00355                                 $vals['commenthidden'] = '';
00356                         } else {
00357                                 if ( $this->fld_comment ) {
00358                                         $vals['comment'] = $row->log_comment;
00359                                 }
00360 
00361                                 if ( $this->fld_parsedcomment ) {
00362                                         $vals['parsedcomment'] = Linker::formatComment( $row->log_comment, $title );
00363                                 }
00364                         }
00365                 }
00366 
00367                 if ( $this->fld_tags ) {
00368                         if ( $row->ts_tags ) {
00369                                 $tags = explode( ',', $row->ts_tags );
00370                                 $this->getResult()->setIndexedTagName( $tags, 'tag' );
00371                                 $vals['tags'] = $tags;
00372                         } else {
00373                                 $vals['tags'] = array();
00374                         }
00375                 }
00376 
00377                 return $vals;
00378         }
00379 
00380         public function getCacheMode( $params ) {
00381                 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
00382                         // formatComment() calls wfMessage() among other things
00383                         return 'anon-public-user-private';
00384                 } elseif ( LogEventsList::getExcludeClause( $this->getDB(), 'user', $this->getUser() )
00385                         === LogEventsList::getExcludeClause( $this->getDB(), 'public' )
00386                 ) { // Output can only contain public data.
00387                         return 'public';
00388                 } else {
00389                         return 'anon-public-user-private';
00390                 }
00391         }
00392 
00393         public function getAllowedParams() {
00394                 global $wgLogTypes, $wgLogActions, $wgLogActionsHandlers;
00395                 return array(
00396                         'prop' => array(
00397                                 ApiBase::PARAM_ISMULTI => true,
00398                                 ApiBase::PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
00399                                 ApiBase::PARAM_TYPE => array(
00400                                         'ids',
00401                                         'title',
00402                                         'type',
00403                                         'user',
00404                                         'userid',
00405                                         'timestamp',
00406                                         'comment',
00407                                         'parsedcomment',
00408                                         'details',
00409                                         'tags'
00410                                 )
00411                         ),
00412                         'type' => array(
00413                                 ApiBase::PARAM_TYPE => $wgLogTypes
00414                         ),
00415                         'action' => array(
00416                                 ApiBase::PARAM_TYPE => array_keys( array_merge( $wgLogActions, $wgLogActionsHandlers ) )
00417                         ),
00418                         'start' => array(
00419                                 ApiBase::PARAM_TYPE => 'timestamp'
00420                         ),
00421                         'end' => array(
00422                                 ApiBase::PARAM_TYPE => 'timestamp'
00423                         ),
00424                         'dir' => array(
00425                                 ApiBase::PARAM_DFLT => 'older',
00426                                 ApiBase::PARAM_TYPE => array(
00427                                         'newer',
00428                                         'older'
00429                                 )
00430                         ),
00431                         'user' => null,
00432                         'title' => null,
00433                         'prefix' => null,
00434                         'tag' => null,
00435                         'limit' => array(
00436                                 ApiBase::PARAM_DFLT => 10,
00437                                 ApiBase::PARAM_TYPE => 'limit',
00438                                 ApiBase::PARAM_MIN => 1,
00439                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00440                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00441                         )
00442                 );
00443         }
00444 
00445         public function getParamDescription() {
00446                 $p = $this->getModulePrefix();
00447                 return array(
00448                         'prop' => array(
00449                                 'Which properties to get',
00450                                 ' ids            - Adds the ID of the log event',
00451                                 ' title          - Adds the title of the page for the log event',
00452                                 ' type           - Adds the type of log event',
00453                                 ' user           - Adds the user responsible for the log event',
00454                                 ' userid         - Adds the user ID who was responsible for the log event',
00455                                 ' timestamp      - Adds the timestamp for the event',
00456                                 ' comment        - Adds the comment of the event',
00457                                 ' parsedcomment  - Adds the parsed comment of the event',
00458                                 ' details        - Lists additional details about the event',
00459                                 ' tags           - Lists tags for the event',
00460                         ),
00461                         'type' => 'Filter log entries to only this type',
00462                         'action' => "Filter log actions to only this type. Overrides {$p}type",
00463                         'start' => 'The timestamp to start enumerating from',
00464                         'end' => 'The timestamp to end enumerating',
00465                         'dir' => $this->getDirectionDescription( $p ),
00466                         'user' => 'Filter entries to those made by the given user',
00467                         'title' => 'Filter entries to those related to a page',
00468                         'prefix' => 'Filter entries that start with this prefix. Disabled in Miser Mode',
00469                         'limit' => 'How many total event entries to return',
00470                         'tag' => 'Only list event entries tagged with this tag',
00471                 );
00472         }
00473 
00474         public function getResultProperties() {
00475                 global $wgLogTypes;
00476                 return array(
00477                         'ids' => array(
00478                                 'logid' => 'integer',
00479                                 'pageid' => 'integer'
00480                         ),
00481                         'title' => array(
00482                                 'ns' => 'namespace',
00483                                 'title' => 'string'
00484                         ),
00485                         'type' => array(
00486                                 'type' => array(
00487                                         ApiBase::PROP_TYPE => $wgLogTypes
00488                                 ),
00489                                 'action' => 'string'
00490                         ),
00491                         'details' => array(
00492                                 'actionhidden' => 'boolean'
00493                         ),
00494                         'user' => array(
00495                                 'userhidden' => 'boolean',
00496                                 'user' => array(
00497                                         ApiBase::PROP_TYPE => 'string',
00498                                         ApiBase::PROP_NULLABLE => true
00499                                 ),
00500                                 'anon' => 'boolean'
00501                         ),
00502                         'userid' => array(
00503                                 'userhidden' => 'boolean',
00504                                 'userid' => array(
00505                                         ApiBase::PROP_TYPE => 'integer',
00506                                         ApiBase::PROP_NULLABLE => true
00507                                 ),
00508                                 'anon' => 'boolean'
00509                         ),
00510                         'timestamp' => array(
00511                                 'timestamp' => 'timestamp'
00512                         ),
00513                         'comment' => array(
00514                                 'commenthidden' => 'boolean',
00515                                 'comment' => array(
00516                                         ApiBase::PROP_TYPE => 'string',
00517                                         ApiBase::PROP_NULLABLE => true
00518                                 )
00519                         ),
00520                         'parsedcomment' => array(
00521                                 'commenthidden' => 'boolean',
00522                                 'parsedcomment' => array(
00523                                         ApiBase::PROP_TYPE => 'string',
00524                                         ApiBase::PROP_NULLABLE => true
00525                                 )
00526                         )
00527                 );
00528         }
00529 
00530         public function getDescription() {
00531                 return 'Get events from logs';
00532         }
00533 
00534         public function getPossibleErrors() {
00535                 return array_merge( parent::getPossibleErrors(), array(
00536                         array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
00537                         array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
00538                         array( 'code' => 'param_prefix', 'info' => 'Bad title value \'prefix\'' ),
00539                         array( 'code' => 'prefixsearchdisabled', 'info' => 'Prefix search disabled in Miser Mode' ),
00540                 ) );
00541         }
00542 
00543         public function getExamples() {
00544                 return array(
00545                         'api.php?action=query&list=logevents'
00546                 );
00547         }
00548 
00549         public function getHelpUrls() {
00550                 return 'https://www.mediawiki.org/wiki/API:Logevents';
00551         }
00552 }