2013-03-18 19:56:12 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Hooks for Thanks extension
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ThanksHooks {
|
2015-01-15 00:14:12 +00:00
|
|
|
/**
|
|
|
|
* ResourceLoaderTestModules hook handler
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderTestModules
|
|
|
|
*
|
|
|
|
* @param array $testModules
|
|
|
|
* @param ResourceLoader $resourceLoader
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function onResourceLoaderTestModules( array &$testModules,
|
|
|
|
ResourceLoader &$resourceLoader
|
|
|
|
) {
|
|
|
|
if ( class_exists( 'SpecialMobileDiff' ) ) {
|
|
|
|
$testModules['qunit']['tests.ext.thanks.mobilediff'] = array(
|
|
|
|
'localBasePath' => __DIR__,
|
|
|
|
'remoteExtPath' => 'Thanks',
|
|
|
|
'dependencies' => array( 'ext.thanks.mobilediff' ),
|
|
|
|
'scripts' => array(
|
|
|
|
'tests/qunit/test_ext.thanks.mobilediff.js',
|
|
|
|
),
|
|
|
|
'targets' => array( 'desktop', 'mobile' ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-18 19:56:12 +00:00
|
|
|
/**
|
|
|
|
* Handler for HistoryRevisionTools and DiffRevisionTools hooks.
|
|
|
|
* Inserts 'thank' link into revision interface
|
|
|
|
* @param $rev Revision object to add the thank link for
|
2013-10-07 20:38:51 +00:00
|
|
|
* @param &$links array Links to add to the revision interface
|
2014-01-03 21:33:27 +00:00
|
|
|
* @param $oldRev Revision object of the "old" revision when viewing a diff
|
2013-03-18 19:56:12 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-01-03 21:33:27 +00:00
|
|
|
public static function insertThankLink( $rev, &$links, $oldRev = null ) {
|
2013-03-18 19:56:12 +00:00
|
|
|
global $wgUser, $wgThanksSendToBots;
|
|
|
|
// Make sure Echo is turned on.
|
|
|
|
// Exclude anonymous users.
|
|
|
|
// Don't let users thank themselves.
|
|
|
|
// Exclude users who are blocked.
|
|
|
|
if ( class_exists( 'EchoNotifier' )
|
|
|
|
&& !$wgUser->isAnon()
|
|
|
|
&& $rev->getUser() !== $wgUser->getId()
|
|
|
|
&& !$wgUser->isBlocked()
|
2014-02-26 23:03:11 +00:00
|
|
|
&& !$rev->isDeleted( Revision::DELETED_TEXT )
|
|
|
|
&& ( !$oldRev || $rev->getParentId() == $oldRev->getId() )
|
2013-03-18 19:56:12 +00:00
|
|
|
) {
|
|
|
|
$recipient = User::newFromId( $rev->getUser() );
|
|
|
|
$recipientAllowed = true;
|
|
|
|
// If bots are not allowed, exclude them as recipients
|
|
|
|
if ( !$wgThanksSendToBots ) {
|
|
|
|
$recipientAllowed = !in_array( 'bot', $recipient->getGroups() );
|
|
|
|
}
|
|
|
|
if ( $recipientAllowed && !$recipient->isAnon() ) {
|
2013-08-20 23:23:37 +00:00
|
|
|
$links[] = self::generateThankElement( $rev, $recipient );
|
2013-03-18 19:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-25 20:43:00 +00:00
|
|
|
/**
|
|
|
|
* Helper for self::insertThankLink
|
|
|
|
* Creates either a thank link or thanked span based on users session
|
|
|
|
* @param $rev Revision object to generate the thank element for
|
2014-02-26 02:12:47 +00:00
|
|
|
* @param $recipient User who receives thanks notification
|
2013-10-07 20:38:51 +00:00
|
|
|
* @return string
|
2013-04-25 20:43:00 +00:00
|
|
|
*/
|
2013-08-20 23:23:37 +00:00
|
|
|
protected static function generateThankElement( $rev, $recipient ) {
|
2013-04-25 20:43:00 +00:00
|
|
|
global $wgUser;
|
|
|
|
// User has already thanked for revision
|
|
|
|
if ( $wgUser->getRequest()->getSessionData( "thanks-thanked-{$rev->getId()}" ) ) {
|
|
|
|
return Html::element(
|
|
|
|
'span',
|
|
|
|
array( 'class' => 'mw-thanks-thanked' ),
|
2015-04-22 23:21:27 +00:00
|
|
|
wfMessage( 'thanks-thanked', $wgUser, $recipient->getName() )->text()
|
2013-04-25 20:43:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add 'thank' link
|
2013-08-20 23:23:37 +00:00
|
|
|
$tooltip = wfMessage( 'thanks-thank-tooltip' )
|
|
|
|
->params( $wgUser->getName(), $recipient->getName() )
|
|
|
|
->text();
|
2013-04-25 20:43:00 +00:00
|
|
|
|
|
|
|
return Html::element(
|
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'class' => 'mw-thanks-thank-link',
|
2013-10-24 01:13:43 +00:00
|
|
|
'href' => SpecialPage::getTitleFor( 'Thanks', $rev->getId() )->getFullURL(),
|
2013-04-25 20:43:00 +00:00
|
|
|
'title' => $tooltip,
|
|
|
|
'data-revision-id' => $rev->getId(),
|
|
|
|
),
|
2015-05-05 04:35:20 +00:00
|
|
|
wfMessage( 'thanks-thank', $wgUser, $recipient->getName() )->text()
|
2013-04-25 20:43:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-03-18 19:56:12 +00:00
|
|
|
/**
|
|
|
|
* Handler for PageHistoryBeforeList hook.
|
|
|
|
* @see http://www.mediawiki.org/wiki/Manual:Hooks/PageHistoryBeforeList
|
|
|
|
* @param &$page WikiPage|Article|ImagePage|CategoryPage|Page The page that the history is loading for.
|
|
|
|
* @param $context RequestContext object
|
|
|
|
* @return bool true in all cases
|
|
|
|
*/
|
|
|
|
public static function onPageHistoryBeforeList( &$page, $context ) {
|
2013-06-08 05:06:46 +00:00
|
|
|
global $wgThanksConfirmationRequired;
|
2013-03-18 19:56:12 +00:00
|
|
|
if ( class_exists( 'EchoNotifier' )
|
|
|
|
&& $context->getUser()->isLoggedIn()
|
|
|
|
) {
|
|
|
|
// Load the module for the thank links
|
2014-02-26 02:12:47 +00:00
|
|
|
$context->getOutput()->addModules( array( 'ext.thanks.revthank' ) );
|
2013-06-08 05:06:46 +00:00
|
|
|
$context->getOutput()->addJsConfigVars( 'thanks-confirmation-required',
|
|
|
|
$wgThanksConfirmationRequired );
|
2013-03-18 19:56:12 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for DiffViewHeader hook.
|
|
|
|
* @see http://www.mediawiki.org/wiki/Manual:Hooks/DiffViewHeader
|
2013-10-24 04:16:50 +00:00
|
|
|
* @param $diff DifferenceEngine
|
2013-03-18 19:56:12 +00:00
|
|
|
* @param $oldRev Revision object of the "old" revision (may be null/invalid)
|
|
|
|
* @param $newRev Revision object of the "new" revision
|
|
|
|
* @return bool true in all cases
|
|
|
|
*/
|
|
|
|
public static function onDiffViewHeader( $diff, $oldRev, $newRev ) {
|
2013-06-08 05:06:46 +00:00
|
|
|
global $wgThanksConfirmationRequired;
|
2013-03-18 19:56:12 +00:00
|
|
|
if ( class_exists( 'EchoNotifier' )
|
|
|
|
&& $diff->getUser()->isLoggedIn()
|
|
|
|
) {
|
|
|
|
// Load the module for the thank link
|
2014-02-26 02:12:47 +00:00
|
|
|
$diff->getOutput()->addModules( array( 'ext.thanks.revthank' ) );
|
2013-06-08 05:06:46 +00:00
|
|
|
$diff->getOutput()->addJsConfigVars( 'thanks-confirmation-required',
|
|
|
|
$wgThanksConfirmationRequired );
|
2013-03-18 19:56:12 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-29 04:53:49 +00:00
|
|
|
* Add Thanks events to Echo
|
|
|
|
*
|
|
|
|
* @param $notifications array of Echo notifications
|
|
|
|
* @param $notificationCategories array of Echo notification categories
|
|
|
|
* @param $icons array of icon details
|
2013-10-07 20:38:51 +00:00
|
|
|
* @return bool
|
2013-03-18 19:56:12 +00:00
|
|
|
*/
|
2013-04-29 04:53:49 +00:00
|
|
|
public static function onBeforeCreateEchoEvent( &$notifications, &$notificationCategories, &$icons ) {
|
|
|
|
$notificationCategories['edit-thank'] = array(
|
2013-03-18 19:56:12 +00:00
|
|
|
'priority' => 3,
|
2013-05-30 20:52:39 +00:00
|
|
|
'tooltip' => 'echo-pref-tooltip-edit-thank',
|
2013-03-18 19:56:12 +00:00
|
|
|
);
|
|
|
|
|
2013-04-29 04:53:49 +00:00
|
|
|
$notifications['edit-thank'] = array(
|
2014-01-14 19:00:33 +00:00
|
|
|
'primary-link' => array( 'message' => 'notification-link-text-view-edit', 'destination' => 'diff' ),
|
2013-03-18 19:56:12 +00:00
|
|
|
'category' => 'edit-thank',
|
2013-04-30 22:31:40 +00:00
|
|
|
'group' => 'positive',
|
2013-03-18 19:56:12 +00:00
|
|
|
'formatter-class' => 'EchoThanksFormatter',
|
|
|
|
'title-message' => 'notification-thanks',
|
|
|
|
'title-params' => array( 'agent', 'difflink', 'title' ),
|
2013-06-19 22:30:52 +00:00
|
|
|
'flyout-message' => 'notification-thanks-flyout2',
|
|
|
|
'flyout-params' => array( 'agent', 'title' ),
|
2013-03-18 19:56:12 +00:00
|
|
|
'payload' => array( 'summary' ),
|
|
|
|
'email-subject-message' => 'notification-thanks-email-subject',
|
|
|
|
'email-subject-params' => array( 'agent' ),
|
|
|
|
'email-body-batch-message' => 'notification-thanks-email-batch-body',
|
|
|
|
'email-body-batch-params' => array( 'agent', 'title' ),
|
2013-06-28 20:44:17 +00:00
|
|
|
'icon' => 'thanks',
|
|
|
|
);
|
|
|
|
|
2014-02-26 02:12:47 +00:00
|
|
|
$notifications['flow-thank'] = array(
|
|
|
|
'primary-link' => array ( 'message' => 'notification-link-text-view-post', 'destination' => 'post' ),
|
|
|
|
'category' => 'edit-thank',
|
|
|
|
'group' => 'positive',
|
|
|
|
'formatter-class' => 'EchoFlowThanksFormatter',
|
|
|
|
'title-message' => 'notification-flow-thanks',
|
2014-09-01 12:33:23 +00:00
|
|
|
'title-params' => array( 'agent', 'postlink', 'topictitle', 'title', 'user' ),
|
2014-02-26 02:12:47 +00:00
|
|
|
'flyout-message' => 'notification-flow-thanks-flyout',
|
2014-09-01 12:33:23 +00:00
|
|
|
'flyout-params' => array( 'agent', 'topictitle', 'title', 'user' ),
|
2014-02-26 02:12:47 +00:00
|
|
|
'email-subject-message' => 'notification-flow-thanks-email-subject',
|
2014-09-01 12:33:23 +00:00
|
|
|
'email-subject-params' => array( 'agent', 'user' ),
|
2014-02-26 02:12:47 +00:00
|
|
|
'email-body-batch-message' => 'notification-flow-thanks-email-batch-body',
|
2014-09-01 12:33:23 +00:00
|
|
|
'email-body-batch-params' => array( 'agent', 'topictitle', 'title', 'user' ),
|
2014-02-26 02:12:47 +00:00
|
|
|
'icon' => 'thanks',
|
|
|
|
);
|
|
|
|
|
2013-06-28 20:44:17 +00:00
|
|
|
$icons['thanks'] = array(
|
|
|
|
'path' => 'Thanks/ThankYou.png',
|
2013-03-18 19:56:12 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add user to be notified on echo event
|
|
|
|
* @param $event EchoEvent
|
|
|
|
* @param $users array
|
2013-03-27 10:45:25 +00:00
|
|
|
* @return bool
|
2013-03-18 19:56:12 +00:00
|
|
|
*/
|
|
|
|
public static function onEchoGetDefaultNotifiedUsers( $event, &$users ) {
|
|
|
|
switch ( $event->getType() ) {
|
|
|
|
case 'edit-thank':
|
2014-02-26 02:12:47 +00:00
|
|
|
case 'flow-thank':
|
2013-03-18 19:56:12 +00:00
|
|
|
$extra = $event->getExtra();
|
|
|
|
if ( !$extra || !isset( $extra['thanked-user-id'] ) ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$recipientId = $extra['thanked-user-id'];
|
|
|
|
$recipient = User::newFromId( $recipientId );
|
|
|
|
$users[$recipientId] = $recipient;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-04-26 23:36:24 +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".
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function onAccountCreated( $user, $byEmail ) {
|
|
|
|
// New users get echo preferences set that are not the default settings for existing users.
|
|
|
|
// Specifically, new users are opted into email notifications for thanks.
|
|
|
|
$user->setOption( 'echo-subscriptions-email-edit-thank', true );
|
|
|
|
$user->saveSettings();
|
|
|
|
return true;
|
|
|
|
}
|
2013-09-26 01:16:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add thanks button to SpecialMobileDiff page
|
|
|
|
* @param &$output OutputPage object
|
|
|
|
* @param $ctx MobileContext object
|
2014-01-15 20:07:17 +00:00
|
|
|
* @param $revisions Array of the two revisions that are being compared in the diff
|
2013-09-26 01:16:56 +00:00
|
|
|
* @return bool true in all cases
|
|
|
|
*/
|
2014-01-15 20:07:17 +00:00
|
|
|
public static function onBeforeSpecialMobileDiffDisplay( &$output, $ctx, $revisions ) {
|
2013-09-26 01:16:56 +00:00
|
|
|
// If the Echo and MobileFrontend extensions are installed and the user is
|
|
|
|
// logged in, show a 'Thank' link.
|
|
|
|
if ( class_exists( 'EchoNotifier' )
|
|
|
|
&& class_exists( 'SpecialMobileDiff' )
|
|
|
|
&& $output->getUser()->isLoggedIn()
|
|
|
|
) {
|
|
|
|
$output->addModules( array( 'ext.thanks.mobilediff' ) );
|
2014-01-15 20:07:17 +00:00
|
|
|
$rev = $revisions[1];
|
|
|
|
if ( $rev ) {
|
|
|
|
if ( $output->getRequest()->getSessionData( 'thanks-thanked-' . $rev->getId() ) ) {
|
|
|
|
// User already sent thanks for this revision
|
|
|
|
$output->addJsConfigVars( 'wgThanksAlreadySent', true );
|
|
|
|
}
|
|
|
|
}
|
2013-09-26 01:16:56 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-02 13:36:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook to add PHPUnit test cases.
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/UnitTestsList
|
|
|
|
*
|
|
|
|
* @param array &$files
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function registerUnitTests( array &$files ) {
|
2015-02-19 22:43:38 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2014-10-23 16:46:47 +00:00
|
|
|
$files[] = __DIR__ . '/tests/';
|
2013-11-02 13:36:44 +00:00
|
|
|
return true;
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
}
|
2013-11-13 02:46:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* So users can just type in a username for target and it'll work
|
|
|
|
* @param array $types
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function onGetLogTypesOnUser( array &$types ) {
|
|
|
|
$types[] = 'thanks';
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-26 02:12:47 +00:00
|
|
|
|
|
|
|
/**
|
2015-06-22 21:14:19 +00:00
|
|
|
* Handler for BeforePageDisplay. Inserts javascript to enhance thank
|
2014-07-14 19:08:43 +00:00
|
|
|
* links from static urls to in-page dialogs along with reloading
|
|
|
|
* the previously thanked state.
|
|
|
|
*
|
2014-02-26 02:12:47 +00:00
|
|
|
* @param OutputPage $out OutputPage object
|
2015-06-22 21:14:19 +00:00
|
|
|
* @param Skin $skin
|
2014-02-26 02:12:47 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-07-14 19:08:43 +00:00
|
|
|
public static function onBeforePageDisplay( OutputPage $out, $skin ) {
|
2014-08-28 23:44:57 +00:00
|
|
|
if ( $out->getTitle()->hasContentModel( 'flow-board' ) ) {
|
2014-07-14 19:08:43 +00:00
|
|
|
$out->addModules( 'ext.thanks.flowthank' );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-03-18 19:56:12 +00:00
|
|
|
}
|