Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
359 lines (313 loc) · 10.5 KB

File metadata and controls

359 lines (313 loc) · 10.5 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<?php
/**
* ProcessWire Upgrade Check
*
* Automatically checks for core and installed module upgrades at routine intervals.
*
* ProcessWire 2.x
* Copyright (C) 2014 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://processwire.com
*
*/
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' => 5,
'autoload' => "template=admin",
'singular' => true,
'author' => 'Ryan Cramer',
'icon' => 'coffee'
);
}
const branchesURL = 'https://api.github.com/repos/ryancramerdesign/Processwire/branches';
const versionURL = 'https://raw.githubusercontent.com/ryancramerdesign/ProcessWire/{branch}/wire/core/ProcessWire.php';
const zipURL = 'https://github.com/ryancramerdesign/ProcessWire/archive/{branch}.zip';
const timeout = 4.5;
public function __construct() {
$this->set('useLoginHook', 1);
}
/**
* Initialize and perform access checks
*
*/
public function init() {
if($this->useLoginHook) {
$this->session->addHookAfter('login', $this, 'loginHook');
}
}
/**
* Check for upgrades at login (superuser only)
*
*/
public function loginHook($e = null) {
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) {
// 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
*
* @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() {
$versions = array();
$branches = $this->getCoreBranches(false);
foreach($branches as $branch) {
$name = "ProcessWire $branch[name]";
$new = version_compare($branch['version'], $this->config->version);
$versions[$name] = array(
'title' => "ProcessWire Core ($branch[title])",
'local' => $this->config->version,
'remote' => $branch['version'],
'new' => $new,
'branch' => $branch['name'],
);
}
if($this->config->moduleServiceKey) {
foreach($this->getModuleVersions(false) as $name => $info) {
$versions[$name] = $info;
}
}
return $versions;
}
/**
* Cached module versions data
*
* @param array
*
*/
protected $getModuleVersionsData = array();
/**
* Check all site modules for newer versions from the directory
*
* @param bool $onlyNew Only return array of modules with new versions available
* @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) {
if(!$this->config->moduleServiceKey) throw new WireException("This feature requires ProcessWire 2.4.19+");
$url = $this->config->moduleServiceURL .
"?apikey=" . $this->config->moduleServiceKey .
"&limit=100" .
"&field=module_version,version,requires_versions" .
"&class_name=";
$names = array();
$versions = array();
foreach($this->modules as $module) {
$name = $module->className();
$info = $this->modules->getModuleInfoVerbose($name);
if($info['core']) continue;
$names[] = $name;
$versions[$name] = array(
'title' => $info['title'],
'local' => $this->modules->formatVersion($info['version']),
'remote' => false,
'new' => 0,
'requiresVersions' => $info['requiresVersions']
);
}
if(!count($names)) return array();
$url .= implode(',', $names);
$data = $this->getModuleVersionsData;
if(empty($data)) {
// if not cached
$http = new WireHttp();
$http->setTimeout(self::timeout);
$data = $http->getJSON($url);
$this->getModuleVersionsData = $data;
if(!is_array($data)) {
$error = $http->getError();
if(!$error) $error = $this->_('Error retrieving modules directory data');
$this->error($error . " (" . $this->className() . ")");
return array();
}
}
foreach($data['items'] as $item) {
$name = $item['class_name'];
$versions[$name]['remote'] = $item['module_version'];
$new = version_compare($versions[$name]['remote'], $versions[$name]['local']);
$versions[$name]['new'] = $new;
if($new <= 0) {
// local is up-to-date or newer than remote
if($onlyNew) unset($versions[$name]);
} else {
// remote is newer than local
$versions[$name]['requiresVersions'] = $item['requires_versions'];
}
}
if($onlyNew) foreach($versions as $name => $data) {
if($data['remote'] === false) unset($versions[$name]);
}
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->get('ProcessWireUpgrade_branches');
if($branches && count($branches)) return $branches;
}
$branches = array();
$http = new WireHttp();
$http->setTimeout(self::timeout);
$http->setHeader('User-Agent', 'ProcessWireUpgrade');
$json = $http->get(self::branchesURL);
if(!$json) {
$error = $this->_('Error loading GitHub branches') . ' - ' . self::branchesURL;
$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 = "Error JSON decoding GitHub branches " . self::branchesURL;
if($throw) throw new WireException($error);
$this->error($error);
return array();
}
foreach($data as $key => $info) {
$name = $info['name'];
$branch = array(
'name' => $name,
'title' => ucfirst($name),
'zipURL' => str_replace('{branch}', $name, self::zipURL),
'version' => '',
'versionURL' => str_replace('{branch}', $name, self::versionURL),
);
if($name == 'dev') $branch['title'] = 'Development';
if($name == 'master') $branch['title'] = 'Stable/Master';
$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]";
$branches[$name] = $branch;
}
$this->session->set('ProcessWireUpgrade_branches', $branches);
return $branches;
}
/**
* Get versions that are newer than given version
*
* @param string $version
* @param string $branchName optionlly limit to given branch name
* @return array of versions newer than current
* @todo I think we can drop this method
*
public function getNewerCoreVersions($version, $branchName = '') {
$branches = $this->getCoreBranches(false);
if(empty($branches)) return array();
$newerBranches = array();
$olderBranches = array();
foreach($branches as $branch) {
if($branchName && $branch['name'] != $branchName) continue;
if(version_compare($version, $branch['version']) >= 0) {
// current is newer than this branch
} else {
// branch is newer than this
$parts = explode('.', $branch['version']);
foreach($parts as $key => $value) {
$parts[$key] = str_pad($value, 3, '0', STR_PAD_LEFT);
}
$longVersion = (int) implode('', $parts);
while(isset($newerBranches[$longVersion])) $longVersion++;
$newerBranches[$longVersion] = $branch;
}
}
if(!count($newerBranches)) return array();
if(count($newerBranches > 1)) ksort($newerBranches);
return $newerBranches;
}
*/
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.