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
|
|
|
|
112
|
|
|
function delete_updated_index_list($guids) { |
113
|
|
|
$ids = array(); |
114
|
|
|
foreach ($guids as $guid) { |
115
|
|
|
// guid must be an integer |
116
|
|
|
if (is_numeric($guid)) |
117
|
|
|
$ids[] = $guid; |
118
|
|
|
else |
119
|
|
|
return "there is an issue deleting a record in the database, please see function delete_updated_index_list()"; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (sizeof($ids) > 0) { |
123
|
|
|
$ids = implode(",", $ids); |
124
|
|
|
$query = "DELETE FROM deleted_object_tracker WHERE id IN ({$ids})"; |
125
|
|
|
$result = delete_data($query); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$arr[] = array('message' => 'deleted the following guid'); |
|
|
|
|
129
|
|
|
$arr[] = $ids; |
130
|
|
|
|
131
|
|
|
return $arr; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
// this is not an api call, this is a hook that will initiate once user attempts deletion |
136
|
|
|
function intercept_object_deletion($event, $type, $object) { |
137
|
|
|
$unixtime = time(); |
138
|
|
|
|
139
|
|
|
$query = "INSERT INTO deleted_object_tracker (id, time_deleted) VALUES ({$object->guid}, {$unixtime})"; |
140
|
|
|
|
141
|
|
|
// just in case for some reason, the user is able to delete the same thing twice |
142
|
|
|
try { |
143
|
|
|
$insert_record = insert_data($query); |
144
|
|
|
} catch (Exception $e) { |
145
|
|
|
error_log("error occurred: {$e}"); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$query = "SELECT * FROM deleted_object_tracker"; |
149
|
|
|
$deleted_records = get_data($query); |
150
|
|
|
|
151
|
|
|
return true; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
function get_list_of_deleted_records() { |
156
|
|
|
$query = "SELECT * FROM deleted_object_tracker"; |
157
|
|
|
$deleted_records = get_data($query); |
158
|
|
|
|
159
|
|
|
foreach ($deleted_records as $deleted_record) { |
160
|
|
|
$arr[] = array( |
|
|
|
|
161
|
|
|
'guid' => $deleted_record->id, |
162
|
|
|
'time_deleted' => $deleted_record->time_deleted |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
return $arr; |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
function get_user_list($offset) { |
170
|
|
|
// @TODO check if the site url is either localhost or local ip address, replace with elgg if it is |
171
|
|
|
$site_url = elgg_get_site_url(); |
172
|
|
|
$platform = explode('.', $site_url); |
173
|
|
|
$db_prefix = elgg_get_config('dbprefix'); |
174
|
|
|
|
175
|
|
|
$query = " SELECT e.guid, ue.name, ue.username, ue.email, e.type, e.time_created, e.enabled |
176
|
|
|
FROM {$db_prefix}users_entity ue |
177
|
|
|
LEFT JOIN {$db_prefix}entities e ON ue.guid = e.guid |
178
|
|
|
WHERE e.enabled = 'yes' |
179
|
|
|
LIMIT 10 OFFSET {$offset}"; |
180
|
|
|
|
181
|
|
|
$users = get_data($query); |
182
|
|
|
|
183
|
|
|
foreach ($users as $user) { |
184
|
|
|
$arr[] = array ( |
|
|
|
|
185
|
|
|
'guid' => $user->guid, |
186
|
|
|
'name' => array('en' => $user->name, 'fr' => $user->name), |
187
|
|
|
'username' => $user->username, |
188
|
|
|
'email' => $user->email, |
189
|
|
|
'type' => $user->type, |
190
|
|
|
'date_created' => date("Y-m-d\TH:m:s\Z", $user->time_created), |
191
|
|
|
'date_modified' => date("Y-m-d\TH:m:s\Z", $user->time_created), |
192
|
|
|
'url' => "{$site_url}profile/{$user->username}", |
193
|
|
|
'platform' => $platform[0] |
194
|
|
|
); |
195
|
|
|
} |
196
|
|
|
return $arr; |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
function get_group_list($offset) { |
201
|
|
|
// @TODO use SQL query syntax instead of using function elgg_get_entities() |
202
|
|
|
$site_url = elgg_get_site_url(); |
203
|
|
|
$platform = explode('.', $site_url); |
204
|
|
|
|
205
|
|
|
$groups = elgg_get_entities(array( |
206
|
|
|
'type' => 'group', |
207
|
|
|
'limit' => 10, |
208
|
|
|
'offset' => $offset |
209
|
|
|
)); |
210
|
|
|
|
211
|
|
|
foreach ($groups as $group) { |
212
|
|
|
|
213
|
|
View Code Duplication |
if (is_Json($group->name)) { |
214
|
|
|
$name_array = json_decode($group->name, true); |
215
|
|
|
$name_array['en'] = str_replace('"', '\"', $name_array['en']); |
216
|
|
|
$name_array['fr'] = str_replace('"', '\"', $name_array['fr']); |
217
|
|
|
} else { |
218
|
|
|
$name_array['en'] = $group->name_array; |
|
|
|
|
219
|
|
|
$name_array['fr'] = $group->name; |
|
|
|
|
220
|
|
|
} |
221
|
|
|
|
222
|
|
View Code Duplication |
if (is_Json($group->description)) { |
223
|
|
|
$description_array = json_decode($group->description, true); |
224
|
|
|
$description_array['en'] = str_replace('"', '\"', $description_array['en']); |
225
|
|
|
$description_array['fr'] = str_replace('"', '\"', $description_array['fr']); |
226
|
|
|
} else { |
227
|
|
|
$description_array['en'] = $group->description; |
|
|
|
|
228
|
|
|
$description_array['fr'] = $group->description; |
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$arr[] = array( |
|
|
|
|
232
|
|
|
'guid' => $group->getGUID(), |
233
|
|
|
'name' => $name_array, |
234
|
|
|
'description' => $description_array, |
235
|
|
|
'type' => $group->getType(), |
236
|
|
|
'access_id' => $group->access_id, |
237
|
|
|
'date_created' => date("Y-m-d\TH:m:s\Z", $group->time_created), |
238
|
|
|
'date_modified' => date("Y-m-d\TH:m:s\Z", $group->time_updated), |
239
|
|
|
'url' => $group->getURL(), |
240
|
|
|
'platform' => $platform[0] |
241
|
|
|
); |
242
|
|
|
} |
243
|
|
|
return $arr; |
|
|
|
|
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
|
247
|
|
|
function get_entity_list($type, $subtype, $offset) { |
248
|
|
|
$site_url = elgg_get_site_url(); |
249
|
|
|
$platform = explode('.', $site_url); |
250
|
|
|
$entities = elgg_get_entities(array( |
251
|
|
|
'type' => $type, |
252
|
|
|
'subtype' => $subtype, |
253
|
|
|
'limit' => 10, |
254
|
|
|
'offset' => $offset |
255
|
|
|
)); |
256
|
|
|
|
257
|
|
|
foreach ($entities as $entity) { |
258
|
|
|
|
259
|
|
View Code Duplication |
if (is_Json($entity->title)) { |
260
|
|
|
$title_array['en'] = str_replace('"', '\"', gc_explode_translation($entity->title, 'en')); |
|
|
|
|
261
|
|
|
$title_array['fr'] = str_replace('"', '\"', gc_explode_translation($entity->title, 'fr')); |
|
|
|
|
262
|
|
|
} else { |
263
|
|
|
$title_array['en'] = $entity->title; |
264
|
|
|
$title_array['fr'] = $entity->title; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
View Code Duplication |
if (is_Json($entity->description)) { |
268
|
|
|
$description_array['en'] = str_replace('"', '\"', gc_explode_translation($entity->description, 'en')); |
|
|
|
|
269
|
|
|
$description_array['fr'] = str_replace('"', '\"', gc_explode_translation($entity->description, 'fr')); |
|
|
|
|
270
|
|
|
} else { |
271
|
|
|
$description_array['en'] = $entity->description; |
272
|
|
|
$description_array['fr'] = $entity->description; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$arr[] = array( |
|
|
|
|
276
|
|
|
'guid' => $entity->getGUID(), |
277
|
|
|
'title' => $title_array, |
278
|
|
|
'description' => $description_array, |
279
|
|
|
'type' => $entity->getType(), |
280
|
|
|
'subtype' => $entity->getSubtype(), |
281
|
|
|
'access_id' => $entity->access_id, |
282
|
|
|
'date_created' => date("Y-m-d\TH:m:s\Z", $entity->time_created), |
283
|
|
|
'date_modified' => date("Y-m-d\TH:m:s\Z", $entity->time_updated), |
284
|
|
|
'url' => $entity->getURL(), |
285
|
|
|
'platform' => $platform[0] |
286
|
|
|
); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return $arr; |
|
|
|
|
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
|
293
|
|
|
// @TODO check if this function already exist in other modules |
294
|
|
|
function is_Json($string) { |
295
|
|
|
json_decode($string); |
296
|
|
|
return (json_last_error() == JSON_ERROR_NONE); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
|
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.