Skip to content

Commit 0e9fdf0

Browse files
Major update to include module version detection, automatic upgrade notifications (at login for superuser) and module upgrade links.
1 parent 2b6e6ed commit 0e9fdf0

File tree

3 files changed

+442
-91
lines changed

3 files changed

+442
-91
lines changed

ProcessWireUpgrade.module

Lines changed: 73 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* Tool to Upgrade ProcessWire Core
4+
* Tool to display available core and module upgrades for ProcessWire
55
*
66
* ProcessWire 2.x
77
* Copyright (C) 2014 by Ryan Cramer
@@ -19,16 +19,17 @@ class ProcessWireUpgrade extends Process {
1919
*/
2020
public static function getModuleInfo() {
2121
return array(
22-
'title' => 'Core Upgrade',
23-
'summary' => 'Upgrade ProcessWire core to latest master or dev version automatically in the admin.',
24-
'version' => 1,
22+
'title' => 'Upgrades',
23+
'summary' => 'Tool that helps you identify and install core and module upgrades.',
24+
'version' => 2,
2525
'author' => 'Ryan Cramer',
26+
'installs' => 'ProcessWireUpgradeCheck',
2627
'icon' => 'coffee'
2728
);
2829
}
2930

3031
const debug = false;
31-
const pageName = 'core-upgrade';
32+
const pageName = 'upgrades';
3233
const branchesURL = 'https://api.github.com/repos/ryancramerdesign/Processwire/branches';
3334
const versionURL = 'https://raw.githubusercontent.com/ryancramerdesign/ProcessWire/{branch}/wire/core/ProcessWire.php';
3435
const zipURL = 'https://github.com/ryancramerdesign/ProcessWire/archive/{branch}.zip';
@@ -67,6 +68,12 @@ class ProcessWireUpgrade extends Process {
6768
*/
6869
protected $renames = array(); // scheduled renames to occur after page render
6970

71+
/**
72+
* Instance of ProcessWireUpgradeCheck
73+
*
74+
*/
75+
protected $checker = null;
76+
7077
/**
7178
* Construct
7279
*
@@ -86,75 +93,23 @@ class ProcessWireUpgrade extends Process {
8693
if($this->config->demo) throw new WireException("This module cannot be used in demo mode");
8794
if(!$this->user->isSuperuser()) throw new WireException("This module requires superuser");
8895
set_time_limit(3600);
96+
$this->checker = $this->modules->getInstall('ProcessWireUpgradeCheck');
97+
if(!$this->checker) throw new WireException("Please go to Modules and click 'Check for new modules' - this will auto-update ProcessWireUpgrade.");
8998
parent::init();
9099
}
91100

92101
/**
93-
* Get all available branches with info for each
94-
*
95-
*/
96-
protected function getBranches() {
97-
98-
if(!$this->input->get->refresh_branches) {
99-
$branches = $this->session->get('ProcessWireUpgrade_branches');
100-
if($branches && count($branches)) return $branches;
101-
}
102-
103-
$branches = array();
104-
$http = new WireHttp();
105-
$http->setHeader('User-Agent', 'ProcessWireUpgrade');
106-
$json = $http->get(self::branchesURL);
107-
if(!$json) throw new WireException("Error loading GitHub branches " . self::branchesURL);
108-
109-
$data = json_decode($json, true);
110-
if(!$data) throw new WireException("Error JSON decoding GitHub branches $json " . self::branchesURL);
111-
112-
$this->message("Retrieved " . count($data) . " branches from GitHub");
113-
114-
foreach($data as $key => $info) {
115-
$name = $info['name'];
116-
$branch = array(
117-
'name' => $name,
118-
'title' => ucfirst($name),
119-
'zipURL' => str_replace('{branch}', $name, self::zipURL),
120-
'version' => '',
121-
'versionURL' => str_replace('{branch}', $name, self::versionURL),
122-
);
123-
124-
if($name == 'dev') $branch['title'] = 'Development';
125-
if($name == 'master') $branch['title'] = 'Stable/Master';
126-
127-
$content = $http->get($branch['versionURL']);
128-
if(!preg_match_all('/const\s+version(Major|Minor|Revision)\s*=\s*(\d+)/', $content, $matches)) {
129-
$branch['version'] = '?';
130-
continue;
131-
}
132-
133-
$version = array();
134-
foreach($matches[1] as $key => $var) {
135-
$version[$var] = (int) $matches[2][$key];
136-
}
137-
138-
$branch['version'] = "$version[Major].$version[Minor].$version[Revision]";
139-
$branches[$name] = $branch;
140-
}
141-
142-
$this->session->set('ProcessWireUpgrade_branches', $branches);
143-
return $branches;
144-
}
145-
146-
/**
147-
* Get info for either a specific branch, or for the currently selected branch
102+
* Get info for either a specific core branch, or for the currently selected core branch
148103
*
149104
*/
150105
protected function getBranch($name = '') {
151-
$branches = $this->getBranches();
106+
$branches = $this->checker->getCoreBranches();
152107
if(empty($name)) $name = $this->session->get('ProcessWireUpgrade_branch');
153108
return isset($branches[$name]) ? $branches[$name] : array();
154109
}
155110

156111
/**
157-
* Set the current branch
112+
* Set the current core branch
158113
*
159114
*/
160115
protected function setBranch($name) {
@@ -178,16 +133,10 @@ class ProcessWireUpgrade extends Process {
178133
$this->error("Please note that your current PHP version (" . PHP_VERSION . ") is not adequate to upgrade to the latest ProcessWire.");
179134
}
180135

181-
$out = '';
136+
if(!extension_loaded('pdo_mysql')) $this->error("Your PHP is not compiled with PDO support. PDO is required by ProcessWire 2.4+.");
137+
if(!class_exists('ZipArchive')) $this->error("Your PHP does not have ZipArchive support. This is required to install core or module upgrades with this tool.");
182138

183-
if(!$this->config->debug) {
184-
$this->error(
185-
"While optional, we recommend that you enable debug mode during the upgrade " .
186-
"so that you will see detailed error messages, should they occur. " .
187-
"Do this by editing /site/config.php and setting the debug " .
188-
"option to true. Example: \$config->debug = true;"
189-
);
190-
}
139+
$out = '';
191140

192141
if(file_exists($this->cachePath) || file_exists($this->tempPath)) {
193142
$out = "<h2>Upgrade files are already present. Please remove them before continuing.</h2>";
@@ -198,23 +147,51 @@ class ProcessWireUpgrade extends Process {
198147
return $out;
199148
}
200149

201-
$out = "<h2>Please select a branch:</h2><p>";
202-
foreach($this->getBranches() as $name => $info) {
203-
$btn = $this->modules->get('InputfieldButton');
204-
$btn->href = "./check?branch=$name";
205-
$btn->icon = 'code-fork';
206-
$btn->value = "$info[title] - $info[version]";
207-
$out .= $btn->render();
150+
$table = $this->modules->get('MarkupAdminDataTable');
151+
$table->setEncodeEntities(false);
152+
$table->headerRow(array(
153+
$this->_('Module'),
154+
$this->_('Class'),
155+
$this->_('Installed'),
156+
$this->_('Latest'),
157+
$this->_('Status')
158+
));
159+
160+
$items = $this->checker->getVersions();
161+
if(count($items)) {
162+
foreach($items as $name => $item) {
163+
if(empty($item['remote'])) continue; // not in directory
164+
$remote = $this->sanitizer->entities($item['remote']);
165+
166+
if($item['new'] > 0) {
167+
$upgradeLabel = $this->_('Upgrade available');
168+
$remote = "<strong>$remote</strong>";
169+
} else if($item['new'] < 0) {
170+
$upgradeLabel = $this->_('Older than current');
171+
} else {
172+
$upgradeLabel = $this->_('Up-to-date');
173+
}
174+
if(empty($item['branch'])) {
175+
$upgradeURL = $this->wire('config')->urls->admin . "module/?update=$name";
176+
} else {
177+
$upgradeURL = "./check?branch=$item[branch]";
178+
}
179+
if($item['new'] > 0) $upgrade = "<a href='$upgradeURL'><i class='fa fa-lightbulb-o'></i> <strong>$upgradeLabel</strong></a>";
180+
else $upgrade = "<span class='detail'>$upgradeLabel</span>";
181+
// else if(!$item['remote']) $upgrade = "<span class='detail'>" . $this->_('Not in directory') . "</span>";
182+
$table->row(array(
183+
"<a href='$upgradeURL'>" . $this->sanitizer->entities($item['title']) . "</a>",
184+
$this->sanitizer->entities($name),
185+
$this->sanitizer->entities($item['local']),
186+
$remote,
187+
$upgrade
188+
));
189+
}
190+
} else {
208191
}
209192

210-
$btn = $this->modules->get('InputfieldButton');
211-
$btn->href = "./?refresh_branches=1";
212-
$btn->icon = 'refresh';
213-
$btn->value = "Refresh";
214-
$btn->addClass('ui-priority-secondary');
215-
$out .= $btn->render();
193+
$out .= $table->render();
216194

217-
$out .= "</p>";
218195
return $out;
219196
}
220197

@@ -233,6 +210,16 @@ class ProcessWireUpgrade extends Process {
233210
*
234211
*/
235212
public function executeCheck() {
213+
214+
if(!$this->config->debug) {
215+
$this->error(
216+
"While optional, we recommend that you enable debug mode during the upgrade " .
217+
"so that you will see detailed error messages, should they occur. " .
218+
"Do this by editing /site/config.php and setting the debug " .
219+
"option to true. Example: \$config->debug = true;"
220+
);
221+
}
222+
236223
$name = $this->input->get->branch;
237224
if(empty($name)) throw new WireException("No branch selected");
238225
$branch = $this->getBranch($name);
@@ -743,7 +730,7 @@ class ProcessWireUpgrade extends Process {
743730
// find the page we installed, locating it by the process field (which has the module ID)
744731
// it would probably be sufficient just to locate by name, but this is just to be extra sure.
745732
$moduleID = $this->modules->getModuleID($this);
746-
$page = $this->pages->get("template=admin, process=$moduleID, name=" . self::pageName);
733+
$page = $this->pages->get("template=admin, process=$moduleID, name=" . self::pageName . "|core-upgrade");
747734

748735
if($page->id) {
749736
// if we found the page, let the user know and delete it

0 commit comments

Comments
 (0)