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