| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Formatters; |
| 4 |
|
| 5 |
use MediaWiki\Extension\Notifications\Model\Event; |
| 6 |
use MediaWiki\Language\Language; |
| 7 |
use MediaWiki\User\User; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class EchoMentionStatusPresentationModel extends EchoEventPresentationModel { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
protected $section; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
protected function __construct( Event $event, Language $language, User $user, $distributionType ) { |
| 27 |
parent::__construct( $event, $language, $user, $distributionType ); |
| 28 |
$this->section = new EchoPresentationModelSection( $event, $user, $language ); |
| 29 |
} |
| 30 |
|
| 31 |
public function getIconType() { |
| 32 |
if ( $this->isMixedBundle() ) { |
| 33 |
return 'mention-status-bundle'; |
| 34 |
} |
| 35 |
if ( $this->isMentionSuccess() ) { |
| 36 |
return 'mention-success'; |
| 37 |
} |
| 38 |
return 'mention-failure'; |
| 39 |
} |
| 40 |
|
| 41 |
public function canRender() { |
| 42 |
return (bool)$this->event->getTitle(); |
| 43 |
} |
| 44 |
|
| 45 |
public function getHeaderMessage() { |
| 46 |
if ( $this->isTooManyMentionsFailure() ) { |
| 47 |
$msg = $this->getMessageWithAgent( 'notification-header-mention-failure-too-many' ); |
| 48 |
$msg->numParams( $this->getMaxMentions() ); |
| 49 |
return $msg; |
| 50 |
} |
| 51 |
|
| 52 |
if ( $this->isBundled() ) { |
| 53 |
if ( $this->isMixedBundle() ) { |
| 54 |
$successCount = $this->getBundleSuccessCount(); |
| 55 |
|
| 56 |
$msg = $this->getMessageWithAgent( 'notification-header-mention-status-bundle' ); |
| 57 |
$msg->numParams( $this->getBundleCount() ); |
| 58 |
$msg->params( $this->getTruncatedTitleText( $this->event->getTitle() ) ); |
| 59 |
$msg->numParams( $this->getBundleCount() - $successCount ); |
| 60 |
$msg->numParams( $successCount ); |
| 61 |
return $msg; |
| 62 |
} |
| 63 |
if ( $this->isMentionSuccess() ) { |
| 64 |
$msgKey = 'notification-header-mention-success-bundle'; |
| 65 |
} else { |
| 66 |
$msgKey = 'notification-header-mention-failure-bundle'; |
| 67 |
} |
| 68 |
$msg = $this->getMessageWithAgent( $msgKey ); |
| 69 |
$msg->numParams( $this->getBundleCount() ); |
| 70 |
$msg->params( $this->getTruncatedTitleText( $this->event->getTitle() ) ); |
| 71 |
return $msg; |
| 72 |
} |
| 73 |
|
| 74 |
if ( $this->isMentionSuccess() ) { |
| 75 |
$msgKey = 'notification-header-mention-success'; |
| 76 |
} else { |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
$msgKey = 'notification-header-mention-failure-' . $this->getFailureType(); |
| 81 |
} |
| 82 |
$msg = $this->getMessageWithAgent( $msgKey ); |
| 83 |
$msg->params( $this->getSubjectName() ); |
| 84 |
return $msg; |
| 85 |
} |
| 86 |
|
| 87 |
public function getCompactHeaderMessage() { |
| 88 |
if ( $this->isMentionSuccess() ) { |
| 89 |
$msg = $this->getMessageWithAgent( 'notification-compact-header-mention-success' ); |
| 90 |
} else { |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
$msg = $this->msg( 'notification-compact-header-mention-failure-' . $this->getFailureType() ); |
| 95 |
} |
| 96 |
$msg->params( $this->getSubjectName() ); |
| 97 |
return $msg; |
| 98 |
} |
| 99 |
|
| 100 |
public function getPrimaryLink() { |
| 101 |
return [ |
| 102 |
|
| 103 |
'url' => $this->section->getTitleWithSection()->getFullURL(), |
| 104 |
'label' => $this->msg( 'notification-link-text-view-mention-failure' ) |
| 105 |
->numParams( $this->getBundleCount() ) |
| 106 |
->text() |
| 107 |
]; |
| 108 |
} |
| 109 |
|
| 110 |
public function getSecondaryLinks() { |
| 111 |
if ( $this->isBundled() ) { |
| 112 |
return []; |
| 113 |
} |
| 114 |
|
| 115 |
$talkPageLink = $this->getPageLink( |
| 116 |
$this->section->getTitleWithSection(), |
| 117 |
'', |
| 118 |
true |
| 119 |
); |
| 120 |
|
| 121 |
return [ $talkPageLink ]; |
| 122 |
} |
| 123 |
|
| 124 |
public function isMentionSuccessEvent( Event $event ) { |
| 125 |
return $event->getType() === 'mention-success'; |
| 126 |
} |
| 127 |
|
| 128 |
private function isMentionSuccess() { |
| 129 |
return $this->isMentionSuccessEvent( $this->event ); |
| 130 |
} |
| 131 |
|
| 132 |
private function getSubjectName() { |
| 133 |
return $this->event->getExtraParam( 'subject-name', '' ); |
| 134 |
} |
| 135 |
|
| 136 |
private function getFailureType() { |
| 137 |
return $this->event->getExtraParam( 'failure-type', 'user-unknown' ); |
| 138 |
} |
| 139 |
|
| 140 |
private function isTooManyMentionsFailure() { |
| 141 |
return $this->getFailureType() === 'too-many' || |
| 142 |
$this->getType() === 'mention-failure-too-many'; |
| 143 |
} |
| 144 |
|
| 145 |
private function getMaxMentions() { |
| 146 |
global $wgEchoMaxMentionsCount; |
| 147 |
return $this->event->getExtraParam( 'max-mentions', $wgEchoMaxMentionsCount ); |
| 148 |
} |
| 149 |
|
| 150 |
private function getBundleSuccessCount() { |
| 151 |
$events = array_merge( $this->getBundledEvents(), [ $this->event ] ); |
| 152 |
return count( array_filter( $events, [ $this, 'isMentionSuccessEvent' ] ) ); |
| 153 |
} |
| 154 |
|
| 155 |
private function isMixedBundle() { |
| 156 |
$successCount = $this->getBundleSuccessCount(); |
| 157 |
$failCount = $this->getBundleCount() - $successCount; |
| 158 |
return $successCount > 0 && $failCount > 0; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
class_alias( EchoMentionStatusPresentationModel::class, 'EchoMentionStatusPresentationModel' ); |