Repository navigation
Expand file tree
/
Copy pathWatchStateRecorder.php
More file actions
386 lines (319 loc) · 10.1 KB
/
Copy pathWatchStateRecorder.php
File metadata and controls
386 lines (319 loc) · 10.1 KB
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
class WatchStateRecorder {
protected $dbr;
protected $dbw;
public function recordedWithinHours( $withinHours = 1 ) {
$withinHours = ( intval( $withinHours ) > 0 ) ? intval( $withinHours ) : 1;
$withinDays = floor( $withinHours / 24 );
if ( $withinDays > 0 ) {
$withinHours = $withinHours % 24;
}
$now = new MWTimestamp();
$diff = $now->diff( $this->getLatestAllWikiTimestamp() );
if ( $diff->h >= $withinHours && $diff->days >= $withinDays ) {
return false;
}
return true;
}
public function getLatestAllWikiTimestamp() {
$dbr = wfGetDB( DB_REPLICA );
$result = $dbr->selectRow(
'watch_tracking_wiki',
'tracking_timestamp',
'', // conds
__METHOD__,
[
'LIMIT' => 1,
'ORDER BY' => 'tracking_timestamp DESC',
],
null // join_conds
);
if ( $result && $result->tracking_timestamp ) {
return new MWTimestamp( $result->tracking_timestamp );
} else {
return new MWTimestamp( '19700101000000' );
}
}
public function recordAll() {
$this->dbw = wfGetDB( DB_MASTER );
// get user and page info
$userWatchQuery = new UserWatchesQuery();
$pageWatchQuery = new PageWatchesQuery();
$userQueryInfo = $userWatchQuery->getQueryInfo();
$pageQueryInfo = $pageWatchQuery->getQueryInfo();
// override fields
$userQueryInfo['fields'] = [
'u.user_id AS user_id',
$userWatchQuery->sqlNumWatches,
$userWatchQuery->sqlNumPending,
];
$pageQueryInfo['fields'] = [
'p.page_id AS page_id',
'p.page_namespace AS page_namespace', // needed only for all-wiki info
$pageWatchQuery->sqlNumWatches,
$pageWatchQuery->sqlNumReviewed,
];
$users = $this->fetchAllFromQueryInfo( $userQueryInfo, [
'user_id', 'num_watches', 'num_pending'
] );
$pages = $this->fetchAllFromQueryInfo( $pageQueryInfo, [
'page_id', 'page_namespace', 'num_watches', 'num_reviewed'
] );
$now = new MWTimestamp();
$now = $now->format( 'YmdHis' );
$unwatched = 0;
$oneWatched = 0;
$unreviewed = 0;
$oneReviewed = 0;
$nsMainUnwatched = 0;
$nsMainOneWatched = 0;
$nsMainUnreviewed = 0;
$nsMainOneReviewed = 0;
foreach ( $users as $key => $user ) {
$users[$key]['tracking_timestamp'] = $now;
}
foreach ( $pages as $key => $page ) {
$page['tracking_timestamp'] = $now;
$numWatches = intval( $page['num_watches'] );
$numReviewed = intval( $page['num_reviewed'] );
$pageNS = intval( $page['page_namespace'] );
if ( $numWatches === 0 ) {
$unwatched++;
if ( $pageNS === NS_MAIN ) {
$nsMainUnwatched++;
}
} elseif ( $numWatches === 1 ) {
$oneWatched++;
if ( $pageNS === NS_MAIN ) {
$nsMainOneWatched++;
}
}
if ( $numReviewed === 0 ) {
$unreviewed++;
if ( $pageNS === NS_MAIN ) {
$nsMainUnreviewed++;
}
} elseif ( $numReviewed === 1 ) {
$oneReviewed++;
if ( $pageNS === NS_MAIN ) {
$nsMainOneReviewed++;
}
}
unset( $page['page_namespace'] ); // can't be present for insert statement below
$pages[$key] = $page;
// unset( $pages[$key]['page_namespace'] );
}
$this->dbw->insert(
'watch_tracking_user',
$users,
__METHOD__
);
foreach ( array_chunk( $pages, 100 ) as $chunk ) {
$this->dbw->insert(
'watch_tracking_page',
$chunk,
__METHOD__
);
}
// Get all wiki info
$allWikiQueryInfo = $this->getWikiQueryInfo();
$mainWikiQueryInfo = $this->getWikiQueryInfo( NS_MAIN, 'content_' );
$allNamespaces = $this->fetchAllFromQueryInfo( $allWikiQueryInfo, [
'num_pages', 'num_watches', 'num_pending', 'max_pending_minutes', 'avg_pending_minutes'
] );
$allNamespaces[0][ 'max_pending_minutes' ] =
$allNamespaces[0][ 'max_pending_minutes' ]
? $allNamespaces[0][ 'max_pending_minutes' ] : 0;
$allNamespaces[0][ 'avg_pending_minutes' ] =
$allNamespaces[0][ 'avg_pending_minutes' ]
? $allNamespaces[0][ 'avg_pending_minutes' ] : 0;
$contentOnly = $this->fetchAllFromQueryInfo( $mainWikiQueryInfo, [
'content_num_pages', 'content_num_watches', 'content_num_pending',
'content_max_pending_minutes', 'content_avg_pending_minutes'
] );
$contentOnly[0][ 'content_max_pending_minutes' ] =
$contentOnly[0][ 'content_max_pending_minutes' ]
? $contentOnly[0][ 'content_max_pending_minutes' ] : 0;
$contentOnly[0][ 'content_avg_pending_minutes' ] =
$contentOnly[0][ 'content_avg_pending_minutes' ]
? $contentOnly[0][ 'content_avg_pending_minutes' ] : 0;
$allWikiAnalytics = $allNamespaces[0] + $contentOnly[0] + [
'tracking_timestamp' => $now,
'num_unwatched' => $unwatched,
'num_one_watched' => $oneWatched,
'num_unreviewed' => $unreviewed,
'num_one_reviewed' => $oneReviewed,
'content_num_unwatched' => $nsMainUnwatched,
'content_num_one_watched' => $nsMainOneWatched,
'content_num_unreviewed' => $nsMainUnreviewed,
'content_num_one_reviewed' => $nsMainOneReviewed,
];
$this->dbw->insert(
'watch_tracking_wiki',
$allWikiAnalytics,
__METHOD__
);
return true;
}
public function fetchAllFromQueryInfo( $queryInfo, $columnsToKeep ) {
$result = $this->dbw->select(
$queryInfo['tables'],
$queryInfo['fields'],
$queryInfo['conds'],
__METHOD__,
$queryInfo['options'],
$queryInfo['join_conds']
);
$output = [];
while ( $row = $result->fetchRow() ) {
$c = count( $output );
foreach ( $columnsToKeep as $col ) {
$output[$c][$col] = $row[$col];
}
}
return $output;
}
public function getWikiQueryInfo( $namespace = false, $prefix = '' ) {
$sqlNumPages = "COUNT( DISTINCT p.page_id ) AS {$prefix}num_pages";
$sqlNumWatches = "SUM( IF( w.wl_title IS NOT NULL, 1, 0) ) AS {$prefix}num_watches";
$sqlNumPending = "SUM( IF( w.wl_notificationtimestamp IS NULL, 0, 1) ) AS {$prefix}num_pending";
$sqlMaxPendingMins = "MAX( TIMESTAMPDIFF(MINUTE, w.wl_notificationtimestamp, UTC_TIMESTAMP()) ) AS {$prefix}max_pending_minutes";
$sqlAvgPendingMins = "AVG( TIMESTAMPDIFF(MINUTE, w.wl_notificationtimestamp, UTC_TIMESTAMP()) ) AS {$prefix}avg_pending_minutes";
$tables = [
'w' => 'watchlist',
'p' => 'page',
];
$fields = [
$sqlNumPages,
$sqlNumWatches,
$sqlNumPending,
$sqlMaxPendingMins,
$sqlAvgPendingMins,
];
$join_conds = [
'p' => [
'RIGHT JOIN', 'p.page_namespace=w.wl_namespace AND p.page_title=w.wl_title'
],
];
$options = [
// unlike pages, group by NOTHING
// 'GROUP BY' => 'p.page_title, p.page_namespace'
];
$conds = '';
if ( $namespace !== false ) {
$conds = 'p.page_namespace=' . $namespace;
}
$return = [
'tables' => $tables,
'fields' => $fields,
'join_conds' => $join_conds,
'conds' => $conds,
'options' => $options,
];
return $return;
}
/**
* Record relevant info in watch_tracking_page and watch_tracking_user
* after a change to a page (e.g. an edit, a move, etc)
*
* @param WikiPage $wikipage
* @return bool
*/
public static function recordPageChange( $wikipage ) {
$timestamp = date( "YmdHis", time() );
$title = $wikipage->getTitle();
// page watch stats
list( $numWatchers, $numReviewed, $userIdArray ) = self::getPageWatchInfo( $title );
// query for each users' total watches/reviews
$userWQ = new UserWatchesQuery();
$userWatchStats = $userWQ->getMultiUserWatchStats( $userIdArray );
$userInsertData = [];
foreach ( $userWatchStats as $uData ) {
$userInsertData[] = [
'tracking_timestamp' => $timestamp,
'user_id' => $uData->wl_user,
'num_watches' => $uData->num_watches,
'num_pending' => $uData->num_pending,
];
}
$dbw = wfGetDB( DB_MASTER );
// insert into watch_tracking_page: $timestamp, $title->getId(), $numWatchers, $numReviewed
$dbw->replace(
'watch_tracking_page',
[ [ 'tracking_timestamp', 'page_id' ] ],
[
[
'tracking_timestamp' => $timestamp,
'page_id' => $title->getArticleID(),
'num_watches' => $numWatchers,
'num_reviewed' => $numReviewed,
],
],
__METHOD__
);
// insert into watch_tracking_user: timestamp, user ID, num watches, num reviews
// @todo FIXME: this incorrectly records the editors watch state if they change from
// watched to unwatched or unwatched to watched during the edit...maybe.
$dbw->replace(
'watch_tracking_user',
[ [ 'tracking_timestamp', 'user_id' ] ],
$userInsertData,
__METHOD__
);
// do a full wiki record?
// take too long? excessive?
// all buildable by page table, except avg/max time
return true;
}
public static function recordReview( User $user, Title $title ) {
$timestamp = date( 'YmdHis', time() );
$userWQ = new UserWatchesQuery();
$userWatchStats = $userWQ->getMultiUserWatchStats( [ $user->getId() ] );
$dbw = wfGetDB( DB_MASTER );
// necessary to use replace here? probably, in case two requests
// are made in rapid succession such that the second gets a read
// request from the database saying that the notification timestamp
// hasn't been cleared yet, suggesting it should be cleared again
$dbw->replace(
'watch_tracking_user',
[ [ 'tracking_timestamp', 'user_id' ] ],
[
'tracking_timestamp' => $timestamp,
'user_id' => $userWatchStats[0]->wl_user,
'num_watches' => $userWatchStats[0]->num_watches,
'num_pending' => $userWatchStats[0]->num_pending,
],
__METHOD__
);
// page watch stats (disregarding third var, array of user IDs)
list( $numWatchers, $numReviewed ) = self::getPageWatchInfo( $title );
$dbw->replace(
'watch_tracking_page',
[ [ 'tracking_timestamp', 'page_id' ] ],
[
'tracking_timestamp' => $timestamp,
'page_id' => $title->getArticleID(),
'num_watches' => $numWatchers,
'num_reviewed' => $numReviewed,
],
__METHOD__
);
return true;
}
public static function getPageWatchInfo( Title $title ) {
// query for page watchers
$pageWQ = new PageWatchesQuery();
$watchers = $pageWQ->getPageWatchers( $title->getDBkey(), $title->getNamespace() );
// PHP count num watchers and reviewers (watchers could change if user (un)checks "watch this page" box)
$numWatchers = count( $watchers );
$numReviewed = 0;
$userIdArray = [];
foreach ( $watchers as $w ) {
if ( $w->wl_notificationtimestamp === null ) {
$numReviewed++;
}
$userIdArray[] = $w->wl_user;
}
return [ $numWatchers, $numReviewed, $userIdArray ];
}
}