]>
Commit | Line | Data |
---|---|---|
510377d2 A |
1 | <?php |
2 | ||
3 | /** | |
4 | * Class Updater. | |
5 | * Used to update stuff when a new Shaarli's version is reached. | |
6 | * Update methods are ran only once, and the stored in a JSON file. | |
7 | */ | |
8 | class Updater | |
9 | { | |
10 | /** | |
11 | * @var array Updates which are already done. | |
12 | */ | |
13 | protected $doneUpdates; | |
14 | ||
510377d2 A |
15 | /** |
16 | * @var LinkDB instance. | |
17 | */ | |
18 | protected $linkDB; | |
19 | ||
20 | /** | |
21 | * @var bool True if the user is logged in, false otherwise. | |
22 | */ | |
23 | protected $isLoggedIn; | |
24 | ||
25 | /** | |
26 | * @var ReflectionMethod[] List of current class methods. | |
27 | */ | |
28 | protected $methods; | |
29 | ||
30 | /** | |
31 | * Object constructor. | |
32 | * | |
33 | * @param array $doneUpdates Updates which are already done. | |
510377d2 A |
34 | * @param LinkDB $linkDB LinkDB instance. |
35 | * @param boolean $isLoggedIn True if the user is logged in. | |
36 | */ | |
684e662a | 37 | public function __construct($doneUpdates, $linkDB, $isLoggedIn) |
510377d2 A |
38 | { |
39 | $this->doneUpdates = $doneUpdates; | |
510377d2 A |
40 | $this->linkDB = $linkDB; |
41 | $this->isLoggedIn = $isLoggedIn; | |
42 | ||
43 | // Retrieve all update methods. | |
44 | $class = new ReflectionClass($this); | |
45 | $this->methods = $class->getMethods(); | |
46 | } | |
47 | ||
48 | /** | |
49 | * Run all new updates. | |
50 | * Update methods have to start with 'updateMethod' and return true (on success). | |
51 | * | |
52 | * @return array An array containing ran updates. | |
53 | * | |
54 | * @throws UpdaterException If something went wrong. | |
55 | */ | |
56 | public function update() | |
57 | { | |
58 | $updatesRan = array(); | |
59 | ||
60 | // If the user isn't logged in, exit without updating. | |
61 | if ($this->isLoggedIn !== true) { | |
62 | return $updatesRan; | |
63 | } | |
64 | ||
65 | if ($this->methods == null) { | |
66 | throw new UpdaterException('Couldn\'t retrieve Updater class methods.'); | |
67 | } | |
68 | ||
69 | foreach ($this->methods as $method) { | |
70 | // Not an update method or already done, pass. | |
71 | if (! startsWith($method->getName(), 'updateMethod') | |
72 | || in_array($method->getName(), $this->doneUpdates) | |
73 | ) { | |
74 | continue; | |
75 | } | |
76 | ||
77 | try { | |
78 | $method->setAccessible(true); | |
79 | $res = $method->invoke($this); | |
80 | // Update method must return true to be considered processed. | |
81 | if ($res === true) { | |
82 | $updatesRan[] = $method->getName(); | |
83 | } | |
84 | } catch (Exception $e) { | |
85 | throw new UpdaterException($method, $e); | |
86 | } | |
87 | } | |
88 | ||
89 | $this->doneUpdates = array_merge($this->doneUpdates, $updatesRan); | |
90 | ||
91 | return $updatesRan; | |
92 | } | |
93 | ||
94 | /** | |
95 | * @return array Updates methods already processed. | |
96 | */ | |
97 | public function getDoneUpdates() | |
98 | { | |
99 | return $this->doneUpdates; | |
100 | } | |
101 | ||
102 | /** | |
103 | * Move deprecated options.php to config.php. | |
104 | * | |
105 | * Milestone 0.9 (old versioning) - shaarli/Shaarli#41: | |
106 | * options.php is not supported anymore. | |
107 | */ | |
108 | public function updateMethodMergeDeprecatedConfigFile() | |
109 | { | |
684e662a | 110 | $conf = ConfigManager::getInstance(); |
510377d2 | 111 | |
684e662a A |
112 | if (is_file($conf->get('config.DATADIR') . '/options.php')) { |
113 | include $conf->get('config.DATADIR') . '/options.php'; | |
510377d2 A |
114 | |
115 | // Load GLOBALS into config | |
684e662a A |
116 | $allowedKeys = array_merge(ConfigPhp::$ROOT_KEYS); |
117 | $allowedKeys[] = 'config'; | |
510377d2 | 118 | foreach ($GLOBALS as $key => $value) { |
684e662a A |
119 | if (in_array($key, $allowedKeys)) { |
120 | $conf->set($key, $value); | |
121 | } | |
510377d2 | 122 | } |
684e662a A |
123 | $conf->write($this->isLoggedIn); |
124 | unlink($conf->get('config.DATADIR').'/options.php'); | |
510377d2 A |
125 | } |
126 | ||
127 | return true; | |
128 | } | |
21979ff1 A |
129 | |
130 | /** | |
131 | * Rename tags starting with a '-' to work with tag exclusion search. | |
132 | */ | |
133 | public function updateMethodRenameDashTags() | |
134 | { | |
684e662a | 135 | $conf = ConfigManager::getInstance(); |
528a6f8a | 136 | $linklist = $this->linkDB->filterSearch(); |
21979ff1 A |
137 | foreach ($linklist as $link) { |
138 | $link['tags'] = preg_replace('/(^| )\-/', '$1', $link['tags']); | |
139 | $link['tags'] = implode(' ', array_unique(LinkFilter::tagsStrToArray($link['tags'], true))); | |
140 | $this->linkDB[$link['linkdate']] = $link; | |
141 | } | |
684e662a | 142 | $this->linkDB->savedb($conf->get('config.PAGECACHE')); |
21979ff1 A |
143 | return true; |
144 | } | |
b74b96bf A |
145 | |
146 | /** | |
147 | * Move old configuration in PHP to the new config system in JSON format. | |
148 | * | |
149 | * Will rename 'config.php' into 'config.save.php' and create 'config.json'. | |
150 | */ | |
151 | public function updateMethodConfigToJson() | |
152 | { | |
153 | $conf = ConfigManager::getInstance(); | |
154 | ||
155 | // JSON config already exists, nothing to do. | |
156 | if ($conf->getConfigIO() instanceof ConfigJson) { | |
157 | return true; | |
158 | } | |
159 | ||
160 | $configPhp = new ConfigPhp(); | |
161 | $configJson = new ConfigJson(); | |
162 | $oldConfig = $configPhp->read($conf::$CONFIG_FILE . '.php'); | |
163 | rename($conf->getConfigFile(), $conf::$CONFIG_FILE . '.save.php'); | |
164 | $conf->setConfigIO($configJson); | |
165 | $conf->reload(); | |
166 | ||
167 | foreach (ConfigPhp::$ROOT_KEYS as $key) { | |
168 | $conf->set($key, $oldConfig[$key]); | |
169 | } | |
170 | ||
171 | // Set sub config keys (config and plugins) | |
172 | $subConfig = array('config', 'plugins'); | |
173 | foreach ($subConfig as $sub) { | |
174 | foreach ($oldConfig[$sub] as $key => $value) { | |
175 | $conf->set($sub .'.'. $key, $value); | |
176 | } | |
177 | } | |
178 | ||
179 | try{ | |
180 | $conf->write($this->isLoggedIn); | |
181 | return true; | |
182 | } catch (IOException $e) { | |
183 | error_log($e->getMessage()); | |
184 | return false; | |
185 | } | |
186 | } | |
510377d2 A |
187 | } |
188 | ||
189 | /** | |
190 | * Class UpdaterException. | |
191 | */ | |
192 | class UpdaterException extends Exception | |
193 | { | |
194 | /** | |
195 | * @var string Method where the error occurred. | |
196 | */ | |
197 | protected $method; | |
198 | ||
199 | /** | |
200 | * @var Exception The parent exception. | |
201 | */ | |
202 | protected $previous; | |
203 | ||
204 | /** | |
205 | * Constructor. | |
206 | * | |
207 | * @param string $message Force the error message if set. | |
208 | * @param string $method Method where the error occurred. | |
209 | * @param Exception|bool $previous Parent exception. | |
210 | */ | |
211 | public function __construct($message = '', $method = '', $previous = false) | |
212 | { | |
213 | $this->method = $method; | |
214 | $this->previous = $previous; | |
215 | $this->message = $this->buildMessage($message); | |
216 | } | |
217 | ||
218 | /** | |
219 | * Build the exception error message. | |
220 | * | |
221 | * @param string $message Optional given error message. | |
222 | * | |
223 | * @return string The built error message. | |
224 | */ | |
225 | private function buildMessage($message) | |
226 | { | |
227 | $out = ''; | |
228 | if (! empty($message)) { | |
229 | $out .= $message . PHP_EOL; | |
230 | } | |
231 | ||
232 | if (! empty($this->method)) { | |
233 | $out .= 'An error occurred while running the update '. $this->method . PHP_EOL; | |
234 | } | |
235 | ||
236 | if (! empty($this->previous)) { | |
237 | $out .= ' '. $this->previous->getMessage(); | |
238 | } | |
239 | ||
240 | return $out; | |
241 | } | |
242 | } | |
243 | ||
510377d2 A |
244 | /** |
245 | * Read the updates file, and return already done updates. | |
246 | * | |
247 | * @param string $updatesFilepath Updates file path. | |
248 | * | |
249 | * @return array Already done update methods. | |
250 | */ | |
251 | function read_updates_file($updatesFilepath) | |
252 | { | |
253 | if (! empty($updatesFilepath) && is_file($updatesFilepath)) { | |
254 | $content = file_get_contents($updatesFilepath); | |
255 | if (! empty($content)) { | |
256 | return explode(';', $content); | |
257 | } | |
258 | } | |
259 | return array(); | |
260 | } | |
261 | ||
262 | /** | |
263 | * Write updates file. | |
264 | * | |
265 | * @param string $updatesFilepath Updates file path. | |
266 | * @param array $updates Updates array to write. | |
267 | * | |
268 | * @throws Exception Couldn't write version number. | |
269 | */ | |
270 | function write_updates_file($updatesFilepath, $updates) | |
271 | { | |
272 | if (empty($updatesFilepath)) { | |
273 | throw new Exception('Updates file path is not set, can\'t write updates.'); | |
274 | } | |
275 | ||
276 | $res = file_put_contents($updatesFilepath, implode(';', $updates)); | |
277 | if ($res === false) { | |
278 | throw new Exception('Unable to write updates in '. $updatesFilepath . '.'); | |
279 | } | |
280 | } |