/
DiagnoseModules.module
177 lines (140 loc) · 6.44 KB
/
DiagnoseModules.module
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
<?php
/**
* Hooks into ProcessDiagnostics to add additional module version related information.
*/
class DiagnoseModules extends Wire implements Module, ConfigurableModule
{
public static function getModuleInfo()
{
return array(
'title' => __('Module version checking', __FILE__),
'summary' => __('Allows collection of module version diagnostics', __FILE__),
'version' => 101,
'permanent' => false,
'autoload' => false,
'singular' => true,
'requires' => 'ProcessDiagnostics',
'installs' => 'ProcessDiagnostics',
'author' => 'Nico Knoll',
);
}
private $defaultServiceUrl = 'http://modules.processwire.com/export-json/';
private $defaultServiceKey = 'pw231';
private $lang;
private $maxCacheAge = 804600; // in seconds (7 * 24 * 60 * 60)
public function init()
{
$this->addHookAfter('ProcessDiagnostics::collectDiagnostics', $this, 'AppendDiagnostics');
$this->lang = array(
'updateAvailable' => $this->_("Update available"),
'newVersion' => $this->_("There is a new version available."),
'clickHere' => $this->_("Click here to go to the update page."),
'upToDate' => $this->_("Up to date"),
'warning' => $this->_("Warning")
);
// reset cache
$updated = $this->getCache('_updated');
$updated = $updated[0];
if((time() - $updated) > $this->maxCacheAge || (int)$this->input->get->modulecachereset == 1) $this->resetCache();
// Javascript config and script
$this->config->js('DiagnosticsModules', $this->lang);
$this->config->scripts->add($this->config->urls->DiagnoseModules . "DiagnoseModules.js");
// if ajax Request don't otput markup
if($this->input->get->moduleAjax != '') {
echo $this->ajaxRemoteVersion($this->input->get->moduleAjax);
exit;
}
}
private function ajaxRemoteVersion($moduleClassName) {
$moduleInfo = wire('modules')->getModuleInfo($moduleClassName);
$localVersion = $this->formatVersion($moduleInfo['version']);
$url = trim($this->defaultServiceUrl, '/') . "/".$moduleClassName."/?apikey=" . $this->defaultServiceKey;
$http = new WireHttp();
$data = json_decode($http->get($url));
if($data->status != 'error') {
$remoteVersion = $this->formatVersion($data->module_version);
$version = $remoteVersion;
} else {
$version = $localVersion;
}
$this->setCache($moduleClassName, (array)$version);
return $version;
}
/**
* Returns a diagnostic array about installed modules and looks for updates
*/
public function GetDiagnostics()
{
foreach($this->modules as $module) {
$moduleInfo = wire('modules')->getModuleInfo($module->className());
// don't show core modules
$filename = $this->config->urls->{$module->className()};
if(preg_match('%^\/wire\/%Uis', $filename)) continue;
$localVersion = $this->formatVersion($moduleInfo['version']);
// check if core module
$core = false;
$pathParts = explode('/', str_replace($this->config->urls->root, '', $this->config->urls->{$module->className()}), 2);
if($pathParts[0] == 'wire') {
$core = true;
}
// only show installes /site/ modules
if($core == false) {
// if not cached
if(!count($this->getCache($module->className()))) {
$results[] = array(
'title' => $moduleInfo['title'].' ('.$module->className().')',
'value' => '<span class="module'.$module->className().' moduleValue" data-version="'.$localVersion.'" data-module="'.$module->className().'"></span>',
'status' => '<span class="module'.$module->className().' moduleStatus modulePending" data-version="'.$localVersion.'" data-module="'.$module->className().'">'.$this->_("Pending").'</span>',
'action' => '<span class="module'.$module->className().' moduleAction" data-version="'.$localVersion.'" data-module="'.$module->className().'"></span>',
);
} else {
// if cache file exists
$data = $this->getCache($module->className());
$updatable = ($data[0] > $localVersion);
$updateUrl = $this->config->urls->admin.'module/?update='.$module->className();
$results[] = array(
'title' => $moduleInfo['title'].' ('.$module->className().')',
'value' => $updatable ? $this->_('Update available') :
$this->_('Up to date'),
'status' => $updatable ? ProcessDiagnostics::$warn : ProcessDiagnostics::$ok,
'action' => $updatable ? sprintf($this->_('There is a new version available. <a href="%s">Click here to go to update page.</a>'), $updateUrl) : '',
);
}
}
}
return $results;
}
private function setCache($key, $value) {
$data = wire('modules')->getModuleConfigData($this);
$data[$key] = $value;
$data['_updated'] = time();
wire('modules')->saveModuleConfigData($this, $data);
}
private function getCache($key) {
$data = wire('modules')->getModuleConfigData($this);
if(in_array($key, array_keys($data))) {
$value = $data[$key];
} else {
$value = array();
}
return (array)$value;
}
private function resetCache() {
wire('modules')->saveModuleConfigData($this, array());
$this->setCache('_updated', time());
}
protected function formatVersion($version)
{
return preg_replace('/(\d)(?=\d)/', '$1.', str_pad( (string)str_replace(',', '.', $version), 3, '0', STR_PAD_LEFT));
}
public function AppendDiagnostics($event)
{
$results = $event->return;
$results[$this->_('Module Version Diagnostics')] = $this->GetDiagnostics();
$event->return = $results;
}
public function __set($a, $b) {}
public static function getModuleConfigInputfields(array $data) {
return array();
}
}