MediaWiki  REL1_19
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 
00200         public static function addLogParams( $result, &$vals, $params, $type, $action, $ts, $legacy = false ) {
00201                 switch ( $type ) {
00202                         case 'move':
00203                                 if ( $legacy ){
00204                                         $targetKey = 0;
00205                                         $noredirKey = 1;
00206                                 } else {
00207                                         $targetKey = '4::target';
00208                                         $noredirKey = '5::noredir';
00209                                 }
00210 
00211                                 if ( isset( $params[ $targetKey ] ) ) {
00212                                         $title = Title::newFromText( $params[ $targetKey ] );
00213                                         if ( $title ) {
00214                                                 $vals2 = array();
00215                                                 ApiQueryBase::addTitleInfo( $vals2, $title, 'new_' );
00216                                                 $vals[$type] = $vals2;
00217                                         }
00218                                 }
00219                                 if ( isset( $params[ $noredirKey ] ) && $params[ $noredirKey ] ) {
00220                                         $vals[$type]['suppressedredirect'] = '';
00221                                 }
00222                                 $params = null;
00223                                 break;
00224                         case 'patrol':
00225                                 if ( $legacy ){
00226                                         $cur = 0;
00227                                         $prev = 1;
00228                                         $auto = 2;
00229                                 } else {
00230                                         $cur = '4::curid';
00231                                         $prev = '5::previd';
00232                                         $auto = '6::auto';
00233                                 }
00234                                 $vals2 = array();
00235                                 $vals2['cur'] = $params[$cur];
00236                                 $vals2['prev'] = $params[$prev];
00237                                 $vals2['auto'] = $params[$auto];
00238                                 $vals[$type] = $vals2;
00239                                 $params = null;
00240                                 break;
00241                         case 'rights':
00242                                 $vals2 = array();
00243                                 list( $vals2['old'], $vals2['new'] ) = $params;
00244                                 $vals[$type] = $vals2;
00245                                 $params = null;
00246                                 break;
00247                         case 'block':
00248                                 if ( $action == 'unblock' ) {
00249                                         break;
00250                                 }
00251                                 $vals2 = array();
00252                                 list( $vals2['duration'], $vals2['flags'] ) = $params;
00253 
00254                                 // Indefinite blocks have no expiry time
00255                                 if ( SpecialBlock::parseExpiryInput( $params[0] ) !== wfGetDB( DB_SLAVE )->getInfinity() ) {
00256                                         $vals2['expiry'] = wfTimestamp( TS_ISO_8601,
00257                                                 strtotime( $params[0], wfTimestamp( TS_UNIX, $ts ) ) );
00258                                 }
00259                                 $vals[$type] = $vals2;
00260                                 $params = null;
00261                                 break;
00262                 }
00263                 if ( !is_null( $params ) ) {
00264                         $result->setIndexedTagName( $params, 'param' );
00265                         $vals = array_merge( $vals, $params );
00266                 }
00267                 return $vals;
00268         }
00269 
00270         private function extractRowInfo( $row ) {
00271                 $logEntry = DatabaseLogEntry::newFromRow( $row );
00272                 $vals = array();
00273 
00274                 if ( $this->fld_ids ) {
00275                         $vals['logid'] = intval( $row->log_id );
00276                 }
00277 
00278                 if ( $this->fld_title || $this->fld_parsedcomment ) {
00279                         $title = Title::makeTitle( $row->log_namespace, $row->log_title );
00280                 }
00281 
00282                 if ( $this->fld_title || $this->fld_ids ) {
00283                         if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
00284                                 $vals['actionhidden'] = '';
00285                         } else {
00286                                 if ( $this->fld_title ) {
00287                                         ApiQueryBase::addTitleInfo( $vals, $title );
00288                                 }
00289                                 if ( $this->fld_ids ) {
00290                                         $vals['pageid'] = intval( $row->page_id );
00291                                 }
00292                         }
00293                 }
00294 
00295                 if ( $this->fld_type || $this->fld_action ) {
00296                         $vals['type'] = $row->log_type;
00297                         $vals['action'] = $row->log_action;
00298                 }
00299 
00300                 if ( $this->fld_details && $row->log_params !== '' ) {
00301                         if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
00302                                 $vals['actionhidden'] = '';
00303                         } else {
00304                                 self::addLogParams(
00305                                         $this->getResult(),
00306                                         $vals,
00307                                         $logEntry->getParameters(),
00308                                         $logEntry->getType(),
00309                                         $logEntry->getSubtype(),
00310                                         $logEntry->getTimestamp(),
00311                                         $logEntry->isLegacy()
00312                                 );
00313                         }
00314                 }
00315 
00316                 if ( $this->fld_user || $this->fld_userid ) {
00317                         if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
00318                                 $vals['userhidden'] = '';
00319                         } else {
00320                                 if ( $this->fld_user ) {
00321                                         $vals['user'] = $row->user_name;
00322                                 }
00323                                 if ( $this->fld_userid ) {
00324                                         $vals['userid'] = $row->user_id;
00325                                 }
00326 
00327                                 if ( !$row->log_user ) {
00328                                         $vals['anon'] = '';
00329                                 }
00330                         }
00331                 }
00332                 if ( $this->fld_timestamp ) {
00333                         $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->log_timestamp );
00334                 }
00335 
00336                 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->log_comment ) ) {
00337                         if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
00338                                 $vals['commenthidden'] = '';
00339                         } else {
00340                                 if ( $this->fld_comment ) {
00341                                         $vals['comment'] = $row->log_comment;
00342                                 }
00343 
00344                                 if ( $this->fld_parsedcomment ) {
00345                                         $vals['parsedcomment'] = Linker::formatComment( $row->log_comment, $title );
00346                                 }
00347                         }
00348                 }
00349 
00350                 if ( $this->fld_tags ) {
00351                         if ( $row->ts_tags ) {
00352                                 $tags = explode( ',', $row->ts_tags );
00353                                 $this->getResult()->setIndexedTagName( $tags, 'tag' );
00354                                 $vals['tags'] = $tags;
00355                         } else {
00356                                 $vals['tags'] = array();
00357                         }
00358                 }
00359 
00360                 return $vals;
00361         }
00362 
00363         public function getCacheMode( $params ) {
00364                 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
00365                         // formatComment() calls wfMsg() among other things
00366                         return 'anon-public-user-private';
00367                 } else {
00368                         return 'public';
00369                 }
00370         }
00371 
00372         public function getAllowedParams() {
00373                 global $wgLogTypes, $wgLogActions;
00374                 return array(
00375                         'prop' => array(
00376                                 ApiBase::PARAM_ISMULTI => true,
00377                                 ApiBase::PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
00378                                 ApiBase::PARAM_TYPE => array(
00379                                         'ids',
00380                                         'title',
00381                                         'type',
00382                                         'user',
00383                                         'userid',
00384                                         'timestamp',
00385                                         'comment',
00386                                         'parsedcomment',
00387                                         'details',
00388                                         'tags'
00389                                 )
00390                         ),
00391                         'type' => array(
00392                                 ApiBase::PARAM_TYPE => $wgLogTypes
00393                         ),
00394                         'action' => array(
00395                                 ApiBase::PARAM_TYPE => array_keys( $wgLogActions )
00396                         ),
00397                         'start' => array(
00398                                 ApiBase::PARAM_TYPE => 'timestamp'
00399                         ),
00400                         'end' => array(
00401                                 ApiBase::PARAM_TYPE => 'timestamp'
00402                         ),
00403                         'dir' => array(
00404                                 ApiBase::PARAM_DFLT => 'older',
00405                                 ApiBase::PARAM_TYPE => array(
00406                                         'newer',
00407                                         'older'
00408                                 )
00409                         ),
00410                         'user' => null,
00411                         'title' => null,
00412                         'prefix' => null,
00413                         'tag' => null,
00414                         'limit' => array(
00415                                 ApiBase::PARAM_DFLT => 10,
00416                                 ApiBase::PARAM_TYPE => 'limit',
00417                                 ApiBase::PARAM_MIN => 1,
00418                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00419                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00420                         )
00421                 );
00422         }
00423 
00424         public function getParamDescription() {
00425                 $p = $this->getModulePrefix();
00426                 return array(
00427                         'prop' => array(
00428                                 'Which properties to get',
00429                                 ' ids            - Adds the ID of the log event',
00430                                 ' title          - Adds the title of the page for the log event',
00431                                 ' type           - Adds the type of log event',
00432                                 ' user           - Adds the user responsible for the log event',
00433                                 ' userid         - Adds the user ID who was responsible for the log event',
00434                                 ' timestamp      - Adds the timestamp for the event',
00435                                 ' comment        - Adds the comment of the event',
00436                                 ' parsedcomment  - Adds the parsed comment of the event',
00437                                 ' details        - Lists addtional details about the event',
00438                                 ' tags           - Lists tags for the event',
00439                         ),
00440                         'type' => 'Filter log entries to only this type',
00441                         'action' => "Filter log actions to only this type. Overrides {$p}type",
00442                         'start' => 'The timestamp to start enumerating from',
00443                         'end' => 'The timestamp to end enumerating',
00444                         'dir' => $this->getDirectionDescription( $p ),
00445                         'user' => 'Filter entries to those made by the given user',
00446                         'title' => 'Filter entries to those related to a page',
00447                         'prefix' => 'Filter entries that start with this prefix. Disabled in Miser Mode',
00448                         'limit' => 'How many total event entries to return',
00449                         'tag' => 'Only list event entries tagged with this tag',
00450                 );
00451         }
00452 
00453         public function getDescription() {
00454                 return 'Get events from logs';
00455         }
00456 
00457         public function getPossibleErrors() {
00458                 return array_merge( parent::getPossibleErrors(), array(
00459                         array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
00460                         array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
00461                         array( 'code' => 'param_prefix', 'info' => 'Bad title value \'prefix\'' ),
00462                         array( 'code' => 'prefixsearchdisabled', 'info' => 'Prefix search disabled in Miser Mode' ),
00463                 ) );
00464         }
00465 
00466         public function getExamples() {
00467                 return array(
00468                         'api.php?action=query&list=logevents'
00469                 );
00470         }
00471 
00472         public function getHelpUrls() {
00473                 return 'https://www.mediawiki.org/wiki/API:Logevents';
00474         }
00475 
00476         public function getVersion() {
00477                 return __CLASS__ . ': $Id$';
00478         }
00479 }