-
Notifications
You must be signed in to change notification settings - Fork 0
/
Item.php
134 lines (107 loc) · 4.2 KB
/
Item.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
<?php
require_once dirname(__FILE__) . "/db.php";
require_once dirname(__FILE__) . "/modules/HttpException/HttpException.php";
require_once dirname(__FILE__) . '/sql2array.php';
require_once dirname(__FILE__) . '/Assert.php';
require_once dirname(__FILE__) . '/SortHelper.php';
class Item
{
const VERSION_LATEST = 'latest';
const VERSION_FIRST = 'first';
public static function getId($name, $version, $stable = NULL)
{
$db_connection = db_ensure_connection();
$name = $db_connection->real_escape_string($name);
$version = $db_connection->real_escape_string($version);
$db_cond = "name = '$name'";
$db_join = $db_order = '';
if (!$special_version = in_array($version, array(self::VERSION_LATEST, self::VERSION_FIRST)))
{
$db_cond .= " AND version = '$version'";
} else {
$db_order = ' ORDER BY `position`' . ($version == self::VERSION_LATEST ? 'DESC' : 'ASC');
$db_join = ' LEFT JOIN `semver_index` USING (`version`)';
SortHelper::PrepareSemverSorting(DB_TABLE_ITEMS, 'version');
}
if ($stable !== NULL) {
$db_cond .= ' AND ' . ($stable ? '' : '!') . 'semver_stable(`version`)';
}
$db_query = 'SELECT HEX(id) AS id, version FROM ' . DB_TABLE_ITEMS . $db_join . ' WHERE ' . $db_cond . $db_order;
$db_result = $db_connection->query($db_query);
Assert::dbMinRows($db_result);
$db_entry = $db_result->fetch_assoc(); # the only (exact version) or first ("latest" or "first" version) entry
return $db_entry["id"];
}
public static function get($id, array $cols)
{
$db_connection = db_ensure_connection();
$id = $db_connection->real_escape_string($id);
$db_query = 'SELECT ' . implode(', ', $c = array_map('EnwrapColName', $cols)) . ' FROM ' . DB_TABLE_ITEMS . " WHERE `id` = UNHEX('$id')";
$db_result = $db_connection->query($db_query);
return $db_result->fetch_assoc();
}
const REVIEW_GOOD = 1;
const REVIEW_INDETERMINATE = 0;
const REVIEW_BAD = -1;
public static function IsReviewed($id) {
$db_connection = db_ensure_connection();
$id = $db_connection->real_escape_string($id);
$db_query = 'SELECT `reviewed` FROM `' . DB_TABLE_ITEMS . '` WHERE `id` = UNHEX("' . $id . '")';
$db_result = $db_connection->query($db_query);
Assert::dbMinRows($db_result);
$db_entry = $db_result->fetch_assoc();
return (int)$db_entry['reviewed'] != self::REVIEW_INDETERMINATE;
}
public static function Review($id, $status) {
$db_connection = db_ensure_connection();
$id = $db_connection->real_escape_string($id);
$status = (int)$status;
if (!in_array($status, array(self::REVIEW_GOOD, self::REVIEW_INDETERMINATE, self::REVIEW_BAD))) {
throw new HttpException(500);
}
$db_query = 'UPDATE `' . DB_TABLE_ITEMS . '` SET `reviewed` = ' . $status . ' WHERE `id` = UNHEX("' . $id . '")';
$db_result = $db_connection->query($db_query);
Assert::dbMinRows($db_connection);
}
public static function existsId($id)
{
$db_connection = db_ensure_connection();
$id = $db_connection->real_escape_string($id);
$db_query = "SELECT COUNT(*) FROM " . DB_TABLE_ITEMS . " WHERE id = UNHEX('$id')";
$db_result = $db_connection->query($db_query);
$db_entry = $db_result->fetch_assoc();
return $db_entry["COUNT(*)"] > 0;
}
public static function exists($name, $version = NULL)
{
$db_connection = db_ensure_connection();
$name = $db_connection->real_escape_string($name);
$version = $version == NULL ? NULL : $db_connection->real_escape_string($version);
$db_cond = "name = '$name'";
if ($version != NULL)
{
$db_cond .= " AND version = '$version'";
}
$db_query = "SELECT COUNT(*) FROM " . DB_TABLE_ITEMS . " WHERE $db_cond";
$db_result = $db_connection->query($db_query);
$db_entry = $db_result->fetch_assoc();
return $db_entry["COUNT(*)"] > 0;
}
public static function getUser($name, $version)
{
return self::getUserForId(self::getId($name, $version));
}
public static function getUserForId($id)
{
$db_connection = db_ensure_connection();
$id = $db_connection->real_escape_string($id);
$db_query = "SELECT HEX(user) AS user FROM " . DB_TABLE_ITEMS . " WHERE id = UNHEX('$id')";
$db_result = $db_connection->query($db_query);
$db_entry = $db_result->fetch_assoc();
return $db_entry["user"];
}
}
function EnwrapColName($col) {
return '`' . $col . '`';
}
?>