mediawiki-extensions-Echo/maintenance/backfillUnreadWikis.php
Kunal Mehta aaf061c725 build: Updating mediawiki/mediawiki-codesniffer to 0.9.0
The following sniffs are failing and were disabled:
* MediaWiki.Commenting.FunctionComment.ExtraParamComment
* MediaWiki.Commenting.FunctionComment.MissingParamComment
* MediaWiki.Commenting.FunctionComment.MissingParamName
* MediaWiki.Commenting.FunctionComment.MissingParamTag
* MediaWiki.Commenting.FunctionComment.MissingReturn
* MediaWiki.Commenting.FunctionComment.ParamNameNoMatch
* MediaWiki.Commenting.FunctionComment.WrongStyle
* MediaWiki.FunctionComment.Missing.Protected
* MediaWiki.FunctionComment.Missing.Public
* MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName
* MediaWiki.WhiteSpace.SpaceBeforeSingleLineComment.NewLineComment

Change-Id: I8401abf121a7413fa191d7bc535e0ddd6cf8c3f7
2017-06-22 14:13:28 +00:00

74 lines
2.5 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 );
$this->requireExtension( 'Echo' );
}
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( [ '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 );
if ( ( $alertCount !== 0 && $alertUnread === false ) || ( $msgCount !== 0 && $msgUnread === false ) ) {
// If there are alerts, there should be an alert timestamp (same for messages).
// Otherwise, there is a race condition between the two values, indicating there's already
// just been an updateCount call, so we can skip this user.
continue;
}
$uw->updateCount( wfWikiID(), $alertCount, $alertUnread, $msgCount, $msgUnread );
}
}
$processed += count( $batch );
$this->output( "Updated $processed users.\n" );
$dbFactory->waitForSlaves();
}
}
}
$maintClass = "BackfillUnreadWikis";
require_once DO_MAINTENANCE;