MediaWiki master
BatchRowUpdate.php
Go to the documentation of this file.
1<?php
6namespace MediaWiki\Utils;
7
37 protected $reader;
38
43 protected $writer;
44
49 protected $generator;
50
54 protected $output;
55
64 public function __construct(
66 ) {
67 $this->reader = $reader;
68 $this->writer = $writer;
69 $this->generator = $generator;
70 $this->output = static function ( $text ) {
71 }; // nop
72 }
73
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
106 public function setOutput( callable $output ) {
107 $this->output = $output;
108 }
109
115 protected function output( $text ) {
116 ( $this->output )( $text );
117 }
118}
119
121class_alias( BatchRowUpdate::class, 'BatchRowUpdate' );
Allows iterating a large number of rows in batches transparently.
Ties together the batch update components to provide a composable method of batch updating rows in a ...
BatchRowIterator $reader
Iterator that returns an array of database rows.
output( $text)
Write out a status update.
setOutput(callable $output)
Accepts a callable which will receive a single parameter containing string status updates.
RowUpdateGenerator $generator
Generates single row updates based on the rows content.
__construct(BatchRowIterator $reader, BatchRowWriter $writer, RowUpdateGenerator $generator)
execute()
Runs the batch update process.
callable $output
Output callback.
BatchRowWriter $writer
Writer capable of pushing row updates to the database.
Interface for generating updates to single rows in the database.
Copyright (C) 2017 Kunal Mehta legoktm@debian.org