XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
ModuleUpdater.class.php
1<?php
11
12if (!defined('XOOPS_ROOT_PATH')) {
13 exit();
14}
15
16require_once XOOPS_LEGACY_PATH . '/admin/class/ModuleInstallUtils.class.php';
17
49class Legacy_ModulePhasedUpgrader
50{
63 public $_mMilestone = [];
64
71 public $mLog = null;
72
77 public $_mCurrentXoopsModule;
78
82 public $_mCurrentVersion;
83
88 public $_mTargetXoopsModule;
89
93 public $_mTargetVersion;
94
98 public $_mForceMode = false;
99
100 public function Legacy_ModulePhasedUpgrader() {
101 self::__construct();
102 }
103
104 public function __construct()
105 {
106 $this->mLog =new Legacy_ModuleInstallLog();
107 }
108
113 public function setForceMode($isForceMode)
114 {
115 $this->_mForceMode = $isForceMode;
116 }
117
128 public function setCurrentXoopsModule(&$xoopsModule)
129 {
130 $handler =& xoops_gethandler('module');
131 $cloneModule =& $handler->create();
132
133 $cloneModule->unsetNew();
134 $cloneModule->set('mid', $xoopsModule->get('mid'));
135 $cloneModule->set('name', $xoopsModule->get('name'));
136 $cloneModule->set('version', $xoopsModule->get('version'));
137 $cloneModule->set('last_update', $xoopsModule->get('last_update'));
138 $cloneModule->set('weight', $xoopsModule->get('weight'));
139 $cloneModule->set('isactive', $xoopsModule->get('isactive'));
140 $cloneModule->set('dirname', $xoopsModule->get('dirname'));
141 $cloneModule->set('hasmain', $xoopsModule->get('hasmain'));
142 $cloneModule->set('hasadmin', $xoopsModule->get('hasadmin'));
143 $cloneModule->set('hassearch', $xoopsModule->get('hassearch'));
144 $cloneModule->set('hasconfig', $xoopsModule->get('hasconfig'));
145 $cloneModule->set('hascomments', $xoopsModule->get('hascomments'));
146 $cloneModule->set('hasnotification', $xoopsModule->get('hasnotification'));
147
148 $this->_mCurrentXoopsModule =& $cloneModule;
149 $this->_mCurrentVersion = $cloneModule->get('version');
150 }
151
158 public function setTargetXoopsModule(&$xoopsModule)
159 {
160 $this->_mTargetXoopsModule =& $xoopsModule;
161 $this->_mTargetVersion = $this->getTargetPhase();
162 }
163
171 public function executeUpgrade()
172 {
173 if ($this->hasUpgradeMethod()) {
174 return $this->_callUpgradeMethod();
175 } else {
176 return $this->executeAutomaticUpgrade();
177 }
178 }
179
185 public function getCurrentVersion()
186 {
187 return $this->_mCurrentVersion;
188 }
189
196 public function getTargetPhase()
197 {
198 ksort($this->_mMilestone);
199
200 foreach ($this->_mMilestone as $t_version => $t_value) {
201 if ($t_version > $this->getCurrentVersion()) {
202 return $t_version;
203 }
204 }
205
206 return $this->_mTargetXoopsModule->get('version');
207 }
208
212 public function hasUpgradeMethod()
213 {
214 ksort($this->_mMilestone);
215
216 foreach ($this->_mMilestone as $t_version => $t_value) {
217 if ($t_version > $this->getCurrentVersion()) {
218 if (is_callable([$this, $t_value])) {
219 return true;
220 }
221 }
222 }
223
224 return false;
225 }
226
234 public function _callUpgradeMethod()
235 {
236 ksort($this->_mMilestone);
237
238 foreach ($this->_mMilestone as $t_version => $t_value) {
239 if ($t_version > $this->getCurrentVersion()) {
240 if (is_callable([$this, $t_value])) {
241 return $this->$t_value();
242 }
243 }
244 }
245
246 return false;
247 }
248
255 public function isLatestUpgrade()
256 {
257 return ($this->_mTargetXoopsModule->get('version') == $this->getTargetPhase());
258 }
259
266 public function saveXoopsModule(&$module)
267 {
268 $handler =& xoops_gethandler('module');
269 if ($handler->insert($module)) {
270 $this->mLog->addReport('XCL Module is updated.');
271 } else {
272 $this->mLog->addError('Could not update module information.');
273 }
274 }
275
276 public function _processScript()
277 {
278 $installScript = trim($this->_mTargetXoopsModule->getInfo('onUpdate'));
279 if (false != $installScript) {
280 require_once XOOPS_MODULE_PATH . '/' . $this->_mTargetXoopsModule->get('dirname') . '/' . $installScript;
281 $funcName = 'xoops_module_update_' . $this->_mTargetXoopsModule->get('dirname');
282 if (function_exists($funcName)) {
283 // Because X2 can use reference parameter, Legacy doesn't use the following code;'
284 //if (!call_user_func($funcName, $this->_mTargetXoopsModule, $this->getCurrentVersion(), new XCube_Ref($this->mLog))) {
285
286 $result = $funcName($this->_mTargetXoopsModule, $this->getCurrentVersion(), new XCube_Ref($this->mLog));
287 if (!$result) {
288 $this->mLog->addError('Failed to execute ' . $funcName);
289 }
290 }
291 }
292 }
293
294 public function _processReport()
295 {
296 if (!$this->mLog->hasError()) {
297 $this->mLog->add(XCube_Utils::formatString(_AD_LEGACY_MESSAGE_UPDATING_MODULE_SUCCESSFUL, $this->_mCurrentXoopsModule->get('name')));
298 } else {
299 $this->mLog->addError(XCube_Utils::formatString(_AD_LEGACY_ERROR_UPDATING_MODULE_FAILURE, $this->_mCurrentXoopsModule->get('name')));
300 }
301 }
302
309 public function _updateModuleTemplates()
310 {
311 Legacy_ModuleInstallUtils::clearAllOfModuleTemplatesForUpdate($this->_mTargetXoopsModule, $this->mLog);
312 Legacy_ModuleInstallUtils::installAllOfModuleTemplates($this->_mTargetXoopsModule, $this->mLog);
313 }
314
321 public function _updateBlocks()
322 {
323 Legacy_ModuleInstallUtils::smartUpdateAllOfBlocks($this->_mTargetXoopsModule, $this->mLog);
324 }
325
332 public function _updatePreferences()
333 {
334 Legacy_ModuleInstallUtils::smartUpdateAllOfPreferences($this->_mTargetXoopsModule, $this->mLog);
335 }
336
346 public function executeAutomaticUpgrade()
347 {
348 $this->mLog->addReport(_AD_LEGACY_MESSAGE_UPDATE_STARTED);
349
350 //
351 // Updates all of module templates
352 //
353 $this->_updateModuleTemplates();
354 if (!$this->_mForceMode && $this->mLog->hasError()) {
355 $this->_processReport();
356 return false;
357 }
358
359 //
360 // Update blocks.
361 //
362 $this->_updateBlocks();
363 if (!$this->_mForceMode && $this->mLog->hasError()) {
364 $this->_processReport();
365 return false;
366 }
367
368 //
369 // Update preferences & notifications.
370 //
371 $this->_updatePreferences();
372 if (!$this->_mForceMode && $this->mLog->hasError()) {
373 $this->_processReport();
374 return false;
375 }
376
377 //
378 // Update module object.
379 //
380 $this->saveXoopsModule($this->_mTargetXoopsModule);
381 if (!$this->_mForceMode && $this->mLog->hasError()) {
382 $this->_processReport();
383 return false;
384 }
385
386 //
387 // call back 'onUpdate'
388 //
389 $this->_processScript();
390 if (!$this->_mForceMode && $this->mLog->hasError()) {
391 $this->_processReport();
392 return false;
393 }
394
395 $this->_processReport();
396
397 return true;
398 }
399}
static installAllOfModuleTemplates(&$module, &$log)
static formatString()
[Static] Formats string with special care for international.