Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 59 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
FlowFixEditCount | |
0.00% |
0 / 53 |
|
0.00% |
0 / 5 |
182 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getUpdateKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
doDBUpdates | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
refreshBatch | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
30 | |||
getCountableActions | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace Flow\Maintenance; |
4 | |
5 | use Flow\Container; |
6 | use Flow\FlowActions; |
7 | use Flow\Model\UUID; |
8 | use LoggedUpdateMaintenance; |
9 | use MediaWiki\User\User; |
10 | use MediaWiki\WikiMap\WikiMap; |
11 | use Wikimedia\Rdbms\IDatabase; |
12 | |
13 | $IP = getenv( 'MW_INSTALL_PATH' ); |
14 | if ( $IP === false ) { |
15 | $IP = __DIR__ . '/../../..'; |
16 | } |
17 | |
18 | require_once "$IP/maintenance/Maintenance.php"; |
19 | |
20 | /** |
21 | * Adjusts edit counts for all existing Flow data. |
22 | * |
23 | * @ingroup Maintenance |
24 | */ |
25 | class FlowFixEditCount extends LoggedUpdateMaintenance { |
26 | /** |
27 | * Array of [username => increased edit count] |
28 | * |
29 | * @var array |
30 | */ |
31 | protected $updates = []; |
32 | |
33 | public function __construct() { |
34 | parent::__construct(); |
35 | |
36 | $this->addDescription( 'Adjusts edit counts for all existing Flow data' ); |
37 | |
38 | $this->addOption( 'start', 'Timestamp to start counting revisions at', false, true ); |
39 | $this->addOption( 'stop', 'Timestamp to stop counting revisions at', false, true ); |
40 | |
41 | $this->setBatchSize( 300 ); |
42 | |
43 | $this->requireExtension( 'Flow' ); |
44 | } |
45 | |
46 | protected function getUpdateKey() { |
47 | return 'FlowFixEditCount'; |
48 | } |
49 | |
50 | protected function doDBUpdates() { |
51 | /** @var IDatabase $dbr */ |
52 | $dbr = Container::get( 'db.factory' )->getDB( DB_REPLICA ); |
53 | $countableActions = $this->getCountableActions(); |
54 | |
55 | // defaults = date of first Flow commit up until now |
56 | $continue = UUID::getComparisonUUID( $this->getOption( 'start', '20130710230511' ) ); |
57 | $stop = UUID::getComparisonUUID( $this->getOption( 'stop', time() ) ); |
58 | |
59 | $lbFactory = $this->getServiceContainer()->getDBLoadBalancerFactory(); |
60 | |
61 | while ( $continue !== false ) { |
62 | $continue = $this->refreshBatch( $dbr, $continue, $countableActions, $stop ); |
63 | |
64 | // wait for core (we're updating user table) replicas to catch up |
65 | $lbFactory->waitForReplication(); |
66 | } |
67 | |
68 | $this->output( "Done increasing edit counts. Increased:\n" ); |
69 | foreach ( $this->updates as $userId => $count ) { |
70 | $userName = User::newFromId( $userId )->getName(); |
71 | $this->output( " User $userId ($userName): +$count\n" ); |
72 | } |
73 | |
74 | return true; |
75 | } |
76 | |
77 | public function refreshBatch( IDatabase $dbr, UUID $continue, array $countableActions, UUID $stop ) { |
78 | $rows = $dbr->select( |
79 | 'flow_revision', |
80 | [ 'rev_id', 'rev_user_id' ], |
81 | [ |
82 | 'rev_id > ' . $dbr->addQuotes( $continue->getBinary() ), |
83 | 'rev_id <= ' . $dbr->addQuotes( $stop->getBinary() ), |
84 | 'rev_user_id > 0', |
85 | 'rev_user_wiki' => WikiMap::getCurrentWikiId(), |
86 | 'rev_change_type' => $countableActions, |
87 | ], |
88 | __METHOD__, |
89 | [ |
90 | 'ORDER BY' => 'rev_id ASC', |
91 | 'LIMIT' => $this->getBatchSize(), |
92 | ] |
93 | ); |
94 | |
95 | // end of data |
96 | if ( !$rows || $rows->numRows() === 0 ) { |
97 | return false; |
98 | } |
99 | |
100 | $userEditTracker = $this->getServiceContainer()->getUserEditTracker(); |
101 | foreach ( $rows as $row ) { |
102 | // UserEditTracker::incrementUserEditCount only allows for edit count to be |
103 | // increased 1 at a time. It'd be better to immediately be able to increase |
104 | // the edit count by the exact number it should be increased with, but |
105 | // I'd rather re-use existing code, especially in a run-once script, |
106 | // where performance is not the most important thing ;) |
107 | $user = User::newFromId( $row->rev_user_id ); |
108 | $userEditTracker->incrementUserEditCount( $user ); |
109 | |
110 | // save updates so we can print them when the script is done running |
111 | if ( !isset( $this->updates[$user->getId()] ) ) { |
112 | $this->updates[$user->getId()] = 0; |
113 | } |
114 | $this->updates[$user->getId()]++; |
115 | |
116 | // set value for next batch to continue at |
117 | $continue = $row->rev_id; |
118 | } |
119 | |
120 | return UUID::create( $continue ); |
121 | } |
122 | |
123 | /** |
124 | * Returns list of rev_change_type values that warrant an editcount increase. |
125 | * |
126 | * @return string[] |
127 | */ |
128 | protected function getCountableActions() { |
129 | $allowedActions = []; |
130 | |
131 | /** @var FlowActions $actions */ |
132 | $actions = Container::get( 'flow_actions' ); |
133 | foreach ( $actions->getActions() as $action ) { |
134 | if ( $actions->getValue( $action, 'editcount' ) ) { |
135 | $allowedActions[] = $action; |
136 | } |
137 | } |
138 | |
139 | return $allowedActions; |
140 | } |
141 | } |
142 | |
143 | $maintClass = FlowFixEditCount::class; |
144 | require_once RUN_MAINTENANCE_IF_MAIN; |