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