Move Blob class to Rdbms namespaces
[mediawiki.git] / includes / utils / BatchRowUpdate.php
blob39b65c3fb3da9190f817c14b6acee64d046d5f0e
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.
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 * );
18 * Run:
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
39 * @file
40 * @ingroup Maintenance
42 class BatchRowUpdate {
43 /**
44 * @var BatchRowIterator $reader Iterator that returns an array of
45 * database rows
47 protected $reader;
49 /**
50 * @var BatchRowWriter $writer Writer capable of pushing row updates
51 * to the database
53 protected $writer;
55 /**
56 * @var RowUpdateGenerator $generator Generates single row updates
57 * based on the rows content
59 protected $generator;
61 /**
62 * @var callable $output Output callback
64 protected $output;
66 /**
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
76 ) {
77 $this->reader = $reader;
78 $this->writer = $writer;
79 $this->generator = $generator;
80 $this->output = function() {
81 }; // nop
84 /**
85 * Runs the batch update process
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,
100 if ( $updates ) {
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 );