1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// REFERENCE: http://learn.elgg.org/en/stable/guides/web-services.html#api-authentication |
4
|
|
|
// Admin > Web Services > Create API Keys |
5
|
|
|
|
6
|
|
|
elgg_register_event_handler('init','system','solr_api_init'); |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
function your_plugin_auth_handler($credentials) { |
10
|
|
|
error_log("credentials..."); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
function api_hook_handler() { |
14
|
|
|
return "hey hey"; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
function solr_api_init() { |
18
|
|
|
|
19
|
|
|
elgg_register_event_handler('delete', 'object', 'intercept_object_deletion'); |
20
|
|
|
|
21
|
|
|
// Register the authentication handler |
22
|
|
|
//register_pam_handler('your_plugin_auth_handler'); |
23
|
|
|
//elgg_register_plugin_hook_handler('rest:output', 'get_entity_list', 'api_hook_handler'); |
24
|
|
|
|
25
|
|
|
// @TODO limit access to only the specified user agent string for more strict security |
26
|
|
|
// @TODO will need authentication mechanism for this |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
//$method, $function, array $parameters = NULL, $description = "", |
30
|
|
|
// $call_method = "GET", $require_api_auth = false, $require_user_auth = false |
31
|
|
|
|
32
|
|
|
elgg_ws_expose_function( |
33
|
|
|
'delete.updated_index_list', |
34
|
|
|
'delete_updated_index_list', |
35
|
|
|
[ |
36
|
|
|
'guids' => [ |
37
|
|
|
'type' => 'array', |
38
|
|
|
'required' => false, |
39
|
|
|
'description' => 'deletes records based on collection of entity guids' |
40
|
|
|
] |
41
|
|
|
], |
42
|
|
|
'deletes record that already updated the solr index', |
43
|
|
|
'POST', |
44
|
|
|
true, |
45
|
|
|
false |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
elgg_ws_expose_function( |
50
|
|
|
'get.entity_list', |
51
|
|
|
'get_entity_list', |
52
|
|
|
[ |
53
|
|
|
'type' => [ |
54
|
|
|
'type' => 'string', |
55
|
|
|
'required' => true, |
56
|
|
|
'description' => 'the type of entity in string format', |
57
|
|
|
], |
58
|
|
|
'subtype' => [ |
59
|
|
|
'type' => 'string', |
60
|
|
|
'required' => false, |
61
|
|
|
'description' => 'the subtype of entity in string format, not required', |
62
|
|
|
], |
63
|
|
|
'offset' => [ |
64
|
|
|
'type' => 'int', |
65
|
|
|
'required' => true, |
66
|
|
|
'description' => 'the subtype of entity in string format, not required', |
67
|
|
|
], |
68
|
|
|
|
69
|
|
|
], |
70
|
|
|
'retrieves all entities filtered by type [and subtype]', |
71
|
|
|
'GET', |
72
|
|
|
true, |
73
|
|
|
false |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
elgg_ws_expose_function( |
78
|
|
|
'get.user_list', |
79
|
|
|
'get_user_list', |
80
|
|
|
[ |
81
|
|
|
'offset' => [ |
82
|
|
|
'type' => 'int', |
83
|
|
|
'required' => true, |
84
|
|
|
'description' => 'paging mechanism' |
85
|
|
|
] |
86
|
|
|
], |
87
|
|
|
'retrieves a user list', |
88
|
|
|
'GET', |
89
|
|
|
true, |
90
|
|
|
false |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
//$method, $function, array $parameters = NULL, $description = "", |
94
|
|
|
// $call_method = "GET", $require_api_auth = false, $require_user_auth = false |
95
|
|
|
|
96
|
|
|
elgg_ws_expose_function( |
97
|
|
|
'get.group_list', |
98
|
|
|
'get_group_list', |
99
|
|
|
[ |
100
|
|
|
'offset' => [ |
101
|
|
|
'type' => 'int', |
102
|
|
|
'required' => true, |
103
|
|
|
'description' => 'api loads 20 groups at a time' |
104
|
|
|
] |
105
|
|
|
], |
106
|
|
|
'retrieves a group list', |
107
|
|
|
'GET', |
108
|
|
|
true, |
109
|
|
|
false |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
elgg_ws_expose_function( |
114
|
|
|
'get.list_of_deleted_records', |
115
|
|
|
'get_list_of_deleted_records', |
116
|
|
|
null, |
117
|
|
|
'retrieves a list of deleted content', |
118
|
|
|
'GET', |
119
|
|
|
true, |
120
|
|
|
false |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
function delete_updated_index_list($guids) { |
126
|
|
|
|
127
|
|
|
$ids = array(); |
128
|
|
|
foreach ($guids as $guid) { |
129
|
|
|
// guid must be an integer |
130
|
|
|
if (is_numeric($guid)) |
131
|
|
|
$ids[] = $guid; |
132
|
|
|
else |
133
|
|
|
return "there is an issue deleting a record in the database, please see function delete_updated_index_list()"; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (sizeof($ids) > 0) { |
137
|
|
|
$ids = implode(",", $ids); |
138
|
|
|
$query = "DELETE FROM deleted_object_tracker WHERE id IN ({$ids})"; |
139
|
|
|
$result = delete_data($query); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$arr[] = array('message' => 'deleted the following guid'); |
|
|
|
|
143
|
|
|
$arr[] = $ids; |
144
|
|
|
|
145
|
|
|
return $arr; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// this is not an api call, this is a hook that will initiate once user attempts deletion |
149
|
|
|
function intercept_object_deletion($event, $type, $object) { |
150
|
|
|
$unixtime = time(); |
151
|
|
|
error_log("deleting ... id: {$object->guid} // title: {$object->title} on " . time()); |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
$query = "INSERT INTO deleted_object_tracker (id, time_deleted) VALUES ({$object->guid}, {$unixtime})"; |
155
|
|
|
|
156
|
|
|
// just in case for some reason, the user is able to delete the same thing twice |
157
|
|
|
try { |
158
|
|
|
$insert_record = insert_data($query); |
159
|
|
|
} catch (Exception $e) { |
160
|
|
|
error_log("error occurred: {$e}"); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$query = "SELECT * FROM deleted_object_tracker"; |
164
|
|
|
$deleted_records = get_data($query); |
165
|
|
|
|
166
|
|
|
foreach ($deleted_records as $deleted_record) { |
167
|
|
|
error_log("the id: " . $deleted_record->id); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
return false; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
// api calls with sample set of limit 15 |
176
|
|
|
function get_list_of_deleted_records() { |
177
|
|
|
|
178
|
|
|
$query = "SELECT * FROM deleted_object_tracker"; |
179
|
|
|
$deleted_records = get_data($query); |
180
|
|
|
|
181
|
|
|
foreach ($deleted_records as $deleted_record) { |
182
|
|
|
error_log("the id: " . $deleted_record->id); |
183
|
|
|
//$datetime = new DateTime("@{$deleted_record->time_deleted}"); |
184
|
|
|
//$datetime->setTimeZone(new DateTimeZone('America/New York')); |
185
|
|
|
$arr[] = array( |
|
|
|
|
186
|
|
|
'guid' => $deleted_record->id, |
187
|
|
|
'time_deleted' => $deleted_record->time_deleted |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
return $arr; |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
function get_user_list($offset) { |
195
|
|
|
|
196
|
|
|
$users = elgg_get_entities(array( |
197
|
|
|
'type' => 'user', |
198
|
|
|
'limit' => 10, |
199
|
|
|
'offset' => $offset |
200
|
|
|
)); |
201
|
|
|
|
202
|
|
|
foreach ($users as $user) { |
203
|
|
|
|
204
|
|
|
$name_array['en'] = $user->name; |
|
|
|
|
205
|
|
|
$name_array['fr'] = $user->name; |
|
|
|
|
206
|
|
|
|
207
|
|
|
$arr[] = array( |
|
|
|
|
208
|
|
|
'guid' => $user->getGUID(), |
209
|
|
|
'name' => $name_array, |
210
|
|
|
'username' => $user->username, |
211
|
|
|
'email' => $user->email, |
212
|
|
|
'type' => $user->getType(), |
213
|
|
|
'date_created' => date("Y-m-d\TH:m:s\Z", $user->time_created), |
214
|
|
|
'date_modified' => date("Y-m-d\TH:m:s\Z", $user->time_created), |
215
|
|
|
'url' => $user->getURL() |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
return $arr; |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
function get_group_list($offset) { |
222
|
|
|
|
223
|
|
|
$groups = elgg_get_entities(array( |
224
|
|
|
'type' => 'group', |
225
|
|
|
'limit' => 10, |
226
|
|
|
'offset' => $offset |
227
|
|
|
)); |
228
|
|
|
|
229
|
|
|
foreach ($groups as $group) { |
230
|
|
|
|
231
|
|
View Code Duplication |
if (isJson($group->name)) { |
232
|
|
|
$name_array = json_decode($group->name, true); |
233
|
|
|
$name_array['en'] = str_replace('"', '\"', $name_array['en']); |
234
|
|
|
$name_array['fr'] = str_replace('"', '\"', $name_array['fr']); |
235
|
|
|
} else { |
236
|
|
|
$name_array['en'] = $group->name_array; |
|
|
|
|
237
|
|
|
$name_array['fr'] = $group->name; |
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
View Code Duplication |
if (isJson($group->description)) { |
241
|
|
|
$description_array = json_decode($group->description, true); |
242
|
|
|
$description_array['en'] = str_replace('"', '\"', $description_array['en']); |
243
|
|
|
$description_array['fr'] = str_replace('"', '\"', $description_array['fr']); |
244
|
|
|
} else { |
245
|
|
|
$description_array['en'] = $group->description; |
|
|
|
|
246
|
|
|
$description_array['fr'] = $group->description; |
|
|
|
|
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$arr[] = array( |
|
|
|
|
250
|
|
|
'guid' => $group->getGUID(), |
251
|
|
|
'name' => $name_array, |
252
|
|
|
'description' => $description_array, |
253
|
|
|
'type' => $group->getType(), |
254
|
|
|
'access_id' => $group->access_id, |
255
|
|
|
'date_created' => date("Y-m-d\TH:m:s\Z", $group->time_created), |
256
|
|
|
'date_modified' => date("Y-m-d\TH:m:s\Z", $group->time_updated), |
257
|
|
|
'url' => $group->getURL() |
258
|
|
|
); |
259
|
|
|
} |
260
|
|
|
return $arr; |
|
|
|
|
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
|
264
|
|
|
function get_entity_list($type, $subtype, $offset) { |
265
|
|
|
|
266
|
|
|
$entities = elgg_get_entities(array( |
267
|
|
|
'type' => $type, |
268
|
|
|
'subtype' => $subtype, |
269
|
|
|
'limit' => 10, |
270
|
|
|
'offset' => $offset |
271
|
|
|
)); |
272
|
|
|
|
273
|
|
|
foreach ($entities as $entity) { |
274
|
|
|
|
275
|
|
View Code Duplication |
if (isJson($entity->title)) { |
276
|
|
|
$title_array = json_decode($entity->title, true); |
277
|
|
|
if (!isset($title_array['en']) || !isset($title_array['en'])) { |
278
|
|
|
$title_array['en'] = str_replace('"', '\"', $title_array); |
279
|
|
|
$title_array['fr'] = str_replace('"', '\"', $title_array); |
280
|
|
|
} else { |
281
|
|
|
$title_array['en'] = str_replace('"', '\"', $title_array['en']); |
282
|
|
|
$title_array['fr'] = str_replace('"', '\"', $title_array['fr']); |
283
|
|
|
} |
284
|
|
|
} else { |
285
|
|
|
$title_array['en'] = $entity->title; |
|
|
|
|
286
|
|
|
$title_array['fr'] = $entity->title; |
|
|
|
|
287
|
|
|
} |
288
|
|
|
|
289
|
|
View Code Duplication |
if (isJson($entity->description)) { |
290
|
|
|
$description_array = json_decode($entity->description, true); |
291
|
|
|
if (!isset($description_array['en']) || !isset($description_array['en'])) { |
292
|
|
|
$description_array['en'] = str_replace('"', '\"', $description_array); |
293
|
|
|
$description_array['fr'] = str_replace('"', '\"', $description_array); |
294
|
|
|
} else { |
295
|
|
|
$description_array['en'] = str_replace('"', '\"', $description_array['en']); |
296
|
|
|
$description_array['fr'] = str_replace('"', '\"', $description_array['fr']); |
297
|
|
|
} |
298
|
|
|
} else { |
299
|
|
|
$description_array['en'] = $entity->description; |
|
|
|
|
300
|
|
|
$description_array['fr'] = $entity->description; |
|
|
|
|
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
$arr[] = array( |
|
|
|
|
304
|
|
|
'guid' => $entity->getGUID(), |
305
|
|
|
'title' => $title_array, |
306
|
|
|
'description' => $description_array, |
307
|
|
|
'type' => $entity->getType(), |
308
|
|
|
'subtype' => $entity->getSubtype(), |
309
|
|
|
'access_id' => $entity->access_id, |
310
|
|
|
'date_created' => date("Y-m-d\TH:m:s\Z", $entity->time_created), |
311
|
|
|
'date_modified' => date("Y-m-d\TH:m:s\Z", $entity->time_updated), |
312
|
|
|
'url' => $entity->getURL() |
313
|
|
|
); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
return $arr; |
|
|
|
|
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
|
320
|
|
|
|
321
|
|
|
function isJson($string) { |
|
|
|
|
322
|
|
|
json_decode($string); |
323
|
|
|
return (json_last_error() == JSON_ERROR_NONE); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
|
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.