2013-03-18 19:56:12 +00:00
|
|
|
<?php
|
2016-06-16 16:12:38 +00:00
|
|
|
|
2021-03-14 03:43:32 +00:00
|
|
|
namespace MediaWiki\Extension\Thanks;
|
|
|
|
|
|
|
|
use ApiModuleManager;
|
|
|
|
use Article;
|
|
|
|
use CategoryPage;
|
|
|
|
use ConfigException;
|
|
|
|
use DatabaseLogEntry;
|
|
|
|
use DifferenceEngine;
|
|
|
|
use EchoEvent;
|
|
|
|
use ExtensionRegistry;
|
|
|
|
use Html;
|
|
|
|
use ImagePage;
|
|
|
|
use LogEventsList;
|
|
|
|
use LogPage;
|
2020-05-04 22:48:20 +00:00
|
|
|
use MediaWiki\Linker\LinkTarget;
|
2018-02-06 17:01:25 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-05-04 22:48:20 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2020-06-15 21:35:45 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2021-03-14 03:43:32 +00:00
|
|
|
use MobileContext;
|
|
|
|
use OutputPage;
|
|
|
|
use RequestContext;
|
|
|
|
use Skin;
|
|
|
|
use SpecialPage;
|
|
|
|
use Title;
|
|
|
|
use User;
|
|
|
|
use WikiPage;
|
2018-02-06 17:01:25 +00:00
|
|
|
|
2013-03-18 19:56:12 +00:00
|
|
|
/**
|
|
|
|
* Hooks for Thanks extension
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
2021-03-14 03:43:32 +00:00
|
|
|
class Hooks {
|
2018-02-09 07:09:43 +00:00
|
|
|
|
2013-03-18 19:56:12 +00:00
|
|
|
/**
|
2021-06-29 02:23:36 +00:00
|
|
|
* Handler for the HistoryTools hook
|
2019-08-30 01:05:07 +00:00
|
|
|
*
|
2021-06-29 02:23:36 +00:00
|
|
|
* @param RevisionRecord $revisionRecord
|
|
|
|
* @param array &$links
|
|
|
|
* @param RevisionRecord|null $oldRevisionRecord
|
|
|
|
* @param UserIdentity $userIdentity
|
|
|
|
*/
|
|
|
|
public static function onHistoryTools(
|
|
|
|
RevisionRecord $revisionRecord,
|
|
|
|
array &$links,
|
|
|
|
?RevisionRecord $oldRevisionRecord,
|
|
|
|
UserIdentity $userIdentity
|
|
|
|
) {
|
|
|
|
self::insertThankLink( $revisionRecord,
|
|
|
|
$links, $userIdentity );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the DiffTools hook
|
|
|
|
*
|
|
|
|
* @param RevisionRecord $revisionRecord
|
|
|
|
* @param array &$links
|
|
|
|
* @param RevisionRecord|null $oldRevisionRecord
|
|
|
|
* @param UserIdentity $userIdentity
|
|
|
|
*/
|
|
|
|
public static function onDiffTools(
|
|
|
|
RevisionRecord $revisionRecord,
|
|
|
|
array &$links,
|
|
|
|
?RevisionRecord $oldRevisionRecord,
|
|
|
|
UserIdentity $userIdentity
|
|
|
|
) {
|
|
|
|
// Don't allow thanking for a diff that includes multiple revisions
|
|
|
|
// This does a query that is too expensive for history rows (T284274)
|
|
|
|
$previous = MediaWikiServices::getInstance()
|
|
|
|
->getRevisionLookup()
|
|
|
|
->getPreviousRevision( $revisionRecord );
|
|
|
|
if ( $oldRevisionRecord && $previous &&
|
|
|
|
$previous->getId() !== $oldRevisionRecord->getId()
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::insertThankLink( $revisionRecord,
|
|
|
|
$links, $userIdentity );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-30 01:05:07 +00:00
|
|
|
* Insert a 'thank' link into revision interface, if the user is allowed to thank.
|
|
|
|
*
|
2020-06-15 21:35:45 +00:00
|
|
|
* @param RevisionRecord $revisionRecord RevisionRecord object to add the thank link for
|
2017-09-24 19:54:35 +00:00
|
|
|
* @param array &$links Links to add to the revision interface
|
2020-06-15 21:35:45 +00:00
|
|
|
* @param UserIdentity $userIdentity The user performing the thanks.
|
2013-03-18 19:56:12 +00:00
|
|
|
*/
|
2021-06-29 02:23:36 +00:00
|
|
|
private static function insertThankLink(
|
2020-06-15 21:35:45 +00:00
|
|
|
RevisionRecord $revisionRecord,
|
|
|
|
array &$links,
|
|
|
|
UserIdentity $userIdentity
|
|
|
|
) {
|
2020-05-04 22:48:20 +00:00
|
|
|
$recipient = $revisionRecord->getUser();
|
|
|
|
if ( $recipient === null ) {
|
|
|
|
// Cannot see the user
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-19 00:44:44 +00:00
|
|
|
$user = MediaWikiServices::getInstance()->getUserFactory()->newFromUserIdentity( $userIdentity );
|
2020-06-15 21:35:45 +00:00
|
|
|
|
2013-03-18 19:56:12 +00:00
|
|
|
// Don't let users thank themselves.
|
2015-11-24 17:27:25 +00:00
|
|
|
// Exclude anonymous users.
|
2013-03-18 19:56:12 +00:00
|
|
|
// Exclude users who are blocked.
|
2015-11-24 17:27:25 +00:00
|
|
|
// Check whether bots are allowed to receive thanks.
|
2021-06-29 02:23:36 +00:00
|
|
|
// Don't allow thanking for a diff that includes multiple revisions
|
2022-12-21 16:10:22 +00:00
|
|
|
// Check wether we have a revision id to link to
|
2021-12-19 00:44:44 +00:00
|
|
|
if ( $userIdentity->isRegistered()
|
|
|
|
&& !$userIdentity->equals( $recipient )
|
2020-05-04 22:48:20 +00:00
|
|
|
&& !self::isUserBlockedFromTitle( $user, $revisionRecord->getPageAsLinkTarget() )
|
2021-04-20 15:41:21 +00:00
|
|
|
&& !self::isUserBlockedFromThanks( $user )
|
2015-11-24 17:27:25 +00:00
|
|
|
&& self::canReceiveThanks( $recipient )
|
2020-05-04 22:48:20 +00:00
|
|
|
&& !$revisionRecord->isDeleted( RevisionRecord::DELETED_TEXT )
|
2022-12-21 16:10:22 +00:00
|
|
|
&& $revisionRecord->getId() !== 0
|
2013-03-18 19:56:12 +00:00
|
|
|
) {
|
2020-05-04 22:48:20 +00:00
|
|
|
$links[] = self::generateThankElement(
|
|
|
|
$revisionRecord->getId(),
|
|
|
|
$user,
|
|
|
|
$recipient
|
|
|
|
);
|
2015-11-24 17:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 16:12:19 +00:00
|
|
|
/**
|
|
|
|
* Check whether the user is blocked from the title associated with the revision.
|
|
|
|
*
|
2019-08-30 01:05:07 +00:00
|
|
|
* This queries the replicas for a block; if 'no block' is incorrectly reported, it
|
2021-04-20 15:41:21 +00:00
|
|
|
* will be caught by ApiThank::dieOnUserBlockedFromTitle when the user attempts to thank.
|
2019-08-30 01:05:07 +00:00
|
|
|
*
|
2019-04-26 16:12:19 +00:00
|
|
|
* @param User $user
|
2020-05-04 22:48:20 +00:00
|
|
|
* @param LinkTarget $title
|
2019-04-26 16:12:19 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2020-05-04 22:48:20 +00:00
|
|
|
private static function isUserBlockedFromTitle( User $user, LinkTarget $title ) {
|
2019-04-26 16:12:19 +00:00
|
|
|
return MediaWikiServices::getInstance()->getPermissionManager()
|
2019-08-30 01:05:07 +00:00
|
|
|
->isBlockedFrom( $user, $title, true );
|
2019-04-26 16:12:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 15:41:21 +00:00
|
|
|
/**
|
|
|
|
* Check whether the user is blocked from giving thanks.
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private static function isUserBlockedFromThanks( User $user ) {
|
|
|
|
$block = $user->getBlock();
|
|
|
|
return $block && ( $block->isSitewide() || $block->appliesToRight( 'thanks' ) );
|
|
|
|
}
|
|
|
|
|
2015-11-24 17:27:25 +00:00
|
|
|
/**
|
|
|
|
* Check whether a user is allowed to receive thanks or not
|
|
|
|
*
|
2021-12-19 00:44:44 +00:00
|
|
|
* @param UserIdentity $user Recipient
|
2015-11-24 17:27:25 +00:00
|
|
|
* @return bool true if allowed, false if not
|
|
|
|
*/
|
2021-12-19 00:44:44 +00:00
|
|
|
protected static function canReceiveThanks( UserIdentity $user ) {
|
2015-11-24 17:27:25 +00:00
|
|
|
global $wgThanksSendToBots;
|
|
|
|
|
2021-12-19 00:44:44 +00:00
|
|
|
$legacyUser = MediaWikiServices::getInstance()->getUserFactory()->newFromUserIdentity( $user );
|
|
|
|
if ( !$user->isRegistered() || $legacyUser->isSystemUser() ) {
|
2015-11-24 17:27:25 +00:00
|
|
|
return false;
|
2013-03-18 19:56:12 +00:00
|
|
|
}
|
2015-11-24 17:27:25 +00:00
|
|
|
|
2021-12-19 00:44:44 +00:00
|
|
|
if ( !$wgThanksSendToBots && $legacyUser->isBot() ) {
|
2015-11-24 17:27:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
2018-03-29 04:41:58 +00:00
|
|
|
* @param int $id Revision or log ID to generate the thank element for.
|
2019-08-06 12:11:26 +00:00
|
|
|
* @param User $sender User who sends thanks notification.
|
2021-12-19 00:44:44 +00:00
|
|
|
* @param UserIdentity $recipient User who receives thanks notification.
|
2018-03-29 04:41:58 +00:00
|
|
|
* @param string $type Either 'revision' or 'log'.
|
2013-10-07 20:38:51 +00:00
|
|
|
* @return string
|
2013-04-25 20:43:00 +00:00
|
|
|
*/
|
2019-08-06 12:11:26 +00:00
|
|
|
protected static function generateThankElement(
|
2021-12-19 00:44:44 +00:00
|
|
|
$id, User $sender, UserIdentity $recipient, $type = 'revision'
|
2019-08-06 12:11:26 +00:00
|
|
|
) {
|
2018-03-29 04:41:58 +00:00
|
|
|
// Check if the user has already thanked for this revision or log entry.
|
|
|
|
// Session keys are backwards-compatible, and are also used in the ApiCoreThank class.
|
|
|
|
$sessionKey = ( $type === 'revision' ) ? $id : $type . $id;
|
2019-08-06 12:11:26 +00:00
|
|
|
if ( $sender->getRequest()->getSessionData( "thanks-thanked-$sessionKey" ) ) {
|
2013-04-25 20:43:00 +00:00
|
|
|
return Html::element(
|
|
|
|
'span',
|
2016-04-22 20:13:56 +00:00
|
|
|
[ 'class' => 'mw-thanks-thanked' ],
|
2019-08-06 12:11:26 +00:00
|
|
|
wfMessage( 'thanks-thanked', $sender->getName(), $recipient->getName() )->text()
|
2013-04-25 20:43:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-24 04:44:16 +00:00
|
|
|
$genderCache = MediaWikiServices::getInstance()->getGenderCache();
|
2013-04-25 20:43:00 +00:00
|
|
|
// Add 'thank' link
|
2013-08-20 23:23:37 +00:00
|
|
|
$tooltip = wfMessage( 'thanks-thank-tooltip' )
|
2019-08-06 12:11:26 +00:00
|
|
|
->params( $sender->getName(), $recipient->getName() )
|
|
|
|
->text();
|
2013-04-25 20:43:00 +00:00
|
|
|
|
2018-03-29 04:41:58 +00:00
|
|
|
$subpage = ( $type === 'revision' ) ? '' : 'Log/';
|
2013-04-25 20:43:00 +00:00
|
|
|
return Html::element(
|
|
|
|
'a',
|
2016-04-22 20:13:56 +00:00
|
|
|
[
|
2013-04-25 20:43:00 +00:00
|
|
|
'class' => 'mw-thanks-thank-link',
|
2018-03-29 04:41:58 +00:00
|
|
|
'href' => SpecialPage::getTitleFor( 'Thanks', $subpage . $id )->getFullURL(),
|
2013-04-25 20:43:00 +00:00
|
|
|
'title' => $tooltip,
|
2018-03-29 04:41:58 +00:00
|
|
|
'data-' . $type . '-id' => $id,
|
2018-01-24 04:44:16 +00:00
|
|
|
'data-recipient-gender' => $genderCache->getGenderOf( $recipient->getName(), __METHOD__ ),
|
2016-04-22 20:13:56 +00:00
|
|
|
],
|
2019-08-06 12:11:26 +00:00
|
|
|
wfMessage( 'thanks-thank', $sender->getName(), $recipient->getName() )->text()
|
2013-04-25 20:43:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-02 05:37:16 +00:00
|
|
|
/**
|
|
|
|
* @param OutputPage $outputPage The OutputPage to add the module to.
|
|
|
|
*/
|
|
|
|
protected static function addThanksModule( OutputPage $outputPage ) {
|
|
|
|
$confirmationRequired = MediaWikiServices::getInstance()
|
|
|
|
->getMainConfig()
|
|
|
|
->get( 'ThanksConfirmationRequired' );
|
|
|
|
$outputPage->addModules( [ 'ext.thanks.corethank' ] );
|
|
|
|
$outputPage->addJsConfigVars( 'thanks-confirmation-required', $confirmationRequired );
|
|
|
|
}
|
|
|
|
|
2013-03-18 19:56:12 +00:00
|
|
|
/**
|
|
|
|
* Handler for PageHistoryBeforeList hook.
|
2018-10-30 16:50:28 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/PageHistoryBeforeList
|
2020-02-18 12:56:47 +00:00
|
|
|
*
|
|
|
|
* @param WikiPage|Article|ImagePage|CategoryPage $page Not used
|
2017-09-24 19:54:35 +00:00
|
|
|
* @param RequestContext $context RequestContext object
|
2013-03-18 19:56:12 +00:00
|
|
|
*/
|
2020-02-18 12:56:47 +00:00
|
|
|
public static function onPageHistoryBeforeList( $page, $context ) {
|
2020-12-18 02:24:11 +00:00
|
|
|
if ( $context->getUser()->isRegistered() ) {
|
2018-03-02 05:37:16 +00:00
|
|
|
static::addThanksModule( $context->getOutput() );
|
2013-03-18 19:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-24 00:41:11 +00:00
|
|
|
* Handler for DifferenceEngineViewHeader hook.
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/DifferenceEngineViewHeader
|
2018-02-09 07:09:43 +00:00
|
|
|
* @param DifferenceEngine $diff DifferenceEngine object that's calling.
|
2013-03-18 19:56:12 +00:00
|
|
|
*/
|
2020-06-24 00:41:11 +00:00
|
|
|
public static function onDifferenceEngineViewHeader( $diff ) {
|
2020-12-18 02:24:11 +00:00
|
|
|
if ( $diff->getUser()->isRegistered() ) {
|
2018-03-02 05:37:16 +00:00
|
|
|
static::addThanksModule( $diff->getOutput() );
|
2013-03-18 19:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-29 04:53:49 +00:00
|
|
|
* Add Thanks events to Echo
|
|
|
|
*
|
2017-09-24 19:54:35 +00:00
|
|
|
* @param array &$notifications array of Echo notifications
|
|
|
|
* @param array &$notificationCategories array of Echo notification categories
|
|
|
|
* @param array &$icons array of icon details
|
2013-03-18 19:56:12 +00:00
|
|
|
*/
|
2016-04-22 20:13:56 +00:00
|
|
|
public static function onBeforeCreateEchoEvent(
|
|
|
|
&$notifications, &$notificationCategories, &$icons
|
|
|
|
) {
|
|
|
|
$notificationCategories['edit-thank'] = [
|
2013-03-18 19:56:12 +00:00
|
|
|
'priority' => 3,
|
2013-05-30 20:52:39 +00:00
|
|
|
'tooltip' => 'echo-pref-tooltip-edit-thank',
|
2016-04-22 20:13:56 +00:00
|
|
|
];
|
2013-03-18 19:56:12 +00:00
|
|
|
|
2016-04-22 20:13:56 +00:00
|
|
|
$notifications['edit-thank'] = [
|
2013-03-18 19:56:12 +00:00
|
|
|
'category' => 'edit-thank',
|
2013-04-30 22:31:40 +00:00
|
|
|
'group' => 'positive',
|
2016-06-10 12:53:03 +00:00
|
|
|
'section' => 'message',
|
2021-03-06 19:18:00 +00:00
|
|
|
'presentation-model' => EchoCoreThanksPresentationModel::class,
|
2016-06-16 16:12:38 +00:00
|
|
|
'bundle' => [
|
|
|
|
'web' => true,
|
|
|
|
'expandable' => true,
|
|
|
|
],
|
2016-04-22 20:13:56 +00:00
|
|
|
];
|
2013-06-28 20:44:17 +00:00
|
|
|
|
2018-07-06 16:02:10 +00:00
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'Flow' ) ) {
|
2016-11-04 01:20:11 +00:00
|
|
|
$notifications['flow-thank'] = [
|
|
|
|
'category' => 'edit-thank',
|
|
|
|
'group' => 'positive',
|
|
|
|
'section' => 'message',
|
2021-03-06 19:18:00 +00:00
|
|
|
'presentation-model' => EchoFlowThanksPresentationModel::class,
|
2016-11-04 01:20:11 +00:00
|
|
|
'bundle' => [
|
|
|
|
'web' => true,
|
|
|
|
'expandable' => true,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2014-02-26 02:12:47 +00:00
|
|
|
|
2016-04-22 20:13:56 +00:00
|
|
|
$icons['thanks'] = [
|
2016-11-11 01:32:27 +00:00
|
|
|
'path' => [
|
2021-03-06 18:45:55 +00:00
|
|
|
'ltr' => 'Thanks/modules/userTalk-constructive-ltr.svg',
|
|
|
|
'rtl' => 'Thanks/modules/userTalk-constructive-rtl.svg'
|
2016-11-11 01:32:27 +00:00
|
|
|
]
|
2016-04-22 20:13:56 +00:00
|
|
|
];
|
2013-03-18 19:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add user to be notified on echo event
|
2018-02-09 07:09:43 +00:00
|
|
|
* @param EchoEvent $event The event.
|
|
|
|
* @param User[] &$users The user list to add to.
|
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;
|
|
|
|
}
|
|
|
|
}
|
2013-04-26 23:36:24 +00:00
|
|
|
|
|
|
|
/**
|
2016-05-25 18:42:20 +00:00
|
|
|
* Handler for LocalUserCreated hook
|
2018-10-30 16:50:28 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/LocalUserCreated
|
2017-09-24 19:54:35 +00:00
|
|
|
* @param User $user User object that was created.
|
|
|
|
* @param bool $autocreated True when account was auto-created
|
2013-04-26 23:36:24 +00:00
|
|
|
*/
|
2016-05-25 18:42:20 +00:00
|
|
|
public static function onAccountCreated( $user, $autocreated ) {
|
2013-04-26 23:36:24 +00:00
|
|
|
// 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.
|
2016-05-25 18:42:20 +00:00
|
|
|
if ( !$autocreated ) {
|
2021-04-02 17:22:40 +00:00
|
|
|
$userOptionsManager = MediaWikiServices::getInstance()->getUserOptionsManager();
|
|
|
|
$userOptionsManager->setOption( $user, 'echo-subscriptions-email-edit-thank', true );
|
2016-05-25 18:42:20 +00:00
|
|
|
}
|
2013-04-26 23:36:24 +00:00
|
|
|
}
|
2013-09-26 01:16:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add thanks button to SpecialMobileDiff page
|
2017-09-24 19:54:35 +00:00
|
|
|
* @param OutputPage &$output OutputPage object
|
|
|
|
* @param MobileContext $ctx MobileContext object
|
2020-06-30 07:31:23 +00:00
|
|
|
* @param array $revisions Array with two elements, either nulls or RevisionRecord objects for
|
|
|
|
* the two revisions that are being compared in the diff
|
2013-09-26 01:16:56 +00:00
|
|
|
*/
|
2014-01-15 20:07:17 +00:00
|
|
|
public static function onBeforeSpecialMobileDiffDisplay( &$output, $ctx, $revisions ) {
|
2015-11-24 17:27:25 +00:00
|
|
|
$rev = $revisions[1];
|
|
|
|
|
2018-04-11 08:00:30 +00:00
|
|
|
// If the MobileFrontend extension is installed and the user is
|
2015-11-24 17:27:25 +00:00
|
|
|
// logged in or recipient is not a bot if bots cannot receive thanks, show a 'Thank' link.
|
|
|
|
if ( $rev
|
2018-07-06 16:02:10 +00:00
|
|
|
&& ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' )
|
2020-04-15 04:47:04 +00:00
|
|
|
&& $rev->getUser()
|
2021-12-19 00:44:44 +00:00
|
|
|
&& self::canReceiveThanks( $rev->getUser() )
|
2020-12-18 02:24:11 +00:00
|
|
|
&& $output->getUser()->isRegistered()
|
2013-09-26 01:16:56 +00:00
|
|
|
) {
|
2016-04-22 20:13:56 +00:00
|
|
|
$output->addModules( [ 'ext.thanks.mobilediff' ] );
|
2015-11-24 17:27:25 +00:00
|
|
|
|
|
|
|
if ( $output->getRequest()->getSessionData( 'thanks-thanked-' . $rev->getId() ) ) {
|
|
|
|
// User already sent thanks for this revision
|
|
|
|
$output->addJsConfigVars( 'wgThanksAlreadySent', true );
|
2014-01-15 20:07:17 +00:00
|
|
|
}
|
2015-11-24 17:27:25 +00:00
|
|
|
|
2013-09-26 01:16:56 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-02 13:36:44 +00:00
|
|
|
|
2013-11-13 02:46:18 +00:00
|
|
|
/**
|
2018-02-09 07:09:43 +00:00
|
|
|
* Handler for GetLogTypesOnUser.
|
|
|
|
* So users can just type in a username for target and it'll work.
|
|
|
|
* @link https://www.mediawiki.org/wiki/Manual:Hooks/GetLogTypesOnUser
|
|
|
|
* @param string[] &$types The list of log types, to add to.
|
2013-11-13 02:46:18 +00:00
|
|
|
*/
|
|
|
|
public static function onGetLogTypesOnUser( array &$types ) {
|
|
|
|
$types[] = 'thanks';
|
|
|
|
}
|
2014-02-26 02:12:47 +00:00
|
|
|
|
2021-04-20 15:41:21 +00:00
|
|
|
public static function onGetAllBlockActions( array &$actions ) {
|
|
|
|
$actions[ 'thanks' ] = 100;
|
|
|
|
}
|
|
|
|
|
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.
|
2018-02-09 07:09:43 +00:00
|
|
|
* @link https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
|
2014-02-26 02:12:47 +00:00
|
|
|
* @param OutputPage $out OutputPage object
|
2018-02-09 07:09:43 +00:00
|
|
|
* @param Skin $skin The skin in use.
|
2014-02-26 02:12:47 +00:00
|
|
|
*/
|
2014-07-14 19:08:43 +00:00
|
|
|
public static function onBeforePageDisplay( OutputPage $out, $skin ) {
|
2015-07-30 11:11:13 +00:00
|
|
|
$title = $out->getTitle();
|
2018-03-02 05:37:16 +00:00
|
|
|
// Add to Flow boards.
|
2015-07-30 11:11:13 +00:00
|
|
|
if ( $title instanceof Title && $title->hasContentModel( 'flow-board' ) ) {
|
2014-07-14 19:08:43 +00:00
|
|
|
$out->addModules( 'ext.thanks.flowthank' );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
2022-12-16 00:37:46 +00:00
|
|
|
// Add to special pages where thank links appear
|
|
|
|
if (
|
|
|
|
$title->isSpecial( 'Log' ) ||
|
|
|
|
$title->isSpecial( 'Contributions' ) ||
|
|
|
|
$title->isSpecial( 'DeletedContributions' ) ||
|
|
|
|
$title->isSpecial( 'Recentchanges' ) ||
|
|
|
|
$title->isSpecial( 'Recentchangeslinked' ) ||
|
|
|
|
$title->isSpecial( 'Watchlist' )
|
|
|
|
) {
|
2018-03-02 05:37:16 +00:00
|
|
|
static::addThanksModule( $out );
|
|
|
|
}
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
2015-06-25 21:25:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Conditionally load API module 'flowthank' depending on whether or not
|
|
|
|
* Flow is installed.
|
|
|
|
*
|
|
|
|
* @param ApiModuleManager $moduleManager Module manager instance
|
|
|
|
*/
|
|
|
|
public static function onApiMainModuleManager( ApiModuleManager $moduleManager ) {
|
2018-07-06 16:02:10 +00:00
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'Flow' ) ) {
|
2015-06-25 21:25:27 +00:00
|
|
|
$moduleManager->addModule(
|
|
|
|
'flowthank',
|
|
|
|
'action',
|
2021-03-14 03:43:32 +00:00
|
|
|
ApiFlowThank::class
|
2015-06-25 21:25:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-06-16 16:12:38 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-09 07:09:43 +00:00
|
|
|
* Handler for EchoGetBundleRule hook, which defines the bundle rules for each notification.
|
2016-06-16 16:12:38 +00:00
|
|
|
*
|
2018-02-09 07:09:43 +00:00
|
|
|
* @param EchoEvent $event The event being notified.
|
|
|
|
* @param string &$bundleString Determines how the notification should be bundled.
|
2016-06-16 16:12:38 +00:00
|
|
|
*/
|
|
|
|
public static function onEchoGetBundleRules( $event, &$bundleString ) {
|
|
|
|
switch ( $event->getType() ) {
|
|
|
|
case 'edit-thank':
|
2016-06-30 15:38:51 +00:00
|
|
|
$bundleString = 'edit-thank';
|
2018-05-11 14:39:11 +00:00
|
|
|
// Try to get either the revid or logid parameter.
|
|
|
|
$revOrLogId = $event->getExtraParam( 'logid' );
|
|
|
|
if ( $revOrLogId ) {
|
|
|
|
// avoid collision with revision ids
|
|
|
|
$revOrLogId = 'log' . $revOrLogId;
|
|
|
|
} else {
|
|
|
|
$revOrLogId = $event->getExtraParam( 'revid' );
|
2018-03-03 04:00:40 +00:00
|
|
|
}
|
|
|
|
if ( $revOrLogId ) {
|
|
|
|
$bundleString .= $revOrLogId;
|
2016-06-16 16:12:38 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'flow-thank':
|
2016-06-30 15:38:51 +00:00
|
|
|
$bundleString = 'flow-thank';
|
2016-06-16 16:12:38 +00:00
|
|
|
$postId = $event->getExtraParam( 'post-id' );
|
|
|
|
if ( $postId ) {
|
2016-06-30 15:38:51 +00:00
|
|
|
$bundleString .= $postId;
|
2016-06-16 16:12:38 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-03-02 05:37:16 +00:00
|
|
|
|
|
|
|
/**
|
2019-08-30 01:05:07 +00:00
|
|
|
* Insert a 'thank' link into the log interface, if the user is allowed to thank.
|
|
|
|
*
|
2018-03-02 05:37:16 +00:00
|
|
|
* @link https://www.mediawiki.org/wiki/Manual:Hooks/LogEventsListLineEnding
|
|
|
|
* @param LogEventsList $page The log events list.
|
2021-03-14 03:43:32 +00:00
|
|
|
* @param string &$ret The line ending HTML, to modify.
|
2018-03-02 05:37:16 +00:00
|
|
|
* @param DatabaseLogEntry $entry The log entry.
|
|
|
|
* @param string[] &$classes CSS classes to add to the line.
|
|
|
|
* @param string[] &$attribs HTML attributes to add to the line.
|
|
|
|
* @throws ConfigException
|
|
|
|
*/
|
|
|
|
public static function onLogEventsListLineEnding(
|
|
|
|
LogEventsList $page, &$ret, DatabaseLogEntry $entry, &$classes, &$attribs
|
|
|
|
) {
|
2019-08-06 12:11:26 +00:00
|
|
|
$user = $page->getUser();
|
2018-03-02 05:37:16 +00:00
|
|
|
|
2019-07-11 09:26:22 +00:00
|
|
|
// Don't thank if anonymous or blocked or if user is deleted from the log entry
|
2019-04-26 16:12:19 +00:00
|
|
|
if (
|
2019-08-06 12:11:26 +00:00
|
|
|
$user->isAnon()
|
2019-07-11 09:26:22 +00:00
|
|
|
|| $entry->isDeleted( LogPage::DELETED_USER )
|
2019-08-06 12:11:26 +00:00
|
|
|
|| self::isUserBlockedFromTitle( $user, $entry->getTarget() )
|
2021-04-20 15:41:21 +00:00
|
|
|
|| self::isUserBlockedFromThanks( $user )
|
2019-04-26 16:12:19 +00:00
|
|
|
) {
|
2018-03-02 05:37:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-20 04:43:13 +00:00
|
|
|
// Make sure this log type is allowed.
|
|
|
|
$allowedLogTypes = MediaWikiServices::getInstance()
|
2018-03-02 05:37:16 +00:00
|
|
|
->getMainConfig()
|
2021-03-20 04:43:13 +00:00
|
|
|
->get( 'ThanksAllowedLogTypes' );
|
|
|
|
if ( !in_array( $entry->getType(), $allowedLogTypes )
|
|
|
|
&& !in_array( $entry->getType() . '/' . $entry->getSubtype(), $allowedLogTypes ) ) {
|
2018-03-02 05:37:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-29 04:41:58 +00:00
|
|
|
// Don't thank if no recipient,
|
|
|
|
// or if recipient is the current user or unable to receive thanks.
|
|
|
|
// Don't check for deleted revision (this avoids extraneous queries from Special:Log).
|
2021-02-23 15:02:15 +00:00
|
|
|
|
2021-12-19 00:44:44 +00:00
|
|
|
$recipient = $entry->getPerformerIdentity();
|
2021-02-23 15:02:15 +00:00
|
|
|
if ( $recipient->getId() === $user->getId() || !self::canReceiveThanks( $recipient ) ) {
|
2018-03-02 05:37:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-03-29 04:41:58 +00:00
|
|
|
|
|
|
|
// Create thank link either for the revision (if there is an associated revision ID)
|
|
|
|
// or the log entry.
|
|
|
|
$type = $entry->getAssociatedRevId() ? 'revision' : 'log';
|
2019-03-11 03:39:42 +00:00
|
|
|
$id = $entry->getAssociatedRevId() ?: $entry->getId();
|
2019-08-06 12:11:26 +00:00
|
|
|
$thankLink = self::generateThankElement( $id, $user, $recipient, $type );
|
2018-03-29 04:41:58 +00:00
|
|
|
|
|
|
|
// Add parentheses to match what's done with Thanks in revision lists and diff displays.
|
|
|
|
$ret .= ' ' . wfMessage( 'parentheses' )->rawParams( $thankLink )->escaped();
|
2018-03-02 05:37:16 +00:00
|
|
|
}
|
2013-03-18 19:56:12 +00:00
|
|
|
}
|