-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRealTimeUsersHooks.php
More file actions
120 lines (101 loc) · 3.1 KB
/
RealTimeUsersHooks.php
File metadata and controls
120 lines (101 loc) · 3.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
<?php
/**
* All hooked functions used by RealTimeUsers
* @ingroup Extensions
* @author Josef Martiňák
* @license MIT
*/
class RealTimeUsersHooks {
/**
* Set up the parser hooks
* @param object $parser: instance of OutputPage
* @return Boolean: true
*/
public static function registerParserHook( &$parser ) {
$parser->setHook( 'realtimeusers', 'RealTimeUsersHooks::realTimeUsersRender' );
return true;
}
/**
* Callback function for registerParserHook
* @param string $input: user-supplied input, unused
* @param array $args: user-supplied arguments, unused
* @param object $parser: instance of Parser, unused
* @return String: HTML
*/
public static function realTimeUsersRender( $input, $args, $parser ) {
$context = new RequestContext();
$out = $context->getOutput();
$config = $context->getConfig();
$refreshInterval = $config->get("refreshInterval");
$output = "<div id='rtContainer'>";
$mode = 'combo';
if(in_array($args['mode'], ['number','chart'])) $mode = $args['mode'];
// prepare data
$data = RealTimeUsersHooks::getChartData('today');
$points1 = [];
foreach($data as $point) {
array_push($points1, "{\"x\":\"$point[1]\",\"y\":$point[0]}");
}
$data = RealTimeUsersHooks::getChartData('yesterday');
$points2 = [];
foreach($data as $point) {
array_push($points2, "{\"x\":\"$point[1]\",\"y\":$point[0]}");
}
if(in_array($mode, ['number', 'combo'])) {
// refreshing number of RT users
$output .= "<div id='rtUsers'>" . $out->msg( 'realtimeusers-boxtext' )->text() . "</div>";
}
if(in_array($mode, ['chart', 'combo'])) {
// chart of active users
$output .= "<canvas id='rtuChart' width='400' height='300' data-refresh='$refreshInterval' data-chart1='[" . implode(',', $points1) . "]' data-chart2='[" . implode(',', $points2) . "]'></canvas></div>";
}
$output .= "</div>";
$out->addModules('ext.RealTimeUsers');
return $output;
}
/**
* Show chart
* @param object $out: instance of OutputPage
* @param object $skin: instance of Skin, unused
*/
public static function showChart( &$out, &$skin ) {
$title = $out->getTitle();
if($title->getArticleID() == 1 && !isset( $query['action'] )) {
// mainpage only
$out->addModules('ext.RealTimeUsers');
}
return true;
}
/**
* Get data for RT users chart
* @param string $interval: 'today|yesterday'
* @return array: [number of RT users,datetime]
*/
public static function getChartData($interval) {
$now = new DateTime();
switch($interval) {
case 'today':
$datestr = $now->format('Y-m-d');
break;
case 'yesterday':
$now->modify("-1 day");
$datestr = $now->format('Y-m-d');
break;
default:
return false;
}
$output = array();
$handle = @fopen(__DIR__ . "/data/chart.csv", "r");
while (($buffer = fgets($handle, 4096)) !== false) {
preg_match("/^([0-9]*);(.*)$/", rtrim($buffer), $m);
$number = $m[1];
$date = $m[2];
if(preg_match("/$datestr/", $date)) {
$date = preg_replace("/[0-9]{4}-[0-9]{2}-[0-9]{2} /", '', $date);
array_push($output, array($number, $date));
}
}
fclose($handle);
return $output;
}
}