Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.78% covered (warning)
77.78%
14 / 18
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
BatchRowWriter
82.35% covered (warning)
82.35%
14 / 17
33.33% covered (danger)
33.33%
1 / 3
5.14
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 setCaller
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 write
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
3.01
1<?php
2/**
3 * Updates database rows by primary key in batches.
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 * @ingroup Maintenance
8 */
9
10namespace MediaWiki\Utils;
11
12use MediaWiki\MediaWikiServices;
13use Wikimedia\Rdbms\IDatabase;
14
15class BatchRowWriter {
16    /**
17     * @var IDatabase The database to write to
18     */
19    protected $db;
20
21    /**
22     * @var string The name of the table to update
23     */
24    protected $table;
25
26    /**
27     * @var string|false A cluster name valid for use with LBFactory
28     */
29    protected $clusterName;
30
31    /**
32     * @var string|null For debugging which method is using this class.
33     */
34    protected $caller;
35
36    /**
37     * @param IDatabase $db The database to write to
38     * @param string $table The name of the table to update
39     * @param string|false $clusterName A cluster name valid for use with LBFactory
40     */
41    public function __construct( IDatabase $db, $table, $clusterName = false ) {
42        $this->db = $db;
43        $this->table = $table;
44        $this->clusterName = $clusterName;
45    }
46
47    /**
48     * Use ->setCaller( __METHOD__ ) to indicate which code is using this
49     * class. Only used in debugging output.
50     * @since 1.36
51     *
52     * @param string $caller
53     * @return self
54     */
55    public function setCaller( $caller ) {
56        $this->caller = $caller;
57        return $this;
58    }
59
60    /**
61     * @param array[][] $updates Array of arrays each containing two keys, 'primaryKey'
62     *  and 'changes'. primaryKey must contain a map of column names to values
63     *  sufficient to uniquely identify the row. changes must contain a map of column
64     *  names to update values to apply to the row.
65     * @phan-param array<int,array{primaryKey:array,changes:array}> $updates
66     */
67    public function write( array $updates ) {
68        $dbProvider = MediaWikiServices::getInstance()->getConnectionProvider();
69        $ticket = $dbProvider->getEmptyTransactionTicket( __METHOD__ );
70
71        $caller = __METHOD__;
72        if ( (string)$this->caller !== '' ) {
73            $caller .= " (for {$this->caller})";
74        }
75
76        foreach ( $updates as $update ) {
77            $this->db->newUpdateQueryBuilder()
78                ->update( $this->table )
79                ->set( $update['changes'] )
80                ->where( $update['primaryKey'] )
81                ->caller( $caller )->execute();
82        }
83
84        $dbProvider->commitAndWaitForReplication( __METHOD__, $ticket );
85    }
86}
87
88/** @deprecated class alias since 1.46 */
89class_alias( BatchRowWriter::class, 'BatchRowWriter' );