MediaWiki master
runBatchedQuery.php
Go to the documentation of this file.
1<?php
26require_once __DIR__ . '/Maintenance.php';
27
29
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription(
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 );
47 }
48
49 public function execute() {
50 $table = $this->getOption( 'table' );
51 $key = $this->getOption( 'key' );
52 $set = $this->getOption( 'set' );
53 $where = $this->getOption( 'where', null );
54 $where = $where === null ? [] : [ $where ];
55 $batchSize = $this->getOption( 'batch-size', 1000 );
56
57 $dbName = $this->getOption( 'db', null );
58 if ( $dbName === null ) {
59 $dbw = $this->getPrimaryDB();
60 } else {
61 $dbw = $this->getServiceContainer()->getConnectionProvider()->getPrimaryDatabase( $dbName );
62 }
63
64 $selectConds = $where;
65 $prevEnd = false;
66
67 $n = 1;
68 do {
69 $this->output( "Batch $n: " );
70 $n++;
71
72 // Note that the update conditions do not rely on the atomicity of the
73 // SELECT query in order to guarantee that all rows are updated. The
74 // results of the SELECT are merely a partitioning hint. Simultaneous
75 // updates merely result in the wrong number of rows being updated
76 // in a batch.
77
78 $res = $dbw->newSelectQueryBuilder()
79 ->select( $key )
80 ->from( $table )
81 ->where( $selectConds )
82 ->orderBy( $key )
83 ->limit( $batchSize )
84 ->caller( __METHOD__ )
85 ->fetchResultSet();
86
87 if ( $res->numRows() ) {
88 $res->seek( $res->numRows() - 1 );
89 $row = $res->fetchObject();
90 $end = $dbw->addQuotes( $row->$key );
91 $selectConds = array_merge( $where, [ "$key > $end" ] );
92 $updateConds = array_merge( $where, [ "$key <= $end" ] );
93 } else {
94 $updateConds = $where;
95 $end = false;
96 }
97 if ( $prevEnd !== false ) {
98 $updateConds = array_merge( [ "$key > $prevEnd" ], $updateConds );
99 }
100
101 $query = "UPDATE " . $dbw->tableName( $table ) .
102 " SET " . $set .
103 " WHERE " . $dbw->makeList( $updateConds, ISQLPlatform::LIST_AND );
104
105 $dbw->query( $query, __METHOD__ );
106
107 $prevEnd = $end;
108
109 $affected = $dbw->affectedRows();
110 $this->output( "$affected rows affected\n" );
111 $this->waitForReplication();
112 } while ( $res->numRows() );
113 }
114
115 public function getDbType() {
117 }
118}
119
120$maintClass = RunBatchedQuery::class;
121require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
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...
Interface for query language.