1
|
|
|
<?php |
2
|
|
|
namespace NRC; |
3
|
|
|
|
4
|
|
|
require_once(elgg_get_plugins_path() . 'missions/api/v0/api.php'); |
5
|
|
|
|
6
|
|
|
class export { |
7
|
|
|
|
8
|
|
|
public static $version = 'v0.0.3'; |
9
|
|
|
public static $help = <<<EOD |
10
|
|
|
<!DOCTYPE html> |
11
|
|
|
<html> |
12
|
|
|
<body> |
13
|
|
|
<H1> |
14
|
|
|
NRC Recommendation Provider API |
15
|
|
|
<sub style="font-size: 0.5em">::version::</sub> |
16
|
|
|
</H1> |
17
|
|
|
<p> |
18
|
|
|
This API provides the means by which recommendation services can collect |
19
|
|
|
GCConnex data. A private key is required to access this service. |
20
|
|
|
</p> |
21
|
|
|
<H3>Endpoints</H3> |
22
|
|
|
<ul> |
23
|
|
|
<li> |
24
|
|
|
<h4>/missions/api/v0/export[/guid]</h4> |
25
|
|
|
<p> |
26
|
|
|
Used to export any entity. If a guid is provided, only that entity |
27
|
|
|
is returned. |
28
|
|
|
</p> |
29
|
|
|
Examples: |
30
|
|
|
<ul> |
31
|
|
|
<li>/missions/api/v0/export</li> |
32
|
|
|
<li>/missions/api/v0/export/205</li> |
33
|
|
|
<li>/missions/api/v0/export/42922688</li> |
34
|
|
|
<li>/missions/api/v0/export?since=1539475200&limit=2</li> |
35
|
|
|
</ul> |
36
|
|
|
</li> |
37
|
|
|
<li> |
38
|
|
|
<h4>/missions/api/v0/user[/guid]</h4> |
39
|
|
|
<p> |
40
|
|
|
Used to export users. If a guid is provided, only that user is |
41
|
|
|
returned. |
42
|
|
|
</p> |
43
|
|
|
Examples: |
44
|
|
|
<ul> |
45
|
|
|
<li>/missions/api/v0/user</li> |
46
|
|
|
<li>/missions/api/v0/user/205</li> |
47
|
|
|
<li>/missions/api/v0/user?since=1539475200</li> |
48
|
|
|
</ul> |
49
|
|
|
</li> |
50
|
|
|
<li> |
51
|
|
|
<h4> |
52
|
|
|
/missions/api/v0/object[/guid]<br> |
53
|
|
|
/missions/api/v0/object/subtype[/guid] |
54
|
|
|
</h4> |
55
|
|
|
<p> |
56
|
|
|
Used to export objects. If a guid is provided, only that object is |
57
|
|
|
returned. A subtype can also be specified. |
58
|
|
|
</p> |
59
|
|
|
Examples: |
60
|
|
|
<ul> |
61
|
|
|
<li>/missions/api/v0/object</li> |
62
|
|
|
<li>/missions/api/v0/object/42922688</li> |
63
|
|
|
<li>/missions/api/v0/object?since=1539475200&limit=1</li> |
64
|
|
|
<li>/missions/api/v0/object/widget?since=1539475200</li> |
65
|
|
|
</ul> |
66
|
|
|
</li> |
67
|
|
|
</ul> |
68
|
|
|
<H3>Query parameters</H3> |
69
|
|
|
<p>Parameters can be used to perform more complex queries.</p> |
70
|
|
|
<table> |
71
|
|
|
<thead> |
72
|
|
|
<tr> |
73
|
|
|
<th>Parameter</th> |
74
|
|
|
<th>Description</th> |
75
|
|
|
</tr> |
76
|
|
|
</thead> |
77
|
|
|
<tbody> |
78
|
|
|
<tr> |
79
|
|
|
<td>before</td> |
80
|
|
|
<td> |
81
|
|
|
Fetch entities that have been modified before the specified time. |
82
|
|
|
Expects a unix timetamp. |
83
|
|
|
</td> |
84
|
|
|
</tr> |
85
|
|
|
<tr> |
86
|
|
|
<td>since</td> |
87
|
|
|
<td> |
88
|
|
|
Fetch entities that have been modified after the specified time. |
89
|
|
|
Expects a unix timetamp. |
90
|
|
|
</td> |
91
|
|
|
</tr> |
92
|
|
|
<tr> |
93
|
|
|
<td>limit</td> |
94
|
|
|
<td> |
95
|
|
|
Limits the number of returned entities. Expects an positive |
96
|
|
|
integer. |
97
|
|
|
</td> |
98
|
|
|
</tr> |
99
|
|
|
<tr> |
100
|
|
|
<td>resume</td> |
101
|
|
|
<td> |
102
|
|
|
Resume starting at the specified GUID. Expects a valid GUID. |
103
|
|
|
</td> |
104
|
|
|
</tr> |
105
|
|
|
<tr> |
106
|
|
|
<td>sort</td> |
107
|
|
|
<td> |
108
|
|
|
If specified sorts returned entities by created time - By default |
109
|
|
|
rows are returned in natural order without any sorting guarantees. |
110
|
|
|
This parameter doesn't take any value, its presence enables |
111
|
|
|
sorting. |
112
|
|
|
</td> |
113
|
|
|
</tr> |
114
|
|
|
<tr> |
115
|
|
|
<td>omit</td> |
116
|
|
|
<td> |
117
|
|
|
List of GUIDs to omit from the results. Useful to omit large |
118
|
|
|
objects we know we are not interested in. For example GUID "1". |
119
|
|
|
Takes a comma separated list of GUIDs. |
120
|
|
|
</td> |
121
|
|
|
</tr> |
122
|
|
|
<tr> |
123
|
|
|
<td>version<strong>*<strong></td> |
124
|
|
|
<td> |
125
|
|
|
Returns the API version as a JSON object and exits. No query is |
126
|
|
|
performed regardless of other parameters. |
127
|
|
|
</td> |
128
|
|
|
</tr> |
129
|
|
|
<tr> |
130
|
|
|
<td>help<strong>*<strong></td> |
131
|
|
|
<td> |
132
|
|
|
Returns the API help text as an HTML page and exits. No query is |
133
|
|
|
performed regardless of other parameters. |
134
|
|
|
</td> |
135
|
|
|
</tr> |
136
|
|
|
</table> |
137
|
|
|
<small><strong>*<strong> These parameters do not require a private key</small> |
138
|
|
|
<H3>Returns</H3> |
139
|
|
|
Successful queries return JSON objects like this: |
140
|
|
|
<pre> |
141
|
|
|
{ |
142
|
|
|
"query": { |
143
|
|
|
"object_type": "export", // type of export |
144
|
|
|
"api_version": "v0.0.3", // current API version |
145
|
|
|
"subtype": false, // requested subtype, if specified |
146
|
|
|
"guid": null, // requested GUID, if specified |
147
|
|
|
"since": "1539475200", // "since" parameter, if specified |
148
|
|
|
"before": null, // "before" parameter, if specified |
149
|
|
|
"limit": null, // "limit" parameter, if specified |
150
|
|
|
"request_time": 1546958611, // server's current unix timestamp |
151
|
|
|
"count": 14 // Total number of matched entities |
152
|
|
|
}, |
153
|
|
|
"export": [ ... ] // Array of requested entities |
154
|
|
|
} |
155
|
|
|
</pre> |
156
|
|
|
</body> |
157
|
|
|
</html> |
158
|
|
|
EOD; |
159
|
|
|
|
160
|
|
|
private $object_type = false; |
161
|
|
|
private $subtype = false; |
162
|
|
|
private $guid = null; |
163
|
|
|
private $since = null; |
164
|
|
|
private $before = null; |
165
|
|
|
private $limit = null; |
166
|
|
|
private $omit = null; |
167
|
|
|
|
168
|
|
|
static function getHelp() { |
169
|
|
|
return str_replace('::version::', self::$version, self::$help); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
function __construct($object_type, $subtype = false, $guid = null, |
173
|
|
|
$since = null, $before = null, $limit = null, $resume = null, |
174
|
|
|
$sort = false, $omit = null) { |
175
|
|
|
|
176
|
|
|
mm_api_secure(); |
177
|
|
|
|
178
|
|
|
$this->object_type = $object_type; |
179
|
|
|
$this->subtype = $subtype; |
180
|
|
|
$this->guid = $guid; |
181
|
|
|
$this->since = $since; |
182
|
|
|
$this->before = $before; |
183
|
|
|
$this->limit = $limit; |
184
|
|
|
$this->resume = $resume; |
|
|
|
|
185
|
|
|
$this->sort = $sort; |
|
|
|
|
186
|
|
|
$this->omit = $omit; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Stream the export results using JSON format |
191
|
|
|
*/ |
192
|
|
|
function outputJSON() { |
193
|
|
|
while (@ob_end_flush()); |
194
|
|
|
$guids = mm_api_get_entity_guids( |
195
|
|
|
$this->object_type, |
|
|
|
|
196
|
|
|
$this->subtype, |
|
|
|
|
197
|
|
|
$this->guid, |
198
|
|
|
$this->since, |
199
|
|
|
$this->before, |
200
|
|
|
$this->limit, |
201
|
|
|
$this->resume, |
202
|
|
|
$this->sort, |
203
|
|
|
$this->omit |
204
|
|
|
); |
205
|
|
|
echo '{"query":' .json_encode([ |
206
|
|
|
'object_type' => $this->object_type, |
207
|
|
|
'api_version' => self::$version, |
208
|
|
|
'subtype' => $this->subtype, |
209
|
|
|
'guid' => $this->guid, |
210
|
|
|
'since' => $this->since, |
211
|
|
|
'before' => $this->before, |
212
|
|
|
'limit' => $this->limit, |
213
|
|
|
'request_time' => time(), |
214
|
|
|
'count' => $guids->current() |
215
|
|
|
]). ',"export":['; |
216
|
|
|
flush(); |
217
|
|
|
$guids->next(); |
218
|
|
|
$export = mm_api_entity_export($guids); |
|
|
|
|
219
|
|
|
|
220
|
|
|
// ignore the first comma |
221
|
|
|
$export->next(); |
|
|
|
|
222
|
|
|
|
223
|
|
|
while ($export->valid()) { |
|
|
|
|
224
|
|
|
$output = $export->current(); |
|
|
|
|
225
|
|
|
echo $output; |
226
|
|
|
flush(); |
227
|
|
|
ob_flush(); |
228
|
|
|
$export->next(); |
|
|
|
|
229
|
|
|
} |
230
|
|
|
echo "]}\r\n"; |
231
|
|
|
exit; |
|
|
|
|
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
?> |
|
|
|
|
235
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: