mediawiki-extensions-Echo/maintenance/backfillUnreadWikis.php
Roan Kattouw 1d05d9298c backfillUnreadWikis.php: Add rebuild mode
In rebuild mode, we only process users who already have
a row in the euw table. This reduces the number of users
we have to process significantly. Thanks to Stephane
for this idea.

Also, don't force a recache of the alert and message counts,
it's OK to fetch them from cache. We also don't need to invalidate
any caches in this script, because the transition flag from
Ibcc8ac102 will ensure that the cached global counts are
correct.

Change-Id: I69c54cac47468647df2778b2d82f1b5dd9e574d3
2016-06-17 16:02:01 +01:00

65 lines
2.1 KiB
PHP

<?php
$IP = getenv( 'MW_INSTALL_PATH' );
if ( $IP === false ) {
$IP = __DIR__ . '/../../..';
}
require_once ( "$IP/maintenance/Maintenance.php" );
class BackfillUnreadWikis extends Maintenance {
public function __construct() {
parent::__construct();
$this->mDescription = "Backfill echo_unread_wikis table";
$this->addOption( 'rebuild', 'Only recompute already-existing rows' );
$this->setBatchSize( 300 );
}
public function execute() {
$dbFactory = MWEchoDbFactory::newFromDefault();
$lookup = CentralIdLookup::factory();
$rebuild = $this->hasOption( 'rebuild' );
if ( $rebuild ) {
$iterator = new BatchRowIterator( $dbFactory->getSharedDb( DB_SLAVE ), 'echo_unread_wikis', 'euw_user', $this->mBatchSize );
$iterator->addConditions( array( 'euw_wiki' => wfWikiId() ) );
} else {
$iterator = new BatchRowIterator( wfGetDB( DB_SLAVE ), 'user', 'user_id', $this->mBatchSize );
$iterator->setFetchColumns( User::selectFields() );
}
$processed = 0;
foreach ( $iterator as $batch ) {
foreach ( $batch as $row ) {
if ( $rebuild ) {
$user = $lookup->localUserFromCentralId( $row->euw_user, CentralIdLookup::AUDIENCE_RAW );
} else {
$user = User::newFromRow( $row );
}
if ( !$user ) {
continue;
}
$notifUser = MWEchoNotifUser::newFromUser( $user );
$uw = EchoUnreadWikis::newFromUser( $user );
if ( $uw ) {
$alertCount = $notifUser->getNotificationCount( true, DB_SLAVE, EchoAttributeManager::ALERT, false );
$alertUnread = $notifUser->getLastUnreadNotificationTime( true, DB_SLAVE, EchoAttributeManager::ALERT, false );
$msgCount = $notifUser->getNotificationCount( true, DB_SLAVE, EchoAttributeManager::MESSAGE, false );
$msgUnread = $notifUser->getLastUnreadNotificationTime( true, DB_SLAVE, EchoAttributeManager::MESSAGE, false );
$uw->updateCount( wfWikiID(), $alertCount, $alertUnread, $msgCount, $msgUnread );
}
}
$processed += count( $batch );
$this->output( "Updated $processed users.\n" );
$dbFactory->waitForSlaves();
}
}
}
$maintClass = "BackfillUnreadWikis";
require_once ( DO_MAINTENANCE );