-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdb_functions.php
More file actions
114 lines (105 loc) · 4.05 KB
/
Copy pathdb_functions.php
File metadata and controls
114 lines (105 loc) · 4.05 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
<?php
namespace CluebotNG;
use mysqli_sql_exception;
/*
* Copyright (C) 2015 Jacobi Carter and Chris Breneman
*
* This file is part of ClueBot NG.
*
* ClueBot NG is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* ClueBot NG is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ClueBot NG. If not, see <http://www.gnu.org/licenses/>.
*/
class Db
{
private static function connect()
{
$cb_mysql = mysqli_connect(
'p:' . Config::$cb_mysql_host,
Config::$cb_mysql_user,
Config::$cb_mysql_pass,
Config::$cb_mysql_db,
Config::$cb_mysql_port
);
if (!$cb_mysql) {
Metrics::increment('bot_mysql_cb_connection_failures_total');
die('cb mysql error: ' . mysqli_connect_error());
}
mysqli_select_db($cb_mysql, Config::$cb_mysql_db);
return $cb_mysql;
}
private static function runQuery($cb_mysql, string $identifier, string $context, string $sql)
{
global $logger;
try {
return mysqli_query($cb_mysql, $sql);
} catch (mysqli_sql_exception $e) {
$logger->error("$identifier query returned an error for $context: " . $e->getMessage());
Metrics::increment('bot_mysql_cb_query_failures_total', [$identifier]);
return false;
}
}
// Returns the edit id for the vandalism
public static function detectedVandalism($user, $title, $heuristic, $reason, $url, $old_rev_id, $rev_id)
{
$cb_mysql = self::connect();
$query = 'INSERT INTO `vandalism` ' .
'(`id`,`user`,`article`,`heuristic`,`reason`,`diff`,`old_id`,`new_id`,`reverted`) ' .
'VALUES ' .
'(NULL,\'' . mysqli_real_escape_string($cb_mysql, $user) . '\',' .
'\'' . mysqli_real_escape_string($cb_mysql, $title) . '\',' .
'\'' . mysqli_real_escape_string($cb_mysql, $heuristic) . '\',' .
'\'' . mysqli_real_escape_string($cb_mysql, $reason) . '\',' .
'\'' . mysqli_real_escape_string($cb_mysql, $url) . '\',' .
'\'' . mysqli_real_escape_string($cb_mysql, $old_rev_id) . '\',' .
'\'' . mysqli_real_escape_string($cb_mysql, $rev_id) . '\',0)';
self::runQuery($cb_mysql, 'vandalism_insert', $title, $query);
$edit_id = mysqli_insert_id($cb_mysql);
mysqli_close($cb_mysql);
return $edit_id;
}
// Returns nothing
public static function vandalismReverted($edit_id)
{
$cb_mysql = self::connect();
self::runQuery(
$cb_mysql,
'vandalism_update_reverted',
$edit_id,
'UPDATE `vandalism` SET `reverted` = 1 WHERE `id` = \'' .
mysqli_real_escape_string($cb_mysql, $edit_id) . '\''
);
mysqli_close($cb_mysql);
}
// Returns nothing
public static function vandalismRevertBeaten($edit_id, $title, $user, $diff)
{
$cb_mysql = self::connect();
self::runQuery(
$cb_mysql,
'vandalism_update_beaten',
$edit_id,
'UPDATE `vandalism` SET `reverted` = 0 WHERE `id` = \'' .
mysqli_real_escape_string($cb_mysql, $edit_id) . '\''
);
self::runQuery(
$cb_mysql,
'beaten_insert',
$title,
'INSERT INTO `beaten` (`id`,`article`,`diff`,`user`) VALUES (NULL,\'' .
mysqli_real_escape_string($cb_mysql, $title) . '\',\'' .
mysqli_real_escape_string($cb_mysql, $diff) . '\',\'' .
mysqli_real_escape_string($cb_mysql, $user) . '\')'
);
mysqli_close($cb_mysql);
}
}