Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 66
0.00% covered (danger)
0.00%
0 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
EchoMentionStatusPresentationModel
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 15
702
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getIconType
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 canRender
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHeaderMessage
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
42
 getCompactHeaderMessage
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getPrimaryLink
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getSecondaryLinks
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 isMentionSuccessEvent
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isMentionSuccess
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSubjectName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFailureType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isTooManyMentionsFailure
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getMaxMentions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getBundleSuccessCount
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 isMixedBundle
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Extension\Notifications\Formatters;
4
5use Language;
6use MediaWiki\Extension\Notifications\Model\Event;
7use MediaWiki\User\User;
8
9/**
10 * Presenter for 'mention-failure' and 'mention-success' notifications
11 *
12 * @author Christoph Fischer <christoph.fischer@wikimedia.de>
13 *
14 * @license MIT
15 */
16class EchoMentionStatusPresentationModel extends EchoEventPresentationModel {
17
18    /**
19     * @var EchoPresentationModelSection
20     */
21    protected $section;
22
23    /**
24     * @inheritDoc
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            // Messages that can be used here:
78            // * notification-header-mention-failure-user-unknown
79            // * notification-header-mention-failure-user-anonymous
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            // Messages that can be used here:
92            // * notification-compact-header-mention-failure-user-unknown
93            // * notification-compact-header-mention-failure-user-anonymous
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            // Need FullURL so the section is included
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
162class_alias( EchoMentionStatusPresentationModel::class, 'EchoMentionStatusPresentationModel' );