-
Notifications
You must be signed in to change notification settings - Fork 1
/
UserJourney.body.php
406 lines (306 loc) · 10.9 KB
/
UserJourney.body.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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
class UserJourney {
static $referers = null;
// To get hooks on a page load:
// global $hookLogFile;
// if ( ! isset( $hookLogFile ) ) {
// $hookLogFile = __DIR__ . "/../hooklog/" . date( "Ymd_His", time() ) . "_" . rand() . ".txt";
// file_put_contents( $hookLogFile, $_SERVER["REQUEST_URI"] . "\n", FILE_APPEND );
// }
// file_put_contents( $hookLogFile, "$event\n", FILE_APPEND );
// 1 of the earliest hooks in page load
// public static function onArticlePageDataBefore( $article, $fields ){
// return true;
// }
public static function onBeforeInitialize( &$title, &$article, &$output, &$user, $request, $mediaWiki ) {
$output->enableClientCache( false );
$output->addMeta( 'http:Pragma', 'no-cache' );
global $wgRequestTime,
$egCurrentHit, //data about this page load
$egUserUnreviewedPages, //number of unreviewed pages upon begin page load
$egUserid,
$egPageSave,
$egRecordedInDB;
//consider comparing before-after table for edge case of new page added to wl in page load
//maybe use this method to preclude 2x view logging
//escape if this function is called following the PageContentSaveComplete hook
// if ( $egPageSave == true ) {
// return true;
// }
//Temp vars contained in $egCurrentHit upon completion of this function
$UserPoints = 0; // init user points
$UserActions = ""; // init blank
$UserBadges = ""; // init blank
//Logic to only reward 1st view of a given page per day
$ts = date("Ymd000000", time() ); //rename to $dayStart
$pid = $title->getArticleId();
$username = $user->getName();
// $articleNS = $article->showNamespaceHeader();
$egUserid = $user->getId();
$dbr = wfGetDB( DB_SLAVE );
$egUserUnreviewedPages = $dbr->selectRow(
array('wl' => 'watchlist'),
array(
"COUNT(*) AS number",
// "wl.wl_user AS wl_user",
),
array(
"wl.wl_user" => $egUserid,
// "wl.wl_namespace" => NS_MAIN,//$articleNS,
"wl.wl_notificationtimestamp IS NOT NULL",
),
__METHOD__,
null,
null
);
$userViewsOfThisPageToday = $dbr->selectRow(
array('uj' => 'userjourney'),
array(
"COUNT(*) AS number",
),
array(
"uj.hit_timestamp>$ts",
"uj.user_name" => $username,
"uj.page_id=$pid",
"uj.user_actions" => "View",
// "uj.user_actions LIKE %View%",
),
__METHOD__,
array(
"LIMIT" => "1",
),
null // join conditions
);
$numUserViewsOfThisPageToday = $userViewsOfThisPageToday->number;
if ( $UserActions != "" ) {
$UserActions = $UserActions . ", ";
}
$UserActions = $UserActions . "View";
if( $numUserViewsOfThisPageToday > 0 ) {
//Leave $egUserPoints at current value
} else {
$UserPoints += 1;
}
$now = time();
$hit = array(
'user_points' => $UserPoints, //Eventually this will be a variable based on action specifics
'user_badges' => $UserBadges,
'user_actions' => $UserActions,
'page_id' => $title->getArticleId(),
'page_name' => $title->getFullText(),
'user_name' => $user->getName(),
'hit_timestamp' => wfTimestampNow(),
'hit_year' => date('Y',$now),
'hit_month' => date('m',$now),
'hit_day' => date('d',$now),
'hit_hour' => date('H',$now),
'hit_weekday' => date('w',$now), // 0' => sunday, 1=monday, ... , 6=saturday
'page_action' => $request->getVal( 'action' ),
'oldid' => $request->getVal( 'oldid' ),
'diff' => $request->getVal( 'diff' ),
);
$hit['referer_url'] = isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : null;
$hit['referer_title'] = self::getRefererTitleText( $request->getVal('refererpage') );
if ( $egCurrentHit ) {
// file_put_contents("/var/www/html/MWHooks.txt", "egCurrentHit true\n", FILE_APPEND);
//Need to handle if this event is repeated (like redirect after save)
} else {
// file_put_contents("/var/www/html/MWHooks.txt", "egCurrentHit false\n", FILE_APPEND);
$egCurrentHit = $hit;
}
// self::recordInDatabase(); //remove this after linking flow of hooks
// file_put_contents("/var/www/html/MWHooks.txt", "onBeforeInitializeEnd\n", FILE_APPEND);
return true;
}
// public static function onArticleUpdateBeforeRedirect( $article, &$sectionanchor, &$extraq ) {
// global $egCurrentHit;
// $egCurrentHit['user_actions'] = $egCurrentHit['user_actions'] . "Test";
// return true;
// }
// After save page request has been completed
public static function onPageContentSaveComplete( $article, $user, $content, $summary,
$isMinor, $isWatch, $section, $flags, $revision, $status, $baseRevId ) {
// file_put_contents("/var/www/html/MWHooks.txt", "onPageContentSaveComplete\n", FILE_APPEND);
global $wgRequestTime, $egCurrentHit, $egPageSave;
$egPageSave = true;
//Logic to only reward 1st save of a given page per day
$ts = date("Ymd000000", time() );
$ptitle = $article->getTitle();
$pid = $ptitle->getArticleId();
$username = $user->getName();
if ( $egCurrentHit['user_actions'] != "" ) {
$egCurrentHit['user_actions'] = $egCurrentHit['user_actions'] . ", ";
}
$egCurrentHit['user_actions'] = "SaveEdit";
$dbr = wfGetDB( DB_SLAVE );
$userHasSavedThisPageToday = $dbr->select(
array('uj' => 'userjourney'),
array(
"uj.page_id AS page_id",
"uj.hit_timestamp AS hit_timestamp",
"uj.user_name AS user_name",
"uj.user_actions AS user_actions",
),
array(
"uj.hit_timestamp>$ts",
"uj.user_name" => $username,
"uj.page_id=$pid",
"uj.user_actions" => "SaveEdit",
),
__METHOD__,
array(
"LIMIT" => "1",
),
null // join conditions
);
//add new $dbr->select query for first edit and award 10 bonus points, then do one for 10 edits, etc
$listOfUserRevisions = $dbr->select(
array('uj' => 'userjourney'),
array(
"uj.page_id AS page_id",
"uj.hit_timestamp AS hit_timestamp",
"uj.user_name AS user_name",
"uj.user_actions AS user_actions",
),
array(
//"uj.hit_timestamp>$ts",
"uj.user_name" => $username,
//"uj.page_id=$pid",//need to calc unique page saves per day?
"uj.user_actions" => "SaveEdit",
),
__METHOD__,
array(
//"LIMIT" => "1",
),
null // join conditions
);
$numberOfUserRevisions = $dbr->numRows( $listOfUserRevisions );
if( $dbr->numRows( $userHasSavedThisPageToday ) == 0 ) {
$egCurrentHit['user_points'] += 3;
} else if ( $numberOfUserRevisions == 10 ) {
$egCurrentHit['user_points'] += 10;
$user_badge = $user_badge . "10th Edit";
$egCurrentHit['user_badges'] = $egCurrentHit['user_badges'] . "10th Edit";
}
// echo "<script type='text/javascript'>alert('test')</script>";
$egCurrentHit['user_badges'] = "Test";
//Unsure why I need to call this here when it seems AfterFinalPageOutput is called after this hook
self::recordInDatabase();
return true;
}
public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin ){
// file_put_contents("/var/www/html/MWHooks.txt", "onBeforePageDisplay\n", FILE_APPEND);
global $egCurrentHit, $egUserUnreviewedPages, $egUserid;
//Logic to only reward 1st save of a given page per day
// $ts = date("Ymd000000", time() );
// $ptitle = $article->getTitle();
// $pid = $ptitle->getArticleId();
// $username = $user->getName();
$dbr = wfGetDB( DB_SLAVE );
$deltaUserUnreviewedPages = $dbr->selectRow(
array('wl' => 'watchlist'),
array(
"COUNT(*) AS number",
),
array(
"wl.wl_user" => $egUserid,
"wl.wl_notificationtimestamp IS NOT NULL",
),
__METHOD__,
//OPTIONS
null,
null // join conditions
);
if ( $deltaUserUnreviewedPages->number != $egUserUnreviewedPages->number ) {
$egCurrentHit['user_points'] += 15; //indicate user has un-reviewed page, move reward to later hook
if ( $egCurrentHit['user_actions'] != "" ) {
$egCurrentHit['user_actions'] = $egCurrentHit['user_actions'] . ", ";
}
$egCurrentHit['user_actions'] = $egCurrentHit['user_actions'] . "Review";
}
// self::recordInDatabase();
return true;
}
public static function recordInDatabase ( ) { // could have param &$output
global $wgRequestTime, $egCurrentHit, $egRecordedInDB;
// file_put_contents("/var/www/html/MWHooks.txt", "recordInDatabase\n", FILE_APPEND);
// calculate response time now, in the last hook (that I know of).
$egCurrentHit['response_time'] = round( ( microtime( true ) - $wgRequestTime ) * 1000 );
$dbw = wfGetDB( DB_MASTER );
$dbw->insert(
'userjourney',
$egCurrentHit,
__METHOD__
);
//Annunciate to user if they earned points or badge
//Issue: Doesn't annunciate on page save earning points
$alertPoints = $egCurrentHit['user_points'];
$alertBadges = $egCurrentHit['user_badges'];
$alertMessage = ""; //NULL;
$alertAction = $egCurrentHit['page_action'];
// echo "<script type='text/javascript'>alert('$alertPoints and $alertBadges')</script>";
// echo "<script>console.log( 'Notifications: $alertPoints Points, $alertBadges Badges, and $alertMessage Message. Event: $alertAction' );</script>";
if ( $alertPoints > 0 || $alertBadges != "" ){
$alertMessage = $alertMessage . "Awesome!";
}
if ( $alertPoints > 0 ){
$alertMessage = $alertMessage . "\\nYou got $alertPoints points!";
}
if ( $alertBadges != "" ){
$alertMessage = $alertMessage . "\\nYou got the $alertBadges badge!";
}
if ( $alertMessage != "" ){
// echo "<script type='text/javascript'>alert('$alertMessage')</script>";
}
// echo "<script type='text/javascript'>alert('Test $alertMessage')</script>";
return true;
}
public static function updateDatabase( DatabaseUpdater $updater ) {
global $wgDBprefix;
$userJourneyTable = $wgDBprefix . 'userjourney';
$schemaDir = __DIR__ . '/schema';
$updater->addExtensionTable(
$userJourneyTable,
"$schemaDir/UserJourney.sql"
);
// $updater->addExtensionField(
// $userjourneyTable
// 'response_time',
// "$schemaDir/patch-1-response-time.sql"
// );
return true;
}
//
// See WebRequest::getPathInfo() for ideas/info
// Make better use of: $wgScript, $wgScriptPath, $wgArticlePath;
//
// Other recommendations:
// wfSuppressWarnings();
// $a = parse_url( $url );
// wfRestoreWarnings();
//
public static function getRefererTitleText ( $refererpage=null ) {
global $wgScriptPath;
if ( $refererpage )
return $refererpage;
else if ( ! isset($_SERVER["HTTP_REFERER"]) )
return null;
$wikiBaseUrl = WebRequest::detectProtocol() . '://' . $_SERVER['HTTP_HOST'] . $wgScriptPath;
// if referer URL starts
if ( strpos($_SERVER["HTTP_REFERER"], $wikiBaseUrl) === 0 ) {
$questPos = strpos( $_SERVER['HTTP_REFERER'], '?' );
$hashPos = strpos( $_SERVER['HTTP_REFERER'], '#' );
if ($hashPos !== false) {
$queryStringLength = $hashPos - $questPos;
$queryString = substr($_SERVER['HTTP_REFERER'], $questPos+1, $queryStringLength);
} else {
$queryString = substr($_SERVER['HTTP_REFERER'], $questPos+1);
}
$query = array();
parse_str( $queryString, $query );
return isset($query['title']) ? $query['title'] : false;
}
else
return false;
}
}