-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmart-add-edit-to-multiple-sites-airtable-api.php
327 lines (293 loc) · 10.3 KB
/
smart-add-edit-to-multiple-sites-airtable-api.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
<?php
declare(strict_types=1);
/**
* Add or Edit Joomla! Articles Via API Using Airtable API
* - When id = 0 in csv it's doing a POST. If alias exists it add a random slug at the end of your alias and do POST again
* - When id > 0 in csv it's doing a PATCH. If alias exists it add a random slug at the end of your alias and do PATCH again
*
* @author Alexandre ELISÉ <[email protected]>
* @copyright (c) 2009 - present. Alexandre ELISÉ. All rights reserved.
* @license GNU Affero General Public License version 3 (AGPLv3)
* @link https://apiadept.com
*/
// Your Airtable endpoint url (CHANGE WITH YOUR OWN URL IF YOU WISH)
$dataSourceUrl = 'https://api.airtable.com/v0/apptBHEIQEEuQZDUg/tblVWSWobBIo5q6bX';
// Your Airtable Api token
$dataSourceToken = 'yourairtableapikey';
// Your Joomla! 4.x website base url
$baseUrl = [
'app-001' => 'https://app-001.example.org',
'app-002' => 'https://app-002.example.org',
'app-003' => 'https://app-003.example.org',
];
// Your Joomla! 4.x Api Token (DO NOT STORE IT IN YOUR REPO USE A VAULT OR A PASSWORD MANAGER)
$token = [
'app-001' => 'yourapp001joomlaapitoken',
'app-002' => 'yourapp002joomlaapitoken',
'app-003' => 'yourapp003joomlaapitoken',
];
$basePath = 'api/index.php/v1';
// Request timeout
$timeout = 10;
// Add custom fields support (shout-out to Marc DECHÈVRE : CUSTOM KING)
// The keys are the columns in the csv with the custom fields names (that's how Joomla! Web Services Api work as of today)
// For the custom fields to work they need to be added in the csv and to exists in the Joomla! site.
$customFieldKeys = []; //['with-coffee','with-dessert','extra-water-bottle'];
// This time we need endpoint to be a function to make it more dynamic
$endpoint = function (string $givenBaseUrl, string $givenBasePath, int $givenResourceId = 0): string {
return $givenResourceId ? sprintf('%s/%s/%s/%d', $givenBaseUrl, $givenBasePath, 'content/articles', $givenResourceId)
: sprintf('%s/%s/%s', $givenBaseUrl, $givenBasePath, 'content/articles');
};
// PHP Generator to efficiently process Airtable Api response
$generator = function (string $airtableResponse, array $keys): Generator {
if (empty($airtableResponse))
{
yield new RuntimeException('Url MUST NOT be empty', 422);
}
$defaultKeys = [
'id',
'access',
'title',
'alias',
'catid',
'articletext',
'introtext',
'fulltext',
'language',
'metadesc',
'metakey',
'state',
'featured',
'images',
'urls',
'tokenindex',
'picture',
];
$mergedKeys = empty($keys) ? $defaultKeys : array_unique(array_merge($defaultKeys, $keys));
// Assess robustness of the code by trying random key order
//shuffle($mergedKeys);
$resource = json_decode($airtableResponse);
if ($resource === false)
{
yield new RuntimeException('Could not read airtable response', 500);
}
try
{
if (is_array($resource->records) && empty($resource->records))
{
throw new RuntimeException('No records found in your Airtable table. Cannot continue.', 422);
}
$picturePath = __DIR__ . '/images/';
$downloadPicture = function (stdClass $givenPicture, string $destination) {
if (!property_exists($givenPicture, 'url'))
{
throw new DomainException('Cannot download current picture. Malformed datastructure url and filename MUST be present in givenPicture class parameter', 422);
}
if (empty($destination))
{
throw new DomainException('Destination path for pictures download MUST not be empty', 422);
}
$contentType = explode(';', get_headers($givenPicture->url, true)['Content-Type'])[0];
if (!in_array($contentType, ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'], true))
{
throw new DomainException('Picture format not allowed at the moment', 415);
}
$imageData = file_get_contents($givenPicture->url);
if (!file_exists($destination))
{
mkdir($destination, 0700, true);
}
$destinationFile = sprintf('%s/%s.%s', $destination, hash('sha3-512', $imageData, false), explode('/', $contentType)[1]);
if (!file_exists($destinationFile))
{
file_put_contents($destinationFile, $imageData);
}
};
foreach ($resource->records as $record)
{
if (empty((array) $record->fields))
{
echo 'No fields found in this record' . PHP_EOL;
continue;
}
foreach ($record->fields as $fieldKey => $fieldValue)
{
if (!in_array($fieldKey, $mergedKeys, true))
{
unset($record->fields->$fieldKey);
}
if (!isset($record->fields->$fieldKey))
{
continue;
}
if (is_string($fieldValue) && mb_strpos($fieldValue, '{') === 0)
{
//IMPORTANT: This one line allows to see intro/fulltext images and urla,urlb,urlc
$record->fields->$fieldKey = json_decode(str_replace(["\n", "\r", "\t"], '', trim($fieldValue)));
if ($fieldKey === 'images')
{
$moreThanOnePicture = [];
foreach ($record->fields->picture as $index => $currentPicture)
{
try
{
// In all case we want to download the full thumbnail
$downloadPicture($currentPicture->thumbnails->large, $picturePath);
if ($index === 0)
{
//Just for the first image we want to download both full and large
$downloadPicture($currentPicture->thumbnails->full, $picturePath);
$record->fields->images->image_intro = $currentPicture->thumbnails->large->url;
$record->fields->images->image_fulltext = $currentPicture->thumbnails->full->url;
}
$moreThanOnePicture[] = <<<MEDIA
<figure>
<img src="{$currentPicture->thumbnails->large->url}" alt="">
<figcaption>Picture from Datasource - Airtable</figcaption>
</figure>
MEDIA;
}
catch (Throwable $pictureDownloadThrowable)
{
// On failure to download images try to add them as url anyway
$record->fields->images->image_intro = $currentPicture->thumbnails->large->url;
$record->fields->images->image_fulltext = $currentPicture->thumbnails->full->url;
echo $pictureDownloadThrowable->getTraceAsString() . PHP_EOL;
continue;
}
}
//Prepend articletext and fulltext when there is more than 1 picture provided in the Airtable attachments fields
$record->fields->articletext = sprintf('%s<br><hr>%s', implode('', $moreThanOnePicture), $record->fields->articletext);
$record->fields->fulltext = sprintf('%s<br><hr>%s', implode('', $moreThanOnePicture), $record->fields->fulltext);
}
}
}
// Re-encode the fields to send it back as JSON
yield json_encode($record->fields);
}
} finally
{
echo 'DONE processing data' . PHP_EOL;
}
};
// Process data returned by the PHP Generator
$process = function (string $givenHttpVerb, string $endpoint, string $dataString, array $headers, int $timeout, $transport) {
curl_setopt_array($transport, [
CURLOPT_URL => $endpoint,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => 'utf-8',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2TLS,
CURLOPT_CUSTOMREQUEST => $givenHttpVerb,
CURLOPT_POSTFIELDS => $dataString,
CURLOPT_HTTPHEADER => $headers,
]
);
$response = curl_exec($transport);
// Continue even on partial failure
if (empty($response))
{
throw new RuntimeException('Empty output', 422);
}
return $response;
};
// Airtable Api response (First call to GET all records of specific table)
$dataSourceHttpVerb = 'GET';
$dataSourceHeaders = [
'Accept: application/json',
sprintf('Authorization: Bearer %s', trim($dataSourceToken)),
];
$dataSourceTimeout = 30;
// Response from Airtable Api
$dataSourceTransport = curl_init();
try
{
$dataSourceResponse = $process($dataSourceHttpVerb, $dataSourceUrl, '', $dataSourceHeaders, $dataSourceTimeout, $dataSourceTransport);
$streamData = $generator($dataSourceResponse, $customFieldKeys);
$storage = [];
foreach ($streamData as $dataKey => $dataString)
{
if (!is_string($dataString))
{
continue;
}
$curl = curl_init();
try
{
$decodedDataString = json_decode($dataString);
if ($decodedDataString === false)
{
continue;
}
// HTTP request headers
$headers = [
'Accept: application/vnd.api+json',
'Content-Type: application/json',
'Content-Length: ' . mb_strlen($dataString),
sprintf('X-Joomla-Token: %s', trim($token[$decodedDataString->tokenindex])),
];
// Article primary key. Usually 'id'
$pk = (int) $decodedDataString->id;
$output = $process($pk ? 'PATCH' : 'POST', $endpoint($baseUrl[$decodedDataString->tokenindex], $basePath, $pk), $dataString, $headers, $timeout, $curl);
$decodedJsonOutput = json_decode($output);
// don't show errors, handle them gracefully
if (isset($decodedJsonOutput->errors))
{
// If article is potentially a duplicate (already exists with same alias)
$storage[$dataKey] = ['mightExists' => $decodedJsonOutput->errors[0]->code === 400, 'decodedDataString' => $decodedDataString];
continue;
}
echo $output . PHP_EOL;
}
catch (Throwable $streamDataThrowable)
{
echo $streamDataThrowable->getTraceAsString() . PHP_EOL;
continue;
} finally
{
curl_close($curl);
}
}
// Handle errors and retries
foreach ($storage as $item)
{
$storageCurl = curl_init();
try
{
if ($item['mightExists'])
{
$pk = (int) $item['decodedDataString']->id;
$item['decodedDataString']->alias = sprintf('%s-%s', $item['decodedDataString']->alias, bin2hex(random_bytes(4)));
// No need to do another json_encode anymore
$dataString = json_encode($item['decodedDataString']);
// HTTP request headers
$headers = [
'Accept: application/vnd.api+json',
'Content-Type: application/json',
'Content-Length: ' . mb_strlen($dataString),
sprintf('X-Joomla-Token: %s', trim($token[$item['decodedDataString']->tokenindex])),
];
$output = $process($pk ? 'PATCH' : 'POST', $endpoint($baseUrl[$item['decodedDataString']->tokenindex], $basePath, $pk), $dataString, $headers, $timeout, $storageCurl);
echo $output . PHP_EOL;
}
}
catch (Throwable $storageThrowable)
{
echo $storageThrowable->getTraceAsString() . PHP_EOL;
continue;
} finally
{
curl_close($storageCurl);
}
}
}
catch (Throwable $e)
{
echo $e->getTraceAsString() . PHP_EOL;
} finally
{
curl_close($dataSourceTransport);
}