summaryrefslogtreecommitdiff
blob: de5fa946b83a3ad95facc4643213c27145efe279 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
 * A script to remove orphaned actors whose users are no longer
 * present in the user table. See T225999
 *
 * @author Abijeet Patro
 * @license GPL-2.0-or-later
 * @file
 */

// Standard boilerplate to define $IP
if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
	$IP = getenv( 'MW_INSTALL_PATH' );
} else {
	$dir = __DIR__;
	$IP = "$dir/../../..";
}
require_once "$IP/maintenance/Maintenance.php";

class RemoveOrphanedActors extends Maintenance {
	public function __construct() {
		parent::__construct();
		$this->addDescription( 'A script to remove orphaned actor records.' );

		$this->addOption(
			'really',
			'(optional) Really delete, no dry-run',
			false,
			false
		);
	}

	public function execute() {
		$dbw = wfGetDB( DB_MASTER );

		$orphanedActors = $dbw->select(
			[ 'actor', 'user' ],
			[ 'actor_id', 'actor_name', 'actor_user' ],
			[ 'user_id IS NULL', 'actor_user IS NOT NULL' ],
			__METHOD__,
			[],
			[ 'user' => [ 'LEFT JOIN', [ 'actor_user = user_id' ] ] ]
		);

		$this->output( 'found ' . $orphanedActors->numRows() . " orphaned actors...\n" );

		if ( $orphanedActors->numRows() === 0 ) {
			$this->output( "nothing to delete ... exit!\n" );
			return;
		}

		$orphanedActorIds = [];

		$this->output( "----\n" );
		foreach ( $orphanedActors as $actor ) {
			$this->output(
				"Actor: '$actor->actor_name'; " .
				"Actor Id: $actor->actor_id; " .
				"Actor User: $actor->actor_user;\n"
			);

			$orphanedActorIds[] = $actor->actor_id;
		}
		$this->output( "----\n" );

		if ( !$this->hasOption( 'really' ) ) {
			$this->output( "dry run...exiting!\n" );
			return;
		}

		// Delete orphaned actors
		$dbw->delete( 'actor', [ 'actor_id' => $orphanedActorIds ], __METHOD__ );

		$this->output( "deleted orphaned actors\n" );

		// Update the site stats
		$users = $dbw->selectField( 'user', 'COUNT(*)', [], __METHOD__ );
		$dbw->update(
			'site_stats',
			[ 'ss_users' => $users ],
			[ 'ss_row_id' => 1 ],
			__METHOD__
		);

		$this->output( "updated the site stats for users\n" );
	}
}

$maintClass = RemoveOrphanedActors::class;
require_once RUN_MAINTENANCE_IF_MAIN;