|
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
|
|
|
'name' => [ |
|
131
|
|
|
'type' => 'string', |
|
132
|
|
|
'required' => false, |
|
133
|
|
|
'description' => 'search member' |
|
134
|
|
|
] |
|
135
|
|
|
], |
|
136
|
|
|
'retrieves a list of group members', |
|
137
|
|
|
'GET', |
|
138
|
|
|
false, |
|
139
|
|
|
false |
|
140
|
|
|
); |
|
141
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
// retrieves a list of group members with defined limit and offset |
|
145
|
|
|
function get_group_member_list($group_guid, $offset, $limit, $name) { |
|
146
|
|
|
$users = array(); |
|
147
|
|
|
error_log('name: ' . $name); |
|
148
|
|
|
$name_param = ($name != '') ? "AND ue.name LIKE '{$name}%' " : ''; |
|
149
|
|
|
|
|
150
|
|
|
$query = "SELECT ue.guid, r.time_created AS date_joined, ue.name, ue.username, ue.email |
|
151
|
|
|
FROM elggentity_relationships r |
|
152
|
|
|
LEFT JOIN elggusers_entity ue ON r.guid_one = ue.guid |
|
153
|
|
|
WHERE r.guid_two = {$group_guid} AND r.relationship = 'member' {$name_param} |
|
154
|
|
|
ORDER BY ue.name ASC LIMIT {$limit} OFFSET {$offset}"; |
|
155
|
|
|
|
|
156
|
|
|
$users = get_data($query); |
|
157
|
|
|
|
|
158
|
|
|
$site_url = elgg_get_site_url(); |
|
159
|
|
|
|
|
160
|
|
|
$query = "SELECT COUNT(*) AS member_count |
|
161
|
|
|
FROM elggentity_relationships r |
|
162
|
|
|
LEFT JOIN elggusers_entity ue ON r.guid_one = ue.guid |
|
163
|
|
|
WHERE r.guid_two = {$group_guid} AND r.relationship = 'member' {$name_param}"; |
|
164
|
|
|
|
|
165
|
|
|
$member_count = get_data($query); |
|
166
|
|
|
|
|
167
|
|
|
$arr['count'] = $member_count[0]->member_count; |
|
|
|
|
|
|
168
|
|
|
foreach ($users as $user) { |
|
169
|
|
|
$arr['members'][] = array ( |
|
170
|
|
|
'guid' => $user->guid, |
|
171
|
|
|
'name' => $user->name, |
|
172
|
|
|
'username' => $user->username, |
|
173
|
|
|
'email' => $user->email, |
|
174
|
|
|
'date_joined' => date("Y-m-d H:m:s", $user->date_joined), |
|
175
|
|
|
'url' => "{$site_url}profile/{$user->username}", |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
return $arr; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
function delete_updated_index_list($guids) { |
|
182
|
|
|
$ids = array(); |
|
183
|
|
|
foreach ($guids as $guid) { |
|
184
|
|
|
// guid must be an integer |
|
185
|
|
|
if (is_numeric($guid)) |
|
186
|
|
|
$ids[] = $guid; |
|
187
|
|
|
else |
|
188
|
|
|
return "there is an issue deleting a record in the database, please see function delete_updated_index_list()"; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
if (sizeof($ids) > 0) { |
|
192
|
|
|
$ids = implode(",", $ids); |
|
193
|
|
|
$query = "DELETE FROM deleted_object_tracker WHERE id IN ({$ids})"; |
|
194
|
|
|
$result = delete_data($query); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$arr[] = array('message' => 'deleted the following guid'); |
|
|
|
|
|
|
198
|
|
|
$arr[] = $ids; |
|
199
|
|
|
|
|
200
|
|
|
return $arr; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
|
|
204
|
|
|
// this is not an api call, this is a hook that will initiate once user attempts deletion |
|
205
|
|
|
function intercept_object_deletion($event, $type, $object) { |
|
206
|
|
|
$unixtime = time(); |
|
207
|
|
|
|
|
208
|
|
|
$query = "INSERT INTO deleted_object_tracker (id, time_deleted) VALUES ({$object->guid}, {$unixtime})"; |
|
209
|
|
|
|
|
210
|
|
|
// just in case for some reason, the user is able to delete the same thing twice |
|
211
|
|
|
try { |
|
212
|
|
|
$insert_record = insert_data($query); |
|
213
|
|
|
} catch (Exception $e) { |
|
214
|
|
|
error_log("error occurred: {$e}"); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
$query = "SELECT * FROM deleted_object_tracker"; |
|
218
|
|
|
$deleted_records = get_data($query); |
|
219
|
|
|
|
|
220
|
|
|
return true; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
function get_list_of_deleted_records() { |
|
225
|
|
|
$query = "SELECT * FROM deleted_object_tracker"; |
|
226
|
|
|
$deleted_records = get_data($query); |
|
227
|
|
|
|
|
228
|
|
|
foreach ($deleted_records as $deleted_record) { |
|
229
|
|
|
$arr[] = array( |
|
|
|
|
|
|
230
|
|
|
'guid' => $deleted_record->id, |
|
231
|
|
|
'time_deleted' => $deleted_record->time_deleted |
|
232
|
|
|
); |
|
233
|
|
|
} |
|
234
|
|
|
return $arr; |
|
|
|
|
|
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
|
|
238
|
|
|
function get_user_list($offset) { |
|
239
|
|
|
$site_url = elgg_get_site_url(); |
|
240
|
|
|
$db_prefix = elgg_get_config('dbprefix'); |
|
241
|
|
|
|
|
242
|
|
|
$query = " SELECT e.guid, ue.name, ue.username, ue.email, e.type, e.time_created, e.enabled |
|
243
|
|
|
FROM {$db_prefix}users_entity ue |
|
244
|
|
|
LEFT JOIN {$db_prefix}entities e ON ue.guid = e.guid |
|
245
|
|
|
WHERE e.enabled = 'yes' |
|
246
|
|
|
LIMIT 10 OFFSET {$offset}"; |
|
247
|
|
|
|
|
248
|
|
|
$users = get_data($query); |
|
249
|
|
|
|
|
250
|
|
|
foreach ($users as $user) { |
|
251
|
|
|
$arr[] = array ( |
|
|
|
|
|
|
252
|
|
|
'guid' => $user->guid, |
|
253
|
|
|
'name' => array('en' => $user->name, 'fr' => $user->name), |
|
254
|
|
|
'username' => $user->username, |
|
255
|
|
|
'email' => $user->email, |
|
256
|
|
|
'type' => $user->type, |
|
257
|
|
|
'date_created' => date("Y-m-d\TH:m:s\Z", $user->time_created), |
|
258
|
|
|
'date_modified' => date("Y-m-d\TH:m:s\Z", $user->time_created), |
|
259
|
|
|
'url' => "{$site_url}profile/{$user->username}" |
|
260
|
|
|
); |
|
261
|
|
|
} |
|
262
|
|
|
return $arr; |
|
|
|
|
|
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
|
|
266
|
|
|
function get_group_list($offset) { |
|
267
|
|
|
// @TODO use SQL query syntax instead of using function elgg_get_entities() |
|
268
|
|
|
$groups = elgg_get_entities(array( |
|
269
|
|
|
'type' => 'group', |
|
270
|
|
|
'limit' => 10, |
|
271
|
|
|
'offset' => $offset |
|
272
|
|
|
)); |
|
273
|
|
|
|
|
274
|
|
|
foreach ($groups as $group) { |
|
275
|
|
|
|
|
276
|
|
View Code Duplication |
if (is_Json($group->name)) { |
|
277
|
|
|
$name_array = json_decode($group->name, true); |
|
278
|
|
|
$name_array['en'] = str_replace('"', '\"', $name_array['en']); |
|
279
|
|
|
$name_array['fr'] = str_replace('"', '\"', $name_array['fr']); |
|
280
|
|
|
} else { |
|
281
|
|
|
$name_array['en'] = $group->name_array; |
|
|
|
|
|
|
282
|
|
|
$name_array['fr'] = $group->name; |
|
|
|
|
|
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
View Code Duplication |
if (is_Json($group->description)) { |
|
286
|
|
|
$description_array = json_decode($group->description, true); |
|
287
|
|
|
$description_array['en'] = str_replace('"', '\"', $description_array['en']); |
|
288
|
|
|
$description_array['fr'] = str_replace('"', '\"', $description_array['fr']); |
|
289
|
|
|
} else { |
|
290
|
|
|
$description_array['en'] = $group->description; |
|
|
|
|
|
|
291
|
|
|
$description_array['fr'] = $group->description; |
|
|
|
|
|
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
$arr[] = array( |
|
|
|
|
|
|
295
|
|
|
'guid' => $group->getGUID(), |
|
296
|
|
|
'name' => $name_array, |
|
297
|
|
|
'description' => $description_array, |
|
298
|
|
|
'type' => $group->getType(), |
|
299
|
|
|
'access_id' => $group->access_id, |
|
300
|
|
|
'date_created' => date("Y-m-d\TH:m:s\Z", $group->time_created), |
|
301
|
|
|
'date_modified' => date("Y-m-d\TH:m:s\Z", $group->time_updated), |
|
302
|
|
|
'url' => $group->getURL() |
|
303
|
|
|
); |
|
304
|
|
|
} |
|
305
|
|
|
return $arr; |
|
|
|
|
|
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
|
|
309
|
|
|
function get_entity_list($type, $subtype, $offset) { |
|
310
|
|
|
$entities = elgg_get_entities(array( |
|
311
|
|
|
'type' => $type, |
|
312
|
|
|
'subtype' => $subtype, |
|
313
|
|
|
'limit' => 10, |
|
314
|
|
|
'offset' => $offset |
|
315
|
|
|
)); |
|
316
|
|
|
|
|
317
|
|
|
foreach ($entities as $entity) { |
|
318
|
|
|
|
|
319
|
|
|
|
|
320
|
|
View Code Duplication |
if (is_Json($entity->title)) { |
|
321
|
|
|
$title_array = json_decode($entity->title, true); |
|
322
|
|
|
if (!isset($title_array['en']) || !isset($title_array['fr'])) { |
|
323
|
|
|
$title_array['en'] = str_replace('"', '\"', $title_array); |
|
324
|
|
|
$title_array['fr'] = str_replace('"', '\"', $title_array); |
|
325
|
|
|
} else { |
|
326
|
|
|
$title_array['en'] = str_replace('"', '\"', gc_explode_translation($entity->title, 'en')); |
|
327
|
|
|
$title_array['fr'] = str_replace('"', '\"', gc_explode_translation($entity->title, 'fr')); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
} else { |
|
331
|
|
|
$title_array['en'] = $entity->title; |
|
|
|
|
|
|
332
|
|
|
$title_array['fr'] = $entity->title; |
|
|
|
|
|
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
View Code Duplication |
if (is_Json($entity->description)) { |
|
336
|
|
|
$description_array = json_decode($entity->description, true); |
|
337
|
|
|
if (!isset($description_array['en']) || !isset($description_array['fr'])) { |
|
338
|
|
|
$description_array['en'] = str_replace('"', '\"', $description_array); |
|
339
|
|
|
$description_array['fr'] = str_replace('"', '\"', $description_array); |
|
340
|
|
|
} else { |
|
341
|
|
|
$description_array['en'] = str_replace('"', '\"', gc_explode_translation($entity->description, 'en')); |
|
342
|
|
|
$description_array['fr'] = str_replace('"', '\"', gc_explode_translation($entity->description, 'fr')); |
|
343
|
|
|
} |
|
344
|
|
|
} else { |
|
345
|
|
|
$description_array['en'] = $entity->description; |
|
|
|
|
|
|
346
|
|
|
$description_array['fr'] = $entity->description; |
|
|
|
|
|
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
$arr[] = array( |
|
|
|
|
|
|
350
|
|
|
'guid' => $entity->getGUID(), |
|
351
|
|
|
'title' => $title_array, |
|
352
|
|
|
'description' => $description_array, |
|
353
|
|
|
'type' => $entity->getType(), |
|
354
|
|
|
'subtype' => $entity->getSubtype(), |
|
355
|
|
|
'access_id' => $entity->access_id, |
|
356
|
|
|
'date_created' => date("Y-m-d\TH:m:s\Z", $entity->time_created), |
|
357
|
|
|
'date_modified' => date("Y-m-d\TH:m:s\Z", $entity->time_updated), |
|
358
|
|
|
'url' => $entity->getURL() |
|
359
|
|
|
); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
return $arr; |
|
|
|
|
|
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
|
|
366
|
|
|
// @TODO check if this function already exist in other modules |
|
367
|
|
|
function is_Json($string) { |
|
368
|
|
|
json_decode($string); |
|
369
|
|
|
return (json_last_error() == JSON_ERROR_NONE); |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
|
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.