]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - includes/api/ApiQueryRecentChanges.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / includes / api / ApiQueryRecentChanges.php
index 2aecea85df87de440b49c7c0f6239b08fc42e315..74c30d12847ec1cc0bf46b2baba8a714a0096640 100644 (file)
@@ -1,10 +1,10 @@
 <?php
 /**
- * API for MediaWiki 1.8+
+ *
  *
  * Created on Oct 19, 2006
  *
- * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
+ * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * @file
  */
 
-if ( !defined( 'MEDIAWIKI' ) ) {
-       // Eclipse helper - will be ignored in production
-       require_once( 'ApiQueryBase.php' );
-}
-
 /**
  * A query action to enumerate the recent changes that were done to the wiki.
  * Various filters are supported.
  *
  * @ingroup API
  */
-class ApiQueryRecentChanges extends ApiQueryBase {
+class ApiQueryRecentChanges extends ApiQueryGeneratorBase {
 
-       public function __construct( $query, $moduleName ) {
+       public function __construct( ApiQuery $query, $moduleName ) {
                parent::__construct( $query, $moduleName, 'rc' );
        }
 
+       private $commentStore;
+
        private $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_userid = false,
-                       $fld_flags = false, $fld_timestamp = false, $fld_title = false, $fld_ids = false,
-                       $fld_sizes = false, $fld_redirect = false, $fld_patrolled = false, $fld_loginfo = false, $fld_tags = false;
+               $fld_flags = false, $fld_timestamp = false, $fld_title = false, $fld_ids = false,
+               $fld_sizes = false, $fld_redirect = false, $fld_patrolled = false, $fld_loginfo = false,
+               $fld_tags = false, $fld_sha1 = false, $token = [];
 
        private $tokenFunctions;
 
@@ -51,7 +49,8 @@ class ApiQueryRecentChanges extends ApiQueryBase {
         * Get an array mapping token names to their handler functions.
         * The prototype for a token function is func($pageid, $title, $rc)
         * it should return a token or false (permission denied)
-        * @return array(tokenname => function)
+        * @deprecated since 1.24
+        * @return array [ tokenname => function ]
         */
        protected function getTokenFunctions() {
                // Don't call the hooks twice
@@ -59,38 +58,59 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                        return $this->tokenFunctions;
                }
 
-               // If we're in JSON callback mode, no tokens can be obtained
-               if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
-                       return array();
+               // If we're in a mode that breaks the same-origin policy, no tokens can
+               // be obtained
+               if ( $this->lacksSameOriginSecurity() ) {
+                       return [];
                }
 
-               $this->tokenFunctions = array(
-                       'patrol' => array( 'ApiQueryRecentChanges', 'getPatrolToken' )
-               );
-               wfRunHooks( 'APIQueryRecentChangesTokens', array( &$this->tokenFunctions ) );
+               $this->tokenFunctions = [
+                       'patrol' => [ 'ApiQueryRecentChanges', 'getPatrolToken' ]
+               ];
+               Hooks::run( 'APIQueryRecentChangesTokens', [ &$this->tokenFunctions ] );
+
                return $this->tokenFunctions;
        }
 
-       public static function getPatrolToken( $pageid, $title, $rc ) {
+       /**
+        * @deprecated since 1.24
+        * @param int $pageid
+        * @param Title $title
+        * @param RecentChange|null $rc
+        * @return bool|string
+        */
+       public static function getPatrolToken( $pageid, $title, $rc = null ) {
                global $wgUser;
-               if ( !$wgUser->useRCPatrol() && ( !$wgUser->useNPPatrol() ||
-                               $rc->getAttribute( 'rc_type' ) != RC_NEW ) )
-               {
-                       return false;
+
+               $validTokenUser = false;
+
+               if ( $rc ) {
+                       if ( ( $wgUser->useRCPatrol() && $rc->getAttribute( 'rc_type' ) == RC_EDIT ) ||
+                               ( $wgUser->useNPPatrol() && $rc->getAttribute( 'rc_type' ) == RC_NEW )
+                       ) {
+                               $validTokenUser = true;
+                       }
+               } elseif ( $wgUser->useRCPatrol() || $wgUser->useNPPatrol() ) {
+                       $validTokenUser = true;
                }
 
-               // The patrol token is always the same, let's exploit that
-               static $cachedPatrolToken = null;
-               if ( is_null( $cachedPatrolToken ) ) {
-                       $cachedPatrolToken = $wgUser->editToken( 'patrol' );
+               if ( $validTokenUser ) {
+                       // The patrol token is always the same, let's exploit that
+                       static $cachedPatrolToken = null;
+
+                       if ( is_null( $cachedPatrolToken ) ) {
+                               $cachedPatrolToken = $wgUser->getEditToken( 'patrol' );
+                       }
+
+                       return $cachedPatrolToken;
                }
-               
-               return $cachedPatrolToken;
+
+               return false;
        }
 
        /**
         * Sets internal state to include the desired properties in the output.
-        * @param $prop Array associative array of properties, only keys are used here
+        * @param array $prop Associative array of properties, only keys are used here
         */
        public function initProperties( $prop ) {
                $this->fld_comment = isset( $prop['comment'] );
@@ -106,29 +126,63 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                $this->fld_patrolled = isset( $prop['patrolled'] );
                $this->fld_loginfo = isset( $prop['loginfo'] );
                $this->fld_tags = isset( $prop['tags'] );
+               $this->fld_sha1 = isset( $prop['sha1'] );
+       }
+
+       public function execute() {
+               $this->run();
+       }
+
+       public function executeGenerator( $resultPageSet ) {
+               $this->run( $resultPageSet );
        }
 
        /**
         * Generates and outputs the result of this query based upon the provided parameters.
+        *
+        * @param ApiPageSet $resultPageSet
         */
-       public function execute() {
-               global $wgUser;
+       public function run( $resultPageSet = null ) {
+               $user = $this->getUser();
                /* Get the parameters of the request. */
                $params = $this->extractRequestParams();
 
                /* Build our basic query. Namely, something along the lines of:
                 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
-                *              AND rc_timestamp < $end AND rc_namespace = $namespace
-                *              AND rc_deleted = '0'
+                *   AND rc_timestamp < $end AND rc_namespace = $namespace
                 */
                $this->addTables( 'recentchanges' );
-               $index = array( 'recentchanges' => 'rc_timestamp' ); // May change
-               $this->addWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
+               $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
+
+               if ( !is_null( $params['continue'] ) ) {
+                       $cont = explode( '|', $params['continue'] );
+                       $this->dieContinueUsageIf( count( $cont ) != 2 );
+                       $db = $this->getDB();
+                       $timestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
+                       $id = intval( $cont[1] );
+                       $this->dieContinueUsageIf( $id != $cont[1] );
+                       $op = $params['dir'] === 'older' ? '<' : '>';
+                       $this->addWhere(
+                               "rc_timestamp $op $timestamp OR " .
+                               "(rc_timestamp = $timestamp AND " .
+                               "rc_id $op= $id)"
+                       );
+               }
+
+               $order = $params['dir'] === 'older' ? 'DESC' : 'ASC';
+               $this->addOption( 'ORDER BY', [
+                       "rc_timestamp $order",
+                       "rc_id $order",
+               ] );
+
                $this->addWhereFld( 'rc_namespace', $params['namespace'] );
-               $this->addWhereFld( 'rc_deleted', 0 );
 
                if ( !is_null( $params['type'] ) ) {
-                       $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) );
+                       try {
+                               $this->addWhereFld( 'rc_type', RecentChange::parseToRCType( $params['type'] ) );
+                       } catch ( Exception $e ) {
+                               ApiBase::dieDebug( __METHOD__, $e->getMessage() );
+                       }
                }
 
                if ( !is_null( $params['show'] ) ) {
@@ -136,19 +190,20 @@ class ApiQueryRecentChanges extends ApiQueryBase {
 
                        /* Check for conflicting parameters. */
                        if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
-                                       || ( isset( $show['bot'] ) && isset( $show['!bot'] ) )
-                                       || ( isset( $show['anon'] ) && isset( $show['!anon'] ) )
-                                       || ( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
-                                       || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
-                       )
-                       {
-                               $this->dieUsageMsg( array( 'show' ) );
+                               || ( isset( $show['bot'] ) && isset( $show['!bot'] ) )
+                               || ( isset( $show['anon'] ) && isset( $show['!anon'] ) )
+                               || ( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
+                               || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
+                               || ( isset( $show['patrolled'] ) && isset( $show['unpatrolled'] ) )
+                               || ( isset( $show['!patrolled'] ) && isset( $show['unpatrolled'] ) )
+                       ) {
+                               $this->dieWithError( 'apierror-show' );
                        }
 
                        // Check permissions
-                       if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
-                               if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
-                                       $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
+                       if ( $this->includesPatrollingFlags( $show ) ) {
+                               if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
+                                       $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
                                }
                        }
 
@@ -163,17 +218,27 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                        $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
                        $this->addWhereIf( 'page_is_redirect = 1', isset( $show['redirect'] ) );
 
+                       if ( isset( $show['unpatrolled'] ) ) {
+                               // See ChangesList::isUnpatrolled
+                               if ( $user->useRCPatrol() ) {
+                                       $this->addWhere( 'rc_patrolled = 0' );
+                               } elseif ( $user->useNPPatrol() ) {
+                                       $this->addWhere( 'rc_patrolled = 0' );
+                                       $this->addWhereFld( 'rc_type', RC_NEW );
+                               }
+                       }
+
                        // Don't throw log entries out the window here
-                       $this->addWhereIf( 'page_is_redirect = 0 OR page_is_redirect IS NULL', isset( $show['!redirect'] ) );
+                       $this->addWhereIf(
+                               'page_is_redirect = 0 OR page_is_redirect IS NULL',
+                               isset( $show['!redirect'] )
+                       );
                }
 
-               if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
-                       $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
-               }
+               $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
 
                if ( !is_null( $params['user'] ) ) {
                        $this->addWhereFld( 'rc_user_text', $params['user'] );
-                       $index['recentchanges'] = 'rc_user_text';
                }
 
                if ( !is_null( $params['excludeuser'] ) ) {
@@ -184,17 +249,17 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                }
 
                /* Add the fields we're concerned with to our query. */
-               $this->addFields( array(
+               $this->addFields( [
+                       'rc_id',
                        'rc_timestamp',
                        'rc_namespace',
                        'rc_title',
                        'rc_cur_id',
                        'rc_type',
-                       'rc_moved_to_ns',
-                       'rc_moved_to_title',
                        'rc_deleted'
-               ) );
+               ] );
 
+               $showRedirects = false;
                /* Determine what properties we need to display. */
                if ( !is_null( $params['prop'] ) ) {
                        $prop = array_flip( $params['prop'] );
@@ -202,169 +267,226 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                        /* Set up internal members based upon params. */
                        $this->initProperties( $prop );
 
-                       if ( $this->fld_patrolled && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
-                               $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
+                       if ( $this->fld_patrolled && !$user->useRCPatrol() && !$user->useNPPatrol() ) {
+                               $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
                        }
 
                        /* Add fields to our query if they are specified as a needed parameter. */
-                       $this->addFieldsIf( 'rc_id', $this->fld_ids );
-                       $this->addFieldsIf( 'rc_this_oldid', $this->fld_ids );
-                       $this->addFieldsIf( 'rc_last_oldid', $this->fld_ids );
-                       $this->addFieldsIf( 'rc_comment', $this->fld_comment || $this->fld_parsedcomment );
-                       $this->addFieldsIf( 'rc_user', $this->fld_user );
-                       $this->addFieldsIf( 'rc_user_text', $this->fld_user || $this->fld_userid );
-                       $this->addFieldsIf( 'rc_minor', $this->fld_flags );
-                       $this->addFieldsIf( 'rc_bot', $this->fld_flags );
-                       $this->addFieldsIf( 'rc_new', $this->fld_flags );
-                       $this->addFieldsIf( 'rc_old_len', $this->fld_sizes );
-                       $this->addFieldsIf( 'rc_new_len', $this->fld_sizes );
-                       $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
-                       $this->addFieldsIf( 'rc_logid', $this->fld_loginfo );
-                       $this->addFieldsIf( 'rc_log_type', $this->fld_loginfo );
-                       $this->addFieldsIf( 'rc_log_action', $this->fld_loginfo );
-                       $this->addFieldsIf( 'rc_params', $this->fld_loginfo );
-                       if ( $this->fld_redirect || isset( $show['redirect'] ) || isset( $show['!redirect'] ) ) {
-                               $this->addTables( 'page' );
-                               $this->addJoinConds( array( 'page' => array( 'LEFT JOIN', array( 'rc_namespace=page_namespace', 'rc_title=page_title' ) ) ) );
-                               $this->addFields( 'page_is_redirect' );
-                       }
+                       $this->addFieldsIf( [ 'rc_this_oldid', 'rc_last_oldid' ], $this->fld_ids );
+                       $this->addFieldsIf( 'rc_user', $this->fld_user || $this->fld_userid );
+                       $this->addFieldsIf( 'rc_user_text', $this->fld_user );
+                       $this->addFieldsIf( [ 'rc_minor', 'rc_type', 'rc_bot' ], $this->fld_flags );
+                       $this->addFieldsIf( [ 'rc_old_len', 'rc_new_len' ], $this->fld_sizes );
+                       $this->addFieldsIf( [ 'rc_patrolled', 'rc_log_type' ], $this->fld_patrolled );
+                       $this->addFieldsIf(
+                               [ 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ],
+                               $this->fld_loginfo
+                       );
+                       $showRedirects = $this->fld_redirect || isset( $show['redirect'] )
+                               || isset( $show['!redirect'] );
                }
+               $this->addFieldsIf( [ 'rc_this_oldid' ],
+                       $resultPageSet && $params['generaterevisions'] );
 
                if ( $this->fld_tags ) {
                        $this->addTables( 'tag_summary' );
-                       $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rc_id=ts_rc_id' ) ) ) );
+                       $this->addJoinConds( [ 'tag_summary' => [ 'LEFT JOIN', [ 'rc_id=ts_rc_id' ] ] ] );
                        $this->addFields( 'ts_tags' );
                }
 
+               if ( $this->fld_sha1 ) {
+                       $this->addTables( 'revision' );
+                       $this->addJoinConds( [ 'revision' => [ 'LEFT JOIN',
+                               [ 'rc_this_oldid=rev_id' ] ] ] );
+                       $this->addFields( [ 'rev_sha1', 'rev_deleted' ] );
+               }
+
+               if ( $params['toponly'] || $showRedirects ) {
+                       $this->addTables( 'page' );
+                       $this->addJoinConds( [ 'page' => [ 'LEFT JOIN',
+                               [ 'rc_namespace=page_namespace', 'rc_title=page_title' ] ] ] );
+                       $this->addFields( 'page_is_redirect' );
+
+                       if ( $params['toponly'] ) {
+                               $this->addWhere( 'rc_this_oldid = page_latest' );
+                       }
+               }
+
                if ( !is_null( $params['tag'] ) ) {
                        $this->addTables( 'change_tag' );
-                       $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rc_id=ct_rc_id' ) ) ) );
-                       $this->addWhereFld( 'ct_tag' , $params['tag'] );
-                       global $wgOldChangeTagsIndex;
-                       $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
+                       $this->addJoinConds( [ 'change_tag' => [ 'INNER JOIN', [ 'rc_id=ct_rc_id' ] ] ] );
+                       $this->addWhereFld( 'ct_tag', $params['tag'] );
+               }
+
+               // Paranoia: avoid brute force searches (T19342)
+               if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
+                       if ( !$user->isAllowed( 'deletedhistory' ) ) {
+                               $bitmask = Revision::DELETED_USER;
+                       } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
+                               $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
+                       } else {
+                               $bitmask = 0;
+                       }
+                       if ( $bitmask ) {
+                               $this->addWhere( $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask" );
+                       }
+               }
+               if ( $this->getRequest()->getCheck( 'namespace' ) ) {
+                       // LogPage::DELETED_ACTION hides the affected page, too.
+                       if ( !$user->isAllowed( 'deletedhistory' ) ) {
+                               $bitmask = LogPage::DELETED_ACTION;
+                       } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
+                               $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
+                       } else {
+                               $bitmask = 0;
+                       }
+                       if ( $bitmask ) {
+                               $this->addWhere( $this->getDB()->makeList( [
+                                       'rc_type != ' . RC_LOG,
+                                       $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
+                               ], LIST_OR ) );
+                       }
                }
 
                $this->token = $params['token'];
+
+               if ( $this->fld_comment || $this->fld_parsedcomment || $this->token ) {
+                       $this->commentStore = new CommentStore( 'rc_comment' );
+                       $commentQuery = $this->commentStore->getJoin();
+                       $this->addTables( $commentQuery['tables'] );
+                       $this->addFields( $commentQuery['fields'] );
+                       $this->addJoinConds( $commentQuery['joins'] );
+               }
+
                $this->addOption( 'LIMIT', $params['limit'] + 1 );
-               $this->addOption( 'USE INDEX', $index );
 
+               $hookData = [];
                $count = 0;
                /* Perform the actual query. */
-               $res = $this->select( __METHOD__ );
+               $res = $this->select( __METHOD__, [], $hookData );
+
+               $revids = [];
+               $titles = [];
+
+               $result = $this->getResult();
 
                /* Iterate through the rows, adding data extracted from them to our query result. */
                foreach ( $res as $row ) {
-                       if ( ++ $count > $params['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->rc_timestamp ) );
+                       if ( $count === 0 && $resultPageSet !== null ) {
+                               // Set the non-continue since the list of recentchanges is
+                               // prone to having entries added at the start frequently.
+                               $this->getContinuationManager()->addGeneratorNonContinueParam(
+                                       $this, 'continue', "$row->rc_timestamp|$row->rc_id"
+                               );
+                       }
+                       if ( ++$count > $params['limit'] ) {
+                               // We've reached the one extra which shows that there are
+                               // additional pages to be had. Stop here...
+                               $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
                                break;
                        }
 
-                       /* Extract the data from a single row. */
-                       $vals = $this->extractRowInfo( $row );
+                       if ( is_null( $resultPageSet ) ) {
+                               /* Extract the data from a single row. */
+                               $vals = $this->extractRowInfo( $row );
 
-                       /* Add that row's data to our final output. */
-                       if ( !$vals ) {
-                               continue;
-                       }
-                       $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
-                       if ( !$fit ) {
-                               $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
-                               break;
+                               /* Add that row's data to our final output. */
+                               $fit = $this->processRow( $row, $vals, $hookData ) &&
+                                       $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
+                               if ( !$fit ) {
+                                       $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
+                                       break;
+                               }
+                       } elseif ( $params['generaterevisions'] ) {
+                               $revid = (int)$row->rc_this_oldid;
+                               if ( $revid > 0 ) {
+                                       $revids[] = $revid;
+                               }
+                       } else {
+                               $titles[] = Title::makeTitle( $row->rc_namespace, $row->rc_title );
                        }
                }
 
-               /* Format the result */
-               $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'rc' );
+               if ( is_null( $resultPageSet ) ) {
+                       /* Format the result */
+                       $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'rc' );
+               } elseif ( $params['generaterevisions'] ) {
+                       $resultPageSet->populateFromRevisionIDs( $revids );
+               } else {
+                       $resultPageSet->populateFromTitles( $titles );
+               }
        }
 
        /**
         * Extracts from a single sql row the data needed to describe one recent change.
         *
-        * @param $row The row from which to extract the data.
-        * @return An array mapping strings (descriptors) to their respective string values.
+        * @param stdClass $row The row from which to extract the data.
+        * @return array An array mapping strings (descriptors) to their respective string values.
         * @access public
         */
        public function extractRowInfo( $row ) {
-               /* If page was moved somewhere, get the title of the move target. */
-               $movedToTitle = false;
-               if ( isset( $row->rc_moved_to_title ) && $row->rc_moved_to_title !== '' )
-               {
-                       $movedToTitle = Title::makeTitle( $row->rc_moved_to_ns, $row->rc_moved_to_title );
-               }
-
                /* Determine the title of the page that has been changed. */
                $title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
+               $user = $this->getUser();
 
                /* Our output data. */
-               $vals = array();
+               $vals = [];
 
                $type = intval( $row->rc_type );
+               $vals['type'] = RecentChange::parseFromRCType( $type );
 
-               /* Determine what kind of change this was. */
-               switch ( $type ) {
-                       case RC_EDIT:
-                               $vals['type'] = 'edit';
-                               break;
-                       case RC_NEW:
-                               $vals['type'] = 'new';
-                               break;
-                       case RC_MOVE:
-                               $vals['type'] = 'move';
-                               break;
-                       case RC_LOG:
-                               $vals['type'] = 'log';
-                               break;
-                       case RC_MOVE_OVER_REDIRECT:
-                               $vals['type'] = 'move over redirect';
-                               break;
-                       default:
-                               $vals['type'] = $type;
-               }
+               $anyHidden = false;
 
                /* Create a new entry in the result for the title. */
-               if ( $this->fld_title ) {
-                       ApiQueryBase::addTitleInfo( $vals, $title );
-                       if ( $movedToTitle ) {
-                               ApiQueryBase::addTitleInfo( $vals, $movedToTitle, 'new_' );
+               if ( $this->fld_title || $this->fld_ids ) {
+                       if ( $type === RC_LOG && ( $row->rc_deleted & LogPage::DELETED_ACTION ) ) {
+                               $vals['actionhidden'] = true;
+                               $anyHidden = true;
+                       }
+                       if ( $type !== RC_LOG ||
+                               LogEventsList::userCanBitfield( $row->rc_deleted, LogPage::DELETED_ACTION, $user )
+                       ) {
+                               if ( $this->fld_title ) {
+                                       ApiQueryBase::addTitleInfo( $vals, $title );
+                               }
+                               if ( $this->fld_ids ) {
+                                       $vals['pageid'] = intval( $row->rc_cur_id );
+                                       $vals['revid'] = intval( $row->rc_this_oldid );
+                                       $vals['old_revid'] = intval( $row->rc_last_oldid );
+                               }
                        }
                }
 
-               /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
                if ( $this->fld_ids ) {
                        $vals['rcid'] = intval( $row->rc_id );
-                       $vals['pageid'] = intval( $row->rc_cur_id );
-                       $vals['revid'] = intval( $row->rc_this_oldid );
-                       $vals['old_revid'] = intval( $row->rc_last_oldid );
                }
 
-               /* Add user data and 'anon' flag, if use is anonymous. */
+               /* Add user data and 'anon' flag, if user is anonymous. */
                if ( $this->fld_user || $this->fld_userid ) {
-
-                       if ( $this->fld_user ) {
-                               $vals['user'] = $row->rc_user_text;
+                       if ( $row->rc_deleted & Revision::DELETED_USER ) {
+                               $vals['userhidden'] = true;
+                               $anyHidden = true;
                        }
+                       if ( Revision::userCanBitfield( $row->rc_deleted, Revision::DELETED_USER, $user ) ) {
+                               if ( $this->fld_user ) {
+                                       $vals['user'] = $row->rc_user_text;
+                               }
 
-                       if ( $this->fld_userid ) {
-                               $vals['userid'] = $row->rc_user;
-                       }
+                               if ( $this->fld_userid ) {
+                                       $vals['userid'] = (int)$row->rc_user;
+                               }
 
-                       if ( !$row->rc_user ) {
-                               $vals['anon'] = '';
+                               if ( !$row->rc_user ) {
+                                       $vals['anon'] = true;
+                               }
                        }
                }
 
                /* Add flags, such as new, minor, bot. */
                if ( $this->fld_flags ) {
-                       if ( $row->rc_bot ) {
-                               $vals['bot'] = '';
-                       }
-                       if ( $row->rc_new ) {
-                               $vals['new'] = '';
-                       }
-                       if ( $row->rc_minor ) {
-                               $vals['minor'] = '';
-                       }
+                       $vals['bot'] = (bool)$row->rc_bot;
+                       $vals['new'] = $row->rc_type == RC_NEW;
+                       $vals['minor'] = (bool)$row->rc_minor;
                }
 
                /* Add sizes of each revision. (Only available on 1.10+) */
@@ -379,44 +501,67 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                }
 
                /* Add edit summary / log summary. */
-               if ( $this->fld_comment && isset( $row->rc_comment ) ) {
-                       $vals['comment'] = $row->rc_comment;
-               }
+               if ( $this->fld_comment || $this->fld_parsedcomment ) {
+                       if ( $row->rc_deleted & Revision::DELETED_COMMENT ) {
+                               $vals['commenthidden'] = true;
+                               $anyHidden = true;
+                       }
+                       if ( Revision::userCanBitfield( $row->rc_deleted, Revision::DELETED_COMMENT, $user ) ) {
+                               $comment = $this->commentStore->getComment( $row )->text;
+                               if ( $this->fld_comment ) {
+                                       $vals['comment'] = $comment;
+                               }
 
-               if ( $this->fld_parsedcomment && isset( $row->rc_comment ) ) {
-                       global $wgUser;
-                       $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rc_comment, $title );
+                               if ( $this->fld_parsedcomment ) {
+                                       $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
+                               }
+                       }
                }
 
                if ( $this->fld_redirect ) {
-                       if ( $row->page_is_redirect ) {
-                               $vals['redirect'] = '';
-                       }
+                       $vals['redirect'] = (bool)$row->page_is_redirect;
                }
 
                /* Add the patrolled flag */
-               if ( $this->fld_patrolled && $row->rc_patrolled == 1 ) {
-                       $vals['patrolled'] = '';
+               if ( $this->fld_patrolled ) {
+                       $vals['patrolled'] = $row->rc_patrolled == 1;
+                       $vals['unpatrolled'] = ChangesList::isUnpatrolled( $row, $user );
                }
 
                if ( $this->fld_loginfo && $row->rc_type == RC_LOG ) {
-                       $vals['logid'] = intval( $row->rc_logid );
-                       $vals['logtype'] = $row->rc_log_type;
-                       $vals['logaction'] = $row->rc_log_action;
-                       ApiQueryLogEvents::addLogParams(
-                               $this->getResult(),
-                               $vals, $row->rc_params,
-                               $row->rc_log_type, $row->rc_timestamp
-                       );
+                       if ( $row->rc_deleted & LogPage::DELETED_ACTION ) {
+                               $vals['actionhidden'] = true;
+                               $anyHidden = true;
+                       }
+                       if ( LogEventsList::userCanBitfield( $row->rc_deleted, LogPage::DELETED_ACTION, $user ) ) {
+                               $vals['logid'] = intval( $row->rc_logid );
+                               $vals['logtype'] = $row->rc_log_type;
+                               $vals['logaction'] = $row->rc_log_action;
+                               $vals['logparams'] = LogFormatter::newFromRow( $row )->formatParametersForApi();
+                       }
                }
 
                if ( $this->fld_tags ) {
                        if ( $row->ts_tags ) {
                                $tags = explode( ',', $row->ts_tags );
-                               $this->getResult()->setIndexedTagName( $tags, 'tag' );
+                               ApiResult::setIndexedTagName( $tags, 'tag' );
                                $vals['tags'] = $tags;
                        } else {
-                               $vals['tags'] = array();
+                               $vals['tags'] = [];
+                       }
+               }
+
+               if ( $this->fld_sha1 && $row->rev_sha1 !== null ) {
+                       if ( $row->rev_deleted & Revision::DELETED_TEXT ) {
+                               $vals['sha1hidden'] = true;
+                               $anyHidden = true;
+                       }
+                       if ( Revision::userCanBitfield( $row->rev_deleted, Revision::DELETED_TEXT, $user ) ) {
+                               if ( $row->rev_sha1 !== '' ) {
+                                       $vals['sha1'] = Wikimedia\base_convert( $row->rev_sha1, 36, 16, 40 );
+                               } else {
+                                       $vals['sha1'] = '';
+                               }
                        }
                }
 
@@ -426,82 +571,84 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                                $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id,
                                        $title, RecentChange::newFromRow( $row ) );
                                if ( $val === false ) {
-                                       $this->setWarning( "Action '$t' is not allowed for the current user" );
+                                       $this->addWarning( [ 'apiwarn-tokennotallowed', $t ] );
                                } else {
                                        $vals[$t . 'token'] = $val;
                                }
                        }
                }
 
+               if ( $anyHidden && ( $row->rc_deleted & Revision::DELETED_RESTRICTED ) ) {
+                       $vals['suppressed'] = true;
+               }
+
                return $vals;
        }
 
-       private function parseRCType( $type ) {
-               if ( is_array( $type ) ) {
-                       $retval = array();
-                       foreach ( $type as $t ) {
-                               $retval[] = $this->parseRCType( $t );
-                       }
-                       return $retval;
-               }
-               switch( $type ) {
-                       case 'edit':
-                               return RC_EDIT;
-                       case 'new':
-                               return RC_NEW;
-                       case 'log':
-                               return RC_LOG;
-               }
+       /**
+        * @param array $flagsArray flipped array (string flags are keys)
+        * @return bool
+        */
+       private function includesPatrollingFlags( array $flagsArray ) {
+               return isset( $flagsArray['patrolled'] ) ||
+                       isset( $flagsArray['!patrolled'] ) ||
+                       isset( $flagsArray['unpatrolled'] ) ||
+                       isset( $flagsArray['autopatrolled'] ) ||
+                       isset( $flagsArray['!autopatrolled'] );
        }
 
        public function getCacheMode( $params ) {
-               if ( isset( $params['show'] ) ) {
-                       foreach ( $params['show'] as $show ) {
-                               if ( $show === 'patrolled' || $show === '!patrolled' ) {
-                                       return 'private';
-                               }
-                       }
+               if ( isset( $params['show'] ) &&
+                       $this->includesPatrollingFlags( array_flip( $params['show'] ) )
+               ) {
+                       return 'private';
                }
                if ( isset( $params['token'] ) ) {
                        return 'private';
                }
+               if ( $this->userCanSeeRevDel() ) {
+                       return 'private';
+               }
                if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
-                       // formatComment() calls wfMsg() among other things
+                       // formatComment() calls wfMessage() among other things
                        return 'anon-public-user-private';
                }
+
                return 'public';
        }
 
        public function getAllowedParams() {
-               return array(
-                       'start' => array(
+               return [
+                       'start' => [
                                ApiBase::PARAM_TYPE => 'timestamp'
-                       ),
-                       'end' => array(
+                       ],
+                       'end' => [
                                ApiBase::PARAM_TYPE => 'timestamp'
-                       ),
-                       'dir' => array(
+                       ],
+                       'dir' => [
                                ApiBase::PARAM_DFLT => 'older',
-                               ApiBase::PARAM_TYPE => array(
+                               ApiBase::PARAM_TYPE => [
                                        'newer',
                                        'older'
-                               )
-                       ),
-                       'namespace' => array(
+                               ],
+                               ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
+                       ],
+                       'namespace' => [
                                ApiBase::PARAM_ISMULTI => true,
-                               ApiBase::PARAM_TYPE => 'namespace'
-                       ),
-                       'user' => array(
+                               ApiBase::PARAM_TYPE => 'namespace',
+                               ApiBase::PARAM_EXTRA_NAMESPACES => [ NS_MEDIA, NS_SPECIAL ],
+                       ],
+                       'user' => [
                                ApiBase::PARAM_TYPE => 'user'
-                       ),
-                       'excludeuser' => array(
+                       ],
+                       'excludeuser' => [
                                ApiBase::PARAM_TYPE => 'user'
-                       ),
+                       ],
                        'tag' => null,
-                       'prop' => array(
+                       'prop' => [
                                ApiBase::PARAM_ISMULTI => true,
                                ApiBase::PARAM_DFLT => 'title|timestamp|ids',
-                               ApiBase::PARAM_TYPE => array(
+                               ApiBase::PARAM_TYPE => [
                                        'user',
                                        'userid',
                                        'comment',
@@ -514,16 +661,19 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                                        'redirect',
                                        'patrolled',
                                        'loginfo',
-                                       'tags'
-                               )
-                       ),
-                       'token' => array(
+                                       'tags',
+                                       'sha1',
+                               ],
+                               ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
+                       ],
+                       'token' => [
+                               ApiBase::PARAM_DEPRECATED => true,
                                ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
                                ApiBase::PARAM_ISMULTI => true
-                       ),
-                       'show' => array(
+                       ],
+                       'show' => [
                                ApiBase::PARAM_ISMULTI => true,
-                               ApiBase::PARAM_TYPE => array(
+                               ApiBase::PARAM_TYPE => [
                                        'minor',
                                        '!minor',
                                        'bot',
@@ -533,81 +683,40 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                                        'redirect',
                                        '!redirect',
                                        'patrolled',
-                                       '!patrolled'
-                               )
-                       ),
-                       'limit' => array(
+                                       '!patrolled',
+                                       'unpatrolled'
+                               ]
+                       ],
+                       'limit' => [
                                ApiBase::PARAM_DFLT => 10,
                                ApiBase::PARAM_TYPE => 'limit',
                                ApiBase::PARAM_MIN => 1,
                                ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
                                ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
-                       ),
-                       'type' => array(
+                       ],
+                       'type' => [
+                               ApiBase::PARAM_DFLT => 'edit|new|log|categorize',
                                ApiBase::PARAM_ISMULTI => true,
-                               ApiBase::PARAM_TYPE => array(
-                                       'edit',
-                                       'new',
-                                       'log'
-                               )
-                       )
-               );
-       }
-
-       public function getParamDescription() {
-               return array(
-                       'start' => 'The timestamp to start enumerating from',
-                       'end' => 'The timestamp to end enumerating',
-                       'dir' => 'In which direction to enumerate',
-                       'namespace' => 'Filter log entries to only this namespace(s)',
-                       'user' => 'Only list changes by this user',
-                       'excludeuser' => 'Don\'t list changes by this user',
-                       'prop' => array(
-                               'Include additional pieces of information',
-                               ' user           - Adds the user responsible for the edit and tags if they are an IP',
-                               ' userid         - Adds the user id responsible for the edit',
-                               ' comment        - Adds the comment for the edit',
-                               ' parsedcomment  - Adds the parsed comment for the edit',
-                               ' flags          - Adds flags for the edit',
-                               ' timestamp      - Adds timestamp of the edit',
-                               ' title          - Adds the page title of the edit',
-                               ' ids            - Adds the page id, recent changes id and the new and old revision id',
-                               ' sizes          - Adds the new and old page length in bytes',
-                               ' redirect       - Tags edit if page is a redirect',
-                               ' patrolled      - Tags edits have have been patrolled',
-                               ' loginfo        - Adds log information (logid, logtype, etc) to log entries',
-                               ' tags           - Lists tags for the entry',
-                       ),
-                       'token' => 'Which tokens to obtain for each change',
-                       'show' => array(
-                               'Show only items that meet this criteria.',
-                               "For example, to see only minor edits done by logged-in users, set {$this->getModulePrefix()}show=minor|!anon"
-                       ),
-                       'type' => 'Which types of changes to show',
-                       'limit' => 'How many total changes to return',
-                       'tag' => 'Only list changes tagged with this tag',
-               );
-       }
-
-       public function getDescription() {
-               return 'Enumerate recent changes';
-       }
-
-       public function getPossibleErrors() {
-               return array_merge( parent::getPossibleErrors(), array(
-                       array( 'show' ),
-                       array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
-                       array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
-               ) );
+                               ApiBase::PARAM_TYPE => RecentChange::getChangeTypes()
+                       ],
+                       'toponly' => false,
+                       'continue' => [
+                               ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
+                       ],
+                       'generaterevisions' => false,
+               ];
        }
 
-       protected function getExamples() {
-               return array(
-                       'api.php?action=query&list=recentchanges'
-               );
+       protected function getExamplesMessages() {
+               return [
+                       'action=query&list=recentchanges'
+                               => 'apihelp-query+recentchanges-example-simple',
+                       'action=query&generator=recentchanges&grcshow=!patrolled&prop=info'
+                               => 'apihelp-query+recentchanges-example-generator',
+               ];
        }
 
-       public function getVersion() {
-               return __CLASS__ . ': $Id$';
+       public function getHelpUrls() {
+               return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Recentchanges';
        }
 }