Catrope has submitted this change and it was merged. Change subject: Add $wgEchoCrossWikiNotifications flag to disable cross-wiki notifications ......................................................................
Add $wgEchoCrossWikiNotifications flag to disable cross-wiki notifications This is needed because global tables and cache keys will attempt to be used even for users who don't have the preference enabled. Bug: T135266 Change-Id: I6208a12d46c8cd0275a232663cd50ac2bd2fed1c --- M Echo.php M Hooks.php M includes/NotifUser.php M includes/api/ApiEchoNotifications.php 4 files changed, 87 insertions(+), 47 deletions(-) Approvals: Catrope: Verified; Looks good to me, approved diff --git a/Echo.php b/Echo.php index 887d584..0b5e07b 100644 --- a/Echo.php +++ b/Echo.php @@ -210,6 +210,12 @@ // sprintf format of per-user notification agent whitelists. Set to null to disable. $wgEchoPerUserWhitelistFormat = '%s/Echo-whitelist'; +// Whether to enable the cross-wiki notifications feature. To enable this feature you need to: +// - have a global user system (e.g. CentralAuth or a shared user table) +// - have $wgMainStash and $wgMainWANCache shared between wikis +// - configure $wgEchoSharedTrackingDB +$wgEchoCrossWikiNotifications = false; + // Feature flag for the cross-wiki notifications beta feature // If this is true, the cross-wiki notifications preference will appear in the BetaFeatures section; // if this is false, it'll appear in the Notifications section instead. diff --git a/Hooks.php b/Hooks.php index 36f2f29..33a4470 100644 --- a/Hooks.php +++ b/Hooks.php @@ -216,9 +216,9 @@ * @return bool true in all cases */ public static function getBetaFeaturePreferences( User $user, array &$preferences ) { - global $wgExtensionAssetsPath, $wgEchoUseCrossWikiBetaFeature; + global $wgExtensionAssetsPath, $wgEchoUseCrossWikiBetaFeature, $wgEchoCrossWikiNotifications; - if ( $wgEchoUseCrossWikiBetaFeature ) { + if ( $wgEchoUseCrossWikiBetaFeature && $wgEchoCrossWikiNotifications ) { $preferences['echo-cross-wiki-notifications'] = array( 'label-message' => 'echo-pref-beta-feature-cross-wiki-message', 'desc-message' => 'echo-pref-beta-feature-cross-wiki-description', @@ -250,7 +250,7 @@ global $wgAuth, $wgEchoEnableEmailBatch, $wgEchoNotifiers, $wgEchoNotificationCategories, $wgEchoNotifications, $wgEchoNewMsgAlert, $wgAllowHTMLEmail, $wgEchoUseCrossWikiBetaFeature, - $wgEchoShowFooterNotice; + $wgEchoShowFooterNotice, $wgEchoCrossWikiNotifications; $attributeManager = EchoAttributeManager::newFromGlobalVars(); @@ -391,7 +391,7 @@ 'tooltips' => $tooltips, ); - if ( !$wgEchoUseCrossWikiBetaFeature ) { + if ( !$wgEchoUseCrossWikiBetaFeature && $wgEchoCrossWikiNotifications ) { $preferences['echo-cross-wiki-notifications'] = array( 'type' => 'toggle', 'label-message' => 'echo-pref-cross-wiki-notifications', diff --git a/includes/NotifUser.php b/includes/NotifUser.php index 1249759..1a8a133 100644 --- a/includes/NotifUser.php +++ b/includes/NotifUser.php @@ -187,7 +187,9 @@ /** * Retrieves number of unread notifications that a user has, would return - * MWEchoNotifUser::MAX_BADGE_COUNT + 1 at most + * MWEchoNotifUser::MAX_BADGE_COUNT + 1 at most. + * + * If $wgEchoCrossWikiNotifications is disabled, the $global parameter is ignored. * * @param boolean $cached Set to false to bypass the cache. (Optional. Defaults to true) * @param int $dbSource Use master or slave database to pull count (Optional. Defaults to DB_SLAVE) @@ -198,6 +200,12 @@ public function getNotificationCount( $cached = true, $dbSource = DB_SLAVE, $section = EchoAttributeManager::ALL, $global = 'preference' ) { if ( $this->mUser->isAnon() ) { return 0; + } + + global $wgEchoCrossWikiNotifications; + if ( !$wgEchoCrossWikiNotifications ) { + // Ignore the $global parameter + $global = false; } if ( $global === 'preference' ) { @@ -254,6 +262,8 @@ /** * Returns the timestamp of the last unread notification. * + * If $wgEchoCrossWikiNotifications is disabled, the $global parameter is ignored. + * * @param boolean $cached Set to false to bypass the cache. (Optional. Defaults to true) * @param int $dbSource Use master or slave database to pull count (Optional. Defaults to DB_SLAVE) * @param string $section Notification section @@ -263,6 +273,12 @@ public function getLastUnreadNotificationTime( $cached = true, $dbSource = DB_SLAVE, $section = EchoAttributeManager::ALL, $global = 'preference' ) { if ( $this->mUser->isAnon() ) { return false; + } + + global $wgEchoCrossWikiNotifications; + if ( !$wgEchoCrossWikiNotifications ) { + // Ignore the $global parameter + $global = false; } if ( $global === 'preference' ) { @@ -440,16 +456,12 @@ * @param $dbSource int use master or slave database to pull count */ public function resetNotificationCount( $dbSource = DB_SLAVE ) { + global $wgEchoCrossWikiNotifications; // Reset alert and message counts, and store them for later $alertCount = $this->getNotificationCount( false, $dbSource, EchoAttributeManager::ALERT, false ); $msgCount = $this->getNotificationCount( false, $dbSource, EchoAttributeManager::MESSAGE, false ); // For performance, compute the ALL count by adding alerts and messages $allCount = $alertCount + $msgCount; - - // For performance, compute the global counts by adding foreign counts to the above - $globalAlertCount = $alertCount + $this->getForeignNotifications()->getCount( EchoAttributeManager::ALERT ); - $globalMsgCount = $msgCount + $this->getForeignNotifications()->getCount( EchoAttributeManager::MESSAGE ); - $globalAllCount = $globalAlertCount + $globalMsgCount; // When notification counts need to be updated, the last notification may have changed, // so we also need to recompute the cached timestamp values. @@ -460,40 +472,49 @@ ( $msgUnread === false || $alertUnread->diff( $msgUnread )->invert === 1 ) ? $alertUnread : $msgUnread; - // For performance, compute the global timestamps as max( localTimestamp, foreignTimestamp ) - $foreignAlertUnread = $this->getForeignNotifications()->getTimestamp( EchoAttributeManager::ALERT ); - $globalAlertUnread = $alertUnread !== false && - ( $foreignAlertUnread === false || $alertUnread->diff( $foreignAlertUnread )->invert === 1 ) ? - $alertUnread : $foreignAlertUnread; - $foreignMsgUnread = $this->getForeignNotifications()->getTimestamp( EchoAttributeManager::MESSAGE ); - $globalMsgUnread = $msgUnread !== false && - ( $foreignMsgUnread === false || $msgUnread->diff( $foreignMsgUnread )->invert === 1 ) ? - $msgUnread : $foreignMsgUnread; - $globalAllUnread = $globalAlertUnread !== false && - ( $globalMsgUnread === false || $globalAlertUnread->diff( $globalMsgUnread )->invert === 1 ) ? - $globalAlertUnread : $globalMsgUnread; - // Write computed values to cache $this->setInCache( $this->getMemcKey( 'echo-notification-count' ), $allCount, 86400 ); - $this->setInCache( $this->getGlobalMemcKey( 'echo-notification-count-alert' ), $globalAlertCount, 86400 ); - $this->setInCache( $this->getGlobalMemcKey( 'echo-notification-count-message' ), $globalMsgCount, 86400 ); - $this->setInCache( $this->getGlobalMemcKey( 'echo-notification-count' ), $globalAllCount, 86400 ); $this->setInCache( $this->getMemcKey( 'echo-notification-timestamp' ), $allUnread === false ? -1 : $allUnread->getTimestamp( TS_MW ), 86400 ); - $this->setInCache( $this->getGlobalMemcKey( 'echo-notification-timestamp-alert' ), $globalAlertUnread === false ? -1 : $globalAlertUnread->getTimestamp( TS_MW ), 86400 ); - $this->setInCache( $this->getGlobalMemcKey( 'echo-notification-timestamp-message' ), $globalMsgUnread === false ? -1 : $globalMsgUnread->getTimestamp( TS_MW ), 86400 ); - $this->setInCache( $this->getGlobalMemcKey( 'echo-notification-timestamp' ), $globalAllUnread === false ? -1 : $globalAllUnread->getTimestamp( TS_MW ), 86400 ); + + if ( $wgEchoCrossWikiNotifications ) { + // For performance, compute the global counts by adding foreign counts to the above + $globalAlertCount = $alertCount + $this->getForeignNotifications()->getCount( EchoAttributeManager::ALERT ); + $globalMsgCount = $msgCount + $this->getForeignNotifications()->getCount( EchoAttributeManager::MESSAGE ); + $globalAllCount = $globalAlertCount + $globalMsgCount; + + // For performance, compute the global timestamps as max( localTimestamp, foreignTimestamp ) + $foreignAlertUnread = $this->getForeignNotifications()->getTimestamp( EchoAttributeManager::ALERT ); + $globalAlertUnread = $alertUnread !== false && + ( $foreignAlertUnread === false || $alertUnread->diff( $foreignAlertUnread )->invert === 1 ) ? + $alertUnread : $foreignAlertUnread; + $foreignMsgUnread = $this->getForeignNotifications()->getTimestamp( EchoAttributeManager::MESSAGE ); + $globalMsgUnread = $msgUnread !== false && + ( $foreignMsgUnread === false || $msgUnread->diff( $foreignMsgUnread )->invert === 1 ) ? + $msgUnread : $foreignMsgUnread; + $globalAllUnread = $globalAlertUnread !== false && + ( $globalMsgUnread === false || $globalAlertUnread->diff( $globalMsgUnread )->invert === 1 ) ? + $globalAlertUnread : $globalMsgUnread; + + // Write computed values to cache + $this->setInCache( $this->getGlobalMemcKey( 'echo-notification-count-alert' ), $globalAlertCount, 86400 ); + $this->setInCache( $this->getGlobalMemcKey( 'echo-notification-count-message' ), $globalMsgCount, 86400 ); + $this->setInCache( $this->getGlobalMemcKey( 'echo-notification-count' ), $globalAllCount, 86400 ); + $this->setInCache( $this->getGlobalMemcKey( 'echo-notification-timestamp-alert' ), $globalAlertUnread === false ? -1 : $globalAlertUnread->getTimestamp( TS_MW ), 86400 ); + $this->setInCache( $this->getGlobalMemcKey( 'echo-notification-timestamp-message' ), $globalMsgUnread === false ? -1 : $globalMsgUnread->getTimestamp( TS_MW ), 86400 ); + $this->setInCache( $this->getGlobalMemcKey( 'echo-notification-timestamp' ), $globalAllUnread === false ? -1 : $globalAllUnread->getTimestamp( TS_MW ), 86400 ); + + // Schedule an update to the echo_unread_wikis table + $user = $this->mUser; + DeferredUpdates::addCallableUpdate( function () use ( $user, $alertCount, $alertUnread, $msgCount, $msgUnread ) { + $uw = EchoUnreadWikis::newFromUser( $user ); + if ( $uw ) { + $uw->updateCount( wfWikiID(), $alertCount, $alertUnread, $msgCount, $msgUnread ); + } + } ); + } // Invalidate the user's cache - $user = $this->mUser; - $user->invalidateCache(); - - // Schedule an update to the echo_unread_wikis table - DeferredUpdates::addCallableUpdate( function () use ( $user, $alertCount, $alertUnread, $msgCount, $msgUnread ) { - $uw = EchoUnreadWikis::newFromUser( $user ); - if ( $uw ) { - $uw->updateCount( wfWikiID(), $alertCount, $alertUnread, $msgCount, $msgUnread ); - } - } ); + $this->mUser->invalidateCache(); } /** diff --git a/includes/api/ApiEchoNotifications.php b/includes/api/ApiEchoNotifications.php index bff6113..84cf89c 100644 --- a/includes/api/ApiEchoNotifications.php +++ b/includes/api/ApiEchoNotifications.php @@ -16,6 +16,7 @@ } public function execute() { + global $wgEchoCrossWikiNotifications; // To avoid API warning, register the parameter used to bust browser cache $this->getMain()->getVal( '_' ); @@ -41,8 +42,13 @@ ); } - $this->foreignNotifications = new EchoForeignNotifications( $user ); - $this->crossWikiSummary = $params['crosswikisummary']; + if ( $wgEchoCrossWikiNotifications ) { + $this->foreignNotifications = new EchoForeignNotifications( $user ); + $this->crossWikiSummary = $params['crosswikisummary']; + } else { + $this->foreignNotifications = null; + $this->crossWikiSummary = false; + } $result = array(); if ( in_array( 'list', $prop ) ) { @@ -295,6 +301,8 @@ } public function getAllowedParams() { + global $wgEchoCrossWikiNotifications; + $sections = EchoAttributeManager::$sections; $params = array( 'filter' => array( @@ -332,12 +340,6 @@ ), ApiBase::PARAM_HELP_MSG_PER_VALUE => array(), ), - // create "x notifications from y wikis" notification bundle & - // include unread counts from other wikis in prop=count results - 'crosswikisummary' => array( - ApiBase::PARAM_TYPE => 'boolean', - ApiBase::PARAM_DFLT => false, - ), 'limit' => array( ApiBase::PARAM_TYPE => 'limit', ApiBase::PARAM_DFLT => 20, @@ -361,6 +363,17 @@ ); } + if ( $wgEchoCrossWikiNotifications ) { + $params += array( + // create "x notifications from y wikis" notification bundle & + // include unread counts from other wikis in prop=count results + 'crosswikisummary' => array( + ApiBase::PARAM_TYPE => 'boolean', + ApiBase::PARAM_DFLT => false, + ), + ); + } + return $params; } -- To view, visit https://gerrit.wikimedia.org/r/295113 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I6208a12d46c8cd0275a232663cd50ac2bd2fed1c Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Echo Gerrit-Branch: REL1_27 Gerrit-Owner: Catrope <roan.katt...@gmail.com> Gerrit-Reviewer: Catrope <roan.katt...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits