Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 109
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
FlowUpdateRecentChanges
0.00% covered (danger)
0.00%
0 / 103
0.00% covered (danger)
0.00%
0 / 4
870
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 doDBUpdates
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 refreshBatch
0.00% covered (danger)
0.00%
0 / 92
0.00% covered (danger)
0.00%
0 / 1
650
 getUpdateKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Flow\Maintenance;
4
5use Flow\Data\Listener\RecentChangesListener;
6use LoggedUpdateMaintenance;
7use Wikimedia\AtEase\AtEase;
8use Wikimedia\Rdbms\IDatabase;
9
10$IP = getenv( 'MW_INSTALL_PATH' );
11if ( $IP === false ) {
12    $IP = __DIR__ . '/../../..';
13}
14
15require_once "$IP/maintenance/Maintenance.php";
16
17/**
18 * Updates recentchanges entries to contain information to build the
19 * AbstractBlock objects.
20 *
21 * @ingroup Maintenance
22 */
23class FlowUpdateRecentChanges extends LoggedUpdateMaintenance {
24    /**
25     * The number of entries completed
26     *
27     * @var int
28     */
29    private $completeCount = 0;
30
31    public function __construct() {
32        parent::__construct();
33
34        $this->setBatchSize( 300 );
35        $this->requireExtension( 'Flow' );
36    }
37
38    protected function doDBUpdates() {
39        $dbw = $this->getPrimaryDB();
40
41        $continue = 0;
42
43        $lbFactory = $this->getServiceContainer()->getDBLoadBalancerFactory();
44
45        while ( $continue !== null ) {
46            $continue = $this->refreshBatch( $dbw, $continue );
47            $lbFactory->waitForReplication();
48        }
49
50        return true;
51    }
52
53    /**
54     * Refreshes a batch of recentchanges entries
55     *
56     * @param IDatabase $dbw
57     * @param int|null $continue The next batch starting at rc_id
58     * @return int|null Start id for the next batch
59     */
60    public function refreshBatch( IDatabase $dbw, $continue = null ) {
61        $rows = $dbw->newSelectQueryBuilder()
62            ->select( [ 'rc_id', 'rc_params' ] )
63            ->from( 'recentchanges' )
64            ->where( [
65                $dbw->expr( 'rc_id', '>', $continue ),
66                'rc_source' => RecentChangesListener::SRC_FLOW
67            ] )
68            ->limit( $this->getBatchSize() )
69            ->orderBy( 'rc_id' )
70            ->caller( __METHOD__ )
71            ->fetchResultSet();
72
73        $continue = null;
74
75        foreach ( $rows as $row ) {
76            $continue = $row->rc_id;
77
78            // build params
79            AtEase::suppressWarnings();
80            $params = unserialize( $row->rc_params );
81            AtEase::restoreWarnings();
82            if ( !$params ) {
83                $params = [];
84            }
85
86            // Don't fix entries that have been dealt with already
87            if ( !isset( $params['flow-workflow-change']['type'] ) ) {
88                continue;
89            }
90
91            // Set action, based on older 'type' values
92            switch ( $params['flow-workflow-change']['type'] ) {
93                case 'flow-rev-message-edit-title':
94                case 'flow-edit-title':
95                    $params['flow-workflow-change']['action'] = 'edit-title';
96                    $params['flow-workflow-change']['block'] = 'topic';
97                    $params['flow-workflow-change']['revision_type'] = 'PostRevision';
98                    break;
99
100                case 'flow-rev-message-new-post':
101                case 'flow-new-post':
102                    $params['flow-workflow-change']['action'] = 'new-post';
103                    $params['flow-workflow-change']['block'] = 'topic';
104                    $params['flow-workflow-change']['revision_type'] = 'PostRevision';
105                    break;
106
107                case 'flow-rev-message-edit-post':
108                case 'flow-edit-post':
109                    $params['flow-workflow-change']['action'] = 'edit-post';
110                    $params['flow-workflow-change']['block'] = 'topic';
111                    $params['flow-workflow-change']['revision_type'] = 'PostRevision';
112                    break;
113
114                case 'flow-rev-message-reply':
115                case 'flow-reply':
116                    $params['flow-workflow-change']['action'] = 'reply';
117                    $params['flow-workflow-change']['block'] = 'topic';
118                    $params['flow-workflow-change']['revision_type'] = 'PostRevision';
119                    break;
120
121                case 'flow-rev-message-restored-post':
122                case 'flow-post-restored':
123                    $params['flow-workflow-change']['action'] = 'restore-post';
124                    $params['flow-workflow-change']['block'] = 'topic';
125                    $params['flow-workflow-change']['revision_type'] = 'PostRevision';
126                    break;
127
128                case 'flow-rev-message-hid-post':
129                case 'flow-post-hidden':
130                    $params['flow-workflow-change']['action'] = 'hide-post';
131                    $params['flow-workflow-change']['block'] = 'topic';
132                    $params['flow-workflow-change']['revision_type'] = 'PostRevision';
133                    break;
134
135                case 'flow-rev-message-deleted-post':
136                case 'flow-post-deleted':
137                    $params['flow-workflow-change']['action'] = 'delete-post';
138                    $params['flow-workflow-change']['block'] = 'topic';
139                    $params['flow-workflow-change']['revision_type'] = 'PostRevision';
140                    break;
141
142                case 'flow-rev-message-censored-post':
143                case 'flow-post-censored':
144                    $params['flow-workflow-change']['action'] = 'suppress-post';
145                    $params['flow-workflow-change']['block'] = 'topic';
146                    $params['flow-workflow-change']['revision_type'] = 'PostRevision';
147                    break;
148
149                case 'flow-rev-message-edit-header':
150                case 'flow-edit-summary':
151                    $params['flow-workflow-change']['action'] = 'edit-header';
152                    $params['flow-workflow-change']['block'] = 'header';
153                    $params['flow-workflow-change']['revision_type'] = 'Header';
154                    break;
155
156                case 'flow-rev-message-create-header':
157                case 'flow-create-summary':
158                case 'flow-create-header':
159                    $params['flow-workflow-change']['action'] = 'create-header';
160                    $params['flow-workflow-change']['block'] = 'header';
161                    $params['flow-workflow-change']['revision_type'] = 'Header';
162                    break;
163            }
164
165            unset( $params['flow-workflow-change']['type'] );
166
167            // update log entry
168            $dbw->newUpdateQueryBuilder()
169                ->update( 'recentchanges' )
170                ->set( [ 'rc_params' => serialize( $params ) ] )
171                ->where( [ 'rc_id' => $row->rc_id ] )
172                ->caller( __METHOD__ )
173                ->execute();
174
175            $this->completeCount++;
176        }
177
178        return $continue;
179    }
180
181    /**
182     * Get the update key name to go in the update log table
183     *
184     * @return string
185     */
186    protected function getUpdateKey() {
187        return 'FlowUpdateRecentChanges';
188    }
189}
190
191$maintClass = FlowUpdateRecentChanges::class;
192require_once RUN_MAINTENANCE_IF_MAIN;