Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 39 |
|
0.00% |
0 / 20 |
CRAP | |
0.00% |
0 / 1 |
NewsletterStore | |
0.00% |
0 / 39 |
|
0.00% |
0 / 20 |
756 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getDefaultInstance | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
addSubscription | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
removeSubscription | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addPublisher | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
removePublisher | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
addNewsletter | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
updateDescription | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
updateName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
updateMainPage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
deleteNewsletter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
restoreNewsletter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
rollBackNewsletterAddition | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getNewsletter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getNewsletterFromName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getPublishersFromID | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getNewsletterSubscribersCount | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSubscribersFromID | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
newsletterExistsForMainPage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addNewsletterIssue | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\Newsletter; |
4 | |
5 | use MediaWiki\Extension\Newsletter\Logging\NewsletterLogger; |
6 | use MediaWiki\MediaWikiServices; |
7 | use MediaWiki\Title\Title; |
8 | use MediaWiki\User\User; |
9 | use Wikimedia\Rdbms\IResultWrapper; |
10 | |
11 | /** |
12 | * @license GPL-2.0-or-later |
13 | * @author Addshore |
14 | */ |
15 | class NewsletterStore { |
16 | |
17 | /** |
18 | * @var NewsletterDb |
19 | */ |
20 | private $db; |
21 | |
22 | /** |
23 | * @var NewsletterLogger |
24 | */ |
25 | private $logger; |
26 | |
27 | /** |
28 | * @var self |
29 | */ |
30 | private static $instance; |
31 | |
32 | /** |
33 | * @param NewsletterDb $db |
34 | * @param NewsletterLogger $logger |
35 | */ |
36 | public function __construct( NewsletterDb $db, NewsletterLogger $logger ) { |
37 | $this->db = $db; |
38 | $this->logger = $logger; |
39 | } |
40 | |
41 | /** |
42 | * @return self |
43 | */ |
44 | public static function getDefaultInstance() { |
45 | if ( !self::$instance ) { |
46 | self::$instance = new self( |
47 | new NewsletterDb( MediaWikiServices::getInstance()->getDBLoadBalancer() ), |
48 | new NewsletterLogger() |
49 | ); |
50 | } |
51 | return self::$instance; |
52 | } |
53 | |
54 | /** |
55 | * @param Newsletter $newsletter |
56 | * @param array $userIds |
57 | */ |
58 | public function addSubscription( Newsletter $newsletter, array $userIds ): void { |
59 | $this->db->addSubscription( $newsletter, $userIds ); |
60 | } |
61 | |
62 | /** |
63 | * @param Newsletter $newsletter |
64 | * @param array $userIds |
65 | */ |
66 | public function removeSubscription( Newsletter $newsletter, array $userIds ): void { |
67 | $this->db->removeSubscription( $newsletter, $userIds ); |
68 | } |
69 | |
70 | /** |
71 | * @param Newsletter $newsletter |
72 | * @param array $userIds |
73 | */ |
74 | public function addPublisher( Newsletter $newsletter, array $userIds ): void { |
75 | $success = $this->db->addPublisher( $newsletter, $userIds ); |
76 | if ( $success ) { |
77 | foreach ( $userIds as $userId ) { |
78 | $this->logger->logPublisherAdded( $newsletter, User::newFromId( $userId ) ); |
79 | } |
80 | } |
81 | } |
82 | |
83 | /** |
84 | * @param Newsletter $newsletter |
85 | * @param array $userIds |
86 | */ |
87 | public function removePublisher( Newsletter $newsletter, array $userIds ): void { |
88 | $success = $this->db->removePublisher( $newsletter, $userIds ); |
89 | if ( $success ) { |
90 | foreach ( $userIds as $userId ) { |
91 | $this->logger->logPublisherRemoved( $newsletter, User::newFromId( $userId ) ); |
92 | } |
93 | } |
94 | } |
95 | |
96 | /** |
97 | * @param Newsletter $newsletter |
98 | * @return bool Success of the action |
99 | */ |
100 | public function addNewsletter( Newsletter $newsletter ): bool { |
101 | $id = $this->db->addNewsletter( $newsletter ); |
102 | if ( $id ) { |
103 | $newsletter->setId( $id ); |
104 | $this->logger->logNewsletterAdded( $newsletter ); |
105 | } |
106 | return (bool)$id; |
107 | } |
108 | |
109 | /** |
110 | * @param int $id |
111 | * @param string $description |
112 | * |
113 | * @return bool success of the action |
114 | */ |
115 | public function updateDescription( $id, $description ) { |
116 | return $this->db->updateDescription( $id, $description ); |
117 | } |
118 | |
119 | /** |
120 | * @param int $id |
121 | * @param string $name |
122 | * |
123 | * @return bool success of the action |
124 | */ |
125 | public function updateName( $id, $name ) { |
126 | return $this->db->updateName( $id, $name ); |
127 | } |
128 | |
129 | /** |
130 | * @param int $id |
131 | * @param int $pageId |
132 | * @return bool Success of the action |
133 | */ |
134 | public function updateMainPage( int $id, int $pageId ): bool { |
135 | return $this->db->updateMainPage( $id, $pageId ); |
136 | } |
137 | |
138 | /** |
139 | * @param Newsletter $newsletter |
140 | */ |
141 | public function deleteNewsletter( Newsletter $newsletter ): void { |
142 | $this->db->deleteNewsletter( $newsletter ); |
143 | } |
144 | |
145 | /** |
146 | * Restore a newsletter from the delete logs |
147 | * |
148 | * @param string $newsletterName |
149 | */ |
150 | public function restoreNewsletter( string $newsletterName ): void { |
151 | $this->db->restoreNewsletter( $newsletterName ); |
152 | } |
153 | |
154 | /** |
155 | * Roll back a newsletter addition silently due to a failure in creating a |
156 | * content model for it |
157 | * |
158 | * @param Newsletter $newsletter |
159 | */ |
160 | public function rollBackNewsletterAddition( Newsletter $newsletter ): void { |
161 | $this->db->deleteNewsletter( $newsletter ); |
162 | } |
163 | |
164 | /** |
165 | * @param int $id |
166 | * |
167 | * @return Newsletter|null null if no newsletter exists with the provided id |
168 | */ |
169 | public function getNewsletter( $id ) { |
170 | return $this->db->getNewsletter( $id ); |
171 | } |
172 | |
173 | /** |
174 | * @param string $name |
175 | * @param bool $active |
176 | * @return Newsletter|null |
177 | */ |
178 | public function getNewsletterFromName( string $name, bool $active = true ) { |
179 | return $this->db->getNewsletterFromName( $name, $active ); |
180 | } |
181 | |
182 | /** |
183 | * @param int $id |
184 | * @return int[] |
185 | */ |
186 | public function getPublishersFromID( int $id ): array { |
187 | return $this->db->getPublishersFromID( $id ); |
188 | } |
189 | |
190 | /** |
191 | * @param int $id |
192 | * @return int |
193 | */ |
194 | public function getNewsletterSubscribersCount( int $id ): int { |
195 | return $this->db->getNewsletterSubscribersCount( $id ); |
196 | } |
197 | |
198 | /** |
199 | * @param int $id |
200 | * @return int[] |
201 | */ |
202 | public function getSubscribersFromID( int $id ): array { |
203 | return $this->db->getSubscribersFromID( $id ); |
204 | } |
205 | |
206 | /** |
207 | * Fetch all newsletter Main Pages |
208 | * |
209 | * @param int $mainPageId |
210 | * @return IResultWrapper |
211 | */ |
212 | public function newsletterExistsForMainPage( int $mainPageId ) { |
213 | return $this->db->newsletterExistsForMainPage( $mainPageId ); |
214 | } |
215 | |
216 | /** |
217 | * @param Newsletter $newsletter |
218 | * @param Title $title |
219 | * @param User $publisher |
220 | * @param string $summary |
221 | * |
222 | * @return bool |
223 | */ |
224 | public function addNewsletterIssue( |
225 | Newsletter $newsletter, |
226 | Title $title, |
227 | User $publisher, |
228 | $summary |
229 | ) { |
230 | $success = $this->db->addNewsletterIssue( $newsletter, $title, $publisher ); |
231 | if ( $success ) { |
232 | $this->logger->logNewIssue( $publisher, $newsletter, $title, $success, $summary ); |
233 | } |
234 | return (bool)$success; |
235 | } |
236 | |
237 | } |