/
index.php
177 lines (162 loc) · 5.16 KB
/
index.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
<?php
require 'conf.php';
function buildURL($apiCall, $site, $filter, $apiKey) {
if (strpos($apiCall, '?') === false) {
$apiCall .= "?dummy";
}
return "https://api.stackexchange.com/2.2/" . $apiCall
. "&site=" . $site
. "&filter=" . $filter . "&key=" . $apiKey;
}
function apiCall($apiCall, $site, $filter) {
global $apiKey;
$url = buildURL($apiCall, $site, $filter, $apiKey);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if ($result === false) {
$error = curl_error($ch);
curl_close($ch);
throw new Exception("Error calling Stack Exchange API: $error");
}
curl_close($ch);
return $result;
}
function fetchQuestion($qid, $db) {
$filter = "!)rcjzniPuafk4WNG65yr";
$data = apiCall("questions/$qid?order=desc&sort=activity", 'codereview', $filter);
$json = json_decode($data, true);
$items = $json['items'];
if (count($items) == 0) {
return showError('Question ' . $qid . ' not found');
}
$question = $json['items'][0];
$dbfields = array("is_answered", "view_count", "favorite_count", "answer_count", "score", "accepted_answer_id");
$sql = 'INSERT INTO cr_badge (question_id, is_answered, favorite_count, answer_count, view_count, score, fetch_time, accepted_answer_id) ' .
'VALUES (:qid, :is_answered, :favorite_count, :answer_count, :view_count, :score, :time, :accepted_answer_id) ON DUPLICATE KEY UPDATE ' .
'is_answered = :is_answered, favorite_count = :favorite_count, answer_count = :answer_count, view_count = :view_count, score = :score, fetch_time = :time, accepted_answer_id = :accepted_answer_id ;';
$stmt = $db->prepare($sql);
$sql_params = array();
foreach ($dbfields as $field_name) {
if (isset($question[$field_name])) {
$sql_params[':' . $field_name] = $question[$field_name];
} else {
$sql_params[':' . $field_name] = 0;
}
}
$sql_params[':qid'] = $qid;
$sql_params[':time'] = time();
$result = $stmt->execute($sql_params);
if ($result) {
useData($question);
} else {
die($stmt->errorInfo());
}
return $json;
}
function useData($data) {
$text = 'reviewed';
if (isset($data['accepted_answer_id']) && $data['accepted_answer_id'] != 0) {
$color = '97ca00';
$mode = 'views';
} elseif ($data['answer_count'] >= 1) {
$color = 'ff8000';
$right = $data['score'] . ' score';
$mode = 'answers';
} else {
$color = 'e05d44';
$text = 'reviewing';
$mode = 'score';
}
if (isset($_GET['mode'])) {
$mode = $_GET['mode'];
}
$data['answers'] = $data['answer_count'];
$data['views'] = $data['view_count'];
$right = $data[$mode] . ' ' . $mode;
header('Content-type: image/svg+xml; charset=utf-8');
header('Cache-Control: no-cache, private');
header('Vary: Accept');
$etag = $mode . $color . $data[$mode];
header('Etag: "' . $etag . '"');
$svg = <<<END
<svg xmlns="http://www.w3.org/2000/svg" width="137" height="20">
<linearGradient id="b" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
<mask id="a">
<rect width="137" height="20" rx="3" fill="#fff"/>
</mask>
<g mask="url(#a)">
<path fill="#555" d="M0 0h62v20H0z"/>
<path fill="#$color" d="M62 0h75v20H62z"/>
<path fill="url(#b)" d="M0 0h137v20H0z"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="31" y="15" fill="#010101" fill-opacity=".3">$text</text>
<text x="31" y="14">$text</text>
<text x="98.5" y="15" fill="#010101" fill-opacity=".3">$right</text>
<text x="98.5" y="14">$right</text>
</g>
</svg>
END;
echo $svg;
}
function showError($message) {
header('Content-type: image/svg+xml; charset=utf-8');
header('Cache-Control: no-cache, private');
header('Vary: Accept');
$etag = md5($message);
header('Etag: "' . $etag . '"');
$svg = <<<END
<svg xmlns="http://www.w3.org/2000/svg" width="137" height="20">
<mask id="a">
<rect width="137" height="20" rx="3" fill="#fff"/>
</mask>
<g mask="url(#a)">
<path fill="#555" d="M0 0h137v20H0z"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="68" y="15" fill="#010101" fill-opacity=".3">$message</text>
<text x="68" y="14">$message</text>
</g>
</svg>
END;
echo $svg;
}
function dbOrAPI($qid, $db) {
$sql = 'SELECT is_answered, favorite_count, answer_count, view_count, score, fetch_time, accepted_answer_id, question_id FROM cr_badge WHERE question_id = :qid;';
$stmt = $db->prepare($sql);
$result = $stmt->execute(array(':qid' => $qid));
if ($result) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$row) {
return fetchQuestion($qid, $db);
}
if (!$row['question_id']) {
return fetchQuestion($qid, $db);
}
$time = $row['fetch_time'];
if ($time < time() - 3600) { // if time was updated more than one hour ago
// fetch data again
fetchQuestion($qid, $db);
} else {
useData($row);
}
} else {
return showError($stmt->errorInfo());
}
}
if (isset($_GET['qid'])) {
$qid = $_GET['qid'];
try {
$db = new PDO($dbhostname, $dbuser, $dbpass);
dbOrAPI($qid, $db);
} catch (PDOException $e) {
showError($e->getMessage());
}
} else {
showError('No qid set');
}