26 require_once __DIR__ .
'/Maintenance.php';
38 parent::__construct();
40 "Run an update query on all rows of a table. " .
41 "Waits for replicas at appropriate intervals." );
42 $this->
addOption(
'table',
'The table name',
true,
true );
43 $this->
addOption(
'set',
'The SET clause',
true,
true );
44 $this->
addOption(
'where',
'The WHERE clause',
false,
true );
45 $this->
addOption(
'key',
'A column name, the values of which are unique',
true,
true );
46 $this->
addOption(
'batch-size',
'The batch size (default 1000)',
false,
true );
47 $this->
addOption(
'db',
'The database name, or omit to use the current wiki.',
false,
true );
54 $where = $this->
getOption(
'where',
null );
55 $where = $where ===
null ? [] : [ $where ];
56 $batchSize = $this->
getOption(
'batch-size', 1000 );
59 if ( $dbName ===
null ) {
63 $lb = $lbf->getMainLB( $dbName );
64 $dbw = $lb->getConnectionRef(
DB_PRIMARY, [], $dbName );
67 $selectConds = $where;
69 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
73 $this->
output(
"Batch $n: " );
82 $res = $dbw->select( $table, $key, $selectConds, __METHOD__,
83 [
'ORDER BY' => $key,
'LIMIT' => $batchSize ] );
84 if (
$res->numRows() ) {
86 $row =
$res->fetchObject();
87 $end = $dbw->addQuotes( $row->$key );
88 $selectConds = array_merge( $where, [
"$key > $end" ] );
89 $updateConds = array_merge( $where, [
"$key <= $end" ] );
91 $updateConds = $where;
94 if ( $prevEnd !==
false ) {
95 $updateConds = array_merge( [
"$key > $prevEnd" ], $updateConds );
98 $query =
"UPDATE " . $dbw->tableName( $table ) .
102 $dbw->query( $query, __METHOD__ );
106 $affected = $dbw->affectedRows();
107 $this->
output(
"$affected rows affected\n" );
108 $lbFactory->waitForReplication();
109 }
while (
$res->numRows() );
118 require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
output( $out, $channel=null)
Throw some output to the user.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
Maintenance script to run a database query in batches and wait for replica DBs.
__construct()
Default constructor.
execute()
Do the actual work.
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...