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