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