-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathGithubModuleUpdate.php
More file actions
339 lines (281 loc) · 12.1 KB
/
GithubModuleUpdate.php
File metadata and controls
339 lines (281 loc) · 12.1 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
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
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2025 webtrees development team
* <http://webtrees.net>
*
* CustomModuleManager (webtrees custom module):
* Copyright (C) 2025 Markus Hemprich
* <http://www.familienforschung-hemprich.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*
* CustomModuleManager
*
* A weebtrees(https://webtrees.net) 2.2 custom module to manage custom modules
*
*/
declare(strict_types=1);
namespace Jefferson49\Webtrees\Module\CustomModuleManager\ModuleUpdates;
use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Module\ModuleInterface;
use Fisharebest\Webtrees\Services\ModuleService;
use Jefferson49\Webtrees\Exceptions\GithubCommunicationError;
use Jefferson49\Webtrees\Helpers\GithubService;
use Jefferson49\Webtrees\Module\CustomModuleManager\CustomModuleManager;
use Jefferson49\Webtrees\Module\CustomModuleManager\Exceptions\CustomModuleManagerException;
/**
* Update API for a custom module, which is hosted in a Github repository
*/
class GithubModuleUpdate extends AbstractModuleUpdate implements CustomModuleUpdateInterface
{#
const NAME = 'GitHub';
//The Github repository of the module, e.g. Jefferson49/CustomModuleManager
protected string $github_repo;
//Whether we shall get the latest version from Github instead from the module itself
protected bool $get_latest_version_from_github;
//A tag prefix for the module version, e.g. 'v' ('1.2.3' => 'v1.2.3')
protected string $tag_prefix;
//Whether the Github repository does not have a release
protected bool $no_release;
//The default branch of the Github repository. Used to download the source ZIP file for some modules, which do not provide a release
protected string $default_branch;
// The module service
protected ModuleService $module_service;
// The Custom Module Manager module
protected ModuleInterface $custom_module_manager;
/**
* @param string $module_name The custom module name
* @param array $params The configuration parameters of the update service
*
* @return void
*/
public function __construct(string $module_name, array $params) {
$this->module_name = $module_name;
$this->module_service = new ModuleService();
$this->custom_module_manager = $this->module_service->findByName(CustomModuleManager::activeModuleName());
if (array_key_exists('github_repo', $params)) {
$this->github_repo = $params['github_repo'];
}
else {
throw new CustomModuleManagerException(I18N::translate('Could not create the %s update service. Configuration parameter "%s" missing.', basename(str_replace('\\', '/', __CLASS__)) , 'github_repo'));
}
if (array_key_exists('get_latest_version_from_github', $params)) {
$this->get_latest_version_from_github = $params['get_latest_version_from_github'];
}
else {
$this->get_latest_version_from_github = false;
}
if (array_key_exists('tag_prefix', $params)) {
$this->tag_prefix = $params['tag_prefix'];
}
else {
$this->tag_prefix = '';
}
if (array_key_exists('no_release', $params)) {
$this->no_release = $params['no_release'];
}
else {
$this->no_release = false;
}
if (array_key_exists('default_branch', $params)) {
$this->default_branch = $params['default_branch'];
}
else {
$this->default_branch = '';
}
$this->category = self::identifyCategoryFromConfig($module_name, $params);
}
/**
* The name of the module update service
*
* @return string
*/
public function name(): string {
return self::NAME;
}
/**
* Where can we download the module
*
* @param string $version The version of the module; latest version if empty
*
* @return string
*/
public function downloadUrl(string $version = ''): string
{
$download_url = '';
//For certain modules, which do not provide a release, we take the URL of the source code ZIP file of the default branch
if ($this->no_release) {
return 'https://github.com/' . $this->github_repo . '/archive/refs/heads/' . $this->default_branch . '.zip';
}
$github_api_token = $this->custom_module_manager->getPreference(CustomModuleManager::PREF_GITHUB_API_TOKEN, '');
// Get the download URL from Github
try {
$download_url = GithubService::downloadUrl($this->github_repo, $version, $this->tag_prefix, $github_api_token);
}
catch (GithubCommunicationError $ex) {
// Can't connect to GitHub?
$message = I18N::translate('Communication error with %s', $this->name()) . ': ' .
I18N::translate('Cannot retrieve download URL.') . "\n" .
$ex->getMessage();
throw new CustomModuleManagerException($message);
}
return $download_url;
}
/**
* Where can we find a documentation for the module
*
* @return string
*/
public function documentationUrl(): string
{
return 'https://github.com/'. $this->github_repo;
}
/**
* Get the GitHub repository
*
* @return string
*/
public function getGithubRepo(): string
{
return $this->github_repo;
}
/**
* Fetch the latest version of this module
*
* @param bool $fetch_latest Whether to fetch the latest version, e.g. from a GitHub repository
*
* @return string
*/
public function customModuleLatestVersion(bool $fetch_latest = false): string
{
$latest_version = '';
$stored_version = '';
$module = $this->getModule();
// If the installed module is available, try to get latest version from the module
if ($module !== null && !$fetch_latest && !$this->get_latest_version_from_github) {
$latest_version = $module->customModuleLatestVersion();
$short_module_name = substr($module->name(), 0, 25) . '_';
$stored_version = $this->custom_module_manager->getPreference($short_module_name . CustomModuleManager::PREF_LATEST_VERSION, '');
// If we had retrieved and stored a later version before
if (CustomModuleManager::versionCompare($module->name(), $stored_version, $latest_version) > 0) {
$latest_version = $stored_version;
}
}
// For certain modules, which do not provide a release, try to get the latest version by update URL
if ($latest_version === '' && $module !== null && $this->no_release) {
$latest_version = self::getLatestVersionByUpdateURL($module);
}
// Otherwise, try to get the latest version from Github
elseif ($latest_version === '' && $this->github_repo !== '') {
$github_api_token = $this->custom_module_manager->getPreference(CustomModuleManager::PREF_GITHUB_API_TOKEN, '');
try {
$latest_version = GithubService::getLatestReleaseTag($this->github_repo, $github_api_token);
}
catch (GithubCommunicationError $ex) {
// Can't connect to GitHub?
if (!CustomModuleManager::rememberGithubCommunciationError()) {
FlashMessages::addMessage(I18N::translate('Communication error with %s', self::NAME), 'danger');
}
}
}
if ($module !== null) {
$short_module_name = substr($module->name(), 0, 25) . '_';
// If a later version was retrieved, store it
if (CustomModuleManager::versionCompare($module->name(), $latest_version, $this->customModuleVersion()) > 0) {
$this->custom_module_manager->setPreference($short_module_name . CustomModuleManager::PREF_LATEST_VERSION, $latest_version);
}
// Otherwise, reset stored version
elseif ($stored_version !== '') {
$this->custom_module_manager->setPreference($short_module_name . CustomModuleManager::PREF_LATEST_VERSION, '');
}
}
return $latest_version;
}
/**
* Get the release notes for the latest version of this module
*
* @return string
*/
public function getLatestReleaseNotes(): string
{
$github_api_token = $this->custom_module_manager->getPreference(CustomModuleManager::PREF_GITHUB_API_TOKEN, '');
//Get the latest releases note from GitHub
try {
return GithubService::getLatestReleaseNotes($this->github_repo, $github_api_token);
}
catch (GithubCommunicationError $ex) {
// Can't connect to GitHub?
return I18N::translate('Could not retrieve release notes due to a communication error with GitHub.');
}
}
/**
* Whether the module provides releases in the repository
*
* @return bool
*/
public function providesReleasesInRepository(): bool {
return !$this->no_release;
}
/**
* Get the cached download count for this module.
*
* Fetches the maximum download count across the latest 3 releases' assets.
* The result is cached for 24 hours using module preferences to avoid hitting
* GitHub API rate limits on every page load.
*
* @return int The download count, or -1 if unavailable
*/
public function getDownloadCount(): int
{
$short_module_name = substr($this->module_name, 0, 25) . '_';
$cache_key = $short_module_name . CustomModuleManager::PREF_DOWNLOAD_COUNT;
$cache_time_key = $short_module_name . CustomModuleManager::PREF_DOWNLOAD_COUNT_TIME;
// Check if we have a cached value that is less than 24 hours old
$cached_time = (int) $this->custom_module_manager->getPreference($cache_time_key, '0');
$cached_count = $this->custom_module_manager->getPreference($cache_key, '');
if ($cached_count !== '' && (time() - $cached_time) < 86400) {
return (int) $cached_count;
}
// If the module does not provide releases, we cannot get download counts
if ($this->no_release) {
return -1;
}
$github_api_token = $this->custom_module_manager->getPreference(CustomModuleManager::PREF_GITHUB_API_TOKEN, '');
try {
$download_count = GithubService::getRecentReleasesMaxDownloads($this->github_repo, $github_api_token);
// Cache the result
$this->custom_module_manager->setPreference($cache_key, (string) $download_count);
$this->custom_module_manager->setPreference($cache_time_key, (string) time());
return $download_count;
} catch (GithubCommunicationError $ex) {
// If we have a stale cached value, return it rather than nothing
if ($cached_count !== '') {
return (int) $cached_count;
}
if (!CustomModuleManager::rememberGithubCommunciationError()) {
FlashMessages::addMessage(I18N::translate('Communication error with %s', self::NAME), 'danger');
}
return -1;
}
}
/**
* Get the latest release URL
*
* @return string
*/
public function getLatestReleaseURL(): string {
return 'https://github.com/' . $this->github_repo . '/releases/latest';
}
}