1: <?php
2: /**
3: * Login system task for automated upgrade tasks.
4: *
5: * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
6: *
7: * See the enclosed file COPYING for license information (LGPL). If you
8: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
9: *
10: * @author Michael Slusarz <slusarz@horde.org>
11: * @category Horde
12: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
13: * @package Core
14: */
15: abstract class Horde_Core_LoginTasks_SystemTask_Upgrade extends Horde_LoginTasks_SystemTask
16: {
17: /**
18: * The interval at which to run the task.
19: *
20: * @var integer
21: */
22: public $interval = Horde_LoginTasks::EVERY;
23:
24: /**
25: * The current application.
26: *
27: * @var string
28: */
29: protected $_app = 'horde';
30:
31: /**
32: * Do these upgrade tasks require authentication?
33: *
34: * @var boolean
35: */
36: protected $_auth = false;
37:
38: /**
39: * The list of versions to upgrade.
40: *
41: * @var array
42: */
43: protected $_toupgrade = array();
44:
45: /**
46: * The list of versions which upgrades will occur.
47: *
48: * @var array
49: */
50: protected $_versions = array();
51:
52: /**
53: * Constructor.
54: */
55: public function __construct()
56: {
57: usort($this->_versions, 'version_compare');
58:
59: if ($vers = $this->_pref('get')) {
60: foreach ($this->_versions as $val) {
61: if (version_compare($vers, $val) === -1) {
62: $this->_toupgrade[] = $val;
63: }
64: }
65: } else {
66: $this->_toupgrade = $this->_versions;
67: }
68:
69: $this->active = !empty($this->_toupgrade);
70: }
71:
72: /**
73: * Perform upgrade tasks.
74: */
75: public function execute()
76: {
77: foreach ($this->_toupgrade as $val) {
78: $this->_upgrade($val);
79: }
80:
81: $this->_pref('set');
82: }
83:
84: /**
85: * Force re-run of all upgrade tasks.
86: */
87: public function forceUpgrade()
88: {
89: $this->active = true;
90: $this->_toupgrade = $this->_versions;
91: $this->execute();
92: }
93:
94: /**
95: * Perform upgrade tasks for a given version.
96: *
97: * For those running a git checkout, the system task for a given version
98: * will run continuously until that version is released. Code should
99: * be added to not convert already converted values.
100: *
101: * @param string $version A version string.
102: */
103: abstract protected function _upgrade($version);
104:
105: /**
106: */
107: public function skip()
108: {
109: /* Skip task until we are authenticated. */
110: return ($this->_auth &&
111: !$GLOBALS['registry']->isAuthenticated(array('app' => $this->_app)));
112: }
113:
114: /**
115: * Manage the upgrade preferences.
116: *
117: * @param string $action Either 'get' or 'set'.
118: *
119: * @return string The current version.
120: */
121: protected function _pref($action)
122: {
123: global $prefs, $registry;
124:
125: $key = $this->_app;
126: if ($this->_auth) {
127: $key .= '_auth';
128: }
129:
130: $upgrade = @unserialize($prefs->getValue('upgrade_tasks'));
131:
132: switch ($action) {
133: case 'get':
134: $val = isset($upgrade[$key])
135: ? $upgrade[$key]
136: : null;
137: break;
138:
139: case 'set':
140: $val = $registry->getVersion($this->_app, true);
141: $upgrade[$key] = $val;
142: $prefs->setValue('upgrade_tasks', serialize($upgrade));
143: break;
144: }
145:
146: return $val;
147: }
148:
149: }
150: