-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpecialChangeRating.php
289 lines (248 loc) · 8.59 KB
/
SpecialChangeRating.php
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
use MediaWiki\MediaWikiServices;
class SpecialChangeRating extends SpecialPage {
public function __construct() {
parent::__construct( 'ChangeRating', 'change-rating' );
}
/** @inheritDoc */
public function doesWrites() {
return true;
}
/**
* Render the special page.
*
* @param string|null $page Name of the page we're going to rate; if not specified,
* the user will be presented with a form that allows them to choose a page.
*/
public function execute( $page ) {
global $wgARENamespaces;
$this->checkPermissions();
$this->checkReadOnly();
$this->setHeaders();
$out = $this->getOutput();
$request = $this->getRequest();
$user = $this->getUser();
if ( !$page ) {
$page = urldecode( $request->getVal( 'pagetitle' ) );
}
$title = Title::newFromText( $page );
if ( $title === null ) {
$this->displayPageSearchForm();
} elseif ( !$title->exists() ) {
$out->addWikiMsg( 'changerating-no-such-page', $page );
} else {
$namespaces = $wgARENamespaces ?? MediaWikiServices::getInstance()
->getNamespaceInfo()->getContentNamespaces();
if ( !in_array( $title->getNamespace(), $namespaces ) ) {
$out->addWikiMsg( 'are-disallowed' );
return;
}
$out->setSubtitle(
$this->msg( 'changerating-back', $title->getFullText() )->parse()
);
$ratingto = $request->getVal( 'ratingTo' );
$output = '';
if (
$request->wasPosted() &&
$user->matchEditToken( $request->getVal( 'wpRatingToken' ) ) &&
$ratingto !== null
) {
$ratingto = substr( $ratingto, 0, 2 );
$isValidRatingCodename = self::validateRatingCodename( $ratingto );
if ( !$isValidRatingCodename ) {
$out->addHTML( Html::errorBox( $this->msg( 'changerating-error-invalid-rating' )->escaped() ) );
// @todo FIXME: I don't *love* this _but_ it gets the job done
// as tampered forms are such an edge case.
// From the user's POV, it's mildly annoying to have to click to a link
// instead of being shown the form here because they literally _are_ on
// this page already, but oh well.
$out->addReturnTo( $this->getPageTitle( $title->getPrefixedText() ) );
return;
}
// @todo FIXME: this is now _also_ done inside insertOrUpdateRating() :-(
// But we need the value here, too...
$resOldRating = self::getCurrentRatingForPage( $title );
if ( $resOldRating === $ratingto ) {
// Pointless. Raise an error.
// phpcs:disable Generic.Files.LineLength
$out->addHTML( Html::errorBox( $this->msg( 'changerating-error-no-changes-requested' )->escaped() ) );
} else {
$changedRows = self::insertOrUpdateRating( $ratingto, $title );
if ( $changedRows > 0 ) {
$out->addHTML( Html::successBox( $this->msg( 'changerating-success' )->escaped() ) );
} else {
$out->addHTML( Html::errorBox( $this->msg( 'error' )->escaped() ) );
}
$rating = new Rating( $ratingto );
// We're not guaranteed to have an old rating
if ( !$resOldRating ) {
$seed = RatingData::getDefaultRating()->getCodename();
} else {
$seed = $resOldRating;
}
$oldrating = new Rating( $seed );
$reason = $request->getVal( 'reason' );
self::log( $user, $title, $rating, $oldrating, $reason );
}
} elseif ( $request->wasPosted() && !$user->matchEditToken( $request->getVal( 'wpRatingToken' ) ) ) {
// Cross-site request forgery (CSRF) attempt or something, display an error
$output .= Html::errorBox( $this->msg( 'sessionfailure' )->parse() );
}
$output .= $this->msg( 'changerating-intro-text', $title->getFullText() )->parseAsBlock()
. '<form name="change-rating" action="" method="post">';
$currentRating = self::getCurrentRatingForPage( $title );
$ratings = RatingData::getAllRatings();
foreach ( $ratings as $rating ) {
if ( $rating->getCodename() == $currentRating ) {
$attribs = [ 'checked' => 'checked' ];
} else {
$attribs = [];
}
$output .= Html::input( 'ratingTo', $rating->getCodename(), 'radio', $attribs );
$output .= $this->msg( 'word-separator' )->parse();
$output .= $rating->getImage();
$output .= $rating->getAboutLink();
$output .= '<br />';
}
$output .= $this->msg( 'changerating-reason' )->escaped() .
' <input type="text" name="reason" size="50" /><br />' .
Html::hidden( 'wpRatingToken', $user->getEditToken() ) .
Html::input( 'wpSubmit', $this->msg( 'changerating-submit' )->plain(), 'submit' ) .
'</form>';
$output .= $this->msg( 'changerating-log-text', $page )->parseAsBlock();
$out->addHTML( $output );
LogEventsList::showLogExtract( $out, 'ratings', $title );
}
}
/**
* Given a Title, returns that page's current rating (if any).
*
* @param Title $title
* @return string|bool Current rating for the given page title, if any, or bool false on failure
*/
public static function getCurrentRatingForPage( $title ) {
$dbr = ArticleRatingsHooks::getDBHandle( 'read' );
$rating = $dbr->selectField(
'ratings',
'ratings_rating',
[
'ratings_title' => $title->getDBkey(),
'ratings_namespace' => $title->getNamespace()
],
__METHOD__
);
if ( !$rating ) {
return false;
} else {
return $rating;
}
}
/**
* Validate the two-character codename, in case if someone tried to tamper with the form.
*
* @param string $ratingTo Target rating codename
* @return bool True if valid, false if not
*/
public static function validateRatingCodename( $ratingTo ) {
// Make sure it absolutely, definitely, positively is only two characters long
$ratingTo = substr( $ratingTo, 0, 2 );
// Pessimism FTW
$validRatingCodename = false;
$ratings = RatingData::getAllRatings();
foreach ( $ratings as $rating ) {
if ( $rating->getCodename() == $ratingTo ) {
$validRatingCodename = true;
}
}
return $validRatingCodename;
}
/**
* INSERT a rating for a page if it has none yet, or if it has, UPDATE it to $ratingTo.
*
* @param string $ratingTo New rating (two-character codename)
* @param Title $title The page being rated
* @return int Number of rows affected by the DB query, if any
*/
public static function insertOrUpdateRating( $ratingTo, $title ) {
$dbw = ArticleRatingsHooks::getDBHandle( 'write' );
$resOldRating = self::getCurrentRatingForPage( $title );
// If there is an entry, update it.
// If there isn't, we need to *create* it before we can even
// think of updating it :^)
// (Whaddya know, trying to UPDATE something that doesn't exist
// seems to fail silently, nothing tells you that "you gotta INSERT
// first", the code just fails and sends you on a wild goose chase
// for half an hour or so...)
if ( $resOldRating ) {
$dbw->update(
'ratings',
[ 'ratings_rating' => $ratingTo ],
[
'ratings_title' => $title->getDBkey(),
'ratings_namespace' => $title->getNamespace()
],
__METHOD__
);
} else {
$dbw->insert(
'ratings',
[
'ratings_rating' => $ratingTo,
'ratings_title' => $title->getDBkey(),
'ratings_namespace' => $title->getNamespace()
],
__METHOD__
);
}
return $dbw->affectedRows();
}
/**
* Log a rating change to Special:Log/ratings.
*
* @param User $user The user who performed the action
* @param Title $title Rated page
* @param Rating $newRating New Rating object for the given page (Title)
* @param Rating $oldRating Old Rating object for the given page (Title)
* @param string|null $reason User-supplied additional rating comment, if any
*/
public static function log( $user, $title, $newRating, $oldRating, $reason = null ) {
$logEntry = new ManualLogEntry( 'ratings', 'change' );
$logEntry->setPerformer( $user );
$logEntry->setTarget( $title );
$logEntry->setParameters( [
'4::newrating' => $newRating->getName(),
'5::oldrating' => $oldRating->getName()
] );
if ( $reason !== null ) {
$logEntry->setComment( $reason );
}
$logId = $logEntry->insert();
$logEntry->publish( $logId );
}
/**
* Display a form for searching a page, with autocompletion and all that fancy stuff!
*
* @see https://phabricator.wikimedia.org/T164233
*/
private function displayPageSearchForm() {
$fields = [
'target' => [
'type' => 'title',
'creatable' => true,
'name' => 'pagetitle',
'default' => '',
'label-message' => 'changecontentmodel-title-label',
]
];
$htmlForm = HTMLForm::factory( 'ooui', $fields, $this->getContext() );
$htmlForm
->addHiddenField( 'title', $this->getPageTitle() )
->setAction( '' )
->setMethod( 'get' )
->setName( 'are-page-search' )
->setSubmitTextMsg( 'search' )
->setWrapperLegend( '' )
->prepareForm()
->displayForm( false );
}
}