Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
BatchRowUpdate
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 4
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
 setOutput
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 output
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * @license GPL-2.0-or-later
4 */
5
6namespace MediaWiki\Utils;
7
8/**
9 * Ties together the batch update components to provide a composable
10 * method of batch updating rows in a database. To use create a class
11 * implementing the RowUpdateGenerator interface and configure the
12 * BatchRowIterator and BatchRowWriter for access to the correct table.
13 * The components will handle reading, writing, and waiting for replica DBs
14 * while the generator implementation handles generating update arrays
15 * for singular rows.
16 *
17 * Instantiate:
18 *   $updater = new BatchRowUpdate(
19 *       new BatchRowIterator( $dbr, 'some_table', 'primary_key_column', 500 ),
20 *       new BatchRowWriter( $dbw, 'some_table', 'clusterName' ),
21 *       new MyImplementationOfRowUpdateGenerator
22 *   );
23 *
24 * Run:
25 *   $updater->execute();
26 *
27 * An example maintenance script utilizing the BatchRowUpdate can be
28 * located in the Echo extension file maintenance/updateSchema.php
29 *
30 * @ingroup Maintenance
31 */
32class BatchRowUpdate {
33    /**
34     * @var BatchRowIterator Iterator that returns an array of
35     *  database rows
36     */
37    protected $reader;
38
39    /**
40     * @var BatchRowWriter Writer capable of pushing row updates
41     *  to the database
42     */
43    protected $writer;
44
45    /**
46     * @var RowUpdateGenerator Generates single row updates
47     *  based on the rows content
48     */
49    protected $generator;
50
51    /**
52     * @var callable Output callback
53     */
54    protected $output;
55
56    /**
57     * @param BatchRowIterator $reader Iterator that returns an
58     *  array of database rows
59     * @param BatchRowWriter $writer Writer capable of pushing
60     *  row updates to the database
61     * @param RowUpdateGenerator $generator Generates single row updates
62     *  based on the rows content
63     */
64    public function __construct(
65        BatchRowIterator $reader, BatchRowWriter $writer, RowUpdateGenerator $generator
66    ) {
67        $this->reader = $reader;
68        $this->writer = $writer;
69        $this->generator = $generator;
70        $this->output = static function ( $text ) {
71        }; // nop
72    }
73
74    /**
75     * Runs the batch update process
76     */
77    public function execute() {
78        foreach ( $this->reader as $rows ) {
79            $updates = [];
80            foreach ( $rows as $row ) {
81                $update = $this->generator->update( $row );
82                if ( $update ) {
83                    $updates[] = [
84                        'primaryKey' => $this->reader->extractPrimaryKeys( $row ),
85                        'changes' => $update,
86                    ];
87                }
88            }
89
90            if ( $updates ) {
91                $this->output( "Processing " . count( $updates ) . " rows\n" );
92                $this->writer->write( $updates );
93            }
94        }
95
96        $this->output( "Completed\n" );
97    }
98
99    /**
100     * Accepts a callable which will receive a single parameter
101     * containing string status updates
102     *
103     * @param callable $output A callback taking a single string
104     *  parameter to output
105     */
106    public function setOutput( callable $output ) {
107        $this->output = $output;
108    }
109
110    /**
111     * Write out a status update
112     *
113     * @param string $text The value to print
114     */
115    protected function output( $text ) {
116        ( $this->output )( $text );
117    }
118}
119
120/** @deprecated class alias since 1.46 */
121class_alias( BatchRowUpdate::class, 'BatchRowUpdate' );