Debian Codegraph @

extensions/LoginNotify/includes/PresentationModel.php

raw symbols SCIP
1 <?php
2
3 namespace LoginNotify;
4
5 use MediaWiki\Extension\Notifications\Formatters\EchoEventPresentationModel;
6 use MediaWiki\Extension\Notifications\Model\Event;
7 use MediaWiki\Message\Message;
8 use MediaWiki\SpecialPage\SpecialPage;
9
10 class PresentationModel extends EchoEventPresentationModel {
11
12 /**
13 * Show an user avatar.
14 *
15 * @return string Name of icon
16 */
17 public function getIconType() {
18 return 'LoginNotify-user-avatar';
19 }
20
21 /**
22 * Link to help page on mediawiki
23 *
24 * @return array URL to link to
25 */
26 public function getPrimaryLink() {
27 return [
28 'url' => 'https://mediawiki.org/wiki/Special:MyLanguage/Help:Login_notifications',
29 'label' => $this->msg( 'loginnotify-primary-link' )->text()
30 ];
31 }
32
33 /**
34 * Define the email subject string
35 *
36 * @return Message Email subject
37 */
38 public function getSubjectMessage() {
39 switch ( $this->event->getType() ) {
40 case 'login-fail-known':
41 case 'login-fail-new':
42 $msg = $this->msg( 'notification-loginnotify-login-fail-email-subject' );
43 $msg->params( $this->getUser()->getName() );
44 $msg->numParams( $this->event->getExtraParam( 'count', 0 ) );
45 break;
46 default:
47 $msg = $this->msg( 'notification-loginnotify-login-success-email-subject' );
48 $msg->params( $this->getUser()->getName() );
49 break;
50 }
51 return $msg;
52 }
53
54 /**
55 * Include the number of attempts in the message if needed
56 *
57 * @return Message
58 */
59 public function getHeaderMessage() {
60 switch ( $this->event->getType() ) {
61 // Known IP? Don't bundle because we issue notifications after every 5 attempts anyway
62 case 'login-fail-known':
63 $msg = $this->msg( 'notification-known-header-login-fail' );
64 $msg->numParams( $this->event->getExtraParam( 'count', 0 ) );
65 break;
66 // New IP?
67 case 'login-fail-new':
68 // If it's a bundle, pass it the bundle count as param
69 if ( $this->isBundled() ) {
70 $msg = $this->msg( 'notification-new-bundled-header-login-fail' );
71 $totalAttempts = array_reduce(
72 $this->getBundledEvents(),
73 static function ( $sum, Event $event ) {
74 return $sum + $event->getExtraParam( 'count', 0 );
75 },
76 0
77 );
78 $msg->numParams( $totalAttempts );
79 } else {
80 // If the bundle is read or user goes to Special:Notifications, show
81 // one notification per attempt (aligned with how unbundled bundles work)
82 $msg = $this->msg( 'notification-new-unbundled-header-login-fail' );
83 $msg->numParams( $this->event->getExtraParam( 'count', 0 ) );
84 }
85 break;
86 default:
87 $msg = $this->msg( 'notification-header-login-success', $this->getUser()->getName() );
88 }
89 return $msg;
90 }
91
92 /**
93 * Get links to be used in the notification
94 *
95 * @return array Link to Special:ChangePassword
96 */
97 public function getSecondaryLinks() {
98 $changePasswordLink = [
99 'url' => SpecialPage::getTitleFor( 'ChangePassword' )->getFullURL(),
100 'label' => $this->msg( 'changepassword' )->text(),
101 'description' => '',
102 'icon' => 'lock',
103 'prioritized' => true,
104 ];
105
106 return [ $changePasswordLink ];
107 }
108
109 }