forked from djflux/WatchAnalytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hooks.php
323 lines (283 loc) · 10.1 KB
/
Hooks.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
use MediaWiki\MediaWikiServices;
class WatchAnalyticsHooks {
/**
* Handler for PersonalUrls hook. Replace the "watchlist" item on the user
* toolbar ('personal URLs') with a link to Special:PendingReviews.
* @see http://www.mediawiki.org/wiki/Manual:Hooks/PersonalUrls
*
* @param array &$personal_urls Array of URLs to append to.
* @return bool
*/
public static function onPersonalUrls( array &$personal_urls ) {
global $wgUser, $wgOut;
$user = $wgUser;
if ( !$user->isAllowed( 'pendingreviewslink' ) ) {
return true;
}
$wgOut->addModuleStyles( 'ext.watchanalytics.base' );
// NOTE: $wgOut->addModules() does not appear to work here, so
// the onBeforePageDisplay() method was created below.
// Get user's watch/review stats
$watchStats = $user->watchStats; // set in onBeforePageDisplay() hook
$numPending = $watchStats['num_pending'];
$maxPendingDays = $watchStats['max_pending_days'];
// Get user's pending approvals
// Check that Approved Revs is installed
$numPendingApprovals = 0;
if ( class_exists( 'ApprovedRevs' ) ) {
$numPendingApprovals = count( PendingApproval::getUserPendingApprovals( $user ) );
}
// Determine CSS class of Watchlist/PendingReviews link
$personal_urls['watchlist']['class'] = [ 'mw-watchanalytics-watchlist-badge' ];
if ( $numPending != 0 ) {
$personal_urls['watchlist']['class'] = [ 'mw-watchanalytics-watchlist-pending' ];
}
// Determine text of Watchlist/PendingReviews link
global $egPendingReviewsEmphasizeDays;
if ( $maxPendingDays > $egPendingReviewsEmphasizeDays ) {
$personal_urls['watchlist']['class'][] = 'mw-watchanalytics-watchlist-pending-old';
if ( $numPendingApprovals != 0 ) {
$text = wfMessage( 'watchanalytics-personal-url-approvals-old' )->params( $numPending, $maxPendingDays, $numPendingApprovals )->text();
} else {
$text = wfMessage( 'watchanalytics-personal-url-old' )->params( $numPending, $maxPendingDays )->text();
}
} else {
if ( $numPendingApprovals != 0 ) {
$text = wfMessage( 'watchanalytics-personal-url-approvals' )->params( $numPending, $numPendingApprovals )->text();
} else {
// when $sk (third arg) available, replace wfMessage with $sk->msg()
$text = wfMessage( 'watchanalytics-personal-url' )->params( $numPending )->text();
}
}
$personal_urls['watchlist']['text'] = $text;
// set "watchlist" link to Pending Reviews
$personal_urls['watchlist']['href'] = SpecialPage::getTitleFor( 'PendingReviews' )->getLocalURL();
return true;
}
/**
* Handler for BeforePageDisplay hook. This function supports several
* WatchAnalytics features:
*
* 1) Determine if user should see shaky pending reviews link
* 2) Insert page scores on applicable pages
* 3) REMOVED FOR MW 1.27: If a page review has occurred on this page view, display an unreview
* option and record that the review happened.
*
* Also supports parameter: Skin $skin.
* @see http://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
*
* @param OutputPage $out reference to OutputPage object
*
* @return bool true in all cases
*/
public static function onBeforePageDisplay( $out /*, $skin*/ ) {
$user = $out->getUser();
$title = $out->getTitle();
#
# 1) Is user's oldest pending review is old enough to require emphasis
#
$userWatch = new UserWatchesQuery();
$user->watchStats = $userWatch->getUserWatchStats( $user );
$user->watchStats['max_pending_days'] = ceil(
$user->watchStats['max_pending_minutes'] / ( 60 * 24 )
);
global $egPendingReviewsEmphasizeDays;
if ( $user->watchStats['max_pending_days'] > $egPendingReviewsEmphasizeDays ) {
$out->addModules( [ 'ext.watchanalytics.shakependingreviews' ] );
}
#
# 2) Insert page scores
#
if ( in_array( $title->getNamespace(), $GLOBALS['egWatchAnalyticsPageScoreNamespaces'] )
&& $user->isAllowed( 'viewpagescore' )
&& PageScore::pageScoreIsEnabled() ) {
$pageScore = new PageScore( $title );
$out->addScript( $pageScore->getPageScoreTemplate() );
$out->addModules( 'ext.watchanalytics.pagescores.scripts' );
$out->addModuleStyles( 'ext.watchanalytics.pagescores.styles' );
}
// REMOVED FOR MW 1.27
// #
// # 3) If user has reviewed page on this page load show "unreview" option
// #
// $reviewHandler = ReviewHandler::pageHasBeenReviewed();
// if ( $reviewHandler ) {
// // display "unreview" button
// $out->addScript( $reviewHandler->getTemplate() );
// $wgOut->addModules( [
// 'ext.watchanalytics.reviewhandler.scripts',
// 'ext.watchanalytics.reviewhandler.styles'
// ] );
// // record change in user/page stats
// WatchStateRecorder::recordReview( $user, $title );
// }
return true;
}
/**
* Handler for TitleMoveComplete hook. This function makes it so page-moves
* are handled correctly in the `watchlist` table. Prior to a MW 1.25 alpha
* release when a page is moved, the new entries into the `watchlist` table
* are given an notification timestamp of NULL; they should be identical to
* the notification timestamps of the original title so users are notified
* of changes prior to the move. Code taken from MediaWiki core head branch
* WatchedItem::doDuplicateEntries() method.
*
* @todo FIXME: make this work for <1.25 and 1.25+
* @todo document which commit fixes this issue specifically.
*
* @see http://www.mediawiki.org/wiki/Manual:Hooks/TitleMoveComplete
*
* @param Title &$originalTitle
* @param Title &$newTitle
* @param User &$user
* @param int $oldid
* @param int $newid
* @param FIXME string|null $reason
*
* @return bool true in all cases
*/
public static function onTitleMoveComplete( Title &$originalTitle, Title &$newTitle,
User &$user, $oldid, $newid, $reason = null ) {
#
# Record move in watch stats
#
WatchStateRecorder::recordPageChange( Article::newFromID( $oldid ) );
// if a redirect was created, record data for the "new" page (the redirect)
if ( $newid > 0 ) {
WatchStateRecorder::recordPageChange( Article::newFromID( $newid ) );
}
#
# BELOW IS THE pre-MW 1.25 FIX.
#
$oldNS = $originalTitle->getNamespace();
$newNS = $newTitle->getNamespace();
$oldDBkey = $originalTitle->getDBkey();
$newDBkey = $newTitle->getDBkey();
$dbw = wfGetDB( DB_MASTER );
$results = $dbw->select( 'watchlist',
[ 'wl_user', 'wl_notificationtimestamp' ],
[ 'wl_namespace' => $oldNS, 'wl_title' => $oldDBkey ],
__METHOD__
);
# Construct array to replace into the watchlist
$values = [];
foreach ( $results as $oldRow ) {
$values[] = [
'wl_user' => $oldRow->wl_user,
'wl_namespace' => $newNS,
'wl_title' => $newDBkey,
'wl_notificationtimestamp' => $oldRow->wl_notificationtimestamp,
];
}
if ( empty( $values ) ) {
// Nothing to do
return true;
}
# Perform replace
# Note that multi-row replace is very efficient for MySQL but may be inefficient for
# some other DBMSes, mostly due to poor simulation by us
$dbw->replace(
'watchlist',
[ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
$values,
__METHOD__
);
return true;
}
/**
* Register magic-word variable ID to hide page score from select pages.
*
* @see FIXME (include link to hook documentation)
*
* @param Array &$magicWordVariableIDs array of names of magic words
*
* @return bool
*/
public static function addMagicWordVariableIDs( &$magicWordVariableIDs ) {
$magicWordVariableIDs[] = 'MAG_NOPAGESCORE';
return true;
}
/**
* Set values in the page_props table based on the presence of the
* 'NOPAGESCORE' magic word in a page
*
* @see FIXME (include link to hook documentation)
*
* @param Parser &$parser reference to MediaWiki parser.
* @param string &$text FIXME html/wikitext? of output page before complete
*
* @return bool
*/
public static function handleMagicWords( &$parser, &$text ) {
if ( class_exists( MagicWordFactory::class ) ) {
// MW 1.32+
$factory = MediaWikiServices::getInstance()->getMagicWordFactory();
$magicWord = $factory->get( 'MAG_NOPAGESCORE' );
} else {
$magicWord = MagicWord::get( 'MAG_NOPAGESCORE' );
}
if ( $magicWord->matchAndRemove( $text ) ) {
PageScore::noPageScore();
}
return true;
}
/**
* Prior to clearing notification timestamp determines if user is watching page,
* and if so determines what their review status is. Records review and adds
* "defer" banner if required.
*
* @see FIXME (include link to hook documentation)
*
* @param WikiPage $wikiPage
* @param User $user
*
* @return bool
*/
public static function onPageViewUpdates( WikiPage $wikiPage, User $user ) {
global $wgRequest;
$title = $wikiPage->getTitle();
$isDiff = $wgRequest->getText( 'oldid' );
$reviewHandler = ReviewHandler::setup( $user, $title, $isDiff );
if ( $reviewHandler::pageIsBeingReviewed() ) {
global $wgOut;
// display "unreview" button
$wgOut->addScript( $reviewHandler->getTemplate() );
$wgOut->addModules( [
'ext.watchanalytics.reviewhandler.scripts',
'ext.watchanalytics.reviewhandler.styles'
] );
// record change in user/page stats
WatchStateRecorder::recordReview( $user, $title );
}
return true;
}
/**
* Occurs after the save page request has been processed, and causes the
* new state of "watches" and "reviews" to be recorded for the page and all
* of its watchers.
*
* Additional parameters available include: User $user, Content $content,
* string $summary, boolean $isMinor, boolean $isWatch, $section Deprecated,
* integer $flags, {Revision|null} $revision, Status $status, integer $baseRevId
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/PageContentSaveComplete
*
* @param WikiPage $wikipage
*
* @return bool
*/
public static function onPageContentSaveComplete( WikiPage $wikipage ) {
WatchStateRecorder::recordPageChange( $wikipage );
return true;
}
public static function onLanguageGetMagic( &$magicWords, $langCode ) {
switch ( $langCode ) {
default:
$magicWords['underwatched_categories'] = [ 0, 'underwatched_categories' ];
$magicWords['watchers_needed'] = [ 0, 'watchers_needed' ];
$magicWords['MAG_NOPAGESCORE'] = [ 0, '__NOPAGESCORE__' ];
}
return true;
}
}