Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Notification
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 5
42
0.00% covered (danger)
0.00%
0 / 1
 firstTranslation
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 tenthTranslation
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 hundredthTranslation
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 suggestionsAvailable
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 draftNotification
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace ContentTranslation;
4
5use ContentTranslation\Exception\InvalidNotificationTitleException;
6use MediaWiki\Extension\Notifications\Model\Event;
7use MediaWiki\Title\Title;
8use MediaWiki\User\User;
9
10class Notification {
11
12    /**
13     * Notify the user on the first published translation.
14     * @param User $recipient
15     */
16    public static function firstTranslation( User $recipient ) {
17        Event::create( [
18            'type' => 'cx-first-translation',
19            'extra' => [
20                'recipient' => $recipient->getId(),
21            ]
22        ] );
23    }
24
25    /**
26     * Notify the user on the 10th published translation.
27     * @param User $recipient
28     */
29    public static function tenthTranslation( User $recipient ) {
30        Event::create( [
31            'type' => 'cx-tenth-translation',
32            'extra' => [
33                'recipient' => $recipient->getId(),
34            ]
35        ] );
36    }
37
38    /**
39     * Notify the user on the 100th published translation.
40     * @param User $recipient
41     */
42    public static function hundredthTranslation( User $recipient ) {
43        Event::create( [
44            'type' => 'cx-hundredth-translation',
45            'extra' => [
46                'recipient' => $recipient->getId(),
47            ]
48        ] );
49    }
50
51    /**
52     * Notify the user about the availability of personalized suggestions.
53     * @param User $recipient
54     * @param string $lastTranslationTitle
55     */
56    public static function suggestionsAvailable( User $recipient, $lastTranslationTitle ) {
57        Event::create( [
58            'type' => 'cx-suggestions-available',
59            'extra' => [
60                'recipient' => $recipient->getId(),
61                'lastTranslationTitle' => $lastTranslationTitle
62            ]
63        ] );
64    }
65
66    /**
67     * Notify user about the status of his/her old unpublished draft,
68     * depending on notification type:
69     * - That their draft is getting old and may be deleted in the future
70     * - That their draft was too old and thus deleted
71     *
72     * @param string $type 'cx-deleted-draft' or 'cx-continue-translation'
73     * @param int $recipientId ID of user receiving this notification.
74     * @param string $title Title of unpublished draft page which is deleted.
75     * @param string $sourceLanguage
76     * @param string $targetLanguage
77     * @throws InvalidNotificationTitleException
78     */
79    public static function draftNotification(
80        $type, $recipientId, $title, $sourceLanguage, $targetLanguage
81    ) {
82        $titleObj = Title::newFromText( $title );
83        if ( !$titleObj ) {
84            // PurgeUnpublishedDrafts only catches InvalidNotificationTitleException
85            // See also https://phabricator.wikimedia.org/T264855
86            throw new InvalidNotificationTitleException( $title );
87        }
88
89        Event::create( [
90            'type' => $type,
91            'title' => $titleObj,
92            'extra' => [
93                'recipient' => $recipientId,
94                'source' => $sourceLanguage,
95                'target' => $targetLanguage
96            ]
97        ] );
98    }
99}