-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.php
284 lines (233 loc) · 6.42 KB
/
plugin.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
<?php
/*
* Plugin Name: mith-research-explorer-data
* Plugin URI: https://github.com/mith-umd/mith-research-explorer-data/
* Description: MITH Research Explorer Data
* Version: 0.1
* Author: Ed Summers
* License: MIT
*/
require(dirname(__FILE__) . '/../../../wp-config.php');
function projects() {
global $wpdb;
$people = people();
$q = "
SELECT
wp_posts.id AS id,
wp_posts.post_title AS title,
wp_posts.post_name AS slug,
wp_posts.post_excerpt AS description
FROM wp_posts
WHERE wp_posts.post_type = 'mith_research'
AND wp_posts.post_status = 'publish'
";
foreach ($wpdb->get_results($q, ARRAY_A) as $project) {
$project = $project + project_metadata($project["id"], $people);
$project = $project + project_terms($project["id"]);
$projects[] = $project;
}
return $projects;
}
function project_metadata($project_id, $people) {
global $wpdb;
$q = "
SELECT meta_key, meta_value
FROM wp_postmeta
WHERE post_id = %d
";
$start_year = null;
$start_month = "";
$end_year = null;
$end_month = "";
$active = false;
$results = array();
$project_people = array();
$links = array();
foreach ($wpdb->get_results($wpdb->prepare($q, $project_id), ARRAY_A) as $r) {
$k = $r["meta_key"];
$v = $r["meta_value"];
if (! $v or $v == "null") {
continue;
}
if (preg_match('/^research_people_(int_\d+)_research_person$/', $k, $m)) {
$project_people[$m[1]] = array(
"name" => $people[$v],
"affiliation" => "University of Maryland"
);
} else if (preg_match('/^research_people_(ext_\d+)_research_person_(name|affiliation|department)/', $k, $m)) {
if (! $project_people[$m[1]]) {
$project_people[$m[1]] = array("name" => null, "affiliation" => "University of Maryland");
}
$project_people[$m[1]][$m[2]] = $v;
} else if ($k == "_thumbnail_id") {
$results["thumbnail"] = get_thumbnail($v);
} else if ($k == "research_website_url") {
$results["website"] = "//" . $v;
} else if (preg_match('/^research_links_ext_(\d+)_research_link_(url|title)$/', $k, $m)) {
if (! $links[$m[1]]) {
$links[$m[1]] = array("title" => "Website");
}
$links[$m[1]][$m[2]] = $v;
} else if ($k == "research_start_mth") {
$start_month = $v;
} else if ($k == "research_start_yr") {
$start_year = $v;
} else if ($k == "research_end_mth") {
$end_month = $v;
} else if ($k == "research_end_yr") {
$end_year = $v;
} else if ($k == "research_status_active" and $v == 1) {
$active = true;
}
}
if ($start_year) {
if ($start_month != "") {
$results["start"] = sprintf("%s-%s-01", $start_year, $start_month);
}
else {
$results["start"] = $start_year;
}
} else {
$results["start"] = null;
}
if ($end_year) {
if ($end_month != "") {
$results["end"] = sprintf("%s-%s-01", $end_year, $end_month);
}
else {
$results["end"] = $end_year;
}
} else {
$results["end"] = null;
}
$results["member"] = array_values($project_people);
$results["link"] = array_values($links);
$results["active"] = $active;
return $results;
}
function project_terms($project_id) {
global $wpdb;
$q = "
SELECT wp_terms.name AS name,
wp_term_taxonomy.taxonomy AS taxonomy
FROM wp_term_relationships, wp_term_taxonomy, wp_terms
WHERE
wp_term_relationships.object_id = %s
AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.term_id = wp_terms.term_id
";
$results = array();
foreach ($wpdb->get_results($wpdb->prepare($q, $project_id), ARRAY_A) as $r) {
preg_match('/^mith_(.+)/', $r['taxonomy'], $m);
if ($m[1]) {
$results[$m[1]][] = $r['name'];
}
}
return $results;
}
function people() {
global $wpdb;
$q = "
SELECT id, post_title AS name
FROM wp_posts
WHERE post_type = 'mith_person'
";
$people = array();
foreach ($wpdb->get_results($q, ARRAY_A) as $p) {
$people[$p["id"]] = $p["name"];
}
return $people;
}
function get_thumbnail($thumb_id) {
global $wpdb;
$q = "
SELECT guid
FROM wp_posts
WHERE post_type = 'attachment'
AND id = %d
";
$url = $wpdb->get_var($wpdb->prepare($q, $thumb_id));
return preg_replace('/^http:/', 'https:', $url);
}
function topics() {
$t = taxonomy('mith_topic');
$t[] = array(
"name" => "Other",
"slug" => "other",
"broader" => [],
"narrower" => []
);
return $t;
}
function types() {
$t = taxonomy('mith_research_type');
$t[] = array(
"name" => "Other",
"slug" => "other",
"broader" => [],
"narrower" => []
);
return $t;
}
function taxonomy($taxonomy) {
global $wpdb;
$q = "
SELECT wp_terms.term_id, wp_terms.name, wp_terms.slug
FROM wp_terms, wp_term_taxonomy
WHERE
wp_terms.term_id = wp_term_taxonomy.term_id
AND wp_term_taxonomy.taxonomy IN (%s)
";
$terms = array();
foreach ($wpdb->get_results($wpdb->prepare($q, $taxonomy), ARRAY_A) as $term) {
$terms[$term["name"]] = array(
"slug" => $term["slug"],
"broader" => array(),
"narrower" => array()
);
}
$q = "
SELECT wp_term_taxonomy.description AS description,
terms.name AS term_name,
parent_terms.name AS parent_term
FROM wp_term_taxonomy
JOIN wp_terms AS terms ON wp_term_taxonomy.term_id = terms.term_id
LEFT JOIN wp_terms AS parent_terms ON wp_term_taxonomy.parent = parent_terms.term_id
";
foreach ($wpdb->get_results($q, ARRAY_A) as $taxon) {
$term_name = $taxon["term_name"];
if (! array_key_exists($term_name, $terms)) {
continue;
}
if ($taxon["parent_term"]) {
$broader = $taxon["parent_term"];
$terms[$term_name]["broader"][] = $broader;
$terms[$broader]["narrower"][] = $taxon["term_name"];
}
}
$term_list = [];
foreach ($terms as $name => $term) {
$term['name'] = $name;
$term_list[] = $term;
}
return $term_list;
}
function main() {
$action = $_REQUEST["action"];
if ($action == "topics") {
$data = topics();
} else if ($action == "types") {
$data = types();
} else if ($action == "people") {
$data = people();
} else {
$data = projects();
}
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
echo json_encode($data);
}
if (!count(debug_backtrace())) {
main();
}
?>