-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpgd.db.govp.php
371 lines (303 loc) · 10.5 KB
/
wpgd.db.govp.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
<?php /* -*- Mode: php; c-basic-offset:4; -*- */
/* Copyright (C) 2011 Governo do Estado do Rio Grande do Sul
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
function _wpgd_enter_encoding() {
global $wpdb;
if (mysql_client_encoding($wpdb->dbh) == 'utf8') {
mysql_set_charset("latin1", $wpdb->dbh);
}
}
function _wpgd_leave_encoding() {
global $wpdb;
mysql_set_charset("utf8", $wpdb->dbh);
}
function _wpgd_get_contrib_results($sql, $argp = array()) {
global $wpdb;
_wpgd_enter_encoding();
$results = $wpdb->get_results($wpdb->prepare($sql,$argp), ARRAY_A);
_wpgd_leave_encoding();
return $results;
}
function _wpgd_get_contrib_row($sql, $argp = array()) {
global $wpdb;
_wpgd_enter_encoding();
$ret = $wpdb->get_row($wpdb->prepare($sql,$argp), ARRAY_A);
_wpgd_leave_encoding();
return $ret;
}
function wpgd_db_get_contribs($sortby = 'contrib.id',
$page = '0',
$perpage = WPGD_CONTRIBS_PER_PAGE,
$theme = null,
$status = null,
$s = null,
$filter = null) {
$offset = $page * $perpage;
$sortfields = array(
'id' => 'contrib.id' ,
'status' => 'contrib.status',
'theme' => 'contrib.theme',
'date' => 'contrib.creation_date',
'author' => 'user.display_name',
'title' => 'contrib.title'
);
if (isset($sortfields[$sortby])) {
$sortfield = $sortfields[$sortby];
} else {
$sortfield = 'contrib.id';
}
function index_of($arr, $id) {
$f = array_filter($arr, function($x) use ($id) {
return $x['id'] == $id;
});
if (count($f) == 0) {
return -1;
} else {
return key($f);
}
}
/* Handling filters */
$filter = $filter ? " AND ($filter) " : "";
$filters = array();
if ($theme) {
array_push($filters, "contrib.theme = '$theme'");
}
if ($status) {
array_push($filters, "contrib.status = $status");
}
if (count($filters) > 0) {
$filter .= "AND (" . join(" AND ", $filters) . ")";
}
/* Handling the text search */
$search = "";
if ($s) {
$s = '%%' . $s . '%%';
$search = "AND (title LIKE '$s' OR content LIKE '$s')";
}
global $wpdb;
$sql_head = "
SELECT
contrib.id, contrib.title, contrib.content, contrib.creation_date,
contrib.theme, contrib.original, contrib.status, user.display_name,
contrib.parent, contrib.moderation, user.ID as user_id
";
$sql_base ="
FROM
contrib, wp_users user
WHERE
(contrib.user_id=user.ID AND contrib.enabled=1) $filter $search
ORDER BY $sortfield
";
$sql_main = $sql_head . $sql_base . "LIMIT $offset, $perpage";
$sql_count = "SELECT COUNT(contrib.id) AS total " . $sql_base;
$results = _wpgd_get_contrib_results($sql_main);
$count = $wpdb->get_var($wpdb->prepare($sql_count));
return array($results, $count);
}
function wpgd_db_get_unique_contribs($sortby
, $page
, $perpage
, $theme
, $status
, $s) {
return wpgd_db_get_contribs($sortby
, $page
, $perpage
, $theme
, $status
, $s
, "contrib.parent=0");
}
function wpgd_db_get_contrib($id) {
global $wpdb;
$sql = "
SELECT c.id, c.title, c.content, c.creation_date, c.theme, c.original,
c.status, u.display_name, c.parent, c.moderation
FROM contrib c, wp_users u
WHERE c.user_id=u.ID AND c.enabled=true AND c.id=%d";
return _wpgd_get_contrib_row($sql, array($id));
}
function wpgd_db_get_contrib_count() {
global $wpdb;
$sql = "SELECT count(id) FROM contrib WHERE enabled=1 ";
return $wpdb->get_var($wpdb->prepare($sql));
}
function wpgd_db_get_contrib_count_grouped_by_date() {
global $wpdb;
$sql = "SELECT
year(c.creation_date) AS year,
month(c.creation_date) AS month,
day(c.creation_date) AS day,
date(c.creation_date) AS date,
count(c.id) AS count
FROM contrib AS c GROUP BY DATE(c.creation_date);";
return $wpdb->get_results($wpdb->prepare($sql), ARRAY_A);
}
function wpgd_db_get_contrib_count_grouped_by_theme() {
global $wpdb;
$sql = "SELECT
c.theme, count(c.id) AS count FROM contrib AS c
GROUP BY c.theme;";
return $wpdb->get_results($wpdb->prepare($sql), ARRAY_A);
}
function wpgd_db_get_contrib_count_grouped_by_themedate() {
global $wpdb;
$sql = "SELECT
c.theme,
date(c.creation_date) AS date,
count(c.id) AS count,
year(c.creation_date) AS year,
month(c.creation_date) AS month,
day(c.creation_date) AS day,
date(c.creation_date) AS date
FROM contrib AS c GROUP BY c.theme, date(c.creation_date);";
return $wpdb->get_results($wpdb->prepare($sql), ARRAY_A);
}
function wpgd_db_get_theme_counts() {
global $wpdb;
$ret = array();
$sql = "SELECT COUNT(c.id) count, theme FROM contrib c GROUP BY c.theme";
foreach ($wpdb->get_results($wpdb->prepare($sql), ARRAY_A) as $row) {
$ret[$row['theme']] = $row['count'];
}
return $ret;
}
//contrib functions
function wpgd_contrib_has_duplicates($contrib) {
global $wpdb;
if ($contrib['parent'] > 0) return true;
$sql = "SELECT COUNT(*)
FROM contrib
WHERE parent=%d AND enabled=1";
return $wpdb->get_var($wpdb->prepare($sql, $contrib['id'])) > 0;
}
function wpgd_contrib_get_duplicates($contrib) {
global $wpdb;
$sql = "SELECT * FROM contrib WHERE parent=%d AND enabled=1";
return _wpgd_get_contrib_results($wpdb->prepare($sql, $contrib['id']));
}
function wpgd_contrib_get_all_duplicates($contrib) {
global $wpdb;
/*
the $parent contrib
+ contribs with same parent <> 0 (ie. siblings)
+ every other contrib with parent = $parent <> 0
+ every child contrib (everyone whose parent=$id)
- self
*/
$sql = "SELECT *
FROM contrib
WHERE (
id=${contrib[parent]}
OR
( parent=${contrib[parent]}
AND parent <> 0)
OR
parent=${contrib[id]})
AND id<>${contrib[id]}
AND enabled=1";
return _wpgd_get_contrib_results($sql);
}
function wpgd_contrib_get_dup_parent($contrib) {
global $wpdb;
if ($contrib['parent'] == 0)
return null;
$sql = "SELECT * FROM contrib WHERE id=%d AND enabled=1";
return _wpgd_get_contrib_row($sql, $contrib['parent']);
}
function wpgd_contrib_get_owner($contrib) {
return get_userdata($contrib['user_id']);
}
function wpgd_contrib_get_authors($contrib) {
function push_unique($users, $user) {
foreach($users as $u) {
if ($u->ID == $user->ID) return;
}
array_push($users, $user);
}
$dups = wpgd_contrib_get_duplicates($contrib);
$ret = array(get_userdata($contrib['user_id']));
foreach($dups as $c) {
push_unique($ret, get_userdata($c['user_id']));
}
return $ret;
}
/* -- Children API -- */
function wpgd_contrib_append_part($contrib, $child) {
global $wpdb;
$wpdb->insert(
"contrib_children__contrib",
array("inverse_id" => $contrib['id'],
"children_id" => $child['id'])
);
}
function wpgd_contrib_remove_part($contrib, $child) {
global $wpdb;
$sql = "DELETE FROM contrib_children__contrib WHERE
contrib_children__contrib.inverse_id = ${contrib[id]} AND
contrib_children__contrib.children_id = ${child[id]};";
$wpdb->query($wpdb->prepare($sql));
}
function wpgd_contrib_remove_all_parts($contrib) {
global $wpdb;
$sql = "DELETE FROM contrib_children__contrib WHERE
contrib_children__contrib.inverse_id = ${contrib[id]};";
$wpdb->query($wpdb->prepare($sql));
}
function wpgd_contrib_has_children($contrib) {
global $wpdb;
$sql = "SELECT count(*) FROM contrib, contrib_children__contrib
WHERE
contrib_children__contrib.inverse_id = ${contrib[id]} AND
contrib_children__contrib.children_id = contrib.id";
return $wpdb->get_var($wpdb->prepare($sql)) > 0;
}
function wpgd_contrib_get_children($contrib) {
global $wpdb;
$sql = "SELECT * FROM contrib, contrib_children__contrib
WHERE
contrib_children__contrib.inverse_id = ${contrib[id]} AND
contrib_children__contrib.children_id = contrib.id";
return _wpgd_get_contrib_results($sql);
}
function wpgd_contrib_get_parents($contrib) {
global $wpdb;
$sql = "SELECT * FROM contrib, contrib_children__contrib
WHERE
contrib_children__contrib.children_id = ${contrib[id]} AND
contrib_children__contrib.inverse_id = contrib.id";
return _wpgd_get_contrib_results($sql);
}
function wpgd_db_get_contribs_sorted_by_score(
$page = '0'
, $perpage = WPGD_CONTRIBS_PER_PAGE) {
list($scores, $count, $total_votes) = wpgd_pairwise_get_sorted_by_score($page-1, $perpage);
$contribs = array();
foreach($scores as $sdata) {
$json = json_decode($sdata['data']);
$contrib = wpgd_db_get_contrib($json->id);
$contrib['score'] = $sdata['score'];
$contrib['votes'] = $sdata['votes'];
$contrib['won'] = $sdata['won'];
$contrib['lost'] = $sdata['lost'];
$contribs[] = $contrib;
}
return array($contribs, $count, $total_votes);
}
function wpgd_db_get_vote_count_grouped_by_date() {
return wpgd_pairwise_get_vote_count_grouped_by_date();
}
?>