26 require_once __DIR__ .
'/Maintenance.php';
37 parent::__construct();
39 "Run an update query on all rows of a table. " .
40 "Waits for replicas at appropriate intervals." );
41 $this->
addOption(
'table',
'The table name',
true,
true );
42 $this->
addOption(
'set',
'The SET clause',
true,
true );
43 $this->
addOption(
'where',
'The WHERE clause',
false,
true );
44 $this->
addOption(
'key',
'A column name, the values of which are unique',
true,
true );
45 $this->
addOption(
'batch-size',
'The batch size (default 1000)',
false,
true );
46 $this->
addOption(
'db',
'The database name, or omit to use the current wiki.',
false,
true );
53 $where = $this->
getOption(
'where',
null );
54 $where = $where ===
null ? [] : [ $where ];
55 $batchSize = $this->
getOption(
'batch-size', 1000 );
58 if ( $dbName ===
null ) {
62 $lb = $lbf->getMainLB( $dbName );
63 $dbw = $lb->getConnectionRef(
DB_PRIMARY, [], $dbName );
66 $selectConds = $where;
71 $this->
output(
"Batch $n: " );
80 $res = $dbw->select( $table, $key, $selectConds, __METHOD__,
81 [
'ORDER BY' => $key,
'LIMIT' => $batchSize ] );
82 if ( $res->numRows() ) {
83 $res->seek( $res->numRows() - 1 );
84 $row = $res->fetchObject();
85 $end = $dbw->addQuotes( $row->$key );
86 $selectConds = array_merge( $where, [
"$key > $end" ] );
87 $updateConds = array_merge( $where, [
"$key <= $end" ] );
89 $updateConds = $where;
92 if ( $prevEnd !==
false ) {
93 $updateConds = array_merge( [
"$key > $prevEnd" ], $updateConds );
96 $query =
"UPDATE " . $dbw->tableName( $table ) .
100 $dbw->query( $query, __METHOD__ );
104 $affected = $dbw->affectedRows();
105 $this->
output(
"$affected rows affected\n" );
107 }
while ( $res->numRows() );
116 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.
waitForReplication()
Wait for replica DBs to catch up.
getServiceContainer()
Returns the main service container.
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...