Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
FlowUpdateRevisionTypeId
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 4
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 doDBUpdates
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
42
 updateRevision
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 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\Container;
6use Flow\DbFactory;
7use LoggedUpdateMaintenance;
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 * Update flow_revision.rev_type_id
19 *
20 * @ingroup Maintenance
21 */
22class FlowUpdateRevisionTypeId extends LoggedUpdateMaintenance {
23
24    public function __construct() {
25        parent::__construct();
26        $this->addDescription( "Update flow_revision.rev_type_id" );
27        $this->requireExtension( 'Flow' );
28        $this->setBatchSize( 300 );
29    }
30
31    protected function doDBUpdates() {
32        $revId = '';
33        $batchSize = $this->getBatchSize();
34        $count = $this->getBatchSize();
35        /** @var DbFactory $dbFactory */
36        $dbFactory = Container::get( 'db.factory' );
37        $dbr = $dbFactory->getDB( DB_REPLICA );
38        $dbw = $dbFactory->getDB( DB_PRIMARY );
39
40        // If table flow_header_revision does not exist, that means the wiki
41        // has run the data migration before or the wiki starts from scratch,
42        // there is no point to run the script against invalid tables
43        if ( !$dbr->tableExists( 'flow_header_revision', __METHOD__ ) ) {
44            return true;
45        }
46
47        while ( $count == $batchSize ) {
48            $count = 0;
49            $res = $dbr->select(
50                [ 'flow_revision', 'flow_tree_revision', 'flow_header_revision' ],
51                [ 'rev_id', 'rev_type', 'tree_rev_descendant_id', 'header_workflow_id' ],
52                [ 'rev_id > ' . $dbr->addQuotes( $revId ) ],
53                __METHOD__,
54                [ 'ORDER BY' => 'rev_id ASC', 'LIMIT' => $batchSize ],
55                [
56                    'flow_tree_revision' => [ 'LEFT JOIN', 'rev_id=tree_rev_id' ],
57                    'flow_header_revision' => [ 'LEFT JOIN', 'rev_id=header_rev_id' ]
58                ]
59            );
60
61            foreach ( $res as $row ) {
62                $count++;
63                $revId = $row->rev_id;
64                switch ( $row->rev_type ) {
65                    case 'header':
66                        $this->updateRevision( $dbw, $row->rev_id, $row->header_workflow_id );
67                        break;
68                    case 'post':
69                        $this->updateRevision( $dbw, $row->rev_id, $row->tree_rev_descendant_id );
70                        break;
71                }
72            }
73            $dbFactory->waitForReplicas();
74        }
75
76        $dbw->dropTable( 'flow_header_revision', __METHOD__ );
77
78        return true;
79    }
80
81    /**
82     * @param IDatabase $dbw
83     * @param int $revId
84     * @param string $revTypeId
85     */
86    private function updateRevision( $dbw, $revId, $revTypeId ) {
87        if ( $revTypeId === null ) {
88            // this shouldn't actually be happening, but if it is, ignoring it
89            // will not make things worse - the revision is lost already
90            return;
91        }
92
93        $dbw->newUpdateQueryBuilder()
94            ->update( 'flow_revision' )
95            ->set( [ 'rev_type_id' => $revTypeId ] )
96            ->where( [ 'rev_id' => $revId ] )
97            ->caller( __METHOD__ )
98            ->execute();
99    }
100
101    /**
102     * Get the update key name to go in the update log table
103     *
104     * @return string
105     */
106    protected function getUpdateKey() {
107        return 'FlowUpdateRevisionTypeId';
108    }
109}
110
111$maintClass = FlowUpdateRevisionTypeId::class;
112require_once RUN_MAINTENANCE_IF_MAIN;