Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 77
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
FlowPopulateLinksTables
0.00% covered (danger)
0.00%
0 / 71
0.00% covered (danger)
0.00%
0 / 5
132
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
 getUpdateKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doDBUpdates
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 processHeaders
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
20
 processPosts
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace Flow\Maintenance;
4
5use Flow\Container;
6use Flow\DbFactory;
7use Flow\Model\UUID;
8use LoggedUpdateMaintenance;
9
10$IP = getenv( 'MW_INSTALL_PATH' );
11if ( $IP === false ) {
12    $IP = __DIR__ . '/../../..';
13}
14
15require_once "$IP/maintenance/Maintenance.php";
16
17/**
18 * Currently iterates through all revisions for debugging purposes, the
19 * production version will want to only process the most recent revision
20 * of each object.
21 *
22 * @ingroup Maintenance
23 */
24class FlowPopulateLinksTables extends LoggedUpdateMaintenance {
25    public function __construct() {
26        parent::__construct();
27        $this->addDescription( "Populates links tables for wikis deployed before change 110090" );
28        $this->setBatchSize( 300 );
29        $this->requireExtension( 'Flow' );
30    }
31
32    public function getUpdateKey() {
33        return "FlowPopulateLinksTables";
34    }
35
36    public function doDBUpdates() {
37        $this->output( "Populating links tables...\n" );
38        $recorder = Container::get( 'reference.recorder' );
39        $this->processHeaders( $recorder );
40        $this->processPosts( $recorder );
41
42        return true;
43    }
44
45    protected function processHeaders( $recorder ) {
46        $storage = Container::get( 'storage.header' );
47        $batchSize = $this->getBatchSize();
48        $count = $batchSize;
49        $id = '';
50        /** @var DbFactory $dbf */
51        $dbf = Container::get( 'db.factory' );
52        $dbr = $dbf->getDB( DB_REPLICA );
53        while ( $count === $batchSize ) {
54            $count = 0;
55            $res = $dbr->select(
56                [ 'flow_revision' ],
57                [ 'rev_type_id' ],
58                [ 'rev_type' => 'header', 'rev_type_id > ' . $dbr->addQuotes( $id ) ],
59                __METHOD__,
60                [ 'ORDER BY' => 'rev_type_id ASC', 'LIMIT' => $batchSize ]
61            );
62            foreach ( $res as $row ) {
63                $count++;
64                $id = $row->rev_type_id;
65                $uuid = UUID::create( $id );
66                $alpha = $uuid->getAlphadecimal();
67                $header = $storage->get( $uuid );
68                if ( $header ) {
69                    echo "Processing header $alpha\n";
70                    $recorder->onAfterInsert(
71                        $header, [],
72                        [
73                            'workflow' => $header->getCollection()->getWorkflow()
74                        ]
75                    );
76                }
77            }
78            $dbf->waitForReplicas();
79        }
80    }
81
82    protected function processPosts( $recorder ) {
83        $storage = Container::get( 'storage.post' );
84        $batchSize = $this->getBatchSize();
85        $count = $batchSize;
86        $id = '';
87        $dbr = Container::get( 'db.factory' )->getDB( DB_REPLICA );
88        while ( $count === $batchSize ) {
89            $count = 0;
90            $res = $dbr->select(
91                [ 'flow_tree_revision' ],
92                [ 'tree_rev_id' ],
93                [
94                    'tree_parent_id IS NOT NULL',
95                    'tree_rev_id > ' . $dbr->addQuotes( $id ),
96                ],
97                __METHOD__,
98                [ 'ORDER BY' => 'tree_rev_id ASC', 'LIMIT' => $batchSize ]
99            );
100            foreach ( $res as $row ) {
101                $count++;
102                $id = $row->tree_rev_id;
103                $uuid = UUID::create( $id );
104                $alpha = $uuid->getAlphadecimal();
105                $post = $storage->get( $uuid );
106                if ( $post ) {
107                    echo "Processing post $alpha\n";
108                    $recorder->onAfterInsert(
109                        $post, [],
110                        [
111                            'workflow' => $post->getCollection()->getWorkflow()
112                        ]
113                    );
114                }
115            }
116        }
117    }
118}
119
120$maintClass = FlowPopulateLinksTables::class;
121require_once RUN_MAINTENANCE_IF_MAIN;