2015-07-30 22:09:11 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Updates database rows by primary key in batches.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2017-12-28 15:30:05 +00:00
|
|
|
|
|
|
|
|
use MediaWiki\MediaWikiServices;
|
2017-02-10 18:09:05 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2016-08-22 03:03:17 +00:00
|
|
|
|
2015-07-30 22:09:11 +00:00
|
|
|
class BatchRowWriter {
|
|
|
|
|
/**
|
2020-04-07 21:38:17 +00:00
|
|
|
* @var IDatabase The database to write to
|
2015-07-30 22:09:11 +00:00
|
|
|
*/
|
|
|
|
|
protected $db;
|
|
|
|
|
|
|
|
|
|
/**
|
2020-04-07 21:38:17 +00:00
|
|
|
* @var string The name of the table to update
|
2015-07-30 22:09:11 +00:00
|
|
|
*/
|
|
|
|
|
protected $table;
|
|
|
|
|
|
|
|
|
|
/**
|
2020-04-07 21:38:17 +00:00
|
|
|
* @var string|false A cluster name valid for use with LBFactory
|
2015-07-30 22:09:11 +00:00
|
|
|
*/
|
|
|
|
|
protected $clusterName;
|
|
|
|
|
|
2020-08-17 22:05:37 +00:00
|
|
|
/**
|
2020-10-28 19:44:09 +00:00
|
|
|
* @var string|null For debugging which method is using this class.
|
2020-08-17 22:05:37 +00:00
|
|
|
*/
|
|
|
|
|
protected $caller;
|
|
|
|
|
|
2015-07-30 22:09:11 +00:00
|
|
|
/**
|
2015-10-04 09:07:25 +00:00
|
|
|
* @param IDatabase $db The database to write to
|
2017-08-11 21:45:25 +00:00
|
|
|
* @param string $table The name of the table to update
|
2019-12-30 12:34:27 +00:00
|
|
|
* @param string|false $clusterName A cluster name valid for use with LBFactory
|
2015-07-30 22:09:11 +00:00
|
|
|
*/
|
2015-10-04 09:07:25 +00:00
|
|
|
public function __construct( IDatabase $db, $table, $clusterName = false ) {
|
2015-07-30 22:09:11 +00:00
|
|
|
$this->db = $db;
|
|
|
|
|
$this->table = $table;
|
|
|
|
|
$this->clusterName = $clusterName;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 22:05:37 +00:00
|
|
|
/**
|
|
|
|
|
* Use ->setCaller( __METHOD__ ) to indicate which code is using this
|
|
|
|
|
* class. Only used in debugging output.
|
|
|
|
|
* @since 1.36
|
|
|
|
|
*
|
|
|
|
|
* @param string $caller
|
|
|
|
|
* @return self
|
|
|
|
|
*/
|
|
|
|
|
public function setCaller( $caller ) {
|
|
|
|
|
$this->caller = $caller;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-30 22:09:11 +00:00
|
|
|
/**
|
2019-10-11 14:45:54 +00:00
|
|
|
* @param array[][] $updates Array of arrays each containing two keys, 'primaryKey'
|
2015-08-14 21:17:01 +00:00
|
|
|
* and 'changes'. primaryKey must contain a map of column names to values
|
2019-10-11 14:45:54 +00:00
|
|
|
* sufficient to uniquely identify the row. changes must contain a map of column
|
2015-07-30 22:09:11 +00:00
|
|
|
* names to update values to apply to the row.
|
2019-10-11 14:45:54 +00:00
|
|
|
* @phan-param array<int,array{primaryKey:array,changes:array}> $updates
|
2015-07-30 22:09:11 +00:00
|
|
|
*/
|
|
|
|
|
public function write( array $updates ) {
|
2024-01-22 21:32:48 +00:00
|
|
|
$dbProvider = MediaWikiServices::getInstance()->getConnectionProvider();
|
|
|
|
|
$ticket = $dbProvider->getEmptyTransactionTicket( __METHOD__ );
|
2015-07-30 22:09:11 +00:00
|
|
|
|
2020-08-17 22:05:37 +00:00
|
|
|
$caller = __METHOD__;
|
|
|
|
|
if ( (string)$this->caller !== '' ) {
|
|
|
|
|
$caller .= " (for {$this->caller})";
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-30 22:09:11 +00:00
|
|
|
foreach ( $updates as $update ) {
|
2023-06-09 12:19:22 +00:00
|
|
|
$this->db->newUpdateQueryBuilder()
|
|
|
|
|
->update( $this->table )
|
|
|
|
|
->set( $update['changes'] )
|
|
|
|
|
->where( $update['primaryKey'] )
|
|
|
|
|
->caller( $caller )->execute();
|
2015-07-30 22:09:11 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-22 21:32:48 +00:00
|
|
|
$dbProvider->commitAndWaitForReplication( __METHOD__, $ticket );
|
2015-07-30 22:09:11 +00:00
|
|
|
}
|
|
|
|
|
}
|