| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
namespace MediaWiki\Extension\Thanks; |
| 6 |
|
| 7 |
use Article; |
| 8 |
use DatabaseLogEntry; |
| 9 |
use DifferenceEngine; |
| 10 |
use LogEventsList; |
| 11 |
use LogPage; |
| 12 |
use MediaWiki\Api\ApiModuleManager; |
| 13 |
use MediaWiki\Api\Hook\ApiMain__moduleManagerHook; |
| 14 |
use MediaWiki\Auth\Hook\LocalUserCreatedHook; |
| 15 |
use MediaWiki\Block\Hook\GetAllBlockActionsHook; |
| 16 |
use MediaWiki\Cache\GenderCache; |
| 17 |
use MediaWiki\Config\Config; |
| 18 |
use MediaWiki\Config\ConfigException; |
| 19 |
use MediaWiki\Context\IContextSource; |
| 20 |
use MediaWiki\Context\RequestContext; |
| 21 |
use MediaWiki\Diff\Hook\DifferenceEngineViewHeaderHook; |
| 22 |
use MediaWiki\Diff\Hook\DiffToolsHook; |
| 23 |
use MediaWiki\Extension\Thanks\Api\ApiFlowThank; |
| 24 |
use MediaWiki\Hook\ChangesListInitRowsHook; |
| 25 |
use MediaWiki\Hook\GetLogTypesOnUserHook; |
| 26 |
use MediaWiki\Hook\HistoryToolsHook; |
| 27 |
use MediaWiki\Hook\LogEventsListLineEndingHook; |
| 28 |
use MediaWiki\Hook\PageHistoryBeforeListHook; |
| 29 |
use MediaWiki\Hook\PageHistoryPager__doBatchLookupsHook; |
| 30 |
use MediaWiki\Html\Html; |
| 31 |
use MediaWiki\Linker\LinkTarget; |
| 32 |
use MediaWiki\Output\Hook\BeforePageDisplayHook; |
| 33 |
use MediaWiki\Output\OutputPage; |
| 34 |
use MediaWiki\Permissions\PermissionManager; |
| 35 |
use MediaWiki\Registration\ExtensionRegistry; |
| 36 |
use MediaWiki\Revision\RevisionLookup; |
| 37 |
use MediaWiki\Revision\RevisionRecord; |
| 38 |
use MediaWiki\SpecialPage\SpecialPage; |
| 39 |
use MediaWiki\Title\Title; |
| 40 |
use MediaWiki\User\Options\UserOptionsManager; |
| 41 |
use MediaWiki\User\User; |
| 42 |
use MediaWiki\User\UserFactory; |
| 43 |
use MediaWiki\User\UserIdentity; |
| 44 |
use Skin; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
class Hooks implements |
| 53 |
ApiMain__moduleManagerHook, |
| 54 |
BeforePageDisplayHook, |
| 55 |
DiffToolsHook, |
| 56 |
DifferenceEngineViewHeaderHook, |
| 57 |
GetAllBlockActionsHook, |
| 58 |
GetLogTypesOnUserHook, |
| 59 |
HistoryToolsHook, |
| 60 |
LocalUserCreatedHook, |
| 61 |
LogEventsListLineEndingHook, |
| 62 |
PageHistoryBeforeListHook, |
| 63 |
PageHistoryPager__doBatchLookupsHook, |
| 64 |
ChangesListInitRowsHook |
| 65 |
{ |
| 66 |
private Config $config; |
| 67 |
private GenderCache $genderCache; |
| 68 |
private PermissionManager $permissionManager; |
| 69 |
private RevisionLookup $revisionLookup; |
| 70 |
private UserFactory $userFactory; |
| 71 |
private UserOptionsManager $userOptionsManager; |
| 72 |
|
| 73 |
public function __construct( |
| 74 |
Config $config, |
| 75 |
GenderCache $genderCache, |
| 76 |
PermissionManager $permissionManager, |
| 77 |
RevisionLookup $revisionLookup, |
| 78 |
UserFactory $userFactory, |
| 79 |
UserOptionsManager $userOptionsManager |
| 80 |
) { |
| 81 |
$this->config = $config; |
| 82 |
$this->genderCache = $genderCache; |
| 83 |
$this->permissionManager = $permissionManager; |
| 84 |
$this->revisionLookup = $revisionLookup; |
| 85 |
$this->userFactory = $userFactory; |
| 86 |
$this->userOptionsManager = $userOptionsManager; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function onHistoryTools( |
| 98 |
$revisionRecord, |
| 99 |
&$links, |
| 100 |
$oldRevisionRecord, |
| 101 |
$userIdentity |
| 102 |
) { |
| 103 |
$this->insertThankLink( $revisionRecord, |
| 104 |
$links, $userIdentity ); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public function onDiffTools( |
| 116 |
$revisionRecord, |
| 117 |
&$links, |
| 118 |
$oldRevisionRecord, |
| 119 |
$userIdentity |
| 120 |
) { |
| 121 |
|
| 122 |
|
| 123 |
$previous = $this->revisionLookup->getPreviousRevision( $revisionRecord ); |
| 124 |
if ( $oldRevisionRecord && $previous && |
| 125 |
$previous->getId() !== $oldRevisionRecord->getId() |
| 126 |
) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
$this->insertThankLink( $revisionRecord, |
| 131 |
$links, $userIdentity, true ); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
private function insertThankLink( |
| 143 |
RevisionRecord $revisionRecord, |
| 144 |
array &$links, |
| 145 |
UserIdentity $userIdentity, |
| 146 |
bool $isPrimaryButton = false |
| 147 |
) { |
| 148 |
$recipient = $revisionRecord->getUser(); |
| 149 |
if ( $recipient === null ) { |
| 150 |
|
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
$user = $this->userFactory->newFromUserIdentity( $userIdentity ); |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
if ( $user->isNamed() |
| 164 |
&& !$userIdentity->equals( $recipient ) |
| 165 |
&& !$this->isUserBlockedFromTitle( $user, $revisionRecord->getPageAsLinkTarget() ) |
| 166 |
&& !self::isUserBlockedFromThanks( $user ) |
| 167 |
&& self::canReceiveThanks( $this->config, $this->userFactory, $recipient ) |
| 168 |
&& !$revisionRecord->isDeleted( RevisionRecord::DELETED_TEXT ) |
| 169 |
&& $revisionRecord->getId() !== 0 |
| 170 |
) { |
| 171 |
$links[] = $this->generateThankElement( |
| 172 |
$revisionRecord->getId(), |
| 173 |
$user, |
| 174 |
$recipient, |
| 175 |
'revision', |
| 176 |
$isPrimaryButton |
| 177 |
); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
private function isUserBlockedFromTitle( User $user, LinkTarget $title ) { |
| 192 |
return $this->permissionManager->isBlockedFrom( $user, $title, true ); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
private static function isUserBlockedFromThanks( User $user ) { |
| 202 |
$block = $user->getBlock(); |
| 203 |
return $block && ( $block->isSitewide() || $block->appliesToRight( 'thanks' ) ); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public static function canReceiveThanks( |
| 215 |
Config $config, |
| 216 |
UserFactory $userFactory, |
| 217 |
UserIdentity $user |
| 218 |
) { |
| 219 |
$legacyUser = $userFactory->newFromUserIdentity( $user ); |
| 220 |
if ( !$user->isRegistered() || $legacyUser->isSystemUser() ) { |
| 221 |
return false; |
| 222 |
} |
| 223 |
|
| 224 |
if ( !$config->get( 'ThanksSendToBots' ) && |
| 225 |
$legacyUser->isBot() |
| 226 |
) { |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
return true; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
protected function generateThankElement( |
| 244 |
$id, User $sender, UserIdentity $recipient, $type = 'revision', |
| 245 |
bool $isPrimaryButton = false |
| 246 |
) { |
| 247 |
$useCodex = RequestContext::getMain()->getSkin()->getSkinName() === 'minerva'; |
| 248 |
|
| 249 |
|
| 250 |
$sessionKey = ( $type === 'revision' ) ? $id : $type . $id; |
| 251 |
$class = $useCodex ? 'cdx-button cdx-button--fake-button cdx-button--fake-button--enabled' : ''; |
| 252 |
if ( $isPrimaryButton && $useCodex ) { |
| 253 |
$class .= ' cdx-button--weight-primary cdx-button--action-progressive'; |
| 254 |
} |
| 255 |
if ( $sender->getRequest()->getSessionData( "thanks-thanked-$sessionKey" ) ) { |
| 256 |
$class .= ' mw-thanks-thanked'; |
| 257 |
|
| 258 |
return Html::element( |
| 259 |
'span', |
| 260 |
[ 'class' => $class ], |
| 261 |
wfMessage( 'thanks-thanked', $sender->getName(), $recipient->getName() )->text() |
| 262 |
); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
$tooltip = wfMessage( 'thanks-thank-tooltip' ) |
| 267 |
->params( $sender->getName(), $recipient->getName() ) |
| 268 |
->text(); |
| 269 |
|
| 270 |
$class .= ' mw-thanks-thank-link'; |
| 271 |
$subpage = ( $type === 'revision' ) ? '' : 'Log/'; |
| 272 |
return Html::element( |
| 273 |
'a', |
| 274 |
[ |
| 275 |
'class' => $class, |
| 276 |
'href' => SpecialPage::getTitleFor( 'Thanks', $subpage . $id )->getFullURL(), |
| 277 |
'title' => $tooltip, |
| 278 |
'role' => 'button', |
| 279 |
'data-' . $type . '-id' => $id, |
| 280 |
'data-recipient-gender' => $this->genderCache->getGenderOf( $recipient->getName(), __METHOD__ ), |
| 281 |
], |
| 282 |
wfMessage( 'thanks-thank', $sender->getName(), $recipient->getName() )->text() |
| 283 |
); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
protected function addThanksModule( OutputPage $outputPage ) { |
| 290 |
$confirmationRequired = $this->config->get( 'ThanksConfirmationRequired' ); |
| 291 |
$outputPage->addModules( [ 'ext.thanks.corethank' ] ); |
| 292 |
$outputPage->addJsConfigVars( 'thanks-confirmation-required', $confirmationRequired ); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public function onPageHistoryBeforeList( $page, $context ) { |
| 303 |
if ( $context->getUser()->isRegistered() ) { |
| 304 |
$this->addThanksModule( $context->getOutput() ); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
public function onPageHistoryPager__doBatchLookups( $pager, $result ) { |
| 309 |
$userNames = []; |
| 310 |
foreach ( $result as $row ) { |
| 311 |
if ( $row->user_name !== null ) { |
| 312 |
$userNames[] = $row->user_name; |
| 313 |
} |
| 314 |
} |
| 315 |
if ( $userNames ) { |
| 316 |
|
| 317 |
$this->genderCache->doQuery( $userNames, __METHOD__ ); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
public function onChangesListInitRows( $changesList, $rows ) { |
| 322 |
$userNames = []; |
| 323 |
foreach ( $rows as $row ) { |
| 324 |
if ( $row->rc_user_text !== null ) { |
| 325 |
$userNames[] = $row->rc_user_text; |
| 326 |
} |
| 327 |
} |
| 328 |
if ( $userNames ) { |
| 329 |
|
| 330 |
$this->genderCache->doQuery( $userNames, __METHOD__ ); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public function onDifferenceEngineViewHeader( $diff ) { |
| 340 |
if ( $diff->getUser()->isRegistered() ) { |
| 341 |
$this->addThanksModule( $diff->getOutput() ); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
public function onLocalUserCreated( $user, $autocreated ) { |
| 352 |
|
| 353 |
|
| 354 |
if ( !$user->isTemp() && !$autocreated ) { |
| 355 |
$this->userOptionsManager->setOption( $user, 'echo-subscriptions-email-edit-thank', true ); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
public function onGetLogTypesOnUser( &$types ) { |
| 366 |
$types[] = 'thanks'; |
| 367 |
} |
| 368 |
|
| 369 |
public function onGetAllBlockActions( &$actions ) { |
| 370 |
$actions[ 'thanks' ] = 100; |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
public function onBeforePageDisplay( $out, $skin ): void { |
| 382 |
$title = $out->getTitle(); |
| 383 |
|
| 384 |
if ( $title instanceof Title && $title->hasContentModel( 'flow-board' ) ) { |
| 385 |
$out->addModules( 'ext.thanks.flowthank' ); |
| 386 |
} |
| 387 |
|
| 388 |
if ( |
| 389 |
$title->isSpecial( 'Log' ) || |
| 390 |
$title->isSpecial( 'Contributions' ) || |
| 391 |
$title->isSpecial( 'DeletedContributions' ) || |
| 392 |
$title->isSpecial( 'Recentchanges' ) || |
| 393 |
$title->isSpecial( 'Recentchangeslinked' ) || |
| 394 |
$title->isSpecial( 'Watchlist' ) |
| 395 |
) { |
| 396 |
$this->addThanksModule( $out ); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
public function onApiMain__moduleManager( $moduleManager ) { |
| 407 |
if ( ExtensionRegistry::getInstance()->isLoaded( 'Flow' ) ) { |
| 408 |
$moduleManager->addModule( |
| 409 |
'flowthank', |
| 410 |
'action', |
| 411 |
[ |
| 412 |
"class" => ApiFlowThank::class, |
| 413 |
"services" => [ |
| 414 |
"PermissionManager", |
| 415 |
"ThanksLogStore", |
| 416 |
"UserFactory", |
| 417 |
] |
| 418 |
] |
| 419 |
); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
public function onLogEventsListLineEnding( |
| 435 |
$page, &$ret, $entry, &$classes, &$attribs |
| 436 |
) { |
| 437 |
$user = $page->getUser(); |
| 438 |
|
| 439 |
|
| 440 |
if ( |
| 441 |
!$user->isNamed() |
| 442 |
|| $entry->isDeleted( LogPage::DELETED_USER ) |
| 443 |
|| $this->isUserBlockedFromTitle( $user, $entry->getTarget() ) |
| 444 |
|| self::isUserBlockedFromThanks( $user ) |
| 445 |
) { |
| 446 |
return; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
$allowedLogTypes = $this->config->get( 'ThanksAllowedLogTypes' ); |
| 451 |
if ( !in_array( $entry->getType(), $allowedLogTypes ) |
| 452 |
&& !in_array( $entry->getType() . '/' . $entry->getSubtype(), $allowedLogTypes ) ) { |
| 453 |
return; |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
$recipient = $entry->getPerformerIdentity(); |
| 461 |
if ( $recipient->getId() === $user->getId() || |
| 462 |
!self::canReceiveThanks( $this->config, $this->userFactory, $recipient ) |
| 463 |
) { |
| 464 |
return; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
$type = $entry->getAssociatedRevId() ? 'revision' : 'log'; |
| 470 |
$id = $entry->getAssociatedRevId() ?: $entry->getId(); |
| 471 |
$thankLink = $this->generateThankElement( $id, $user, $recipient, $type ); |
| 472 |
|
| 473 |
|
| 474 |
$ret .= ' ' . wfMessage( 'parentheses' )->rawParams( $thankLink )->escaped(); |
| 475 |
} |
| 476 |
} |