1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
<?php
/*
* Copyright (c) 2016 The MITRE Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
namespace MediaWiki\Extension\CommentStreams;
use ApiBase;
use ApiMessage;
use EchoEvent;
use ExtensionRegistry;
use ManualLogEntry;
use WikiPage;
class ApiCSPostComment extends ApiBase {
/**
* @param ApiMain $main main module
* @param string $action name of this module
*/
public function __construct( $main, $action ) {
parent::__construct( $main, $action );
}
/**
* execute the API request
*/
public function execute() {
if ( !in_array( 'cs-comment', $this->getUser()->getRights() ) ||
$this->getUser()->isBlocked() ) {
$this->dieCustomUsageMessage(
'commentstreams-api-error-post-permissions' );
}
$associatedid = $this->getMain()->getVal( 'associatedid' );
$parentid = $this->getMain()->getVal( 'parentid' );
$comment_title = $this->getMain()->getVal( 'commenttitle' );
$wikitext = $this->getMain()->getVal( 'wikitext' );
if ( $parentid === null && $comment_title === null ) {
$this->dieCustomUsageMessage(
'commentstreams-api-error-missingcommenttitle' );
}
if ( $parentid !== null && $comment_title !== null ) {
$this->dieCustomUsageMessage(
'commentstreams-api-error-post-parentandtitle' );
}
if ( $parentid !== null ) {
$parent_page = WikiPage::newFromId( $parentid );
if ( $parent_page === null || !$parent_page->getTitle()->exists() ) {
$this->dieCustomUsageMessage(
'commentstreams-api-error-post-parentpagedoesnotexist' );
}
$parent_comment = Comment::newFromWikiPage( $parent_page );
if ( $parent_comment->getAssociatedId() !== (int)$associatedid ) {
$this->dieCustomUsageMessage(
'commentstreams-api-error-post-associatedpageidmismatch' );
}
}
$associated_page = WikiPage::newFromId( $associatedid );
if ( $associated_page === null ||
!$associated_page->getTitle()->exists() ) {
$this->dieCustomUsageMessage(
'commentstreams-api-error-post-associatedpagedoesnotexist' );
}
$comment = Comment::newFromValues( $associatedid, $parentid,
$comment_title, $wikitext, $this->getUser() );
if ( !$comment ) {
$this->dieCustomUsageMessage( 'commentstreams-api-error-post' );
}
$title = $comment->getWikiPage()->getTitle();
if ( $comment->getParentId() === null ) {
$this->logAction( 'comment-create', $title );
} else {
$this->logAction( 'reply-create', $title );
}
$json = $comment->getJSON();
if ( ExtensionRegistry::getInstance()->isLoaded( 'Echo' ) &&
$comment->getParentId() === null
) {
$json['watching'] = 1;
}
$this->getResult()->addValue( null, $this->getModuleName(), $json );
$this->sendNotifications( $comment, $associated_page );
}
/**
* @return array allowed parameters
*/
public function getAllowedParams() {
return [
'commenttitle' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => false
],
'wikitext' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true
],
'associatedid' => [
ApiBase::PARAM_TYPE => 'integer',
ApiBase::PARAM_REQUIRED => true
],
'parentid' => [
ApiBase::PARAM_TYPE => 'integer',
ApiBase::PARAM_REQUIRED => false
]
];
}
/**
* @return string indicates that this API module requires a CSRF token
*/
public function needstoken() {
return 'csrf';
}
/**
* Send Echo notifications if Echo is installed.
*
* @param Comment $comment the comment to send notifications for
* @param WikiPage $associated_page the associated page for the comment
* @return not used
*/
private function sendNotifications( $comment, $associated_page ) {
if ( !ExtensionRegistry::getInstance()->isLoaded( 'Echo' ) ) {
return;
}
$parent_id = $comment->getParentId();
if ( $parent_id === null ) {
$comment_title = $comment->getCommentTitle();
} else {
$parent_page = WikiPage::newFromId( $parent_id );
if ( $parent_page === null ) {
return;
}
$parent_comment = Comment::newFromWikiPage( $parent_page );
if ( $parent_comment === null ) {
return;
} else {
$comment_title = $parent_comment->getCommentTitle();
}
}
$associated_page_display_title =
$associated_page->getTitle()->getPrefixedText();
if ( class_exists( 'PageProps' ) ) {
$associated_title = $associated_page->getTitle();
$values = \PageProps::getInstance()->getProperties( $associated_title,
'displaytitle' );
if ( array_key_exists( $associated_title->getArticleID(), $values ) ) {
$associated_page_display_title =
$values[$associated_title->getArticleID()];
}
}
$extra = [
'comment_id' => $comment->getId(),
'parent_id' => $comment->getParentId(),
'comment_author_username' => $comment->getUsername(),
'comment_author_display_name' => $comment->getUserDisplayNameUnlinked(),
'comment_title' => $comment_title,
'associated_page_display_title' => $associated_page_display_title,
'comment_wikitext' => $comment->getWikitext()
];
if ( $parent_id !== null ) {
EchoEvent::create( [
'type' => 'commentstreams-reply-on-watched-page',
'title' => $associated_page->getTitle(),
'extra' => $extra,
'agent' => $this->getUser()
] );
EchoEvent::create( [
'type' => 'commentstreams-reply-to-watched-comment',
'title' => $associated_page->getTitle(),
'extra' => $extra,
'agent' => $this->getUser()
] );
} else {
EchoEvent::create( [
'type' => 'commentstreams-comment-on-watched-page',
'title' => $associated_page->getTitle(),
'extra' => $extra,
'agent' => $this->getUser()
] );
}
}
/**
* log action
* @param string $action the name of the action to be logged
* @param string|null $title the title of the page for the comment that the
* action was performed upon
*/
protected function logAction( $action, $title ) {
$logEntry = new ManualLogEntry( 'commentstreams', $action );
$logEntry->setPerformer( $this->getUser() );
$logEntry->setTarget( $title );
$logid = $logEntry->insert();
}
/**
* die with a custom usage message
* @param string $message_name the name of the custom message
*/
private function dieCustomUsageMessage( $message_name ) {
$error_message = wfMessage( $message_name );
$this->dieUsageMsg(
[
ApiMessage::create( $error_message )
]
);
}
}
|