-
Notifications
You must be signed in to change notification settings - Fork 55
/
ApiController.php
467 lines (414 loc) · 18.3 KB
/
ApiController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<?php
namespace YesWiki\Bazar\Controller;
use Exception;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use YesWiki\Bazar\Service\EntryManager;
use YesWiki\Bazar\Service\FormManager;
use YesWiki\Bazar\Service\SemanticTransformer;
use YesWiki\Bazar\Service\BazarListService;
use YesWiki\Core\ApiResponse;
use YesWiki\Core\Service\AclService;
use YesWiki\Core\Service\TripleStore;
use YesWiki\Core\YesWikiController;
class ApiController extends YesWikiController
{
/**
* @Route("/api/forms", methods={"GET"},options={"acl":{"public"}})
* @Route("/api/forms/", methods={"GET"},options={"acl":{"public"}})
*/
public function getAllForms()
{
$forms = $this->getService(FormManager::class)->getAll();
return new ApiResponse(empty($forms) ? null : $forms);
}
/**
* @Route("/api/forms/{formId}", methods={"GET"},options={"acl":{"public"}})
* @Route("/api/forms/{formId}/", methods={"GET"},options={"acl":{"public"}})
*/
public function getForm($formId)
{
$form = $this->getService(FormManager::class)->getOne($formId);
if (!$form || !isset($form['bn_id_nature'])) {
throw new NotFoundHttpException();
}
return new ApiResponse($form);
}
/**
* @Route("/api/forms/{formId}/entries/{output}/{selectedEntries}", methods={"GET"},options={"acl":{"public"}})
*/
public function getAllFormEntries($formId, $output = null, $selectedEntries = null)
{
$entries = $this->getService(EntryManager::class)->search([
'formsIds' => $formId,
'queries' => $this->getService(EntryController::class)
->formatQuery(!empty($selectedEntries) ? ['query' => ['id_fiche' => $selectedEntries]] : [], $_GET),
'minDate' => $_GET['dateMin'] ?? '',
], true, true);
if ($output == 'json-ld' || (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'application/ld+json') !== false)) {
return $this->getAllSemanticEntries($formId, $entries);
} // add entries in html format if asked
elseif ($output == 'html') {
foreach ($entries as $id => $entry) {
$entries[$id]['html_output'] = $this->getService(EntryController::class)->view($entry, '', 0);
}
} elseif ($output == 'geojson') {
$entries = $this->getService(GeoJSONFormatter::class)->formatToGeoJSON($entries);
} elseif ($output == 'ical') {
return $this->getService(IcalFormatter::class)->apiResponse($entries, $formId, $_GET);
} elseif (isset($_GET['fields'])) {
$fields = explode(',', $_GET['fields']);
$lightEntries = [];
if (!empty($entries) && !empty($fields)) {
foreach ($entries as $id => $entry) {
$lightEntry = [];
foreach ($fields as $field_name) {
if (isset($entry[$field_name])) {
$lightEntry[$field_name] = $entry[$field_name];
}
}
if (!empty($lightEntry)) {
$lightEntries[$id] = $lightEntry ;
}
}
}
return new ApiResponse(empty($lightEntries) ? null : $lightEntries);
}
return new ApiResponse(empty($entries) ? null : $entries);
}
/**
* @Route("/api/entries/{output}/{selectedEntries}", methods={"GET"}, options={"acl":{"public"}})
*/
public function getAllEntries($output = null, $selectedEntries = null)
{
// fast access for one entry
if ($this->isEntryViewFastAccess($output, $selectedEntries, $_GET)) {
$entryId = explode(',', $selectedEntries)[0];
if ($this->getService(AclService::class)->hasAccess('read', $entryId)) {
$html = $this->getService(EntryController::class)->view($entryId, '', 1);
} else {
$html = $this->render('@templates/alert-message.twig', [
'type' => 'info',
'message' => _t('ERROR_NO_ACCESS')
]);
}
return new ApiResponse(empty($html) ? null : [$entryId => ['html_output' => $html]]);
}
return $this->getAllFormEntries([], $output, $selectedEntries);
}
/**
* helper to check if EntryView fast access
* @param null|string $output
* @param null|string $selectedEntries
* @param null|array $get
* @param bool
*/
private function isEntryViewFastAccess($output, $selectedEntries, $get): bool
{
return ($output == 'html'
&& !empty($selectedEntries) && is_string($selectedEntries) && count(explode(',', $selectedEntries)) == 1
&& !empty($get['fields']) && $get['fields'] == 'html_output');
}
/**
* helper to check if EntryView fast access for Bazar/Service/Guard
* @param bool
*/
public function isEntryViewFastAccessHelper(): bool
{
$route = array_keys($_GET)[0];
if (substr($route, strlen('api/entries/html'), 1) == '/') {
$output = substr($route, strlen('api/entries/'), strlen('html'));
$selectedEntries = substr($route, strlen('api/entries/html/'));
} else {
$output = '';
$selectedEntries = '';
}
return $this->isEntryViewFastAccess($output, $selectedEntries, $_GET);
}
public function getAllSemanticEntries($formId, $entries)
{
// Put data inside LDP container
$form = $this->getService(FormManager::class)->getOne($formId);
return new ApiResponse(
[
'@context' => (array)json_decode($form['bn_sem_context']) ?: $form['bn_sem_context'],
'@id' => $this->wiki->Href('fiche/' . $formId, 'api'),
'@type' => ['ldp:Container', 'ldp:BasicContainer'],
'dcterms:title' => $form['bn_label_nature'],
'ldp:contains' => array_map(function ($entry) use ($form) {
$resource = $this->getService(SemanticTransformer::class)->convertToSemanticData($form, $entry, true);
unset($resource['@context']);
return $resource;
}, array_values($entries)),
],
Response::HTTP_OK,
['Content-Type: application/ld+json; charset=UTF-8']
);
}
/**
* @Route("/api/entry/url/{sourceUrl}")
*/
public function getEntryUrl($sourceUrl)
{
$triples = $this->getService(TripleStore::class)->getMatching(
null,
'http://outils-reseaux.org/_vocabulary/sourceUrl',
urldecode($sourceUrl)
);
if (!$triples) {
throw new NotFoundHttpException();
}
$resources = array_map(function ($triple) {
return $this->wiki->Href('', $triple['resource']);
}, $triples);
return new ApiResponse($resources);
}
/**
* @Route("/api/entries/{formId}", methods={"POST"})
*/
public function createEntry($formId)
{
if (strpos($_SERVER['CONTENT_TYPE'], 'application/ld+json') !== false) {
$this->createSemanticEntry($formId);
};
$_POST['antispam'] = 1;
$entry = $this->getService(EntryManager::class)->create($formId, $_POST, false, $_SERVER['HTTP_SOURCE_URL']);
if (!$entry) {
throw new BadRequestHttpException();
}
return new ApiResponse(
['success' => $this->wiki->Href('', $entry['id_fiche'])],
Response::HTTP_CREATED
);
}
/**
* @Route("/api/entries/{formId}/json-ld", methods={"POST"})
*/
public function createSemanticEntry($formId)
{
$_POST['antispam'] = 1;
$entry = $this->getService(EntryManager::class)->create($formId, $_POST, true, $_SERVER['HTTP_SOURCE_URL']);
if (!$entry) {
throw new BadRequestHttpException();
}
return new Response('', Response::HTTP_CREATED, [
'Link: <http://www.w3.org/ns/ldp#Resource>; rel="type"',
'Location: ' . $this->wiki->Href('', $entry['id_fiche'])
]);
}
/**
* @Route("/api/entries/bazarlist", methods={"GET"}, options={"acl":{"public"}},priority=2)
*/
public function getBazarListData()
{
$bazarListService = $this->getService(BazarListService::class);
$externalIds = $_GET['externalIds'] ?? null;
$externalModeActivated = (is_array($externalIds) && isset($_GET['externalModeActivated'])) ? in_array($_GET['externalModeActivated'], [1,true,'1','true'], true): false;
$forms = $bazarListService->getForms([
'externalModeActivated' => $externalModeActivated,
'externalIds' => $externalIds,
'refresh' => isset($_GET['refresh']) ? in_array($_GET['refresh'], [1,true,'1','true'], true): false,
]);
$formattedGet = array_map(function ($value) {
return ($value === 'true') ? true : (($value === 'false') ? false : $value);
}, $_GET);
// format search field
$searchfields = isset($GET['search']) && $_GET['search'] == 'dynamic' ? $_GET['searchfields'] ?? [] : [];
$searchfields = (empty($searchfields) || !is_string($searchfields) || !is_array($searchfields))
? ['bf_titre']
: (
is_string($searchfields)
? explode(',', $searchfields)
: $searchfields
);
$formattedGet['searchfields'] = $searchfields;
$formattedGet['externalModeActivated'] = $externalModeActivated;
$entries = $bazarListService->getEntries(
$formattedGet + [
'user' => null,
'dateMin' => null,
'random' => false,
'ordre' => 'asc',
'champ' => 'bf_titre',
'nb' => null,
'colorfield ' => null,
'iconfield ' => null,
],
$forms
);
$filters = $bazarListService->formatFilters($formattedGet, $entries, $forms);
// Basic fields
$fieldList = ['id_fiche', 'bf_titre'];
// If no id, we need idtypeannonce (== formId) to filter
if (!isset($_GET['id'])) {
$fieldList[] = 'id_typeannonce';
}
// fields for colo / icon
$fieldList = array_merge($fieldList, [ $_GET['colorfield'] ?? null, $_GET['iconfield'] ?? null]);
// Fields for filters
foreach ($filters as $field => $config) {
$fieldList[] = $field;
}
// Fields used to search
foreach ($searchfields as $field) {
$fieldList[] = $field;
}
// Fields used by template
foreach ($_GET['displayfields'] ?? [] as $field) {
$fieldList[] = $field;
}
// Fields for external urls
if ($formattedGet['externalModeActivated']) {
$fieldList[] = 'url';
}
// extra fields required by template
$fieldList = array_merge($fieldList, $_GET['necessary_fields'] ?? []);
$fieldList = array_values(array_unique(array_filter($fieldList))); // array_values to have incremental keys
$formIds = array_map(function ($entry) {
return $entry['id_typeannonce'];
}, $entries);
$formIds = array_unique($formIds);
// Reduce the size of the data sent by transforming entries object into array
// we use the $fieldMapping to transform back the data when receiving data in the front end
$entries = array_map(function ($entry) use ($fieldList, $formIds) {
$result = [];
foreach ($fieldList as $field) {
$result[] = $entry[$field] ?? null;
}
return $result;
}, $entries);
$usedForms = array_filter($forms, function ($form) use ($formIds) {
return in_array($form['bn_id_nature'], $formIds);
});
$usedForms = array_map(function ($f) {
return $f['prepared'];
}, $usedForms);
return new ApiResponse(
[
'entries' => $entries,
'fieldMapping' => $fieldList,
'filters' => $filters,
'forms' => $usedForms
],
Response::HTTP_OK
);
}
/**
* Display Bazar api documentation
*
* @return string
*/
public function getDocumentation()
{
$output = '<h2>Bazar</h2>' . "\n";
$output .= '
<p>
<b><code>GET ' . $this->wiki->href('', 'api/forms') . '</code></b><br />
Retourne la liste de tous les formulaires Bazar.
</p>';
$output .= '
<p>
<b><code>GET ' . $this->wiki->href('', 'api/forms/{formId}') . '</code></b><br />
Retourne les informations sur le formulaire <code>formId</code>.
</p>';
$output .= '
<p>
<b><code>GET ' . $this->wiki->href('', '{pageTag}') . '</code></b><br />
Si le header <code>Accept</code> est <code>application/json</code>, retourne la fiche au format JSON.<br />
Si le header <code>Accept</code> est <code>application/ld+json</code>, retourne la fiche au format JSON-LD.<br />
</p>';
$output .= '
<p>
<b><code>PUT ' . $this->wiki->href('', '{pageTag}') . '</code></b><br />
Si le header <code>Content-Type</code> est <code>application/json</code>, modifie la fiche selon le JSON fourni.<br />
Si le header <code>Content-Type</code> est <code>application/ld+json</code>, modifie la fiche selon le JSON-LD fourni.<br />
</p>';
$output .= '
<p>
<b><code>DELETE ' . $this->wiki->href('', '{pageTag}') . '</code></b><br />
Supprime la fiche Bazar.
</p>';
$output .= '
<p>
<b><code>GET ' . $this->wiki->href('', 'api/entries') . '</code></b><br />
Obtenir la liste des fiches de tous les formulaires Bazar.<br />
Si le header <code>Accept</code> est <code>application/ld+json</code>, le JSON retourné sera au format sémantique (container LDP)
</p>';
$output .= '
<p>
<b><code>GET ' . $this->wiki->href('', 'api/forms/{formId}/entries') . '</code></b><br />
Obtenir la liste de toutes les fiches du formulaire <code>formId</code><br />
Si le header <code>Accept</code> est <code>application/ld+json</code>, le JSON retourné sera au format sémantique (container LDP)
</p>';
$output .= '
<p>
<b><code>GET ' . $this->wiki->href('', 'api/forms/{formId}/entries/json-ld') . '</code></b><br />
Obtenir la liste de toutes les fiches du formulaire <code>formId</code> au format sémantique (container LDP)<br />
</p>';
$output .= '
<p>
<b><code>GET ' . $this->wiki->href('', 'api/forms/{formId}/entries/html') . '</code></b><br />
Obtenir la liste de toutes les fiches du formulaire <code>formId</code> au format json, avec la représentation html de la fiche dans le champ <code>html_output</code><br />
</p>';
$output .= '
<p>
<b><code>GET ' . $this->wiki->href('', 'api/forms/{formId}/entries/geojson') . '</code></b><br />
Obtenir la liste de toutes les fiches du formulaire <code>formId</code> au format geojson<br />
</p>';
$output .= '
<p>
<b><code>GET ' . $this->wiki->href('', 'api/forms/{formId}/entries/ical') . '</code></b><br />
Obtenir la liste de toutes les fiches du formulaire <code>formId</code> au format ical<br />
Il est possible de filtrer sur les dates en ajoutant à l\'url <code>&datefilter=>-6M</code> (exemple pour les dates plus récentes que 6 mois)<br />
</p>';
$output .= '
<p>
<b><code>GET ' . $this->wiki->href('', 'api/forms/{formId}/entries&fields=bf_titre') . '</code></b><br />
Obtenir la liste de toutes les fiches du formulaire <code>formId</code> en ne gardant que les titres (il est possible de spécifier d\autres champs en séparant leur nom par des \',\')<br />
</p>';
$output .= '
<p>
<b><code>POST ' . $this->wiki->href('', 'api/entries/{formId}') . '</code></b><br />
Créer une nouvelle fiche en utilisant le formulaire <code>formId</code><br />
Si le header <code>Content-Type</code> est <code>application/ld+json</code>, un JSON sémantique est attendu.
</p>';
$output .= '
<p>
<b><code>GET ' . $this->wiki->href('', 'api/entries/html') . '</code></b><br />
Obtenir la liste de toutes les fiches au format json, avec la représentation html de la fiche dans le champ <code>html_output</code><br />
</p>';
$output .= '
<p>
<b><code>GET ' . $this->wiki->href('', 'api/entries/bazarlist') . '</code></b><br />
Obtenir les données nécessaires à bazarliste dynamic au format json<br />
</p>';
$output .= '
<p>
<b><code>POST ' . $this->wiki->href('', 'api/entries/{formId}/json-ld') . '</code></b><br />
Créer une nouvelle fiche de type <code>formId</code> au format sémantique<br />
</p>';
$output .= '
<p>
<b><code>GET ' . $this->wiki->href('', 'api/entries/geojson') . '</code></b><br />
Obtenir la liste de toutes les fiches au format geojson<br />
</p>';
$output .= '
<p>
<b><code>GET ' . $this->wiki->href('', 'api/entries/ical') . '</code></b><br />
Obtenir la liste de toutes les fiches au format ical<br />
</p>';
$output .= '
<p>
<b><code>GET ' . $this->wiki->href('', 'api/entries/{output}&fields=bf_titre') . '</code></b><br />
Obtenir la liste de toutes les fiches au format spécifié en ne gardant que les titres (il est possible de spécifier d\'autres champs en séparant leur nom par des \',\' ex: <code>&field=bf_titre,url</code>)<br />
</p>';
$output .= '
<p>
<b><code>GET ' . $this->wiki->href('', 'api/entry/url/{sourceUrl}') . '</code></b><br />
Retourne l\'URL de la page Wiki synchronisée avec <code>sourceUrl</code><br />
</p>';
return $output;
}
}