2012-04-27 15:14:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class EchoHooks {
|
2012-05-17 00:29:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $updater DatabaseUpdater object
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return bool true in all cases
|
2012-05-17 00:29:37 +00:00
|
|
|
*/
|
|
|
|
public static function getSchemaUpdates( $updater ) {
|
2012-08-31 21:50:46 +00:00
|
|
|
$dir = __DIR__;
|
2012-04-27 15:14:24 +00:00
|
|
|
$baseSQLFile = "$dir/echo.sql";
|
|
|
|
$updater->addExtensionTable( 'echo_subscription', $baseSQLFile );
|
|
|
|
|
|
|
|
$updater->modifyField( 'echo_event', 'event_agent',
|
|
|
|
"$dir/db_patches/patch-event_agent-split.sql", true );
|
2012-05-18 01:07:30 +00:00
|
|
|
$updater->modifyField( 'echo_event', 'event_variant',
|
|
|
|
"$dir/db_patches/patch-event_variant_nullability.sql", true );
|
2012-07-27 22:16:19 +00:00
|
|
|
$updater->modifyField( 'echo_event', 'event_extra',
|
|
|
|
"$dir/db_patches/patch-event_extra-size.sql", true );
|
2012-04-27 15:14:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-17 00:29:37 +00:00
|
|
|
/**
|
|
|
|
* Handler for EchoGetDefaultNotifiedUsers hook.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $event EchoEvent to get implicitly subscribed users for
|
2012-05-17 00:29:37 +00:00
|
|
|
* @param &$users Array to append implicitly subscribed users to.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return bool true in all cases
|
2012-05-17 00:29:37 +00:00
|
|
|
*/
|
|
|
|
public static function getDefaultNotifiedUsers( $event, &$users ) {
|
2012-08-31 21:50:46 +00:00
|
|
|
switch ( $event->getType() ) {
|
2012-08-01 19:53:05 +00:00
|
|
|
// Everyone deserves to know when something happens
|
|
|
|
// on their user talk page
|
2012-04-27 15:14:24 +00:00
|
|
|
case 'edit-user-talk':
|
|
|
|
if ( !$event->getTitle() || !$event->getTitle()->getNamespace() == NS_USER_TALK ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$username = $event->getTitle()->getText();
|
|
|
|
$user = User::newFromName( $username );
|
2012-08-01 19:53:05 +00:00
|
|
|
if ( $user && $user->getId() ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
$users[$user->getId()] = $user;
|
|
|
|
}
|
2012-08-31 21:50:46 +00:00
|
|
|
break;
|
2012-07-27 22:16:19 +00:00
|
|
|
case 'add-comment':
|
|
|
|
case 'add-talkpage-topic':
|
2012-08-01 19:53:05 +00:00
|
|
|
// Handled by EchoDiscussionParser
|
2012-07-27 22:16:19 +00:00
|
|
|
$extraData = $event->getExtra();
|
|
|
|
|
|
|
|
if ( !isset( $extraData['revid'] ) || !$extraData['revid'] ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$revision = Revision::newFromId( $extraData['revid'] );
|
|
|
|
|
|
|
|
$users = array_merge(
|
|
|
|
$users,
|
2012-08-30 16:04:39 +00:00
|
|
|
EchoDiscussionParser::getNotifiedUsersForComment( $revision )
|
2012-07-27 22:16:19 +00:00
|
|
|
);
|
2012-08-31 21:50:46 +00:00
|
|
|
break;
|
2012-08-31 23:35:16 +00:00
|
|
|
case 'welcome':
|
|
|
|
$users[$event->getAgent()->getId()] = $event->getAgent();
|
|
|
|
break;
|
2012-07-18 19:39:33 +00:00
|
|
|
case 'reverted':
|
|
|
|
$extra = $event->getExtra();
|
|
|
|
|
|
|
|
if ( !$extra || !isset( $extra['reverted-user-id'] ) ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$victimID = $extra['reverted-user-id'];
|
|
|
|
$victim = User::newFromId( $victimID );
|
|
|
|
$users[$victim->getId()] = $victim;
|
|
|
|
break;
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-08 05:27:59 +00:00
|
|
|
public static function getNotificationTypes( $subscription, $event, &$notifyTypes ) {
|
|
|
|
$type = $event->getType();
|
|
|
|
$user = $subscription->getUser();
|
|
|
|
|
|
|
|
// Figure out when to disallow email notifications
|
|
|
|
if ( $type == 'edit' ) {
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !$user->getOption( 'enotifwatchlistpages' ) ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
$notifyTypes = array_diff( $notifyTypes, array( 'email' ) );
|
2012-06-08 05:27:59 +00:00
|
|
|
}
|
|
|
|
} elseif ( $type == 'edit-user-talk' ) {
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !$user->getOption( 'enotifusertalkpages' ) ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
$notifyTypes = array_diff( $notifyTypes, array( 'email' ) );
|
2012-06-08 05:27:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !$user->getOption( 'enotifminoredits' ) ) {
|
2012-06-08 05:27:59 +00:00
|
|
|
$extra = $event->getExtra();
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( !empty( $extra['revid'] ) ) {
|
|
|
|
$rev = Revision::newFromID( $extra['revid'] );
|
2012-06-08 05:27:59 +00:00
|
|
|
|
|
|
|
if ( $rev->isMinor() ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
$notifyTypes = array_diff( $notifyTypes, array( 'email' ) );
|
2012-06-08 05:27:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-17 00:29:37 +00:00
|
|
|
/**
|
|
|
|
* Handler for GetPreferences hook.
|
|
|
|
* @see http://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
|
|
|
|
* @param $user User to get preferences for
|
|
|
|
* @param &$preferences Preferences array
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return bool true in all cases
|
2012-05-17 00:29:37 +00:00
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
public static function getPreferences( $user, &$preferences ) {
|
2012-08-01 18:20:21 +00:00
|
|
|
global $wgEchoEnabledEvents;
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( $wgEchoEnabledEvents !== false && !in_array( 'edit', $wgEchoEnabledEvents ) ) {
|
2012-08-01 18:20:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
$preferences['echo-notify-watchlist'] = array(
|
|
|
|
'type' => 'toggle',
|
|
|
|
'label-message' => 'echo-pref-notify-watchlist',
|
|
|
|
'section' => 'echo',
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-17 00:29:37 +00:00
|
|
|
/**
|
|
|
|
* Handler for WatchArticleComplete hook
|
|
|
|
* @see http://www.mediawiki.org/wiki/Manual:Hooks/WatchArticleComplete
|
|
|
|
* @param $user User who watched the Article
|
|
|
|
* @param $article Article that was watched.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return bool true in all cases
|
2012-05-17 00:29:37 +00:00
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
public static function onWatch( $user, $article ) {
|
2012-08-01 18:20:21 +00:00
|
|
|
global $wgEchoEnabledEvents;
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( $wgEchoEnabledEvents !== false && !in_array( 'edit', $wgEchoEnabledEvents ) ) {
|
2012-08-01 18:20:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !$user->getOption( 'echo-notify-watchlist' ) ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$subscription = new EchoSubscription( $user, 'edit', $article->getTitle() );
|
2012-08-30 16:04:39 +00:00
|
|
|
$subscription->enableNotification( 'notify' );
|
2012-04-27 15:14:24 +00:00
|
|
|
$subscription->save();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-17 00:29:37 +00:00
|
|
|
/**
|
|
|
|
* Handler for UnwatchArticleComplete hook
|
|
|
|
* @see http://www.mediawiki.org/wiki/Manual:Hooks/UnwatchArticleComplete
|
|
|
|
* @param $user User who unwatched the Article
|
|
|
|
* @param $article Article that was unwatched.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return bool true in all cases
|
2012-05-17 00:29:37 +00:00
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
public static function onUnwatch( $user, $article ) {
|
2012-08-01 18:20:21 +00:00
|
|
|
global $wgEchoEnabledEvents;
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( $wgEchoEnabledEvents !== false && !in_array( 'edit', $wgEchoEnabledEvents ) ) {
|
2012-08-01 18:20:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
$subscription = new EchoSubscription( $user, 'edit', $article->getTitle() );
|
2012-08-30 16:04:39 +00:00
|
|
|
$subscription->disableNotification( 'notify' );
|
2012-04-27 15:14:24 +00:00
|
|
|
$subscription->save();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-17 00:29:37 +00:00
|
|
|
/**
|
|
|
|
* Handler for ArticleSaveComplete hook
|
|
|
|
* @see http://www.mediawiki.org/wiki/Manual:Hooks/ArticleSaveComplete
|
|
|
|
* @param $article Article edited
|
|
|
|
* @param $user User who edited
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $text string New article text
|
|
|
|
* @param $summary string Edit summary
|
|
|
|
* @param $minoredit bool Minor edit or not
|
|
|
|
* @param $watchthis bool Watch this article?
|
2012-09-26 05:09:43 +00:00
|
|
|
* @param $sectionanchor string Section that was edited
|
|
|
|
* @param $flags int Edit flags
|
2012-05-17 00:29:37 +00:00
|
|
|
* @param $revision Revision that was created
|
|
|
|
* @param $status Status
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return bool true in all cases
|
2012-05-17 00:29:37 +00:00
|
|
|
*/
|
2012-08-31 21:50:46 +00:00
|
|
|
public static function onArticleSaved( &$article, &$user, $text, $summary, $minoredit, $watchthis, $sectionanchor, &$flags, $revision, &$status ) {
|
2012-11-01 20:15:37 +00:00
|
|
|
global $wgEchoEnabledEvents;
|
2012-07-18 19:39:33 +00:00
|
|
|
if ( $revision ) {
|
|
|
|
EchoEvent::create( array(
|
|
|
|
'type' => 'edit',
|
|
|
|
'title' => $article->getTitle(),
|
|
|
|
'extra' => array( 'revid' => $revision->getID() ),
|
|
|
|
'agent' => $user,
|
|
|
|
) );
|
|
|
|
|
|
|
|
if ( $article->getTitle()->isTalkPage() ) {
|
|
|
|
EchoDiscussionParser::generateEventsForRevision( $revision );
|
|
|
|
}
|
2012-07-27 22:16:19 +00:00
|
|
|
|
2012-07-18 19:39:33 +00:00
|
|
|
// Handle the case of someone undoing an edit, either through the
|
|
|
|
// 'undo' link in the article history or via the API.
|
2012-11-01 20:15:37 +00:00
|
|
|
if ( in_array( 'reverted', $wgEchoEnabledEvents ) ) {
|
|
|
|
$undidRevId = $user->getRequest()->getVal( 'wpUndidRevision' );
|
|
|
|
if ( $undidRevId ) {
|
|
|
|
$undidRevision = Revision::newFromId( $undidRevId );
|
|
|
|
if ( $undidRevision ) {
|
|
|
|
$victimId = $undidRevision->getUser();
|
|
|
|
if ( $victimId ) { // No notifications for anonymous users
|
|
|
|
EchoEvent::create( array(
|
|
|
|
'type' => 'reverted',
|
|
|
|
'title' => $article->getTitle(),
|
|
|
|
'extra' => array(
|
|
|
|
'revid' => $revision->getId(),
|
|
|
|
'reverted-user-id' => $victimId,
|
|
|
|
'reverted-revision-id' => $undidRevId,
|
|
|
|
'method' => 'undo',
|
|
|
|
),
|
|
|
|
'agent' => $user,
|
|
|
|
) );
|
|
|
|
}
|
2012-07-18 19:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-27 15:14:24 +00:00
|
|
|
|
2012-11-01 20:15:37 +00:00
|
|
|
}
|
2012-04-27 15:14:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-06-01 10:57:09 +00:00
|
|
|
|
2012-08-31 23:35:16 +00:00
|
|
|
/**
|
|
|
|
* Handler for AddNewAccount hook.
|
|
|
|
* @see http://www.mediawiki.org/wiki/Manual:Hooks/AddNewAccount
|
|
|
|
* @param $user User object that was created.
|
|
|
|
* @param $byEmail bool True when account was created "by email".
|
2012-09-26 05:09:43 +00:00
|
|
|
* @return bool
|
2012-08-31 23:35:16 +00:00
|
|
|
*/
|
|
|
|
public static function onAccountCreated( $user, $byEmail ) {
|
2012-09-26 05:09:43 +00:00
|
|
|
EchoEvent::create( array(
|
2012-08-31 23:35:16 +00:00
|
|
|
'type' => 'welcome',
|
|
|
|
'agent' => $user,
|
|
|
|
) );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-17 00:29:37 +00:00
|
|
|
/**
|
|
|
|
* Handler for BeforePageDisplay hook.
|
|
|
|
* @see http://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
|
|
|
|
* @param $out OutputPage object
|
|
|
|
* @param $skin Skin being used.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return bool true in all cases
|
2012-05-17 00:29:37 +00:00
|
|
|
*/
|
2012-06-01 10:57:09 +00:00
|
|
|
static function beforePageDisplay( $out, $skin ) {
|
|
|
|
global $wgUser;
|
|
|
|
if ( !$wgUser->isAnon() ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
$out->addModules( array( 'ext.echo.overlay' ) );
|
2012-06-01 10:57:09 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-17 00:29:37 +00:00
|
|
|
/**
|
|
|
|
* Handler for PersonalUrls hook.
|
|
|
|
* @see http://www.mediawiki.org/wiki/Manual:Hooks/PersonalUrls
|
|
|
|
* @param &$personal_urls Array of URLs to append to.
|
|
|
|
* @param &$title Title of page being visited.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return bool true in all cases
|
2012-05-17 00:29:37 +00:00
|
|
|
*/
|
2012-06-01 10:57:09 +00:00
|
|
|
static function onPersonalUrls( &$personal_urls, &$title ) {
|
2012-11-13 23:06:11 +00:00
|
|
|
global $wgUser;
|
2012-08-01 19:53:05 +00:00
|
|
|
// Add a "My notifications" item to personal URLs
|
2012-06-01 10:57:09 +00:00
|
|
|
|
|
|
|
if ( $wgUser->isAnon() ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$notificationCount = EchoNotificationController::getNotificationCount( $wgUser );
|
|
|
|
|
2012-07-31 23:29:30 +00:00
|
|
|
$msg = wfMessage( $notificationCount == 0 ? 'echo-link' : 'echo-link-new' );
|
2012-06-01 10:57:09 +00:00
|
|
|
$url = SpecialPage::getTitleFor( 'Notifications' )->getLocalURL();
|
|
|
|
|
|
|
|
$notificationsLink = array(
|
|
|
|
'href' => $url,
|
2012-11-13 23:06:11 +00:00
|
|
|
'text' => $msg->params( EchoNotificationController::formatNotificationCount( $notificationCount ) )->text(),
|
2012-06-01 10:57:09 +00:00
|
|
|
'active' => $notificationCount > 0,
|
|
|
|
);
|
|
|
|
|
|
|
|
$insertUrls = array( 'notifications' => $notificationsLink );
|
|
|
|
$personal_urls = wfArrayInsertAfter( $personal_urls, $insertUrls, 'watchlist' );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-17 22:19:32 +00:00
|
|
|
|
2012-05-17 00:29:37 +00:00
|
|
|
/**
|
|
|
|
* Handler for AbortEmailNotification hook.
|
|
|
|
* @see http://www.mediawiki.org/wiki/Manual:Hooks/AbortEmailNotification
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return bool true in all cases
|
2012-05-17 00:29:37 +00:00
|
|
|
*/
|
2012-07-17 22:19:32 +00:00
|
|
|
static function abortEmailNotification() {
|
2012-08-02 19:50:46 +00:00
|
|
|
global $wgEchoDisableStandardEmail;
|
2012-08-31 21:50:46 +00:00
|
|
|
return !$wgEchoDisableStandardEmail;
|
2012-07-17 22:19:32 +00:00
|
|
|
}
|
2012-07-31 21:18:16 +00:00
|
|
|
|
2012-09-02 09:30:38 +00:00
|
|
|
public static function makeGlobalVariablesScript( &$vars, OutputPage $outputPage ) {
|
2012-07-31 21:18:16 +00:00
|
|
|
$user = $outputPage->getUser();
|
|
|
|
|
2012-08-01 19:53:05 +00:00
|
|
|
// Provide info for the Overlay
|
|
|
|
|
2012-09-02 09:30:38 +00:00
|
|
|
$timestamp = new MWTimestamp( wfTimestampNow() );
|
|
|
|
if ( ! $user->isAnon() ) {
|
2012-07-31 21:18:16 +00:00
|
|
|
$vars['wgEchoOverlayConfiguration'] = array(
|
2012-09-02 09:30:38 +00:00
|
|
|
'timestamp' => $timestamp->getTimestamp( TS_UNIX ),
|
2012-11-13 23:06:11 +00:00
|
|
|
'notification-count' => EchoNotificationController::getFormattedNotificationCount( $user ),
|
2012-07-31 21:18:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-31 00:29:49 +00:00
|
|
|
|
|
|
|
static function getUnitTests( &$files ) {
|
|
|
|
$dir = dirname( __FILE__ ) . '/tests';
|
|
|
|
$files[] = "$dir/DiscussionParserTest.php";
|
2012-08-07 18:54:42 +00:00
|
|
|
return true;
|
2012-07-31 00:29:49 +00:00
|
|
|
}
|
2012-07-31 20:44:43 +00:00
|
|
|
|
|
|
|
static function abortNewtalkNotification( $article ) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-18 19:39:33 +00:00
|
|
|
|
|
|
|
static function onRollbackComplete( $page, $agent, $newRevision, $oldRevision ) {
|
|
|
|
$victimId = $oldRevision->getUser();
|
|
|
|
|
|
|
|
if ( $victimId ) { // No notifications for anonymous users
|
|
|
|
EchoEvent::create( array(
|
|
|
|
'type' => 'reverted',
|
|
|
|
'title' => $page->getTitle(),
|
|
|
|
'extra' => array(
|
|
|
|
'revid' => $page->getRevision()->getId(),
|
|
|
|
'reverted-user-id' => $victimId,
|
|
|
|
'reverted-revision-id' => $oldRevision->getId(),
|
|
|
|
'method' => 'rollback',
|
|
|
|
),
|
|
|
|
'agent' => $agent,
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-18 16:28:41 +00:00
|
|
|
}
|