Merge "Render namespace lists in the user's language"
[lhc/web/wiklou.git] / includes / api / ApiQueryLogEvents.php
index 9d24794..3cb55e4 100644 (file)
@@ -20,6 +20,9 @@
  * @file
  */
 
+use MediaWiki\MediaWikiServices;
+use MediaWiki\Storage\NameTableAccessException;
+
 /**
  * Query action to List the log events, with optional filtering by various parameters.
  *
@@ -39,9 +42,11 @@ class ApiQueryLogEvents extends ApiQueryBase {
                $fld_details = false, $fld_tags = false;
 
        public function execute() {
+               global $wgChangeTagsSchemaMigrationStage;
+
                $params = $this->extractRequestParams();
                $db = $this->getDB();
-               $this->commentStore = new CommentStore( 'log_comment' );
+               $this->commentStore = CommentStore::getStore();
                $this->requireMaxOneParameter( $params, 'title', 'prefix', 'namespace' );
 
                $prop = array_flip( $params['prop'] );
@@ -62,11 +67,15 @@ class ApiQueryLogEvents extends ApiQueryBase {
                        $this->addWhere( $hideLogs );
                }
 
-               // Order is significant here
-               $this->addTables( [ 'logging', 'user', 'page' ] );
+               $actorMigration = ActorMigration::newMigration();
+               $actorQuery = $actorMigration->getJoin( 'log_user' );
+               $this->addTables( 'logging' );
+               $this->addTables( $actorQuery['tables'] );
+               $this->addTables( [ 'user', 'page' ] );
+               $this->addJoinConds( $actorQuery['joins'] );
                $this->addJoinConds( [
                        'user' => [ 'LEFT JOIN',
-                               'user_id=log_user' ],
+                               'user_id=' . $actorQuery['fields']['log_user'] ],
                        'page' => [ 'LEFT JOIN',
                                [ 'log_namespace=page_namespace',
                                        'log_title=page_title' ] ] ] );
@@ -84,8 +93,8 @@ class ApiQueryLogEvents extends ApiQueryBase {
                // 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( [ 'log_user', 'log_user_text', 'user_name' ], $this->fld_user );
-               $this->addFieldsIf( 'log_user', $this->fld_userid );
+               $this->addFieldsIf( $actorQuery['fields'] + [ 'user_name' ], $this->fld_user );
+               $this->addFieldsIf( $actorQuery['fields'], $this->fld_userid );
                $this->addFieldsIf(
                        [ 'log_namespace', 'log_title' ],
                        $this->fld_title || $this->fld_parsedcomment
@@ -93,7 +102,7 @@ class ApiQueryLogEvents extends ApiQueryBase {
                $this->addFieldsIf( 'log_params', $this->fld_details );
 
                if ( $this->fld_comment || $this->fld_parsedcomment ) {
-                       $commentQuery = $this->commentStore->getJoin();
+                       $commentQuery = $this->commentStore->getJoin( 'log_comment' );
                        $this->addTables( $commentQuery['tables'] );
                        $this->addFields( $commentQuery['fields'] );
                        $this->addJoinConds( $commentQuery['joins'] );
@@ -109,7 +118,17 @@ class ApiQueryLogEvents extends ApiQueryBase {
                        $this->addTables( 'change_tag' );
                        $this->addJoinConds( [ 'change_tag' => [ 'INNER JOIN',
                                [ 'log_id=ct_log_id' ] ] ] );
-                       $this->addWhereFld( 'ct_tag', $params['tag'] );
+                       if ( $wgChangeTagsSchemaMigrationStage > MIGRATION_WRITE_BOTH ) {
+                               $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore();
+                               try {
+                                       $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
+                               } catch ( NameTableAccessException $exception ) {
+                                       // Return nothing.
+                                       $this->addWhere( '1=0' );
+                               }
+                       } else {
+                               $this->addWhereFld( 'ct_tag', $params['tag'] );
+                       }
                }
 
                if ( !is_null( $params['action'] ) ) {
@@ -166,12 +185,12 @@ class ApiQueryLogEvents extends ApiQueryBase {
 
                $user = $params['user'];
                if ( !is_null( $user ) ) {
-                       $userid = User::idFromName( $user );
-                       if ( $userid ) {
-                               $this->addWhereFld( 'log_user', $userid );
-                       } else {
-                               $this->addWhereFld( 'log_user_text', $user );
-                       }
+                       // Note the joins in $q are the same as those from ->getJoin() above
+                       // so we only need to add 'conds' here.
+                       $q = $actorMigration->getWhere(
+                               $db, 'log_user', User::newFromName( $params['user'], false )
+                       );
+                       $this->addWhere( $q['conds'] );
                }
 
                $title = $params['title'];
@@ -244,32 +263,6 @@ class ApiQueryLogEvents extends ApiQueryBase {
                $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'item' );
        }
 
-       /**
-        * @deprecated since 1.25 Use LogFormatter::formatParametersForApi instead
-        * @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
-       ) {
-               wfDeprecated( __METHOD__, '1.25' );
-
-               $entry = new ManualLogEntry( $type, $action );
-               $entry->setParameters( $params );
-               $entry->setTimestamp( $ts );
-               $entry->setLegacy( $legacy );
-               $formatter = LogFormatter::newFromEntry( $entry );
-               $vals['params'] = $formatter->formatParametersForApi();
-
-               return $vals;
-       }
-
        private function extractRowInfo( $row ) {
                $logEntry = DatabaseLogEntry::newFromRow( $row );
                $vals = [
@@ -317,7 +310,7 @@ class ApiQueryLogEvents extends ApiQueryBase {
                        }
                        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;
+                                       $vals['user'] = $row->user_name ?? $row->log_user_text;
                                }
                                if ( $this->fld_userid ) {
                                        $vals['userid'] = intval( $row->log_user );
@@ -338,7 +331,7 @@ class ApiQueryLogEvents extends ApiQueryBase {
                                $anyHidden = true;
                        }
                        if ( LogEventsList::userCan( $row, LogPage::DELETED_COMMENT, $user ) ) {
-                               $comment = $this->commentStore->getComment( $row )->text;
+                               $comment = $this->commentStore->getComment( 'log_comment', $row )->text;
                                if ( $this->fld_comment ) {
                                        $vals['comment'] = $comment;
                                }
@@ -395,6 +388,12 @@ class ApiQueryLogEvents extends ApiQueryBase {
 
        public function getAllowedParams( $flags = 0 ) {
                $config = $this->getConfig();
+               if ( $flags & ApiBase::GET_VALUES_FOR_HELP ) {
+                       $logActions = $this->getAllowedLogActions();
+                       sort( $logActions );
+               } else {
+                       $logActions = null;
+               }
                $ret = [
                        'prop' => [
                                ApiBase::PARAM_ISMULTI => true,
@@ -414,13 +413,11 @@ class ApiQueryLogEvents extends ApiQueryBase {
                                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
                        ],
                        'type' => [
-                               ApiBase::PARAM_TYPE => $config->get( 'LogTypes' )
+                               ApiBase::PARAM_TYPE => LogPage::validTypes(),
                        ],
                        'action' => [
                                // validation on request is done in execute()
-                               ApiBase::PARAM_TYPE => ( $flags & ApiBase::GET_VALUES_FOR_HELP )
-                                       ? $this->getAllowedLogActions()
-                                       : null
+                               ApiBase::PARAM_TYPE => $logActions
                        ],
                        'start' => [
                                ApiBase::PARAM_TYPE => 'timestamp'