Debian Codegraph @

extensions/Echo/maintenance/backfillUnreadWikis.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Extension\Notifications\AttributeManager;
4 use MediaWiki\Extension\Notifications\DbFactory;
5 use MediaWiki\Extension\Notifications\NotifUser;
6 use MediaWiki\Extension\Notifications\UnreadWikis;
7 use MediaWiki\Maintenance\Maintenance;
8 use MediaWiki\User\CentralId\CentralIdLookup;
9 use MediaWiki\User\User;
10 use MediaWiki\WikiMap\WikiMap;
11
12 $IP = getenv( 'MW_INSTALL_PATH' );
13 if ( $IP === false ) {
14 $IP = __DIR__ . '/../../..';
15 }
16 require_once "$IP/maintenance/Maintenance.php";
17
18 class BackfillUnreadWikis extends Maintenance {
19 public function __construct() {
20 parent::__construct();
21
22 $this->addDescription( "Backfill echo_unread_wikis table" );
23 $this->addOption( 'rebuild', 'Only recompute already-existing rows' );
24 $this->setBatchSize( 300 );
25 $this->requireExtension( 'Echo' );
26 }
27
28 public function execute() {
29 $dbFactory = DbFactory::newFromDefault();
30 $lookup = $this->getServiceContainer()->getCentralIdLookup();
31
32 $rebuild = $this->hasOption( 'rebuild' );
33 if ( $rebuild ) {
34 $iterator = new BatchRowIterator(
35 $dbFactory->getSharedDb( DB_REPLICA ),
36 'echo_unread_wikis',
37 'euw_user',
38 $this->getBatchSize()
39 );
40 $iterator->addConditions( [ 'euw_wiki' => WikiMap::getCurrentWikiId() ] );
41 } else {
42 $userQuery = User::getQueryInfo();
43 $iterator = new BatchRowIterator(
44 $this->getReplicaDB(), $userQuery['tables'], 'user_id', $this->getBatchSize()
45 );
46 $iterator->setFetchColumns( $userQuery['fields'] );
47 $iterator->addJoinConditions( $userQuery['joins'] );
48 }
49
50 $iterator->setCaller( __METHOD__ );
51
52 $processed = 0;
53 foreach ( $iterator as $batch ) {
54 foreach ( $batch as $row ) {
55 if ( $rebuild ) {
56 $user = $lookup->localUserFromCentralId(
57 $row->euw_user,
58 CentralIdLookup::AUDIENCE_RAW
59 );
60 if ( !$user ) {
61 continue;
62 }
63 } else {
64 $user = User::newFromRow( $row );
65 }
66
67 $notifUser = NotifUser::newFromUser( $user );
68 $uw = UnreadWikis::newFromUser( $user );
69 if ( $uw ) {
70 $alertCount = $notifUser->getNotificationCount( AttributeManager::ALERT, false );
71 $alertUnread = $notifUser->getLastUnreadNotificationTime( AttributeManager::ALERT, false );
72
73 $msgCount = $notifUser->getNotificationCount( AttributeManager::MESSAGE, false );
74 $msgUnread = $notifUser->getLastUnreadNotificationTime( AttributeManager::MESSAGE, false );
75
76 if ( ( $alertCount !== 0 && $alertUnread === false ) ||
77 ( $msgCount !== 0 && $msgUnread === false )
78 ) {
79 // If there are alerts, there should be an alert timestamp (same for messages).
80
81 // Otherwise, there is a race condition between the two values, indicating there's already
82 // just been an updateCount call, so we can skip this user.
83 continue;
84 }
85
86 $uw->updateCount( WikiMap::getCurrentWikiId(), $alertCount, $alertUnread, $msgCount, $msgUnread );
87 }
88 }
89
90 $processed += count( $batch );
91 $this->output( "Updated $processed users.\n" );
92 $this->waitForReplication();
93 }
94 }
95 }
96
97 $maintClass = BackfillUnreadWikis::class;
98 require_once RUN_MAINTENANCE_IF_MAIN;