3 * Ties together the batch update components to provide a composable
4 * method of batch updating rows in a database. To use create a class
5 * implementing the RowUpdateGenerator interface and configure the
6 * BatchRowIterator and BatchRowWriter for access to the correct table.
7 * The components will handle reading, writing, and waiting for replica DBs
8 * while the generator implementation handles generating update arrays
12 * $updater = new BatchRowUpdate(
13 * new BatchRowIterator( $dbr, 'some_table', 'primary_key_column', 500 ),
14 * new BatchRowWriter( $dbw, 'some_table', 'clusterName' ),
15 * new MyImplementationOfRowUpdateGenerator
19 * $updater->execute();
21 * An example maintenance script utilizing the BatchRowUpdate can be
22 * located in the Echo extension file maintenance/updateSchema.php
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License along
35 * with this program; if not, write to the Free Software Foundation, Inc.,
36 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
37 * http://www.gnu.org/copyleft/gpl.html
40 * @ingroup Maintenance
42 class BatchRowUpdate
{
44 * @var BatchRowIterator Iterator that returns an array of
50 * @var BatchRowWriter Writer capable of pushing row updates
56 * @var RowUpdateGenerator Generates single row updates
57 * based on the rows content
62 * @var callable Output callback
67 * @param BatchRowIterator $reader Iterator that returns an
68 * array of database rows
69 * @param BatchRowWriter $writer Writer capable of pushing
70 * row updates to the database
71 * @param RowUpdateGenerator $generator Generates single row updates
72 * based on the rows content
74 public function __construct(
75 BatchRowIterator
$reader, BatchRowWriter
$writer, RowUpdateGenerator
$generator
77 $this->reader
= $reader;
78 $this->writer
= $writer;
79 $this->generator
= $generator;
80 $this->output
= static function ( $text ) {
85 * Runs the batch update process
87 public function execute() {
88 foreach ( $this->reader
as $rows ) {
90 foreach ( $rows as $row ) {
91 $update = $this->generator
->update( $row );
94 'primaryKey' => $this->reader
->extractPrimaryKeys( $row ),
101 $this->output( "Processing " . count( $updates ) . " rows\n" );
102 $this->writer
->write( $updates );
106 $this->output( "Completed\n" );
110 * Accepts a callable which will receive a single parameter
111 * containing string status updates
113 * @param callable $output A callback taking a single string
114 * parameter to output
116 public function setOutput( callable
$output ) {
117 $this->output
= $output;
121 * Write out a status update
123 * @param string $text The value to print
125 protected function output( $text ) {
126 call_user_func( $this->output
, $text );