-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProcessWireUpgradeCheck.module
More file actions
430 lines (364 loc) · 12.1 KB
/
ProcessWireUpgradeCheck.module
File metadata and controls
430 lines (364 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?php namespace ProcessWire;
/**
* ProcessWire Upgrade Check
*
* Automatically checks for core and installed module upgrades at routine intervals.
* Also provides the API functions for repository info used by ProcessWireUpgrade module.
*
* ProcessWire
* Copyright (C) 2021 by Ryan Cramer
* Licensed under MPL 2.0
*
* https://processwire.com
*
* @property bool $useLoginHook
*
*/
class ProcessWireUpgradeCheck extends WireData implements Module {
/**
* Return information about this module (required)
*
*/
public static function getModuleInfo() {
return array(
'title' => 'Upgrades Checker',
'summary' => 'Automatically checks for core and installed module upgrades at routine intervals.',
'version' => 9,
'autoload' => "template=admin",
'singular' => true,
'author' => 'Ryan Cramer',
'icon' => 'coffee',
'requires' => 'ProcessWireUpgrade',
);
}
const tagsURL = 'https://api.github.com/repos/processwire/processwire/tags';
const branchesURL = 'https://api.github.com/repos/processwire/processwire/branches';
const versionURL = 'https://raw.githubusercontent.com/processwire/processwire/{branch}/wire/core/ProcessWire.php';
const zipURL = 'https://github.com/processwire/processwire/archive/{branch}.zip';
const timeout = 4.5;
/**
* Repository information indexed by branch
*
* @var array
*
*/
protected $repos = array();
/**
* Construct
*
*/
public function __construct() {
$this->set('useLoginHook', 1);
$this->repos = array(
'main' => array(
'branchesURL' => self::branchesURL,
'zipURL' => self::zipURL,
'versionURL' => self::versionURL,
'tagsURL' => self::tagsURL,
),
);
}
/**
* Initialize and perform access checks
*
*/
public function init() {
if($this->useLoginHook) {
$this->session->addHookAfter('login', $this, 'loginHook');
}
}
/**
* Check for upgrades at login (superuser only)
*
* @param null|HookEvent $e
*
*/
public function loginHook($e = null) {
if($e) {} // ignored HookEvent
if(!$this->user->isSuperuser()) return; // only show messages to superuser
$moduleVersions = array();
$cache = $this->cache;
$cacheName = $this->className() . "_loginHook";
$cacheData = $cache ? $cache->get($cacheName) : null;
if(!empty($cacheData) && is_string($cacheData)) $cache = json_decode($cacheData, true);
$branches = empty($cacheData) ? $this->getCoreBranches(false) : $cacheData['branches'];
if(empty($branches)) return;
$master = $branches['master'];
$branch = null;
$new = version_compare($master['version'], $this->config->version);
if($new > 0) {
// master is newer than current
$branch = $master;
} else if($new < 0 && isset($branches['dev'])) {
// we will assume dev branch
$dev = $branches['dev'];
$new = version_compare($dev['version'], $this->config->version);
if($new > 0) $branch = $dev;
}
if($branch) {
$versionStr = "$branch[name] $branch[version]";
$msg = $this->_('A ProcessWire core upgrade is available') . " ($versionStr)";
$this->message($msg);
} else {
$this->message($this->_('Your ProcessWire core is up-to-date'));
}
if($this->config->moduleServiceKey) {
$n = 0;
if(empty($cacheData) || empty($cacheData['moduleVersions'])) {
$moduleVersions = $this->getModuleVersions(true);
} else {
$moduleVersions = $cacheData['moduleVersions'];
}
foreach($moduleVersions as $name => $info) {
$msg = sprintf($this->_('An upgrade for %s is available'), $name) . " ($info[remote])";
$this->message($msg);
$n++;
}
if(!$n) $this->message($this->_('Your modules are up-to-date'));
}
if($cache) {
$cacheData = array(
'branches' => $branches,
'moduleVersions' => $moduleVersions
);
$cache->save($cacheName, $cacheData, 43200); // 43200=12hr
}
}
/**
* Get versions of core or modules
*
* @param bool $refresh
* @return array of array(
* 'ModuleName' => array(
* 'title' => 'Module Title',
* 'local' => '1.2.3', // current installed version
* 'remote' => '1.2.4', // directory version available, or boolean false if not found in directory
* 'new' => true|false, // true if newer version available, false if not
* 'requiresVersions' => array('ModuleName' => array('>', '1.2.3')), // module requirements (for modules only)
* 'branch' => 'master', // branch name (for core only)
* )
* )
*
*/
public function getVersions($refresh = false) {
$versions = array();
$branches = $this->getCoreBranches(false, $refresh);
foreach($branches as $branchName => $branch) {
$name = "ProcessWire $branchName";
$new = version_compare($branch['version'], $this->config->version);
$versions[$name] = array(
'title' => "ProcessWire Core ($branch[title])",
'icon' => 'microchip',
'local' => $this->config->version,
'remote' => $branch['version'],
'new' => $new,
'branch' => $branch['name'],
'author' => 'ProcessWire',
'href' => '',
'summary' => '',
'urls' => isset($branch['urls']) ? $branch['urls'] : array()
);
}
if($this->config->moduleServiceKey) {
foreach($this->getModuleVersions(false, $refresh) as $name => $info) {
$versions[$name] = $info;
}
}
return $versions;
}
protected function getModuleInfoVerbose($name) {
$info = $this->modules->getModuleInfoVerbose($name);
return $info;
}
/**
* Check all site modules for newer versions from the directory
*
* @param bool $onlyNew Only return array of modules with new versions available
* @param bool $refresh
* @return array of array(
* 'ModuleName' => array(
* 'title' => 'Module Title',
* 'local' => '1.2.3', // current installed version
* 'remote' => '1.2.4', // directory version available, or boolean false if not found in directory
* 'new' => true|false, // true if newer version available, false if not
* 'requiresVersions' => array('ModuleName' => array('>', '1.2.3')), // module requirements
* )
* )
* @throws WireException
*
*/
public function getModuleVersions($onlyNew = false, $refresh = false) {
$modules = $this->modules;
$config = $this->config;
if(!$this->config->moduleServiceKey) {
throw new WireException('Missing /site/config.php: value for $config->moduleServiceKey');
}
$url =
$config->moduleServiceURL .
"?apikey=" . $config->moduleServiceKey .
"&version=2" .
"&limit=100" .
"&field=module_version,version,requires,urls" .
"&class_name=";
$names = array();
$versions = array();
foreach($modules as $module) {
$name = $module->className();
$info = $this->getModuleInfoVerbose($name);
if($info['core']) continue;
$names[] = $name;
$versions[$name] = array(
'title' => $info['title'],
'summary' => empty($info['summary']) ? '' : $info['summary'],
'icon' => empty($info['icon']) ? '' : $info['icon'],
'author' => empty($info['author']) ? '' : $info['author'],
'href' => empty($info['href']) ? '' : $info['href'],
'local' => $modules->formatVersion($info['version']),
'remote' => false,
'new' => 0,
'requiresVersions' => $info['requiresVersions'],
'installs' => $info['installs'],
);
}
if(!count($names)) return array();
ksort($versions);
$url .= implode(',', $names);
$data = $refresh ? null : $this->session->getFor($this, 'moduleVersionsData');
if(empty($data)) {
// if not cached
$http = new WireHttp();
$this->wire($http);
$http->setTimeout(self::timeout);
$data = $http->getJSON($url);
$this->session->setFor($this, 'moduleVersionsData', $data);
if(!is_array($data)) {
$error = $http->getError();
if(!$error) $error = $this->_('Error retrieving modules directory data');
$this->error($error . " (" . $this->className() . ")");
return array();
}
}
$newVersions = array();
$installedBy = array(); // moduleName => installedByModuleName
foreach($data['items'] as $item) {
$name = $item['class_name'];
if(empty($versions[$name]) || empty($versions[$name]['local'])) continue;
$versions[$name]['remote'] = $item['version'];
$new = version_compare($versions[$name]['remote'], $versions[$name]['local']);
$versions[$name]['new'] = $new;
$versions[$name]['urls'] = $item['urls'];
$versions[$name]['installer'] = '';
$versions[$name]['pro'] = !empty($item['pro']);
if(!empty($versions[$name]['installs'])) {
foreach($versions[$name]['installs'] as $installsName) {
$installedBy[$installsName] = $name;
}
}
if($new <= 0) {
// local is up-to-date or newer than remote
if($onlyNew) unset($versions[$name]);
continue;
}
// remote is newer than local
$versions[$name]['requiresVersions'] = $item['requires'];
if($new > 0 && !$onlyNew) {
$newVersions[$name] = $versions[$name];
unset($versions[$name]);
}
}
if($onlyNew) {
foreach($versions as $name => $item) {
if($item['remote'] === false) unset($versions[$name]);
}
} else if(count($newVersions)) {
$versions = $newVersions + $versions;
}
foreach($versions as $name => $item) {
if(!empty($versions[$name]['remote'])) continue;
if(!isset($installedBy[$name])) continue;
$versions[$name]['installer'] = $installedBy[$name];
$installer = $versions[$installedBy[$name]];
if(!empty($installer['pro'])) $versions[$name]['pro'] = true;
}
return $versions;
}
/**
* Get all available branches with info for each
*
* @param bool $throw Whether or not to throw exceptions on error (default=true)
* @param bool $refresh Specify true to refresh data from web service
* @return array of branches each with:
* - name (string) i.e. dev
* - title (string) i.e. Development
* - zipURL (string) URL to zip download file
* - version (string) i.e. 2.5.0
* - versionURL (string) URL to we pull version from
* @throws WireException
*
*/
public function getCoreBranches($throw = true, $refresh = false) {
if(!$refresh) {
$branches = $this->session->getFor($this, 'branches');
if($branches && count($branches)) return $branches;
}
$branches = array();
foreach($this->repos as $repoName => $repo) {
$http = new WireHttp();
$this->wire($http);
$http->setTimeout(self::timeout);
$http->setHeader('User-Agent', 'ProcessWireUpgrade');
$json = $http->get($repo['branchesURL']);
$loadError = $this->_('Error loading GitHub branches') . ' - ' . $repo['branchesURL'];
if(!$json) {
$error = $loadError;
$error .= ' - ' . $this->_('HTTP error(s):') . ' ' . $http->getError();
$error .= ' - ' . $this->_('Check that HTTP requests are not blocked by your server.');
if($throw) throw new WireException($error);
$this->error($error);
return array();
}
$data = json_decode($json, true);
if(!$data) {
$error = $loadError;
if($throw) throw new WireException($error);
$this->error($error);
return array();
}
foreach($data as $key => $info) {
if(empty($info['name'])) continue;
$name = $info['name'];
$branch = array(
'name' => $name,
'title' => ucfirst($name),
'zipURL' => str_replace('{branch}', $name, $repo['zipURL']),
'version' => '',
'versionURL' => str_replace('{branch}', $name, $repo['versionURL']),
);
$content = $http->get($branch['versionURL']);
if(!preg_match_all('/const\s+version(Major|Minor|Revision)\s*=\s*(\d+)/', $content, $matches)) {
$branch['version'] = '?';
continue;
}
$version = array();
foreach($matches[1] as $k => $var) {
$version[$var] = (int) $matches[2][$k];
}
$branch['version'] = "$version[Major].$version[Minor].$version[Revision]";
$url = 'https://github.com/processwire/processwire';
$branch['urls'] = array(
'repo' => ($name === 'dev' ? "$url/tree/dev" : $url),
'download' => "$url/archive/refs/heads/$name.zip",
'support' => 'https://processwire.com/talk/',
);
if($repoName != 'main') {
$name = "$repoName-$name";
$branch['name'] = $name;
$branch['title'] = ucfirst($repoName) . "/$branch[title]";
}
$branches[$name] = $branch;
}
}
$this->session->setFor($this, 'branches', $branches);
return $branches;
}
}