MediaWiki master
BatchRowUpdate.php
Go to the documentation of this file.
1<?php
2/*
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
9 * for singular rows.
10 *
11 * Instantiate:
12 * $updater = new BatchRowUpdate(
13 * new BatchRowIterator( $dbr, 'some_table', 'primary_key_column', 500 ),
14 * new BatchRowWriter( $dbw, 'some_table', 'clusterName' ),
15 * new MyImplementationOfRowUpdateGenerator
16 * );
17 *
18 * Run:
19 * $updater->execute();
20 *
21 * An example maintenance script utilizing the BatchRowUpdate can be
22 * located in the Echo extension file maintenance/updateSchema.php
23 *
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.
28 *
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.
33 *
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
38 *
39 * @file
40 * @ingroup Maintenance
41 */
47 protected $reader;
48
53 protected $writer;
54
59 protected $generator;
60
64 protected $output;
65
74 public function __construct(
76 ) {
77 $this->reader = $reader;
78 $this->writer = $writer;
79 $this->generator = $generator;
80 $this->output = static function ( $text ) {
81 }; // nop
82 }
83
87 public function execute() {
88 foreach ( $this->reader as $rows ) {
89 $updates = [];
90 foreach ( $rows as $row ) {
91 $update = $this->generator->update( $row );
92 if ( $update ) {
93 $updates[] = [
94 'primaryKey' => $this->reader->extractPrimaryKeys( $row ),
95 'changes' => $update,
96 ];
97 }
98 }
99
100 if ( $updates ) {
101 $this->output( "Processing " . count( $updates ) . " rows\n" );
102 $this->writer->write( $updates );
103 }
104 }
105
106 $this->output( "Completed\n" );
107 }
108
116 public function setOutput( callable $output ) {
117 $this->output = $output;
118 }
119
125 protected function output( $text ) {
126 call_user_func( $this->output, $text );
127 }
128}
Allows iterating a large number of rows in batches transparently.
callable $output
Output callback.
setOutput(callable $output)
Accepts a callable which will receive a single parameter containing string status updates.
__construct(BatchRowIterator $reader, BatchRowWriter $writer, RowUpdateGenerator $generator)
execute()
Runs the batch update process.
BatchRowWriter $writer
Writer capable of pushing row updates to the database.
RowUpdateGenerator $generator
Generates single row updates based on the rows content.
BatchRowIterator $reader
Iterator that returns an array of database rows.
output( $text)
Write out a status update.