Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
82.35% |
14 / 17 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
BatchRowWriter | |
82.35% |
14 / 17 |
|
33.33% |
1 / 3 |
5.14 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
setCaller | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
write | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
3.01 |
1 | <?php |
2 | /** |
3 | * Updates database rows by primary key in batches. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | * @ingroup Maintenance |
22 | */ |
23 | |
24 | use MediaWiki\MediaWikiServices; |
25 | use Wikimedia\Rdbms\IDatabase; |
26 | |
27 | class BatchRowWriter { |
28 | /** |
29 | * @var IDatabase The database to write to |
30 | */ |
31 | protected $db; |
32 | |
33 | /** |
34 | * @var string The name of the table to update |
35 | */ |
36 | protected $table; |
37 | |
38 | /** |
39 | * @var string|false A cluster name valid for use with LBFactory |
40 | */ |
41 | protected $clusterName; |
42 | |
43 | /** |
44 | * @var string|null For debugging which method is using this class. |
45 | */ |
46 | protected $caller; |
47 | |
48 | /** |
49 | * @param IDatabase $db The database to write to |
50 | * @param string $table The name of the table to update |
51 | * @param string|false $clusterName A cluster name valid for use with LBFactory |
52 | */ |
53 | public function __construct( IDatabase $db, $table, $clusterName = false ) { |
54 | $this->db = $db; |
55 | $this->table = $table; |
56 | $this->clusterName = $clusterName; |
57 | } |
58 | |
59 | /** |
60 | * Use ->setCaller( __METHOD__ ) to indicate which code is using this |
61 | * class. Only used in debugging output. |
62 | * @since 1.36 |
63 | * |
64 | * @param string $caller |
65 | * @return self |
66 | */ |
67 | public function setCaller( $caller ) { |
68 | $this->caller = $caller; |
69 | return $this; |
70 | } |
71 | |
72 | /** |
73 | * @param array[][] $updates Array of arrays each containing two keys, 'primaryKey' |
74 | * and 'changes'. primaryKey must contain a map of column names to values |
75 | * sufficient to uniquely identify the row. changes must contain a map of column |
76 | * names to update values to apply to the row. |
77 | * @phan-param array<int,array{primaryKey:array,changes:array}> $updates |
78 | */ |
79 | public function write( array $updates ) { |
80 | $dbProvider = MediaWikiServices::getInstance()->getConnectionProvider(); |
81 | $ticket = $dbProvider->getEmptyTransactionTicket( __METHOD__ ); |
82 | |
83 | $caller = __METHOD__; |
84 | if ( (string)$this->caller !== '' ) { |
85 | $caller .= " (for {$this->caller})"; |
86 | } |
87 | |
88 | foreach ( $updates as $update ) { |
89 | $this->db->newUpdateQueryBuilder() |
90 | ->update( $this->table ) |
91 | ->set( $update['changes'] ) |
92 | ->where( $update['primaryKey'] ) |
93 | ->caller( $caller )->execute(); |
94 | } |
95 | |
96 | $dbProvider->commitAndWaitForReplication( __METHOD__, $ticket ); |
97 | } |
98 | } |