-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.php
More file actions
280 lines (239 loc) · 9.26 KB
/
Copy pathupdate.php
File metadata and controls
280 lines (239 loc) · 9.26 KB
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
#!/usr/bin/env php
<?php
const PROJECT_ROOT = __DIR__;
const MANIFEST_FILE = PROJECT_ROOT . '/shortcodes.xml';
const UPDATE_FILE = PROJECT_ROOT . '/update.xml';
const GITHUB_REPO = 'PopArtDesign/joomla-shortcodes';
const CLIENT_TYPE = 'site';
const TARGET_PLATFORM_VERSION = '(4\.|5\.|6\.)';
const MIN_PHP_VERSION = '7.4';
/**
* Main function to orchestrate the update process.
*
* @param array $argv Command-line arguments.
*/
function main(array $argv): void
{
$version = parseArguments($argv);
$pluginData = parseManifest(MANIFEST_FILE, $version);
$pluginData['version'] = $version;
$pluginData['phpMinimumVersion'] = MIN_PHP_VERSION;
$pluginData['downloadUrl'] = buildDownloadUrl(GITHUB_REPO, $pluginData['version']);
$pluginData['infoUrl'] = buildInfoUrl(GITHUB_REPO, $pluginData['version']);
$pluginData['clientType'] = CLIENT_TYPE;
$pluginData['targetPlatformVersion'] = TARGET_PLATFORM_VERSION;
$tempZipPath = tempnam(sys_get_temp_dir(), 'joomla-update');
try {
downloadZipFile($pluginData['downloadUrl'], $tempZipPath);
$pluginData['checksums'] = calculateChecksums($tempZipPath);
updateUpdateXml(UPDATE_FILE, $pluginData);
} finally {
if (file_exists($tempZipPath)) {
@unlink($tempZipPath);
echo "Temporary ZIP file deleted.\n";
}
}
}
// Run the main function
main($argv);
/**
* Parses command-line arguments and returns the version.
*
* @param array $argv The command-line arguments.
* @return string|null The version string or null if not provided.
*/
function parseArguments(array $argv): ?string
{
if (isset($argv[1])) {
$version = $argv[1];
if (!preg_match('/^\d+\.\d+\.\d+$/', $version)) {
exitWithError("Invalid version format. Expected X.Y.Z");
}
return $version;
}
return null;
}
/**
* Loads and parses shortcoder.xml, extracting plugin details.
*
* @param string $filePath Path to Manifest.
* @param string|null &$version Reference to version; updated if not provided by arguments.
*
* @return array Extracted plugin data.
*/
function parseManifest(string $filePath, ?string &$version): array
{
$shortcoderXml = simplexml_load_file($filePath);
if ($shortcoderXml === false) {
exitWithError("Could not load " . basename($filePath));
}
$data = [
'pluginName' => (string) $shortcoderXml->name,
'pluginDescription' => (string) $shortcoderXml->description,
'author' => (string) $shortcoderXml->author,
'authorUrl' => (string) $shortcoderXml->authorUrl,
'extensionType' => (string) $shortcoderXml['type'],
'extensionGroup' => (string) $shortcoderXml['group'],
'pluginElement' => '',
];
// If version was not provided as an argument, get it from shortcoder.xml
if ($version === null) {
$version = (string)$shortcoderXml->version;
if (empty($version)) {
exitWithError("Version not specified and could not be read from " . basename($filePath));
}
echo "Using version from " . basename($filePath) . ": " . $version . "\n";
}
// Extract element from <files><folder plugin="shortcoder">
foreach ($shortcoderXml->files->folder as $folder) {
if (isset($folder['plugin'])) {
$data['pluginElement'] = (string)$folder['plugin'];
break;
}
}
if (empty($data['pluginElement'])) {
exitWithError("Could not determine plugin element from " . basename($filePath));
}
return $data;
}
/**
* Constructs the download URL for the ZIP file.
*/
function buildDownloadUrl(string $githubRepo, string $version): string
{
return sprintf(
'https://github.com/%s/archive/refs/tags/v%s.zip',
$githubRepo,
$version
);
}
/**
* Constructs the info URL for the release.
*/
function buildInfoUrl(string $githubRepo, string $version): string
{
return sprintf(
'https://github.com/%s/releases/tag/v%s',
$githubRepo,
$version
);
}
/**
* Downloads a ZIP file and saves it to a temporary path.
*
* @param string $downloadUrl The URL to download from.
* @param string $tempZipPath The path to save the downloaded file.
*/
function downloadZipFile(string $downloadUrl, string $tempZipPath): void
{
echo "Attempting to download: " . $downloadUrl . "\n";
$zipContent = @file_get_contents($downloadUrl);
if ($zipContent === false) {
exitWithError("Failed to download ZIP file from " . $downloadUrl);
}
if (file_put_contents($tempZipPath, $zipContent) === false) {
exitWithError("Failed to save ZIP file to " . $tempZipPath);
}
echo "ZIP file downloaded successfully to " . $tempZipPath . "\n";
}
/**
* Calculates SHA256, SHA384, and SHA512 checksums for a given file.
*
* @param string $filePath The path to the file.
*
* @return array An associative array of checksums.
*/
function calculateChecksums(string $filePath): array
{
$sha256 = hash_file('sha256', $filePath);
$sha384 = hash_file('sha384', $filePath);
$sha512 = hash_file('sha512', $filePath);
if ($sha256 === false || $sha384 === false || $sha512 === false) {
exitWithError("Failed to calculate checksums for " . $filePath);
}
echo "Checksums calculated.
";
return ['sha256' => $sha256, 'sha384' => $sha384, 'sha512' => $sha512];
}
/**
* Updates the update.xml file with a new update entry.
*
* @param string $updateXmlPath Path to update.xml.
* @param array $data All data required for the update entry.
*/
function updateUpdateXml(string $updateXmlPath, array $data): void
{
$dom = new DOMDocument('1.0', 'utf-8');
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
if (file_exists($updateXmlPath)) {
if (!$dom->load($updateXmlPath)) {
exitWithError("Could not load existing " . basename($updateXmlPath));
}
} else {
$updates = $dom->createElement('updates');
$dom->appendChild($updates);
}
$updatesRoot = $dom->getElementsByTagName('updates')->item(0);
if ($updatesRoot === null) {
exitWithError("<updates> root element not found or created.");
}
// Remove existing update for this version if it exists
$existingUpdateNode = null;
foreach ($updatesRoot->getElementsByTagName('update') as $existingUpdate) {
$existingVersion = $existingUpdate->getElementsByTagName('version')->item(0);
if ($existingVersion && $existingVersion->nodeValue === $data['version']) {
echo "Warning: Update for version " . $data['version'] . " already exists. Overwriting.\n";
$existingUpdateNode = $existingUpdate;
break;
}
}
if ($existingUpdateNode) {
$updatesRoot->removeChild($existingUpdateNode);
}
$updateNode = $dom->createElement('update');
$updateNode->appendChild($dom->createElement('name', $data['pluginName']));
$updateNode->appendChild($dom->createElement('description', $data['pluginDescription']));
$updateNode->appendChild($dom->createElement('element', $data['pluginElement']));
$updateNode->appendChild($dom->createElement('type', $data['extensionType']));
$updateNode->appendChild($dom->createElement('folder', $data['extensionGroup']));
$updateNode->appendChild($dom->createElement('client', $data['clientType']));
$updateNode->appendChild($dom->createElement('maintainer', $data['author']));
$updateNode->appendChild($dom->createElement('maintainerurl', $data['authorUrl']));
$updateNode->appendChild($dom->createElement('version', $data['version']));
$downloadsNode = $dom->createElement('downloads');
$downloadUrlNode = $dom->createElement('downloadurl', $data['downloadUrl']);
$downloadUrlNode->setAttribute('type', 'full');
$downloadUrlNode->setAttribute('format', 'zip');
$downloadsNode->appendChild($downloadUrlNode);
$updateNode->appendChild($downloadsNode);
$updateNode->appendChild($dom->createElement('sha512', $data['checksums']['sha512']));
$updateNode->appendChild($dom->createElement('sha384', $data['checksums']['sha384']));
$updateNode->appendChild($dom->createElement('sha256', $data['checksums']['sha256']));
$infoUrlNode = $dom->createElement('infourl', $data['infoUrl']);
$infoUrlNode->setAttribute('title', $data['pluginName']);
$updateNode->appendChild($infoUrlNode);
$targetPlatformNode = $dom->createElement('targetplatform');
$targetPlatformNode->setAttribute('name', 'joomla');
$targetPlatformNode->setAttribute('version', $data['targetPlatformVersion']);
$updateNode->appendChild($targetPlatformNode);
$updateNode->appendChild($dom->createElement('php_minimum', $data['phpMinimumVersion']));
$updatesRoot->appendChild($updateNode);
if (($xml = $dom->saveXML()) === false) {
exitWithError('Failed to generate XML');
}
if (file_put_contents($updateXmlPath, $xml) === false) {
exitWithError("Failed to save " . basename($updateXmlPath));
}
echo basename($updateXmlPath) . " updated successfully.\n";
}
/**
* Exits the script with an error message.
*
* @param string $message The error message.
*/
function exitWithError(string $message): void
{
fprintf(STDERR, "Error: %s\n", $message);
exit(1);
}