From c1f9b1f7b1b77776192048005dcc66dcf3df2bfb Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Sat, 27 Dec 2014 15:41:37 +0100 Subject: Update to MediaWiki 1.24.1 --- includes/api/ApiQueryLogEvents.php | 289 ++++++++++++++++++++----------------- 1 file changed, 156 insertions(+), 133 deletions(-) (limited to 'includes/api/ApiQueryLogEvents.php') diff --git a/includes/api/ApiQueryLogEvents.php b/includes/api/ApiQueryLogEvents.php index ecd117e4..d9dbb5e6 100644 --- a/includes/api/ApiQueryLogEvents.php +++ b/includes/api/ApiQueryLogEvents.php @@ -31,7 +31,7 @@ */ class ApiQueryLogEvents extends ApiQueryBase { - public function __construct( $query, $moduleName ) { + public function __construct( ApiQuery $query, $moduleName ) { parent::__construct( $query, $moduleName, 'le' ); } @@ -43,6 +43,7 @@ class ApiQueryLogEvents extends ApiQueryBase { public function execute() { $params = $this->extractRequestParams(); $db = $this->getDB(); + $this->requireMaxOneParameter( $params, 'title', 'prefix', 'namespace' ); $prop = array_flip( $params['prop'] ); @@ -64,26 +65,32 @@ class ApiQueryLogEvents extends ApiQueryBase { // Order is significant here $this->addTables( array( 'logging', 'user', 'page' ) ); - $this->addOption( 'STRAIGHT_JOIN' ); $this->addJoinConds( array( 'user' => array( 'LEFT JOIN', 'user_id=log_user' ), 'page' => array( 'LEFT JOIN', array( 'log_namespace=page_namespace', 'log_title=page_title' ) ) ) ); - $index = array( 'logging' => 'times' ); // default, may change $this->addFields( array( + 'log_id', 'log_type', 'log_action', 'log_timestamp', 'log_deleted', ) ); - $this->addFieldsIf( array( 'log_id', 'page_id' ), $this->fld_ids ); + $this->addFieldsIf( 'page_id', $this->fld_ids ); + // log_page is the page_id saved at log time, whereas page_id is from a + // join at query time. This leads to different results in various + // scenarios, e.g. deletion, recreation. + $this->addFieldsIf( 'log_page', $this->fld_ids ); $this->addFieldsIf( array( 'log_user', 'log_user_text', 'user_name' ), $this->fld_user ); $this->addFieldsIf( 'log_user', $this->fld_userid ); - $this->addFieldsIf( array( 'log_namespace', 'log_title' ), $this->fld_title || $this->fld_parsedcomment ); + $this->addFieldsIf( + array( 'log_namespace', 'log_title' ), + $this->fld_title || $this->fld_parsedcomment + ); $this->addFieldsIf( 'log_comment', $this->fld_comment || $this->fld_parsedcomment ); $this->addFieldsIf( 'log_params', $this->fld_details ); @@ -95,21 +102,59 @@ class ApiQueryLogEvents extends ApiQueryBase { if ( !is_null( $params['tag'] ) ) { $this->addTables( 'change_tag' ); - $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'log_id=ct_log_id' ) ) ) ); + $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', + array( 'log_id=ct_log_id' ) ) ) ); $this->addWhereFld( 'ct_tag', $params['tag'] ); - $index['change_tag'] = 'change_tag_tag_id'; } if ( !is_null( $params['action'] ) ) { - list( $type, $action ) = explode( '/', $params['action'] ); + // Do validation of action param, list of allowed actions can contains wildcards + // Allow the param, when the actions is in the list or a wildcard version is listed. + $logAction = $params['action']; + if ( strpos( $logAction, '/' ) === false ) { + // all items in the list have a slash + $valid = false; + } else { + $logActions = array_flip( $this->getAllowedLogActions() ); + list( $type, $action ) = explode( '/', $logAction, 2 ); + $valid = isset( $logActions[$logAction] ) || isset( $logActions[$type . '/*'] ); + } + + if ( !$valid ) { + $valueName = $this->encodeParamName( 'action' ); + $this->dieUsage( + "Unrecognized value for parameter '$valueName': {$logAction}", + "unknown_$valueName" + ); + } + $this->addWhereFld( 'log_type', $type ); $this->addWhereFld( 'log_action', $action ); } elseif ( !is_null( $params['type'] ) ) { $this->addWhereFld( 'log_type', $params['type'] ); - $index['logging'] = 'type_time'; } - $this->addTimestampWhereRange( 'log_timestamp', $params['dir'], $params['start'], $params['end'] ); + $this->addTimestampWhereRange( + 'log_timestamp', + $params['dir'], + $params['start'], + $params['end'] + ); + // Include in ORDER BY for uniqueness + $this->addWhereRange( 'log_id', $params['dir'], null, null ); + + if ( !is_null( $params['continue'] ) ) { + $cont = explode( '|', $params['continue'] ); + $this->dieContinueUsageIf( count( $cont ) != 2 ); + $op = ( $params['dir'] === 'newer' ? '>' : '<' ); + $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) ); + $continueId = (int)$cont[1]; + $this->dieContinueUsageIf( $continueId != $cont[1] ); + $this->addWhere( "log_timestamp $op $continueTimestamp OR " . + "(log_timestamp = $continueTimestamp AND " . + "log_id $op= $continueId)" + ); + } $limit = $params['limit']; $this->addOption( 'LIMIT', $limit + 1 ); @@ -117,11 +162,11 @@ class ApiQueryLogEvents extends ApiQueryBase { $user = $params['user']; if ( !is_null( $user ) ) { $userid = User::idFromName( $user ); - if ( !$userid ) { - $this->dieUsage( "User name $user not found", 'param_user' ); + if ( $userid ) { + $this->addWhereFld( 'log_user', $userid ); + } else { + $this->addWhereFld( 'log_user_text', IP::sanitizeIP( $user ) ); } - $this->addWhereFld( 'log_user', $userid ); - $index['logging'] = 'user_time'; } $title = $params['title']; @@ -132,16 +177,16 @@ class ApiQueryLogEvents extends ApiQueryBase { } $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() ); $this->addWhereFld( 'log_title', $titleObj->getDBkey() ); + } - // Use the title index in preference to the user index if there is a conflict - $index['logging'] = is_null( $user ) ? 'page_time' : array( 'page_time', 'user_time' ); + if ( $params['namespace'] !== null ) { + $this->addWhereFld( 'log_namespace', $params['namespace'] ); } $prefix = $params['prefix']; if ( !is_null( $prefix ) ) { - global $wgMiserMode; - if ( $wgMiserMode ) { + if ( $this->getConfig()->get( 'MiserMode' ) ) { $this->dieUsage( 'Prefix search disabled in Miser Mode', 'prefixsearchdisabled' ); } @@ -153,23 +198,34 @@ class ApiQueryLogEvents extends ApiQueryBase { $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) ); } - $this->addOption( 'USE INDEX', $index ); - // Paranoia: avoid brute force searches (bug 17342) - if ( !is_null( $title ) ) { - $this->addWhere( $db->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0' ); - } - if ( !is_null( $user ) ) { - $this->addWhere( $db->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0' ); + if ( $params['namespace'] !== null || !is_null( $title ) || !is_null( $user ) ) { + if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) { + $titleBits = LogPage::DELETED_ACTION; + $userBits = LogPage::DELETED_USER; + } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) { + $titleBits = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED; + $userBits = LogPage::DELETED_USER | LogPage::DELETED_RESTRICTED; + } else { + $titleBits = 0; + $userBits = 0; + } + if ( ( $params['namespace'] !== null || !is_null( $title ) ) && $titleBits ) { + $this->addWhere( $db->bitAnd( 'log_deleted', $titleBits ) . " != $titleBits" ); + } + if ( !is_null( $user ) && $userBits ) { + $this->addWhere( $db->bitAnd( 'log_deleted', $userBits ) . " != $userBits" ); + } } $count = 0; $res = $this->select( __METHOD__ ); $result = $this->getResult(); foreach ( $res as $row ) { - if ( ++ $count > $limit ) { - // We've reached the one extra which shows that there are additional pages to be had. Stop here... - $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) ); + if ( ++$count > $limit ) { + // We've reached the one extra which shows that there are + // additional pages to be had. Stop here... + $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" ); break; } @@ -179,7 +235,7 @@ class ApiQueryLogEvents extends ApiQueryBase { } $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals ); if ( !$fit ) { - $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) ); + $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" ); break; } } @@ -187,16 +243,18 @@ class ApiQueryLogEvents extends ApiQueryBase { } /** - * @param $result ApiResult - * @param $vals array - * @param $params string - * @param $type string - * @param $action string - * @param $ts - * @param $legacy bool + * @param ApiResult $result + * @param array $vals + * @param string $params + * @param string $type + * @param string $action + * @param string $ts + * @param bool $legacy * @return array */ - public static function addLogParams( $result, &$vals, $params, $type, $action, $ts, $legacy = false ) { + public static function addLogParams( $result, &$vals, $params, $type, + $action, $ts, $legacy = false + ) { switch ( $type ) { case 'move': if ( $legacy ) { @@ -284,12 +342,15 @@ class ApiQueryLogEvents extends ApiQueryBase { $result->setIndexedTagName_recursive( $logParams, 'param' ); $vals = array_merge( $vals, $logParams ); } + return $vals; } private function extractRowInfo( $row ) { $logEntry = DatabaseLogEntry::newFromRow( $row ); $vals = array(); + $anyHidden = false; + $user = $this->getUser(); if ( $this->fld_ids ) { $vals['logid'] = intval( $row->log_id ); @@ -299,18 +360,29 @@ class ApiQueryLogEvents extends ApiQueryBase { $title = Title::makeTitle( $row->log_namespace, $row->log_title ); } - if ( $this->fld_title || $this->fld_ids ) { + if ( $this->fld_title || $this->fld_ids || $this->fld_details && $row->log_params !== '' ) { if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) { $vals['actionhidden'] = ''; - } else { - if ( $this->fld_type ) { - $vals['action'] = $row->log_action; - } + $anyHidden = true; + } + if ( LogEventsList::userCan( $row, LogPage::DELETED_ACTION, $user ) ) { if ( $this->fld_title ) { ApiQueryBase::addTitleInfo( $vals, $title ); } if ( $this->fld_ids ) { $vals['pageid'] = intval( $row->page_id ); + $vals['logpage'] = intval( $row->log_page ); + } + if ( $this->fld_details && $row->log_params !== '' ) { + self::addLogParams( + $this->getResult(), + $vals, + $logEntry->getParameters(), + $logEntry->getType(), + $logEntry->getSubtype(), + $logEntry->getTimestamp(), + $logEntry->isLegacy() + ); } } } @@ -320,31 +392,17 @@ class ApiQueryLogEvents extends ApiQueryBase { $vals['action'] = $row->log_action; } - if ( $this->fld_details && $row->log_params !== '' ) { - if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) { - $vals['actionhidden'] = ''; - } else { - self::addLogParams( - $this->getResult(), - $vals, - $logEntry->getParameters(), - $logEntry->getType(), - $logEntry->getSubtype(), - $logEntry->getTimestamp(), - $logEntry->isLegacy() - ); - } - } - if ( $this->fld_user || $this->fld_userid ) { if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) { $vals['userhidden'] = ''; - } else { + $anyHidden = true; + } + if ( LogEventsList::userCan( $row, LogPage::DELETED_USER, $user ) ) { if ( $this->fld_user ) { $vals['user'] = $row->user_name === null ? $row->log_user_text : $row->user_name; } if ( $this->fld_userid ) { - $vals['userid'] = $row->log_user; + $vals['userid'] = intval( $row->log_user ); } if ( !$row->log_user ) { @@ -359,7 +417,9 @@ class ApiQueryLogEvents extends ApiQueryBase { if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->log_comment ) ) { if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) { $vals['commenthidden'] = ''; - } else { + $anyHidden = true; + } + if ( LogEventsList::userCan( $row, LogPage::DELETED_COMMENT, $user ) ) { if ( $this->fld_comment ) { $vals['comment'] = $row->log_comment; } @@ -380,10 +440,25 @@ class ApiQueryLogEvents extends ApiQueryBase { } } + if ( $anyHidden && LogEventsList::isDeleted( $row, LogPage::DELETED_RESTRICTED ) ) { + $vals['suppressed'] = ''; + } + return $vals; } + /** + * @return array + */ + private function getAllowedLogActions() { + $config = $this->getConfig(); + return array_keys( array_merge( $config->get( 'LogActions' ), $config->get( 'LogActionsHandlers' ) ) ); + } + public function getCacheMode( $params ) { + if ( $this->userCanSeeRevDel() ) { + return 'private'; + } if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) { // formatComment() calls wfMessage() among other things return 'anon-public-user-private'; @@ -396,8 +471,8 @@ class ApiQueryLogEvents extends ApiQueryBase { } } - public function getAllowedParams() { - global $wgLogTypes, $wgLogActions, $wgLogActionsHandlers; + public function getAllowedParams( $flags = 0 ) { + $config = $this->getConfig(); return array( 'prop' => array( ApiBase::PARAM_ISMULTI => true, @@ -416,10 +491,13 @@ class ApiQueryLogEvents extends ApiQueryBase { ) ), 'type' => array( - ApiBase::PARAM_TYPE => $wgLogTypes + ApiBase::PARAM_TYPE => $config->get( 'LogTypes' ) ), 'action' => array( - ApiBase::PARAM_TYPE => array_keys( array_merge( $wgLogActions, $wgLogActionsHandlers ) ) + // validation on request is done in execute() + ApiBase::PARAM_TYPE => ( $flags & ApiBase::GET_VALUES_FOR_HELP ) + ? $this->getAllowedLogActions() + : null ), 'start' => array( ApiBase::PARAM_TYPE => 'timestamp' @@ -436,6 +514,9 @@ class ApiQueryLogEvents extends ApiQueryBase { ), 'user' => null, 'title' => null, + 'namespace' => array( + ApiBase::PARAM_TYPE => 'namespace' + ), 'prefix' => null, 'tag' => null, 'limit' => array( @@ -444,12 +525,14 @@ class ApiQueryLogEvents extends ApiQueryBase { ApiBase::PARAM_MIN => 1, ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 - ) + ), + 'continue' => null, ); } public function getParamDescription() { $p = $this->getModulePrefix(); + return array( 'prop' => array( 'Which properties to get', @@ -465,85 +548,25 @@ class ApiQueryLogEvents extends ApiQueryBase { ' tags - Lists tags for the event', ), 'type' => 'Filter log entries to only this type', - 'action' => "Filter log actions to only this type. Overrides {$p}type", + 'action' => array( + "Filter log actions to only this action. Overrides {$p}type", + "Wildcard actions like 'action/*' allows to specify any string for the asterisk" + ), 'start' => 'The timestamp to start enumerating from', 'end' => 'The timestamp to end enumerating', 'dir' => $this->getDirectionDescription( $p ), 'user' => 'Filter entries to those made by the given user', 'title' => 'Filter entries to those related to a page', + 'namespace' => 'Filter entries to those in the given namespace', 'prefix' => 'Filter entries that start with this prefix. Disabled in Miser Mode', 'limit' => 'How many total event entries to return', 'tag' => 'Only list event entries tagged with this tag', - ); - } - - public function getResultProperties() { - global $wgLogTypes; - return array( - 'ids' => array( - 'logid' => 'integer', - 'pageid' => 'integer' - ), - 'title' => array( - 'ns' => 'namespace', - 'title' => 'string' - ), - 'type' => array( - 'type' => array( - ApiBase::PROP_TYPE => $wgLogTypes - ), - 'action' => 'string' - ), - 'details' => array( - 'actionhidden' => 'boolean' - ), - 'user' => array( - 'userhidden' => 'boolean', - 'user' => array( - ApiBase::PROP_TYPE => 'string', - ApiBase::PROP_NULLABLE => true - ), - 'anon' => 'boolean' - ), - 'userid' => array( - 'userhidden' => 'boolean', - 'userid' => array( - ApiBase::PROP_TYPE => 'integer', - ApiBase::PROP_NULLABLE => true - ), - 'anon' => 'boolean' - ), - 'timestamp' => array( - 'timestamp' => 'timestamp' - ), - 'comment' => array( - 'commenthidden' => 'boolean', - 'comment' => array( - ApiBase::PROP_TYPE => 'string', - ApiBase::PROP_NULLABLE => true - ) - ), - 'parsedcomment' => array( - 'commenthidden' => 'boolean', - 'parsedcomment' => array( - ApiBase::PROP_TYPE => 'string', - ApiBase::PROP_NULLABLE => true - ) - ) + 'continue' => 'When more results are available, use this to continue', ); } public function getDescription() { - return 'Get events from logs'; - } - - public function getPossibleErrors() { - return array_merge( parent::getPossibleErrors(), array( - array( 'code' => 'param_user', 'info' => 'User name $user not found' ), - array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ), - array( 'code' => 'param_prefix', 'info' => 'Bad title value \'prefix\'' ), - array( 'code' => 'prefixsearchdisabled', 'info' => 'Prefix search disabled in Miser Mode' ), - ) ); + return 'Get events from logs.'; } public function getExamples() { -- cgit v1.2.3-54-g00ecf