Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
NotificationMessageBuilder
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 10
380
0.00% covered (danger)
0.00%
0 / 1
 getMessageTitle
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 getPriorityClause
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 getDeadlineClause
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getNotificationMessage
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getTranslationURLs
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
30
 getSignupURL
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getUserName
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getNotificationSubject
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getPriorityMessage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTranslationURL
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/*
3* @file
4* @license GPL-2.0-or-later
5*/
6
7namespace MediaWiki\Extension\TranslationNotifications\Utilities;
8
9use Language;
10use MediaWiki\Extension\Translate\PageTranslation\TranslatablePage;
11use MediaWiki\Html\Html;
12use MediaWiki\SpecialPage\SpecialPage;
13use MediaWiki\Title\Title;
14use MediaWiki\User\User;
15use Message;
16
17/**
18 * A class that helps builds the notification message to be sent to users
19 *
20 * @since 2019.10
21 */
22class NotificationMessageBuilder {
23    /**
24     * Returns the message title to be embedded into the message
25     * @param Title $title
26     * @param string $destination
27     * @param string[] $localInterwikis
28     * @return Title|string
29     */
30    public static function getMessageTitle( Title $title, $destination, $localInterwikis ) {
31        $titleForMessage = $title;
32
33        if ( $destination === 'talkpageInOtherWiki' && count( $localInterwikis ) ) {
34            $titleForMessage = ":$localInterwikis[0]:$titleForMessage|$titleForMessage";
35        }
36
37        return $titleForMessage;
38    }
39
40    /**
41     * Returns the message priority clause to be embedded into the message
42     * @param Language $userFirstLanguage Language object set as first preference
43     * @param string $priority
44     * @return string
45     */
46    public static function getPriorityClause( Language $userFirstLanguage, $priority ) {
47        if ( $priority === 'unset' ) {
48            return '';
49        }
50
51        return wfMessage(
52            'translationnotifications-email-priority',
53            self::getPriorityMessage(
54                $priority
55            )->inLanguage( $userFirstLanguage )->text()
56        )->inLanguage( $userFirstLanguage )->text();
57    }
58
59    /**
60     * Return the deadline clause
61     * @param Language $userFirstLanguage
62     * @param string $deadlineDate
63     * @return string
64     */
65    public static function getDeadlineClause( Language $userFirstLanguage, $deadlineDate ) {
66        if ( $deadlineDate === '' ) {
67            return '';
68        }
69
70        return wfMessage(
71            'translationnotifications-email-deadline',
72            $deadlineDate
73        )->inLanguage( $userFirstLanguage )->text();
74    }
75
76    /**
77     * Wrap the text in a div based on the language
78     * @param Language $contLang
79     * @param string $notificationContent
80     * @return string
81     */
82    public static function getNotificationMessage( Language $contLang, $notificationContent ) {
83        // Assume that the message is in the content language
84        // of the originating wiki.
85        $dir = $contLang->getDir();
86        // Possible classes:
87        // mw-content-ltr, mw-content-rtl
88        return Html::element( 'div',
89            [
90                'lang' => $contLang->getCode(),
91                'class' => "mw-content-$dir"
92            ],
93            $notificationContent
94        );
95    }
96
97    /**
98     * Returns a list of URLs for page translation in every language.
99     * @param Title $translatableTitle Title of the page being translated
100     * @param string[] $languages A list of language codes and language names.
101     * @param string $contactMethod The contact method - 'talkpage' or 'email'.
102     * @param string|Language $inLanguage Language code or Language object.
103     * @param string $urlProtocol
104     * @return string
105     */
106    public static function getTranslationURLs(
107        Title $translatableTitle, $languages, $contactMethod, $inLanguage, $urlProtocol
108    ) {
109        $translationURLsItems = [];
110
111        foreach ( $languages as $code => $langName ) {
112            $translationURL = self::getTranslationURL( $translatableTitle, $code, $urlProtocol );
113
114            $translationMsg = wfMessage(
115                'translationnotifications-notification-url-listitem',
116                $langName
117            )->inLanguage( $inLanguage )->text();
118
119            switch ( $contactMethod ) {
120                case 'talkpage':
121                    $translationURLsItems[] = "* [$translationURL $translationMsg]";
122                    break;
123                case 'email':
124                    $translationURLsItems[] = "$translationMsg: <$translationURL>";
125                    break;
126                default:
127                    return '';
128            }
129        }
130
131        return implode( "\n", $translationURLsItems );
132    }
133
134    /**
135     * Returns URL to signup and change notification preferences
136     * @param string $urlProtocol
137     * @return string Signup URL
138     */
139    public static function getSignupURL( $urlProtocol ) {
140        return SpecialPage::getTitleFor( 'TranslatorSignup' )->getFullURL(
141            '',
142            false,
143            $urlProtocol
144        );
145    }
146
147    /**
148     * Returns the user name to be used in the notification
149     * @param User $user
150     * @return string
151     */
152    public static function getUserName( User $user ) {
153        $name = $user->getRealName();
154        if ( $name === '' ) {
155            $name = $user->getName();
156        }
157
158        return $name;
159    }
160
161    /**
162     * Returns the subject of the notification
163     * @param Title $title
164     * @param string|Language $userFirstLanguage
165     * @return string
166     */
167    public static function getNotificationSubject( Title $title, $userFirstLanguage ) {
168        return wfMessage(
169            'translationnotifications-email-subject',
170            $title->getText()
171        )->inLanguage( $userFirstLanguage )->text();
172    }
173
174    /**
175     * @param string $priority
176     * @return Message
177     */
178    public static function getPriorityMessage( $priority ) {
179        // possible messages here:
180        // 'translationnotifications-priority-high'
181        // 'translationnotifications-priority-medium'
182        // 'translationnotifications-priority-low'
183        // 'translationnotifications-priority-unset'
184        return wfMessage( "translationnotifications-priority-$priority" );
185    }
186
187    /**
188     * @param Title $translatablePageTitle
189     * @param string $languageCode
190     * @param string $urlProtocol
191     * @return string Translation URL
192     */
193    private static function getTranslationURL(
194        Title $translatablePageTitle, $languageCode, $urlProtocol
195    ) {
196        $page = TranslatablePage::newFromTitle( $translatablePageTitle );
197        return SpecialPage::getTitleFor( 'Translate' )->getFullURL(
198            [
199                'group' => $page->getMessageGroupId(),
200                'language' => $languageCode,
201                'action' => 'page'
202            ],
203            false,
204            $urlProtocol
205        );
206    }
207}