Merge "Revert "Add type hint against LinkTarget""
[lhc/web/wiklou.git] / maintenance / rebuildrecentchanges.php
index b6421f3..b50825a 100644 (file)
@@ -33,7 +33,7 @@ require_once __DIR__ . '/Maintenance.php';
 class RebuildRecentchanges extends Maintenance {
        public function __construct() {
                parent::__construct();
-               $this->mDescription = "Rebuild recent changes";
+               $this->addDescription( 'Rebuild recent changes' );
        }
 
        public function execute() {
@@ -41,6 +41,7 @@ class RebuildRecentchanges extends Maintenance {
                $this->rebuildRecentChangesTablePass2();
                $this->rebuildRecentChangesTablePass3();
                $this->rebuildRecentChangesTablePass4();
+               $this->rebuildRecentChangesTablePass5();
                $this->purgeFeeds();
                $this->output( "Done.\n" );
        }
@@ -66,8 +67,8 @@ class RebuildRecentchanges extends Maintenance {
                }
 
                $cutoff = time() - $wgRCMaxAge;
-               $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
-                       array(
+               $dbw->insertSelect( 'recentchanges', [ 'page', 'revision' ],
+                       [
                                'rc_timestamp' => 'rev_timestamp',
                                'rc_user' => 'rev_user',
                                'rc_user_text' => 'rev_user_text',
@@ -87,14 +88,14 @@ class RebuildRecentchanges extends Maintenance {
                                                $dbw->addQuotes( RecentChange::SRC_EDIT )
                                ),
                                'rc_deleted' => 'rev_deleted'
-                       ),
-                       array(
+                       ],
+                       [
                                'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
                                'rev_page=page_id'
-                       ),
+                       ],
                        __METHOD__,
-                       array(), // INSERT options
-                       array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
+                       [], // INSERT options
+                       [ 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ] // SELECT options
                );
        }
 
@@ -142,20 +143,20 @@ class RebuildRecentchanges extends Maintenance {
                                $this->output( "Uhhh, something wrong? No curid\n" );
                        } else {
                                # Grab the entry's text size
-                               $size = $dbw->selectField( 'revision', 'rev_len', array( 'rev_id' => $obj->rc_this_oldid ) );
+                               $size = $dbw->selectField( 'revision', 'rev_len', [ 'rev_id' => $obj->rc_this_oldid ] );
 
                                $dbw->update( 'recentchanges',
-                                       array(
+                                       [
                                                'rc_last_oldid' => $lastOldId,
                                                'rc_new' => $new,
                                                'rc_type' => $new,
                                                'rc_source' => $new === 1 ? RecentChange::SRC_NEW : RecentChange::SRC_EDIT,
                                                'rc_old_len' => $lastSize,
                                                'rc_new_len' => $size,
-                                       ), array(
+                                       ], [
                                                'rc_cur_id' => $lastCurId,
                                                'rc_this_oldid' => $obj->rc_this_oldid,
-                                       ),
+                                       ],
                                        __METHOD__
                                );
 
@@ -181,11 +182,11 @@ class RebuildRecentchanges extends Maintenance {
                list( $logging, $page ) = $dbw->tableNamesN( 'logging', 'page' );
                $dbw->insertSelect(
                        'recentchanges',
-                       array(
+                       [
                                'user',
                                "$logging LEFT JOIN $page ON (log_namespace=page_namespace AND log_title=page_title)"
-                       ),
-                       array(
+                       ],
+                       [
                                'rc_timestamp' => 'log_timestamp',
                                'rc_user' => 'log_user',
                                'rc_user_text' => 'user_name',
@@ -206,15 +207,15 @@ class RebuildRecentchanges extends Maintenance {
                                'rc_logid' => 'log_id',
                                'rc_params' => 'log_params',
                                'rc_deleted' => 'log_deleted'
-                       ),
-                       array(
+                       ],
+                       [
                                'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
                                'log_user=user_id',
                                'log_type' => $basicRCLogs,
-                       ),
+                       ],
                        __METHOD__,
-                       array(), // INSERT options
-                       array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
+                       [], // INSERT options
+                       [ 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ] // SELECT options
                );
        }
 
@@ -230,11 +231,11 @@ class RebuildRecentchanges extends Maintenance {
                        $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
 
                $botgroups = User::getGroupsWithPermission( 'bot' );
-               $autopatrolgroups = $wgUseRCPatrol ? User::getGroupsWithPermission( 'autopatrol' ) : array();
+               $autopatrolgroups = $wgUseRCPatrol ? User::getGroupsWithPermission( 'autopatrol' ) : [];
                # Flag our recent bot edits
                if ( !empty( $botgroups ) ) {
                        $botwhere = $dbw->makeList( $botgroups );
-                       $botusers = array();
+                       $botusers = [];
 
                        $this->output( "Flagging bot account edits...\n" );
 
@@ -258,7 +259,7 @@ class RebuildRecentchanges extends Maintenance {
                # Flag our recent autopatrolled edits
                if ( !$wgMiserMode && !empty( $autopatrolgroups ) ) {
                        $patrolwhere = $dbw->makeList( $autopatrolgroups );
-                       $patrolusers = array();
+                       $patrolusers = [];
 
                        $this->output( "Flagging auto-patrolled edits...\n" );
 
@@ -281,6 +282,46 @@ class RebuildRecentchanges extends Maintenance {
                }
        }
 
+       /**
+        * Rebuild pass 5: Delete duplicate entries where we generate both a page revision and a log entry
+        * for a single action (upload only, at the moment, but potentially also move, protect, ...).
+        */
+       private function rebuildRecentChangesTablePass5() {
+               $dbw = wfGetDB( DB_MASTER );
+
+               $this->output( "Removing duplicate revision and logging entries...\n" );
+
+               $res = $dbw->select(
+                       [ 'logging', 'log_search' ],
+                       [ 'ls_value', 'ls_log_id' ],
+                       [
+                               'ls_log_id = log_id',
+                               'ls_field' => 'associated_rev_id',
+                               'log_type' => 'upload',
+                       ],
+                       __METHOD__
+               );
+               foreach ( $res as $obj ) {
+                       $rev_id = $obj->ls_value;
+                       $log_id = $obj->ls_log_id;
+
+                       // Mark the logging row as having an associated rev id
+                       $dbw->update(
+                               'recentchanges',
+                               /*SET*/ [ 'rc_this_oldid' => $rev_id ],
+                               /*WHERE*/ [ 'rc_logid' => $log_id ],
+                               __METHOD__
+                       );
+
+                       // Delete the revision row
+                       $dbw->delete(
+                               'recentchanges',
+                               /*WHERE*/ [ 'rc_this_oldid' => $rev_id, 'rc_logid' => 0 ],
+                               __METHOD__
+                       );
+               }
+       }
+
        /**
         * Purge cached feeds in $messageMemc
         */