-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmart-add-edit-with-subform-custom-field-support-from-streamed-csv-url.php
251 lines (218 loc) · 7.14 KB
/
smart-add-edit-with-subform-custom-field-support-from-streamed-csv-url.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
<?php
declare(strict_types=1);
/**
* Add or Edit Joomla! Articles Via API Using Streamed CSV
* - 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
* - Now supports:
* - subform custom fields in article
* - images: intro / fulltext images in article
* - urls: urla,urlb,urlc in article
* @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
*/
// Public url of the sample csv used in this example (CHANGE WITH YOUR OWN CSV URL IF YOU WISH)
$csvUrl = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vTtV7Bnj-E3mwnBgXbkZlzS476aHVp6vtZ7qdI5vxlPUUqNe_85S3ozT5_gzNkBDih4dL1f8uDeeh_g/pub?gid=522567559&single=true&output=csv';
// Your Joomla! 4.x website base url
$baseUrl = '';
// Your Joomla! 4.x Api Token (DO NOT STORE IT IN YOUR REPO USE A VAULT OR A PASSWORD MANAGER)
$token = '';
$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 = fn(string $givenBaseUrl, string $givenBasePath, int $givenResourceId = 0): string => $givenResourceId ? sprintf('%s/%s/%s/%d', $givenBaseUrl, $givenBasePath, 'content/articles', $givenResourceId)
: sprintf('%s/%s/%s', $givenBaseUrl, $givenBasePath, 'content/articles');
// PHP Generator to efficiently read the csv file
$generator = function (string $url, array $keys = []): Generator {
if (empty($url))
{
yield new RuntimeException('Url MUST NOT be empty', 422);
}
$defaultKeys = [
'id',
'title',
'alias',
'catid',
'articletext',
'introtext',
'fulltext',
'language',
'metadesc',
'metakey',
'state',
'featured',
'publish_up',
'publish_down',
'featured_up',
'featured_down',
'images',
'urls',
];
$mergedKeys = array_unique(array_merge($defaultKeys, $keys));
// Assess robustness of the code by trying random key order
//shuffle($mergedKeys);
$resource = fopen($url, 'r');
if ($resource === false)
{
yield new RuntimeException('Could not read csv file', 500);
}
try
{
//NON-BLOCKING I/O (Does not wait before processing next line.)
stream_set_blocking($resource, false);
$firstLine = stream_get_line(
$resource,
0,
"\r\n"
);
if (empty($firstLine))
{
yield new RuntimeException('First line MUST NOT be empty. It is the header', 422);
}
$csvHeaderKeys = str_getcsv($firstLine);
$commonKeys = array_intersect($csvHeaderKeys, $mergedKeys);
do
{
$currentLine = stream_get_line(
$resource,
0,
"\r\n"
);
if (empty($currentLine))
{
yield new RuntimeException('Current line MUST NOT be empty', 422);
}
$extractedContent = str_getcsv($currentLine);
// Allow using csv keys in any order
$commonValues = array_intersect_key($extractedContent, $commonKeys);
// Iteration on leafs AND nodes
$handleComplexValues = [];
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($commonValues), RecursiveIteratorIterator::CATCH_GET_CHILD);
foreach ($iterator as $key => $value)
{
if (json_decode($value) === false)
{
$handleComplexValues[$key] = json_encode($value);
}
else
{
$handleComplexValues[$key] = $value;
}
echo 'current item key: ' . $key . ' with value ' . $handleComplexValues[$key] . PHP_EOL;
}
$encodedContent = json_encode(array_combine($commonKeys, $handleComplexValues));
if ($encodedContent !== false)
{
yield $encodedContent;
}
yield new RuntimeException('Current line seem to be invalid', 422);
} while (!feof($resource));
} finally
{
fclose($resource);
}
};
// 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_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;
};
// Read CSV in a PHP Generator using streams in non-blocking I/O mode
$streamCsv = $generator($csvUrl, $customFieldKeys);
$storage = [];
foreach ($streamCsv as $dataKey => $dataString)
{
if (!is_string($dataString))
{
continue;
}
$curl = curl_init();
try
{
// 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 = json_decode($dataString, true);
// Article primary key. Usually 'id'
$pk = (int) $decodedDataString['id'];
$output = $process($pk ? 'PATCH' : 'POST', $endpoint($baseUrl, $basePath, $pk), $dataString, $headers, $timeout, $curl);
$decodedJsonOutput = json_decode($output, true);
// 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 $e)
{
echo $e->getMessage() . PHP_EOL;
continue;
} finally
{
curl_close($curl);
}
}
// Handle errors and retries
foreach ($storage as $item)
{
$curl = curl_init();
try
{
if ($item['mightExists'])
{
$pk = (int) $item['decodedDataString']['id'];
$item['decodedDataString']['alias'] = sprintf('%s-%s', $item['decodedDataString']['alias'], bin2hex(random_bytes(4)));
// back to json string after changing alias
$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)),
];
$output = $process($pk ? 'PATCH' : 'POST', $endpoint($baseUrl, $basePath, $pk), $dataString, $headers, $timeout, $curl);
echo $output . PHP_EOL;
}
}
catch (Throwable $e)
{
echo $e->getMessage() . PHP_EOL;
continue;
} finally
{
curl_close($curl);
}
}