2017-03-02 21:21:43 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Initialize a user preference based on the value
|
|
|
|
|
* of another preference.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2017-03-02 21:21:43 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2017-03-02 21:21:43 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maintenance script that initializes a user preference
|
|
|
|
|
* based on the value of another preference.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
class InitUserPreference extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->addOption(
|
|
|
|
|
'target',
|
|
|
|
|
'Name of the user preference to initialize',
|
|
|
|
|
true,
|
|
|
|
|
true,
|
|
|
|
|
't'
|
|
|
|
|
);
|
|
|
|
|
$this->addOption(
|
|
|
|
|
'source',
|
|
|
|
|
'Name of the user preference to take the value from',
|
|
|
|
|
true,
|
|
|
|
|
true,
|
|
|
|
|
's'
|
|
|
|
|
);
|
|
|
|
|
$this->setBatchSize( 300 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$target = $this->getOption( 'target' );
|
|
|
|
|
$source = $this->getOption( 'source' );
|
|
|
|
|
$this->output( "Initializing '$target' based on the value of '$source'\n" );
|
|
|
|
|
|
2024-01-17 18:53:40 +00:00
|
|
|
$dbr = $this->getReplicaDB();
|
|
|
|
|
$dbw = $this->getPrimaryDB();
|
2017-03-02 21:21:43 +00:00
|
|
|
|
|
|
|
|
$iterator = new BatchRowIterator(
|
|
|
|
|
$dbr,
|
|
|
|
|
'user_properties',
|
|
|
|
|
[ 'up_user', 'up_property' ],
|
2017-11-05 08:09:51 +00:00
|
|
|
$this->getBatchSize()
|
2017-03-02 21:21:43 +00:00
|
|
|
);
|
|
|
|
|
$iterator->setFetchColumns( [ 'up_user', 'up_value' ] );
|
|
|
|
|
$iterator->addConditions( [
|
|
|
|
|
'up_property' => $source,
|
2024-07-22 20:29:20 +00:00
|
|
|
$dbr->expr( 'up_value', '!=', null ),
|
|
|
|
|
$dbr->expr( 'up_value', '!=', 0 ),
|
2017-03-02 21:21:43 +00:00
|
|
|
] );
|
2020-09-04 17:14:14 +00:00
|
|
|
$iterator->setCaller( __METHOD__ );
|
2017-03-02 21:21:43 +00:00
|
|
|
|
|
|
|
|
$processed = 0;
|
|
|
|
|
foreach ( $iterator as $batch ) {
|
|
|
|
|
foreach ( $batch as $row ) {
|
2023-09-05 19:21:03 +00:00
|
|
|
$dbw->newInsertQueryBuilder()
|
In query builders, use insertInto() and deleteFrom() instead of insert() and delete()
The design principle for SelectQueryBuilder was to make the chained
builder calls look as much like SQL as possible, so that developers
could leverage their knowledge of SQL to understand what the query
builder is doing.
That's why SelectQueryBuilder::select() takes a list of fields, and by
the same principle, it makes sense for UpdateQueryBuilder::update() to
take a table. However with "insert" and "delete", the SQL designers
chose to add prepositions "into" and "from", and I think it makes sense
to follow that here.
In terms of natural language, we update a table, but we don't delete a
table, or insert a table. We delete rows from a table, or insert rows
into a table. The table is not the object of the verb.
So, add insertInto() as an alias for insert(), and add deleteFrom() as
an alias for delete(). Use the new methods in MW core callers where
PHPStorm knows the type.
Change-Id: Idb327a54a57a0fb2288ea067472c1e9727016000
2023-09-08 00:06:59 +00:00
|
|
|
->insertInto( 'user_properties' )
|
2023-09-05 19:21:03 +00:00
|
|
|
->row( [
|
2020-02-27 23:25:25 +00:00
|
|
|
'up_user' => $row->up_user,
|
|
|
|
|
'up_property' => $target,
|
|
|
|
|
'up_value' => $row->up_value,
|
2023-09-05 19:21:03 +00:00
|
|
|
] )
|
|
|
|
|
->onDuplicateKeyUpdate()
|
|
|
|
|
->uniqueIndexFields( [ 'up_user', 'up_property' ] )
|
|
|
|
|
->set( [ 'up_value' => $row->up_value ] )
|
|
|
|
|
->caller( __METHOD__ )->execute();
|
2017-03-02 21:21:43 +00:00
|
|
|
|
|
|
|
|
$processed += $dbw->affectedRows();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->output( "Processed $processed user(s)\n" );
|
|
|
|
|
$this->output( "Finished!\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2020-05-19 22:10:27 +00:00
|
|
|
$maintClass = InitUserPreference::class;
|
2017-03-02 21:21:43 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|