diff options
Diffstat (limited to 'application/Updater.php')
-rw-r--r-- | application/Updater.php | 209 |
1 files changed, 189 insertions, 20 deletions
diff --git a/application/Updater.php b/application/Updater.php index 555d4c25..034b8ed8 100644 --- a/application/Updater.php +++ b/application/Updater.php | |||
@@ -1,4 +1,7 @@ | |||
1 | <?php | 1 | <?php |
2 | use Shaarli\Config\ConfigJson; | ||
3 | use Shaarli\Config\ConfigPhp; | ||
4 | use Shaarli\Config\ConfigManager; | ||
2 | 5 | ||
3 | /** | 6 | /** |
4 | * Class Updater. | 7 | * Class Updater. |
@@ -69,8 +72,8 @@ class Updater | |||
69 | return $updatesRan; | 72 | return $updatesRan; |
70 | } | 73 | } |
71 | 74 | ||
72 | if ($this->methods == null) { | 75 | if ($this->methods === null) { |
73 | throw new UpdaterException('Couldn\'t retrieve Updater class methods.'); | 76 | throw new UpdaterException(t('Couldn\'t retrieve Updater class methods.')); |
74 | } | 77 | } |
75 | 78 | ||
76 | foreach ($this->methods as $method) { | 79 | foreach ($this->methods as $method) { |
@@ -133,21 +136,6 @@ class Updater | |||
133 | } | 136 | } |
134 | 137 | ||
135 | /** | 138 | /** |
136 | * Rename tags starting with a '-' to work with tag exclusion search. | ||
137 | */ | ||
138 | public function updateMethodRenameDashTags() | ||
139 | { | ||
140 | $linklist = $this->linkDB->filterSearch(); | ||
141 | foreach ($linklist as $key => $link) { | ||
142 | $link['tags'] = preg_replace('/(^| )\-/', '$1', $link['tags']); | ||
143 | $link['tags'] = implode(' ', array_unique(LinkFilter::tagsStrToArray($link['tags'], true))); | ||
144 | $this->linkDB[$key] = $link; | ||
145 | } | ||
146 | $this->linkDB->save($this->conf->get('resource.page_cache')); | ||
147 | return true; | ||
148 | } | ||
149 | |||
150 | /** | ||
151 | * Move old configuration in PHP to the new config system in JSON format. | 139 | * Move old configuration in PHP to the new config system in JSON format. |
152 | * | 140 | * |
153 | * Will rename 'config.php' into 'config.save.php' and create 'config.json.php'. | 141 | * Will rename 'config.php' into 'config.save.php' and create 'config.json.php'. |
@@ -258,6 +246,91 @@ class Updater | |||
258 | } | 246 | } |
259 | 247 | ||
260 | /** | 248 | /** |
249 | <<<<<<< HEAD | ||
250 | ======= | ||
251 | * Rename tags starting with a '-' to work with tag exclusion search. | ||
252 | */ | ||
253 | public function updateMethodRenameDashTags() | ||
254 | { | ||
255 | $linklist = $this->linkDB->filterSearch(); | ||
256 | foreach ($linklist as $key => $link) { | ||
257 | $link['tags'] = preg_replace('/(^| )\-/', '$1', $link['tags']); | ||
258 | $link['tags'] = implode(' ', array_unique(LinkFilter::tagsStrToArray($link['tags'], true))); | ||
259 | $this->linkDB[$key] = $link; | ||
260 | } | ||
261 | $this->linkDB->save($this->conf->get('resource.page_cache')); | ||
262 | return true; | ||
263 | } | ||
264 | |||
265 | /** | ||
266 | * Initialize API settings: | ||
267 | * - api.enabled: true | ||
268 | * - api.secret: generated secret | ||
269 | */ | ||
270 | public function updateMethodApiSettings() | ||
271 | { | ||
272 | if ($this->conf->exists('api.secret')) { | ||
273 | return true; | ||
274 | } | ||
275 | |||
276 | $this->conf->set('api.enabled', true); | ||
277 | $this->conf->set( | ||
278 | 'api.secret', | ||
279 | generate_api_secret( | ||
280 | $this->conf->get('credentials.login'), | ||
281 | $this->conf->get('credentials.salt') | ||
282 | ) | ||
283 | ); | ||
284 | $this->conf->write($this->isLoggedIn); | ||
285 | return true; | ||
286 | } | ||
287 | |||
288 | /** | ||
289 | * New setting: theme name. If the default theme is used, nothing to do. | ||
290 | * | ||
291 | * If the user uses a custom theme, raintpl_tpl dir is updated to the parent directory, | ||
292 | * and the current theme is set as default in the theme setting. | ||
293 | * | ||
294 | * @return bool true if the update is successful, false otherwise. | ||
295 | */ | ||
296 | public function updateMethodDefaultTheme() | ||
297 | { | ||
298 | // raintpl_tpl isn't the root template directory anymore. | ||
299 | // We run the update only if this folder still contains the template files. | ||
300 | $tplDir = $this->conf->get('resource.raintpl_tpl'); | ||
301 | $tplFile = $tplDir . '/linklist.html'; | ||
302 | if (! file_exists($tplFile)) { | ||
303 | return true; | ||
304 | } | ||
305 | |||
306 | $parent = dirname($tplDir); | ||
307 | $this->conf->set('resource.raintpl_tpl', $parent); | ||
308 | $this->conf->set('resource.theme', trim(str_replace($parent, '', $tplDir), '/')); | ||
309 | $this->conf->write($this->isLoggedIn); | ||
310 | |||
311 | // Dependency injection gore | ||
312 | RainTPL::$tpl_dir = $tplDir; | ||
313 | |||
314 | return true; | ||
315 | } | ||
316 | |||
317 | /** | ||
318 | * Move the file to inc/user.css to data/user.css. | ||
319 | * | ||
320 | * Note: Due to hardcoded paths, it's not unit testable. But one line of code should be fine. | ||
321 | * | ||
322 | * @return bool true if the update is successful, false otherwise. | ||
323 | */ | ||
324 | public function updateMethodMoveUserCss() | ||
325 | { | ||
326 | if (! is_file('inc/user.css')) { | ||
327 | return true; | ||
328 | } | ||
329 | |||
330 | return rename('inc/user.css', 'data/user.css'); | ||
331 | } | ||
332 | |||
333 | /** | ||
261 | * * `markdown_escape` is a new setting, set to true as default. | 334 | * * `markdown_escape` is a new setting, set to true as default. |
262 | * | 335 | * |
263 | * If the markdown plugin was already enabled, escaping is disabled to avoid | 336 | * If the markdown plugin was already enabled, escaping is disabled to avoid |
@@ -278,6 +351,102 @@ class Updater | |||
278 | 351 | ||
279 | return true; | 352 | return true; |
280 | } | 353 | } |
354 | |||
355 | /** | ||
356 | * Add 'http://' to Piwik URL the setting is set. | ||
357 | * | ||
358 | * @return bool true if the update is successful, false otherwise. | ||
359 | */ | ||
360 | public function updateMethodPiwikUrl() | ||
361 | { | ||
362 | if (! $this->conf->exists('plugins.PIWIK_URL') || startsWith($this->conf->get('plugins.PIWIK_URL'), 'http')) { | ||
363 | return true; | ||
364 | } | ||
365 | |||
366 | $this->conf->set('plugins.PIWIK_URL', 'http://'. $this->conf->get('plugins.PIWIK_URL')); | ||
367 | $this->conf->write($this->isLoggedIn); | ||
368 | |||
369 | return true; | ||
370 | } | ||
371 | |||
372 | /** | ||
373 | * Use ATOM feed as default. | ||
374 | */ | ||
375 | public function updateMethodAtomDefault() | ||
376 | { | ||
377 | if (!$this->conf->exists('feed.show_atom') || $this->conf->get('feed.show_atom') === true) { | ||
378 | return true; | ||
379 | } | ||
380 | |||
381 | $this->conf->set('feed.show_atom', true); | ||
382 | $this->conf->write($this->isLoggedIn); | ||
383 | |||
384 | return true; | ||
385 | } | ||
386 | |||
387 | /** | ||
388 | * Update updates.check_updates_branch setting. | ||
389 | * | ||
390 | * If the current major version digit matches the latest branch | ||
391 | * major version digit, we set the branch to `latest`, | ||
392 | * otherwise we'll check updates on the `stable` branch. | ||
393 | * | ||
394 | * No update required for the dev version. | ||
395 | * | ||
396 | * Note: due to hardcoded URL and lack of dependency injection, this is not unit testable. | ||
397 | * | ||
398 | * FIXME! This needs to be removed when we switch to first digit major version | ||
399 | * instead of the second one since the versionning process will change. | ||
400 | */ | ||
401 | public function updateMethodCheckUpdateRemoteBranch() | ||
402 | { | ||
403 | if (SHAARLI_VERSION === 'dev' || $this->conf->get('updates.check_updates_branch') === 'latest') { | ||
404 | return true; | ||
405 | } | ||
406 | |||
407 | // Get latest branch major version digit | ||
408 | $latestVersion = ApplicationUtils::getLatestGitVersionCode( | ||
409 | 'https://raw.githubusercontent.com/shaarli/Shaarli/latest/shaarli_version.php', | ||
410 | 5 | ||
411 | ); | ||
412 | if (preg_match('/(\d+)\.\d+$/', $latestVersion, $matches) === false) { | ||
413 | return false; | ||
414 | } | ||
415 | $latestMajor = $matches[1]; | ||
416 | |||
417 | // Get current major version digit | ||
418 | preg_match('/(\d+)\.\d+$/', SHAARLI_VERSION, $matches); | ||
419 | $currentMajor = $matches[1]; | ||
420 | |||
421 | if ($currentMajor === $latestMajor) { | ||
422 | $branch = 'latest'; | ||
423 | } else { | ||
424 | $branch = 'stable'; | ||
425 | } | ||
426 | $this->conf->set('updates.check_updates_branch', $branch); | ||
427 | $this->conf->write($this->isLoggedIn); | ||
428 | return true; | ||
429 | } | ||
430 | |||
431 | /** | ||
432 | * Reset history store file due to date format change. | ||
433 | */ | ||
434 | public function updateMethodResetHistoryFile() | ||
435 | { | ||
436 | if (is_file($this->conf->get('resource.history'))) { | ||
437 | unlink($this->conf->get('resource.history')); | ||
438 | } | ||
439 | return true; | ||
440 | } | ||
441 | |||
442 | /** | ||
443 | * Save the datastore -> the link order is now applied when links are saved. | ||
444 | */ | ||
445 | public function updateMethodReorderDatastore() | ||
446 | { | ||
447 | $this->linkDB->save($this->conf->get('resource.page_cache')); | ||
448 | return true; | ||
449 | } | ||
281 | } | 450 | } |
282 | 451 | ||
283 | /** | 452 | /** |
@@ -324,7 +493,7 @@ class UpdaterException extends Exception | |||
324 | } | 493 | } |
325 | 494 | ||
326 | if (! empty($this->method)) { | 495 | if (! empty($this->method)) { |
327 | $out .= 'An error occurred while running the update '. $this->method . PHP_EOL; | 496 | $out .= t('An error occurred while running the update ') . $this->method . PHP_EOL; |
328 | } | 497 | } |
329 | 498 | ||
330 | if (! empty($this->previous)) { | 499 | if (! empty($this->previous)) { |
@@ -364,11 +533,11 @@ function read_updates_file($updatesFilepath) | |||
364 | function write_updates_file($updatesFilepath, $updates) | 533 | function write_updates_file($updatesFilepath, $updates) |
365 | { | 534 | { |
366 | if (empty($updatesFilepath)) { | 535 | if (empty($updatesFilepath)) { |
367 | throw new Exception('Updates file path is not set, can\'t write updates.'); | 536 | throw new Exception(t('Updates file path is not set, can\'t write updates.')); |
368 | } | 537 | } |
369 | 538 | ||
370 | $res = file_put_contents($updatesFilepath, implode(';', $updates)); | 539 | $res = file_put_contents($updatesFilepath, implode(';', $updates)); |
371 | if ($res === false) { | 540 | if ($res === false) { |
372 | throw new Exception('Unable to write updates in '. $updatesFilepath . '.'); | 541 | throw new Exception(t('Unable to write updates in '. $updatesFilepath . '.')); |
373 | } | 542 | } |
374 | } | 543 | } |