Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageGroupSubscriptionPresentationModel.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use MediaWiki\Extension\Notifications\Formatters\EchoEventPresentationModel;
7use MediaWiki\Extension\Notifications\Model\Event;
8use MediaWiki\SpecialPage\SpecialPage;
9
16class MessageGroupSubscriptionPresentationModel extends EchoEventPresentationModel {
17 public function getIconType() {
18 return 'translate-mgs-icon';
19 }
20
21 public function getPrimaryLink() {
22 if ( $this->isBundled() ) {
23 return false;
24 }
25
26 $groupId = $this->event->getExtraParam( 'groupId' );
27 return [
28 'url' => SpecialPage::getTitleFor( 'Translate', $groupId )->getFullURL(),
29 'label' => $this->msg( 'notification-link-mgs-group-translate' )
30 ->params( $this->event->getExtraParam( 'groupLabel' ) )
31 ];
32 }
33
34 public function getHeaderMessage() {
35 $msg = $this->msg( 'notification-header-message-group-subscription' );
36 $msg->params( $this->event->getExtraParam( 'groupLabel' ) );
37 return $msg;
38 }
39
40 public function getCompactHeaderMessageKey(): string {
41 $type = $this->event->getType();
42 if ( $type === 'translate-mgs-message-added' ) {
43 return 'notification-body-translate-mgs-message-added';
44 }
45
46 return parent::getCompactHeaderMessageKey();
47 }
48
49 public function getCompactHeaderMessage() {
50 $msg = $this->msg( $this->getCompactHeaderMessageKey() );
51 $msg->params( $this->getNumberOfChangedMessages( $this->event ) );
52 return $msg;
53 }
54
55 public function getBodyMessage() {
56 $type = $this->event->getType();
57 if ( $type === 'translate-mgs-message-added' ) {
58 if ( $this->isBundled() ) {
59 $events = $this->getBundledEvents();
60 $events[] = $this->event;
61 } else {
62 $events = [ $this->event ];
63 }
64
65 $addedOrUpdatedMessages = 0;
66 foreach ( $events as $event ) {
67 $addedOrUpdatedMessages += $this->getNumberOfChangedMessages( $event );
68 }
69
70 $msg = $this->msg( 'notification-body-translate-mgs-message-added' );
71 $msg->params( $addedOrUpdatedMessages );
72 return $msg;
73 }
74 }
75
76 private function getNumberOfChangedMessages( Event $event ): int {
77 $changes = $event->getExtraParam( 'changes' );
78 $messageCount = 0;
79 $messageCount += count( $changes[MessageGroupSubscription::STATE_ADDED] ?? [] );
80 $messageCount += count( $changes[MessageGroupSubscription::STATE_UPDATED] ?? [] );
81
82 return $messageCount;
83 }
84}