Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
26.92% |
14 / 52 |
|
16.67% |
1 / 6 |
CRAP | |
0.00% |
0 / 1 |
NotifierHandler | |
26.92% |
14 / 52 |
|
16.67% |
1 / 6 |
115.90 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
run | |
66.67% |
6 / 9 |
|
0.00% |
0 / 1 |
3.33 | |||
preconditionsCheck | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
hasPageChanged | |
37.50% |
3 / 8 |
|
0.00% |
0 / 1 |
7.91 | |||
createNotificationEvent | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
getBodyValidator | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | * @since 0.0.1 |
20 | */ |
21 | |
22 | namespace MediaWiki\Extension\Adiutor\Rest\Handler; |
23 | |
24 | use ExtensionRegistry; |
25 | use HttpError; |
26 | use MediaWiki\Extension\Notifications\Model\Event; |
27 | use MediaWiki\Rest\Response; |
28 | use MediaWiki\Rest\SimpleHandler; |
29 | use MediaWiki\Rest\Validator\JsonBodyValidator; |
30 | use MediaWiki\Rest\Validator\UnsupportedContentTypeBodyValidator; |
31 | use MediaWiki\Revision\RevisionLookup; |
32 | use MediaWiki\Title\Title; |
33 | use MediaWiki\User\UserFactory; |
34 | use RequestContext; |
35 | use User; |
36 | use Wikimedia\ParamValidator\ParamValidator; |
37 | |
38 | /** |
39 | * This is a rest handler for echo notifications. |
40 | */ |
41 | class NotifierHandler extends SimpleHandler { |
42 | |
43 | private UserFactory $userFactory; |
44 | private RevisionLookup $revisionLookup; |
45 | |
46 | /** |
47 | * @param UserFactory $userFactory |
48 | * @param RevisionLookup $revisionLookup |
49 | */ |
50 | public function __construct( UserFactory $userFactory, RevisionLookup $revisionLookup ) { |
51 | $this->userFactory = $userFactory; |
52 | $this->revisionLookup = $revisionLookup; |
53 | } |
54 | |
55 | /** |
56 | * @throws HttpError |
57 | */ |
58 | public function run(): Response { |
59 | $jsonData = $this->getValidatedBody(); |
60 | |
61 | if ( !is_array( $jsonData ) || !isset( $jsonData['content'] ) ) { |
62 | // Handle the error appropriately, for example: |
63 | throw new HttpError( 400, 'Invalid or missing content in the request body' ); |
64 | } |
65 | |
66 | $content = $jsonData['content']; |
67 | |
68 | $agent = RequestContext::getMain()->getUser(); |
69 | $author = $this->userFactory->newFromName( $content['author'] ); |
70 | |
71 | $this->preconditionsCheck( $agent, $content['title'] ); |
72 | |
73 | $event = $this->createNotificationEvent( $content, $author, $agent ); |
74 | |
75 | return $this->getResponseFactory()->createJson( [ 'result' => 'success', 'event' => $event ] ); |
76 | } |
77 | |
78 | /** |
79 | * Check if any preconditions are violated before sending a notification |
80 | * |
81 | * @param User $agent The author user object. |
82 | * @param string $pageTitle Title of the page that the notification is about. |
83 | * @throws HttpError |
84 | */ |
85 | private function preconditionsCheck( User $agent, string $pageTitle ): void { |
86 | if ( $agent->getBlock() ) { |
87 | throw new HttpError( 403, 'Author is blocked' ); |
88 | } |
89 | |
90 | if ( !$this->hasPageChanged( $pageTitle ) ) { |
91 | throw new HttpError( 400, 'No changes made to the page' ); |
92 | } |
93 | } |
94 | |
95 | /** |
96 | * Check whether the page has been changed since the user calling the API has last edited it. |
97 | * |
98 | * @param string $pageTitle Title of the page to check. |
99 | * @return bool True if the page has changed; otherwise, false. |
100 | */ |
101 | private function hasPageChanged( string $pageTitle ): bool { |
102 | $title = Title::newFromText( $pageTitle ); |
103 | |
104 | if ( !$title->exists() ) { |
105 | // If the title does not exist, no changes can have been made |
106 | return false; |
107 | } |
108 | |
109 | // Get the latest revision of the page |
110 | $latestRevision = $title->getLatestRevID(); |
111 | |
112 | if ( !$latestRevision ) { |
113 | // If there's no revision ID, for whatever reason, we can't tell if the page has changed |
114 | return false; |
115 | } |
116 | |
117 | // Check if the agent is the one who made the latest change |
118 | $latestRevisionUser = $this->revisionLookup->getRevisionById( $latestRevision )->getUser(); |
119 | |
120 | // Compare the user IDs to determine if the page has changed recently |
121 | return $latestRevisionUser && $latestRevisionUser->getId() === RequestContext::getMain()->getUser()->getId(); |
122 | } |
123 | |
124 | /** |
125 | * Create a notification event if Echo extension is loaded |
126 | * |
127 | * @param array $content The content array containing 'title' and 'reason'. |
128 | * @param User $author The author user object. |
129 | * @param User $agent The user object representing the agent. |
130 | * |
131 | * @return Event The notification event. |
132 | * @throws HttpError |
133 | */ |
134 | private function createNotificationEvent( array $content, User $author, User $agent ): Event { |
135 | if ( ExtensionRegistry::getInstance()->isLoaded( 'Echo' ) ) { |
136 | $event = Event::create( [ |
137 | 'type' => 'adiutor-csd-notification', |
138 | 'title' => Title::newFromText( $content['title'] ), |
139 | 'extra' => [ |
140 | 'author' => $author, |
141 | 'reason' => $content['reason'], |
142 | 'title' => $content['title'], |
143 | ], |
144 | 'agent' => $agent |
145 | ] ); |
146 | |
147 | if ( $event === false ) { |
148 | throw new HttpError( 500, 'Failed to create notification event' ); |
149 | } |
150 | |
151 | return $event; |
152 | } else { |
153 | throw new HttpError( 404, 'Echo extension is not available' ); |
154 | } |
155 | } |
156 | |
157 | /** |
158 | * Returns the appropriate body validator based on the content type. |
159 | * |
160 | * @param string $contentType The content type of the request body. |
161 | * |
162 | * @return JsonBodyValidator|UnsupportedContentTypeBodyValidator |
163 | */ |
164 | public function getBodyValidator( $contentType ) { |
165 | if ( $contentType === 'application/json' ) { |
166 | return new JsonBodyValidator( |
167 | [ |
168 | 'title' => [ |
169 | ParamValidator::PARAM_TYPE => 'string', |
170 | ParamValidator::PARAM_REQUIRED => true, |
171 | ], |
172 | 'content' => [ |
173 | ParamValidator::PARAM_TYPE => 'string', |
174 | ParamValidator::PARAM_REQUIRED => true, |
175 | ], |
176 | ] |
177 | ); |
178 | } else { |
179 | return new UnsupportedContentTypeBodyValidator( $contentType ); |
180 | } |
181 | } |
182 | } |