-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRealTimeUsers_body.php
More file actions
115 lines (103 loc) · 3.59 KB
/
RealTimeUsers_body.php
File metadata and controls
115 lines (103 loc) · 3.59 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
<?php
/**
* SpecialPage for RealTimeUsers extension
* Show info and realtime users' number
* Called with "/getNumber" param returns the number of RT users
* @ingroup Extensions
* @author Josef Martiňák
* @license MIT
*/
class RealTimeUsers extends SpecialPage {
function __construct() {
parent::__construct( 'RealTimeUsers' );
}
function execute($param) {
$this->setHeaders();
$out = $this->getOutput();
$config = $out->getConfig();
if($param == 'getNumber') {
// Output raw number of RT users
$out->disable();
header( 'Content-type: application/text; charset=utf-8' );
$rtusers = [];
$done = false;
$started = false;
try {
$handle = @fopen(__DIR__ . "/data/wiki.log", "r");
while (($buffer = fgets($handle, 4096)) !== false) {
if(preg_match("/^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})[^\[]*\[([^ ]*)/", $buffer, $m)) {
//188.75.128.28 - - [02/Jan/2020:00:01:02 +0100]
$date = DateTime::createFromFormat('d/M/Y:H:i:s', $m[2]);
$now = new DateTime();
//$now = new DateTime("2020-01-07 14:00:00"); // pro testování
$diff = $now->getTimestamp() - $date->getTimestamp();
if($diff<300) {
// found hits from last 5 minutes
array_push($rtusers, $m[1]);
$started = true;
}
elseif($started) {
$done = true;
break;
}
}
}
fclose($handle);
if(!$started) $done = true; // no recent hit in log, stop searching
// check older logs if necessary
if(!$done) {
for($i=0;$i<5;$i++) {
$started = false;
if($bz = @bzopen(__DIR__ . "/data/wiki.log." . (string)$i . ".bz2", "r")) {
while (!feof($bz)) {
$buffer = bzread($bz);
if(preg_match("/^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})[^\[]*\[([^ ]*)/", $buffer, $m)) {
$date = DateTime::createFromFormat('d/M/Y:H:i:s', $m[2]);
$now = new DateTime();
//$now = new DateTime("2020-01-07 14:00:00"); // pro testování
$diff = $now->getTimestamp() - $date->getTimestamp();
if($diff<300) {
// found hits from last 5 minutes
array_push($rtusers, $m[1]);
$started = true;
}
elseif($started) {
$done = true;
break;
}
}
}
bzclose($bz);
if(!$started) $done = true; // no recent hit in log, stop searching
}
if($done) break;
}
}
} catch(Exception $e) {
echo 'err';
}
$unique = array_unique($rtusers);
echo sizeof($unique);
exit;
}
// Display info
$out->addHTML( "<p>" . $this->msg( 'realtimeusers-desc' )->text() . "</p>" );
$out->addHTML( "<p>" . $this->msg( 'realtimeusers-manual' )->text() . "</p>" );
// prepare data
$data = RealTimeUsersHooks::getChartData('today');
$points1 = [];
foreach($data as $point) {
$point[1] = preg_replace("/[0-9]{4}-[0-9]{2}-[0-9]{2} /", '', $point[1]);
array_push($points1, "{\"x\":\"$point[1]\",\"y\":$point[0]}");
}
$data = RealTimeUsersHooks::getChartData('yesterday');
$points2 = [];
foreach($data as $point) {
$point[1] = preg_replace("/[0-9]{4}-[0-9]{2}-[0-9]{2} /", '', $point[1]);
array_push($points2, "{\"x\":\"$point[1]\",\"y\":$point[0]}");
}
$out->addHTML("<div id='rtContainer'><div id='rtUsers'>" . $this->msg( 'realtimeusers-boxtext' )->text() . "</div>");
$out->addHTML("<canvas id='rtuChart' width='400' height='300' data-refresh='" . $config->get("refreshInterval") . "' data-chart1='[" . implode(',', $points1) . "]' data-chart2='[" . implode(',', $points2) . "]'></canvas></div>");
$out->addModules('ext.RealTimeUsers');
}
}