/
statsLib.php
618 lines (501 loc) · 19.2 KB
/
statsLib.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
require_once('exceptions.php');
require_once('unblocklib.php');
require_once('appealObject.php');
require_once('userObject.php');
/**
* Get an array containing database rows representing the desired appeals.
* @param array $criteria an array of strings that is converted to a WHERE clause.
* {"columnName" => "value", " AND columnName2" => "value2", ... }
* @param int $limit the maximum number to return
* @param string $orderby the column name to sort by
* @return a reference to the result of the query
*/
function queryAppeals(array $criteria = array(), $limit = "", $orderby = "", $timestamp = 0){
$db = connectToDB();
if ($timestamp == 0) {
$query = "SELECT " . Appeal::getColumnsForSelect() . " FROM appeal";
} else {
$query = "SELECT " . Appeal::getColumnsForSelect() . ", l.timestamp FROM appeal,";
$query .= " (SELECT appealID, MAX(timestamp) as timestamp FROM comment GROUP BY appealID) AS l";
}
$query .= " WHERE";
//Parse all of the criteria
foreach($criteria as $item => $value) {
$query .= " " . $item . "= '" . $value . "'";
}
if ($timestamp == 1) {
$query .= "AND appeal.appealID = l.appealID";
}
//If there is an order, use it.
if ($orderby != "") {
$query .= " ORDER BY " . $orderby;
}
//If there is a limit, use it.
if ($limit != "") {
$query .= " LIMIT 0," . $limit;
}
debug($query);
$result = mysql_query($query, $db);
if(!$result){
$error = mysql_error($db);
debug('ERROR: ' . $error . '<br/>');
throw new UTRSDatabaseException($error);
}
return $result;
}
/**
* Returns a list in an HTML table
* @param Array $criteria the column and value to filter by
* @param integer $limit optional the number of items to return
* @param String $orderby optional order of the results and direction
*/
function printAppealList(array $criteria = array(), $limit = "", $orderby = "", $timestamp = 0) {
$currentUser = getCurrentUser();
$secure = $currentUser->getUseSecure();
// get rows from DB. Throws UTRSDatabaseException
$result = queryAppeals($criteria, $limit, $orderby, $timestamp);
$rows = mysql_num_rows($result);
//If there are no new unblock requests
if ($rows == 0) {
$norequests = "<b>No unblock requests in queue</b>";
return $norequests;
} else {
$requests = "<table class=\"appealList\">";
//Begin formatting the unblock requests
for ($i=0; $i < $rows; $i++) {
//Grab the rowset
$data = mysql_fetch_array($result);
$appeal = new Appeal($data);
$requests .= "\t<tr>\n";
$requests .= "\t\t<td>" . $appeal->getID() . ".</td>\n";
$requests .= "\t\t<td><a style=\"color:green\" href='appeal.php?id=" . $appeal->getID() . "'>Zoom</a></td>\n";
$requests .= "\t\t<td><a style=\"color:blue\" href='" . getWikiLink("user:".$appeal->getCommonName(), $secure) . "' target='_NEW'>" . $appeal->getCommonName() . "</a></td>\n";
if ($timestamp == 1) {
$requests .= "\t\t<td>" . $data['timestamp'] . "</td>\n";
}
$requests .= "\t</tr>\n";
}
$requests .= "</table>";
return $requests;
}
}
/**
* Returns a list of all new appeals
*/
function printNewRequests() {
$criteria = array('status' => Appeal::$STATUS_NEW);
return printAppealList($criteria);
}
/**
* Return a list of all appeals where the appealer has replied to a question, and is awaiting further review
*/
function printToolAdmin() {
$criteria = array('status' => Appeal::$STATUS_AWAITING_ADMIN);
return printAppealList($criteria);
}
/**
* Return a list of all appeals where the appealer has replied to a question, and is awaiting further review
*/
function printUserReplyNeeded() {
$criteria = array('status' => Appeal::$STATUS_AWAITING_USER);
return printAppealList($criteria);
}
/**
* Return a list of all appeals that have been flagged for checkuser attention
*/
function printProxyCheckNeeded() {
$criteria = array('status' => Appeal::$STATUS_AWAITING_PROXY);
return printAppealList($criteria);
}
/**
* Return a list of all appeals that have been flagged for checkuser attention
*/
function printCheckuserNeeded() {
$criteria = array('status' => Appeal::$STATUS_AWAITING_CHECKUSER);
return printAppealList($criteria);
}
/**
* Return a list of the last five appeals to be closed
*/
function printRecentClosed() {
$db = connectToDB();
$currentUser = getCurrentUser();
$secure = $currentUser->getUseSecure();
/*
$query = "SELECT a.appealID, a.wikiAccountName, a.ip, l.timestamp";
$query .= " FROM appeal a,";
$query .= " (SELECT appealID, MAX(timestamp) as timestamp";
$query .= " FROM actionAppealLog";
$query .= " WHERE comment = 'Closed'";
$query .= " GROUP BY appealID) l";
$query .= " WHERE l.appealID = a.appealID";
$query .= " AND a.status = 'CLOSED'";
$query .= " ORDER BY l.timestamp DESC LIMIT 0,5";
*/
$query = "SELECT a.appealID, a.wikiAccountName, a.ip, c.timestamp";
$query .= " FROM appeal a, comment c";
$query .= " WHERE a.lastLogId = c.commentID";
$query .= " AND c.comment = 'Closed'";
$query .= " ORDER BY c.timestamp DESC LIMIT 0,5;";
// get rows from DB. Throws UTRSDatabaseException
$result = mysql_query($query, $db);
$rows = mysql_num_rows($result);
//If there are no new unblock requests
if ($rows == 0) {
$norequests = "<b>No unblock requests in queue</b>";
return $norequests;
} else {
$requests = "<table class=\"appealList\">";
//Begin formatting the unblock requests
for ($i=0; $i < $rows; $i++) {
//Grab the rowset
$data = mysql_fetch_array($result);
$appealId = $data['appealID'];
$appeal = Appeal::getAppealById($data['appealID']);
$requests .= "\t<tr>\n";
$requests .= "\t\t<td>" . $appeal->getID() . ".</td>\n";
$requests .= "\t\t<td><a style=\"color:green\" href='appeal.php?id=" . $appeal->getID() . "'>Zoom</a></td>\n";
$requests .= "\t\t<td><a style=\"color:blue\" href='" . getWikiLink("user:" . $appeal->getCommonName(), $secure) . "' target='_NEW'>" . $appeal->getCommonName() . "</a></td>\n";
$requests .= "\t\t<td>" . $data['timestamp'] . "</td>\n";
$requests .= "\t</tr>\n";
}
$requests .= "</table>";
return $requests;
}
}
function printBacklog() {
$db = connectToDB();
$currentUser = getCurrentUser();
$secure = $currentUser->getUseSecure();
/*
$query = "SELECT DISTINCT a.appealID, a.wikiAccountName, a.ip, DateDiff(Now(), cc.last_action) as since_last_action";
$query .= " FROM appeal a LEFT JOIN";
$query .= " (SELECT timestamp, appealID, comment";
$query .= " FROM comment";
$query .= " WHERE action = 1 ORDER BY commentID DESC) c";
$query .= " ON c.appealID = a.appealID";
$query .= " LEFT JOIN (SELECT appealID, Max(timestamp) as last_action";
$query .= " FROM comment";
$query .= " WHERE action = 1";
$query .= " GROUP BY appealID) cc";
$query .= " ON cc.appealID = c.appealID";
$query .= " WHERE DateDiff(Now(), cc.last_action) > 7";
$query .= " AND cc.last_action = c.timestamp";
$query .= " AND c.comment != 'Closed'";
$query .= " ORDER BY last_action ASC;";
*/
$query = "SELECT DISTINCT a.appealID, a.wikiAccountName, a.ip, DateDiff(Now(), c.timestamp) as since_last_action";
$query .= " FROM appeal a, comment c";
$query .= " WHERE a.lastLogId = c.commentID";
$query .= " AND c.comment != 'Closed'";
$query .= " AND DateDiff(Now(), c.timestamp) > 7";
$query .= " ORDER BY c.timestamp ASC";
// get rows from DB. Throws UTRSDatabaseException
$result = mysql_query($query, $db);
$rows = mysql_num_rows($result);
//If there are no new unblock requests
if ($rows == 0) {
$norequests = "<b>No unblock requests in queue</b>";
return $norequests;
} else {
$requests = "<table class=\"appealList\">";
//Begin formatting the unblock requests
for ($i=0; $i < $rows; $i++) {
//Grab the rowset
$data = mysql_fetch_array($result);
$appealId = $data['appealID'];
$appeal = Appeal::getAppealById($data['appealID']);
$requests .= "\t<tr>\n";
$requests .= "\t\t<td>" . $appeal->getID() . ".</td>\n";
$requests .= "\t\t<td><a style=\"color:green\" href='appeal.php?id=" . $appeal->getID(). "'>Zoom</a></td>\n";
$requests .= "\t\t<td><a style=\"color:blue\" href='" . getWikiLink("user:" .$appeal->getCommonName(), $secure) . "' target='_NEW'>" . $appeal->getCommonName() . "</a></td>\n";
$requests .= "\t\t<td> " . $data['since_last_action'] . " days since last action</td>\n";
$requests .= "\t</tr>\n";
}
$requests .= "</table>";
return $requests;
}
}
function printReviewer() {
$criteria = array('status' => Appeal::$STATUS_AWAITING_REVIEWER);
return printAppealList($criteria);
}
function printOnHold() {
$criteria = array('status' => Appeal::$STATUS_ON_HOLD);
return printAppealList($criteria);
}
function printMyQueue() {
$user = User::getUserByUsername($_SESSION['user']);
$criteria = array('handlingAdmin' => $user->getUserId(), ' AND status !' => Appeal::$STATUS_CLOSED);
return printAppealList($criteria, "", "", 1);
}
function printAssigned($userId) {
$user = User::getUserById($userId);
$criteria = array('handlingAdmin' => $user->getUserId(), ' AND status !' => Appeal::$STATUS_CLOSED);
return printAppealList($criteria, "", "", 1);
}
function printClosed($userId) {
$user = User::getUserById($userId);
$criteria = array('handlingAdmin' => $user->getUserId(), ' AND status ' => Appeal::$STATUS_CLOSED);
return printAppealList($criteria, "", "", 1);
}
function printMyReview() {
$user = User::getUserByUsername($_SESSION['user']);
$criteria = array('handlingAdmin' => $user->getUserId(), ' AND status' => Appeal::$STATUS_AWAITING_REVIEWER);
return printAppealList($criteria, "", "", 1);
}
/**
* Get an array containing database rows representing the desired users.
* @param array $criteria an array of strings that is converted to a WHERE clause.
* {"columnName" => "value", " AND columnName2" => "value2", ... }
* @param int $limit the maximum number to return
* @param string $orderby the column name to sort by
* @return a reference to the result of the query
*/
function queryUsers(array $criteria = array(), $limit = "", $orderby = ""){
$db = connectToDB();
$query = "SELECT * FROM user";
$query .= " WHERE";
//Parse all of the criteria
foreach($criteria as $item => $value) {
$query .= " " . $item . " = '" . $value . "'";
}
//If there is an order, use it.
if ($orderby != "") {
$query .= " ORDER BY " . $orderby;
}
//If there is a limit, use it.
if ($limit != "") {
$query .= " LIMIT 0," . $limit;
}
debug($query);
$result = mysql_query($query, $db);
if(!$result){
$error = mysql_error($db);
debug('ERROR: ' . $error . '<br/>');
throw new UTRSDatabaseException($error);
}
return $result;
}
function printUserList(array $criteria = array(), $limit = "", $orderBy = ""){
$currentUser = getCurrentUser();
$secure = $currentUser->getUseSecure();
$result = queryUsers($criteria, $limit, $orderBy);
$rows = mysql_num_rows($result);
if($rows == 0){
echo "<b>No users meet this criteria.</b>";
}
else{
$list = "<table class=\"appealList\">";
//Begin formatting the unblock requests
for ($i=0; $i < $rows; $i++) {
//Grab the rowset
$data = mysql_fetch_array($result);
$userId = $data['userID'];
$username = $data['username'];
$wikiAccount = "User:" . $data['wikiAccount'];
$list .= "\t<tr>\n";
$list .= "\t\t<td>" . $userId . ".</td>\n";
$list .= "\t\t<td><a style=\"color:green\" href=\"userMgmt.php?userId=" . $userId . "\">Manage</a></td>\n";
$list .= "\t\t<td>" . $username . "</td>\n";
$list .= "\t\t<td><a style=\"color:blue\" href='" . getWikiLink($wikiAccount, $secure) . "' target='_NEW'>" . $wikiAccount . "</a></td>\n";
$list .= "\t</tr>\n";
}
$list .= "</table>";
return $list;
}
}
function printUnapprovedAccounts(){
return printUserList(array("approved" => "0"), "", "registered ASC");
}
function printInactiveAccounts(){
return printUserList(array("approved" => "1", " AND active" => "0"), "", "username ASC");
}
function printActiveAccounts(){
return printUserList(array("approved" => "1", " AND active" => "1", " AND toolAdmin" => "0"), "", "username ASC");
}
function printAdmins(){
return printUserList(array("toolAdmin" => "1", " AND active" => "1"), "", "username ASC");
}
function printCheckusers(){
return printUserList(array("checkuser" => "1", " AND active" => "1"), "", "username ASC");
}
function printDevelopers(){
return printUserList(array("developer" => "1"), "", "username ASC");
}
function getNumberAppealsClosedByUser($userId){
$query = "SELECT COUNT(*) AS numClosed FROM appeal WHERE status = '" . Appeal::$STATUS_CLOSED .
"' AND handlingAdmin = '" . $userId . "'";
$db = connectToDB();
$result = mysql_query($query, $db);
if(!$result){
$error = mysql_error($db);
debug('ERROR: ' . $error . '<br/>');
throw new UTRSDatabaseException($error);
}
$data = mysql_fetch_assoc($result);
return $data['numClosed'];
}
function printUserLogs($userId){
if(!$userId){
throw new UTRSIllegalArgumentException($userId, "A valid userID", "printUserLogs()");
}
$db = connectToDB();
$query = "SELECT * FROM userMgmtLog WHERE target='" . $userId . "'";
debug($query);
$result = mysql_query($query, $db);
if(!$result){
$error = mysql_error($db);
debug('ERROR: ' . $error . '<br/>');
throw new UTRSDatabaseException($error);
}
$rows = mysql_num_rows($result);
// shouldn't happen, but meh
if($rows == 0){
echo "<b>No logs exist for this user.</b>";
}
else{
$target = User::getUserById($userId);
$list = "<table class=\"appealList\">";
//Begin formatting the logs
for ($i=0; $i < $rows; $i++) {
//Grab the rowset
$data = mysql_fetch_array($result);
$doneById = $data['doneBy'];
$doneBy = User::getUserById($doneById);
$timestamp = $data['timestamp'];
$action = $data['action'];
$reason = $data['reason'];
$hideTarget = $data['hideTarget'];
$list .= "\t<tr>\n";
$list .= "\t\t<td>" . $timestamp . " UTC</td>\n";
$list .= "\t\t<td>" . $doneBy->getUsername() . " " . $action . ($hideTarget ? "" : " " . $target->getUsername()) .
($reason ? " (<i>" . $reason . "</i>)" : "") . "</td>\n";
$list .= "\t</tr>\n";
}
$list .= "</table>";
return $list;
}
}
function printTemplateList(){
$db = connectToDB();
$query = "SELECT templateID, name FROM template";
debug($query);
$result = mysql_query($query, $db);
if(!$result){
$error = mysql_error($db);
debug('ERROR: ' . $error . '<br/>');
throw new UTRSDatabaseException($error);
}
$rows = mysql_num_rows($result);
// shouldn't happen, but meh
if($rows == 0){
echo "<b>No templates currently exist.</b>";
}
else{
$user = getCurrentUser();
$list = "<table class=\"appealList\">";
//Begin formatting the logs
for ($i=0; $i < $rows; $i++) {
//Grab the rowset
$data = mysql_fetch_array($result);
$id = $data['templateID'];
$name = $data['name'];
$list .= "\t<tr>\n";
$list .= "\t\t<td>" . $name . "</td>\n";
$list .= "\t\t<td><a style=\"color:green\" href=\"tempMgmt.php?id=" . $id . "\">";
if(verifyAccess($GLOBALS['ADMIN'])){
$list .= "Edit";
}
else{
$list .= "View";
}
$list .= "</a></td>\n\t</tr>\n";
}
$list .= "</table>";
return $list;
}
}
function printLastThirtyActions() {
$db = connectToDB();
$sql = "SELECT * FROM comment WHERE action = 1 ORDER BY timestamp DESC LIMIT 0,30;";
$query = mysql_query($sql, $db);
if(!$query){
$error = mysql_error($db);
debug('ERROR: ' . $error . '<br/>');
throw new UTRSDatabaseException($error);
}
$num_rows = mysql_num_rows($query);
$HTMLOutput = "";
$HTMLOutput .= "<table class=\"logLargeTable\">";
$HTMLOutput .= "<tr>";
$HTMLOutput .= "<th class=\"logLargeUserHeader\">Appeal</th>";
$HTMLOutput .= "<th class=\"logLargeUserHeader\">User</th>";
$HTMLOutput .= "<th class=\"logLargeActionHeader\">Action</th>";
$HTMLOutput .= "<th class=\"logLargeTimeHeader\">Timestamp</th>";
$HTMLOutput .= "</tr>";
for ($i = 0; $i < $num_rows; $i++) {
$styleUser = ($i%2 == 1) ? "largeLogUserOne" : "largeLogUserTwo";
$styleAction = ($i%2 == 1) ? "largeLogActionOne" : "largeLogActionTwo";
$styleTime = ($i%2 == 1) ? "largeLogTimeOne" : "largeLogTimeTwo";
$data = mysql_fetch_array($query);
$timestamp = (is_numeric($data['timestamp']) ? date("Y-m-d H:m:s", $data['timestamp']) : $data['timestamp']);
$username = ($data['commentUser']) ? User::getUserById($data['commentUser'])->getUserName() : Appeal::getAppealByID($data['appealID'])->getCommonName();
$italicsStart = ($data['action']) ? "<i>" : "";
$italicsEnd = ($data['action']) ? "</i>" : "";
$appeal = "<a href=\"appeal.php?id=" . $data['appealID'] . "\">" . Appeal::getAppealById($data['appealID'])->getCommonName() . "</a>";
// if posted by appellant
if(!$data['commentUser']){
$styleUser = "highlight";
$styleAction = "highlight";
$styleTime = "highlight";
}
$HTMLOutput .= "<tr>";
$HTMLOutput .= "<td valign=top class=\"" . $styleUser . "\">" . $appeal . "</td>";
$HTMLOutput .= "<td valign=top class=\"" . $styleUser . "\">" . $username . "</td>";
$HTMLOutput .= "<td valign=top class=\"" . $styleAction . "\">" . $italicsStart . str_replace("\\r\\n", "<br>", $data['comment']) . $italicsEnd . "</td>";
$HTMLOutput .= "<td valign=top class=\"" . $styleUser . "\">" . $timestamp . "</td>";
$HTMLOutput .= "</tr>";
}
$HTMLOutput .= "</table>";
return $HTMLOutput;
}
function printSitenoticeMessages(){
$query = "SELECT messageID, LEFT(message, 64) AS summary, CHAR_LENGTH(message) AS length " .
"FROM sitenotice ORDER BY messageID ASC";
$db = connectToDB();
$result = mysql_query($query, $db);
if(!$result){
$error = mysql_error($db);
debug('ERROR: ' . $error . '<br/>');
throw new UTRSDatabaseException($error);
}
$rows = mysql_num_rows($result);
if($rows == 0){
return "<b>There are currently no sitenotice messages.</b>";
}
$table = "<table class=\"sitenoticeTable\">\n";
$table .= "<tr>\n";
$table .= "<th class=\"sitenoticeIDHeader\">ID</th>\n";
$table .= "<th class=\"sitenoticeTextHeader\">Text</th>\n";
$table .= "<th class=\"sitenoticeLinkHeader\"> </th>\n";
$table .= "<th class=\"sitenoticeLinkHeader\"> </th>\n";
$table .= "</tr>";
for($i = 0; $i < $rows; $i++){
$rowData = mysql_fetch_assoc($result);
$table .= "<tr>\n";
$table .= "<td style=\"text-align:center;\">" . $rowData['messageID'] . "</td>\n";
$table .= "<td>\"" . $rowData['summary'] . ($rowData['length'] > 64 ? " ..." : "") . "\"</td>\n";
$table .= "<td style=\"text-align:center;\"><a href=\"" . getRootURL() . "sitenotice.php?id=" .
$rowData['messageID'] . "\">Edit</a></td>\n";
$table .= "<td style=\"text-align:center;\"><a href=\"" . getRootURL() . "sitenotice.php?delete=" .
$rowData['messageID'] . "\">Delete</a></td>\n";
$table .= "</tr>\n";
}
$table .= "</table>\n";
return $table;
}
?>