1
|
|
|
<?php |
2
|
|
|
/* File: .../solr_api/start.php |
3
|
|
|
* |
4
|
|
|
* REFERENCE: http://learn.elgg.org/en/stable/guides/web-services.html#api-authentication |
5
|
|
|
* To get the API Key, web services must be enabled: Admin > Web Services > Create API Keys |
6
|
|
|
* Please note that the API calls will display limit of 10 |
7
|
|
|
* |
8
|
|
|
* Author: Christine Yu <[email protected]> |
9
|
|
|
* Company: Government of Canada |
10
|
|
|
* Date: June 1st 2018 |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
elgg_register_event_handler('init','system','solr_api_init'); |
16
|
|
|
|
17
|
|
|
function solr_api_init() { |
18
|
|
|
|
19
|
|
|
//$method, $function, array $parameters = NULL, $description = "", |
20
|
|
|
// $call_method = "GET", $require_api_auth = false, $require_user_auth = false |
21
|
|
|
|
22
|
|
|
elgg_ws_expose_function( |
23
|
|
|
'delete.updated_index_list', |
24
|
|
|
'delete_updated_index_list', |
25
|
|
|
[ |
26
|
|
|
'guids' => [ |
27
|
|
|
'type' => 'array', |
28
|
|
|
'required' => false, |
29
|
|
|
'description' => 'deletes records based on collection of entity guids' |
30
|
|
|
] |
31
|
|
|
], |
32
|
|
|
'deletes record that already updated the solr index', |
33
|
|
|
'POST', |
34
|
|
|
true, |
35
|
|
|
false |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
elgg_ws_expose_function( |
40
|
|
|
'get.entity_list', |
41
|
|
|
'get_entity_list', |
42
|
|
|
[ |
43
|
|
|
'type' => [ |
44
|
|
|
'type' => 'string', |
45
|
|
|
'required' => true, |
46
|
|
|
'description' => 'the type of entity in string format', |
47
|
|
|
], |
48
|
|
|
'subtype' => [ |
49
|
|
|
'type' => 'string', |
50
|
|
|
'required' => false, |
51
|
|
|
'description' => 'the subtype of entity in string format, not required', |
52
|
|
|
], |
53
|
|
|
'offset' => [ |
54
|
|
|
'type' => 'int', |
55
|
|
|
'required' => true, |
56
|
|
|
'description' => 'the subtype of entity in string format, not required', |
57
|
|
|
], |
58
|
|
|
|
59
|
|
|
], |
60
|
|
|
'retrieves all entities filtered by type [and subtype]', |
61
|
|
|
'GET', |
62
|
|
|
true, |
63
|
|
|
false |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
elgg_ws_expose_function( |
68
|
|
|
'get.user_list', |
69
|
|
|
'get_user_list', |
70
|
|
|
[ |
71
|
|
|
'offset' => [ |
72
|
|
|
'type' => 'int', |
73
|
|
|
'required' => true, |
74
|
|
|
'description' => 'paging mechanism' |
75
|
|
|
] |
76
|
|
|
], |
77
|
|
|
'retrieves a user list', |
78
|
|
|
'GET', |
79
|
|
|
true, |
80
|
|
|
false |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
elgg_ws_expose_function( |
84
|
|
|
'get.group_list', |
85
|
|
|
'get_group_list', |
86
|
|
|
[ |
87
|
|
|
'offset' => [ |
88
|
|
|
'type' => 'int', |
89
|
|
|
'required' => true, |
90
|
|
|
'description' => 'api loads 20 groups at a time' |
91
|
|
|
] |
92
|
|
|
], |
93
|
|
|
'retrieves a group list', |
94
|
|
|
'GET', |
95
|
|
|
true, |
96
|
|
|
false |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
elgg_ws_expose_function( |
101
|
|
|
'get.list_of_deleted_records', |
102
|
|
|
'get_list_of_deleted_records', |
103
|
|
|
null, |
104
|
|
|
'retrieves a list of deleted content', |
105
|
|
|
'GET', |
106
|
|
|
true, |
107
|
|
|
false |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
elgg_ws_expose_function( |
112
|
|
|
'get.group_member_list', |
113
|
|
|
'get_group_member_list', |
114
|
|
|
[ |
115
|
|
|
'group_guid' => [ |
116
|
|
|
'type' => 'int', |
117
|
|
|
'required' => true, |
118
|
|
|
'description' => 'group guid' |
119
|
|
|
], |
120
|
|
|
'offset' => [ |
121
|
|
|
'type' => 'int', |
122
|
|
|
'required' => true, |
123
|
|
|
'description' => 'offset for pagination' |
124
|
|
|
], |
125
|
|
|
'limit' => [ |
126
|
|
|
'type' => 'int', |
127
|
|
|
'required' => true, |
128
|
|
|
'description' => 'limit number of results' |
129
|
|
|
] |
130
|
|
|
], |
131
|
|
|
'retrieves a list of group members', |
132
|
|
|
'GET', |
133
|
|
|
false, |
134
|
|
|
false |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// retrieves a list of group members with defined limit and offset |
140
|
|
|
function get_group_member_list($group_guid, $offset, $limit) { |
141
|
|
|
$users = array(); |
142
|
|
|
|
143
|
|
|
$query = "SELECT ue.guid, r.time_created AS date_joined, ue.name, ue.username, ue.email |
144
|
|
|
FROM elggentity_relationships r |
145
|
|
|
LEFT JOIN elggusers_entity ue ON r.guid_one = ue.guid |
146
|
|
|
WHERE r.guid_two = {$group_guid} AND r.relationship = 'member' |
147
|
|
|
LIMIT {$limit} OFFSET {$offset}"; |
148
|
|
|
|
149
|
|
|
$users = get_data($query); |
150
|
|
|
|
151
|
|
|
$site_url = elgg_get_site_url(); |
152
|
|
|
|
153
|
|
|
$query = "SELECT COUNT(*) AS member_count |
154
|
|
|
FROM elggentity_relationships r |
155
|
|
|
LEFT JOIN elggusers_entity ue ON r.guid_one = ue.guid |
156
|
|
|
WHERE r.guid_two = {$group_guid} AND r.relationship = 'member'"; |
157
|
|
|
|
158
|
|
|
$member_count = get_data($query); |
159
|
|
|
|
160
|
|
|
$arr['count'] = $member_count[0]->member_count; |
|
|
|
|
161
|
|
|
foreach ($users as $user) { |
162
|
|
|
$arr['members'][] = array ( |
163
|
|
|
'guid' => $user->guid, |
164
|
|
|
'name' => $user->name, |
165
|
|
|
'username' => $user->username, |
166
|
|
|
'email' => $user->email, |
167
|
|
|
'date_joined' => date("Y-m-d\TH:m:s\Z", $user->date_joined), |
168
|
|
|
'url' => "{$site_url}profile/{$user->username}", |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
return $arr; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
function delete_updated_index_list($guids) { |
175
|
|
|
$ids = array(); |
176
|
|
|
foreach ($guids as $guid) { |
177
|
|
|
// guid must be an integer |
178
|
|
|
if (is_numeric($guid)) |
179
|
|
|
$ids[] = $guid; |
180
|
|
|
else |
181
|
|
|
return "there is an issue deleting a record in the database, please see function delete_updated_index_list()"; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
if (sizeof($ids) > 0) { |
185
|
|
|
$ids = implode(",", $ids); |
186
|
|
|
$query = "DELETE FROM deleted_object_tracker WHERE id IN ({$ids})"; |
187
|
|
|
$result = delete_data($query); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$arr[] = array('message' => 'deleted the following guid'); |
|
|
|
|
191
|
|
|
$arr[] = $ids; |
192
|
|
|
|
193
|
|
|
return $arr; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
// this is not an api call, this is a hook that will initiate once user attempts deletion |
198
|
|
|
function intercept_object_deletion($event, $type, $object) { |
199
|
|
|
$unixtime = time(); |
200
|
|
|
|
201
|
|
|
$query = "INSERT INTO deleted_object_tracker (id, time_deleted) VALUES ({$object->guid}, {$unixtime})"; |
202
|
|
|
|
203
|
|
|
// just in case for some reason, the user is able to delete the same thing twice |
204
|
|
|
try { |
205
|
|
|
$insert_record = insert_data($query); |
206
|
|
|
} catch (Exception $e) { |
207
|
|
|
error_log("error occurred: {$e}"); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$query = "SELECT * FROM deleted_object_tracker"; |
211
|
|
|
$deleted_records = get_data($query); |
212
|
|
|
|
213
|
|
|
return true; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
function get_list_of_deleted_records() { |
218
|
|
|
$query = "SELECT * FROM deleted_object_tracker"; |
219
|
|
|
$deleted_records = get_data($query); |
220
|
|
|
|
221
|
|
|
foreach ($deleted_records as $deleted_record) { |
222
|
|
|
$arr[] = array( |
|
|
|
|
223
|
|
|
'guid' => $deleted_record->id, |
224
|
|
|
'time_deleted' => $deleted_record->time_deleted |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
return $arr; |
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
function get_user_list($offset) { |
232
|
|
|
$site_url = elgg_get_site_url(); |
233
|
|
|
$db_prefix = elgg_get_config('dbprefix'); |
234
|
|
|
|
235
|
|
|
$query = " SELECT e.guid, ue.name, ue.username, ue.email, e.type, e.time_created, e.enabled |
236
|
|
|
FROM {$db_prefix}users_entity ue |
237
|
|
|
LEFT JOIN {$db_prefix}entities e ON ue.guid = e.guid |
238
|
|
|
WHERE e.enabled = 'yes' |
239
|
|
|
LIMIT 10 OFFSET {$offset}"; |
240
|
|
|
|
241
|
|
|
$users = get_data($query); |
242
|
|
|
|
243
|
|
|
foreach ($users as $user) { |
244
|
|
|
$arr[] = array ( |
|
|
|
|
245
|
|
|
'guid' => $user->guid, |
246
|
|
|
'name' => array('en' => $user->name, 'fr' => $user->name), |
247
|
|
|
'username' => $user->username, |
248
|
|
|
'email' => $user->email, |
249
|
|
|
'type' => $user->type, |
250
|
|
|
'date_created' => date("Y-m-d\TH:m:s\Z", $user->time_created), |
251
|
|
|
'date_modified' => date("Y-m-d\TH:m:s\Z", $user->time_created), |
252
|
|
|
'url' => "{$site_url}profile/{$user->username}" |
253
|
|
|
); |
254
|
|
|
} |
255
|
|
|
return $arr; |
|
|
|
|
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
|
259
|
|
|
function get_group_list($offset) { |
260
|
|
|
// @TODO use SQL query syntax instead of using function elgg_get_entities() |
261
|
|
|
$groups = elgg_get_entities(array( |
262
|
|
|
'type' => 'group', |
263
|
|
|
'limit' => 10, |
264
|
|
|
'offset' => $offset |
265
|
|
|
)); |
266
|
|
|
|
267
|
|
|
foreach ($groups as $group) { |
268
|
|
|
|
269
|
|
View Code Duplication |
if (is_Json($group->name)) { |
270
|
|
|
$name_array = json_decode($group->name, true); |
271
|
|
|
$name_array['en'] = str_replace('"', '\"', $name_array['en']); |
272
|
|
|
$name_array['fr'] = str_replace('"', '\"', $name_array['fr']); |
273
|
|
|
} else { |
274
|
|
|
$name_array['en'] = $group->name_array; |
|
|
|
|
275
|
|
|
$name_array['fr'] = $group->name; |
|
|
|
|
276
|
|
|
} |
277
|
|
|
|
278
|
|
View Code Duplication |
if (is_Json($group->description)) { |
279
|
|
|
$description_array = json_decode($group->description, true); |
280
|
|
|
$description_array['en'] = str_replace('"', '\"', $description_array['en']); |
281
|
|
|
$description_array['fr'] = str_replace('"', '\"', $description_array['fr']); |
282
|
|
|
} else { |
283
|
|
|
$description_array['en'] = $group->description; |
|
|
|
|
284
|
|
|
$description_array['fr'] = $group->description; |
|
|
|
|
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
$arr[] = array( |
|
|
|
|
288
|
|
|
'guid' => $group->getGUID(), |
289
|
|
|
'name' => $name_array, |
290
|
|
|
'description' => $description_array, |
291
|
|
|
'type' => $group->getType(), |
292
|
|
|
'access_id' => $group->access_id, |
293
|
|
|
'date_created' => date("Y-m-d\TH:m:s\Z", $group->time_created), |
294
|
|
|
'date_modified' => date("Y-m-d\TH:m:s\Z", $group->time_updated), |
295
|
|
|
'url' => $group->getURL() |
296
|
|
|
); |
297
|
|
|
} |
298
|
|
|
return $arr; |
|
|
|
|
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
|
302
|
|
|
function get_entity_list($type, $subtype, $offset) { |
303
|
|
|
$entities = elgg_get_entities(array( |
304
|
|
|
'type' => $type, |
305
|
|
|
'subtype' => $subtype, |
306
|
|
|
'limit' => 10, |
307
|
|
|
'offset' => $offset |
308
|
|
|
)); |
309
|
|
|
|
310
|
|
|
foreach ($entities as $entity) { |
311
|
|
|
|
312
|
|
|
|
313
|
|
View Code Duplication |
if (is_Json($entity->title)) { |
314
|
|
|
$title_array = json_decode($entity->title, true); |
315
|
|
|
if (!isset($title_array['en']) || !isset($title_array['fr'])) { |
316
|
|
|
$title_array['en'] = str_replace('"', '\"', $title_array); |
317
|
|
|
$title_array['fr'] = str_replace('"', '\"', $title_array); |
318
|
|
|
} else { |
319
|
|
|
$title_array['en'] = str_replace('"', '\"', gc_explode_translation($entity->title, 'en')); |
320
|
|
|
$title_array['fr'] = str_replace('"', '\"', gc_explode_translation($entity->title, 'fr')); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
} else { |
324
|
|
|
$title_array['en'] = $entity->title; |
|
|
|
|
325
|
|
|
$title_array['fr'] = $entity->title; |
|
|
|
|
326
|
|
|
} |
327
|
|
|
|
328
|
|
View Code Duplication |
if (is_Json($entity->description)) { |
329
|
|
|
$description_array = json_decode($entity->description, true); |
330
|
|
|
if (!isset($description_array['en']) || !isset($description_array['fr'])) { |
331
|
|
|
$description_array['en'] = str_replace('"', '\"', $description_array); |
332
|
|
|
$description_array['fr'] = str_replace('"', '\"', $description_array); |
333
|
|
|
} else { |
334
|
|
|
$description_array['en'] = str_replace('"', '\"', gc_explode_translation($entity->description, 'en')); |
335
|
|
|
$description_array['fr'] = str_replace('"', '\"', gc_explode_translation($entity->description, 'fr')); |
336
|
|
|
} |
337
|
|
|
} else { |
338
|
|
|
$description_array['en'] = $entity->description; |
|
|
|
|
339
|
|
|
$description_array['fr'] = $entity->description; |
|
|
|
|
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
$arr[] = array( |
|
|
|
|
343
|
|
|
'guid' => $entity->getGUID(), |
344
|
|
|
'title' => $title_array, |
345
|
|
|
'description' => $description_array, |
346
|
|
|
'type' => $entity->getType(), |
347
|
|
|
'subtype' => $entity->getSubtype(), |
348
|
|
|
'access_id' => $entity->access_id, |
349
|
|
|
'date_created' => date("Y-m-d\TH:m:s\Z", $entity->time_created), |
350
|
|
|
'date_modified' => date("Y-m-d\TH:m:s\Z", $entity->time_updated), |
351
|
|
|
'url' => $entity->getURL() |
352
|
|
|
); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
return $arr; |
|
|
|
|
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
|
359
|
|
|
// @TODO check if this function already exist in other modules |
360
|
|
|
function is_Json($string) { |
361
|
|
|
json_decode($string); |
362
|
|
|
return (json_last_error() == JSON_ERROR_NONE); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.