-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmart-add-edit-random-defined-sites-github-graphql.php
305 lines (272 loc) · 8.29 KB
/
smart-add-edit-random-defined-sites-github-graphql.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
<?php
declare(strict_types=1);
/**
* Add or Edit Joomla! Articles Via API To Multiple Sites Chosen Randomly in from a predefined list Using GitHub GraphQL 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 GitHub GraphQL API endpoint url
$dataSourceUrl = 'https://api.github.com/graphql';
// Your GitHub Personal Token (classic) (CHANGE WITH YOUR OWN TOKEN)
$dataSourceToken = 'ghp_yourowngithubpersonalclassictoken';
// Your repository owner (CHANGE THIS WITH YOUR OWN)
$yourRepositoryOwner = 'alexandreelise';
// Your repository name (CHANGE THIS WITH YOUR OWN)
$yourRepositoryName = 'j4x-api-examples';
// 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' => 'yourownjoomlaapitoken',
'app-002' => 'yourownjoomlaapitoken',
'app-003' => 'yourownjoomlaapitoken',
];
$basePath = 'api/index.php/v1';
// Request timeout
$timeout = 10;
// PHP Generator to efficiently process GitHub GraphQL Api response
$generator = function (string $dataSourceResponse, array $appIndexes): Generator {
if (empty($dataSourceResponse))
{
yield new RuntimeException('Github GraphQL Api response MUST NOT be empty', 422);
}
$defaultKeys = [
'id',
'access',
'title',
'alias',
'catid',
'articletext',
'introtext',
'fulltext',
'language',
'metadesc',
'metakey',
'state',
'featured',
'tokenindex',
];
// Assess robustness of the code by trying random key order
//shuffle($mergedKeys);
$resource = json_decode($dataSourceResponse);
if ($resource === false)
{
yield new RuntimeException('Could not read response', 500);
}
try
{
$repositoryName = $resource->data->repository->name;
$repositoryUrl = $resource->data->repository->url;
$id = 0;
$title = sprintf('Stargazers of your %s Github repository', $repositoryName);
$alias = '';
$catid = 2;
$language = '*';
$metadesc = '';
$metakey = '';
$state = 1;
$featured = 0;
$access = 1;
//choosen random tokenindex to deploy result to random url matched by this tokenindex
$tokenindex = array_rand($appIndexes);
$introtext = <<<HTML
<p>Want to know who are your repo stargazers? Here they are</p>
HTML;
$stargazerList = '';
foreach ($resource->data->repository->stargazers->edges as $stargazer)
{
$stargazerList .= sprintf('<li>%s</li>', $stargazer->node->name);
}
$fulltext = <<<HTML
<p>The stargazers for your GitHub repository named: <a href="$repositoryUrl" title="Your Github repository $repositoryName stargazers" target="_blank" rel="noopener nofollow">$repositoryName</a></p>
<ul>
$stargazerList
</ul>
HTML;
$articletext = <<<HTML
$introtext
<hr id="system-readmore" />
$fulltext
HTML;
yield json_encode(
array_combine(
$defaultKeys,
[
$id,
$access,
$title,
$alias,
$catid,
$articletext,
$introtext,
$fulltext,
$language,
$metadesc,
$metakey,
$state,
$featured,
$tokenindex,
]
)
);
} finally
{
echo 'DONE processing data' . PHP_EOL;
}
};
// 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');
};
// 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 = 'POST';
$graphQL = <<<GRAPHQL
{"query": "query {
repository(owner:\"$yourRepositoryOwner\", name:\"$yourRepositoryName\") {
name
url
stargazerCount
stargazers(last:10){
edges {
node {
name
}
}
}
}
}"}
GRAPHQL;
$dataSourceDataString = str_replace(["\n", "\r", "\t"], '', trim($graphQL));
$dataSourceHeaders = [
'Accept: application/json',
'Accept-Encoding: deflate, gzip, br',
'Content-Type: application/json',
'Connection: keep-alive',
'User-Agent: Alexeli/1.0.0',
sprintf('Content-Length: %d', mb_strlen($dataSourceDataString)),
sprintf('Authorization: Bearer %s', trim($dataSourceToken)),
];
$dataSourceTimeout = 3;
$dataSourceTransport = curl_init();
try
{
$dataSourceResponse = $process($dataSourceHttpVerb, $dataSourceUrl, $dataSourceDataString, $dataSourceHeaders, $dataSourceTimeout, $dataSourceTransport);
$streamData = $generator($dataSourceResponse, $baseUrl);
$storage = [];
foreach ($streamData as $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[] = ['mightExists' => $decodedJsonOutput->errors[0]->code === 400, 'decodedDataString' => $decodedDataString];
continue;
}
echo $output . PHP_EOL;
}
catch (Throwable $streamDataThrowable)
{
echo $streamDataThrowable->getMessage() . 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->getMessage() . PHP_EOL;
continue;
} finally
{
curl_close($storageCurl);
}
}
}
catch (Throwable $e)
{
echo $e->getMessage() . PHP_EOL;
} finally
{
curl_close($dataSourceTransport);
}