aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
Diffstat (limited to 'application')
-rw-r--r--application/config/ConfigPhp.php2
-rw-r--r--application/updater/Updater.php (renamed from application/Updater.php)134
-rw-r--r--application/updater/UpdaterUtils.php39
-rw-r--r--application/updater/exception/UpdaterException.php60
4 files changed, 125 insertions, 110 deletions
diff --git a/application/config/ConfigPhp.php b/application/config/ConfigPhp.php
index 9ed5d31f..cad34594 100644
--- a/application/config/ConfigPhp.php
+++ b/application/config/ConfigPhp.php
@@ -27,7 +27,7 @@ class ConfigPhp implements ConfigIO
27 /** 27 /**
28 * Map legacy config keys with the new ones. 28 * Map legacy config keys with the new ones.
29 * If ConfigPhp is used, getting <newkey> will actually look for <legacykey>. 29 * If ConfigPhp is used, getting <newkey> will actually look for <legacykey>.
30 * The Updater will use this array to transform keys when switching to JSON. 30 * The updater will use this array to transform keys when switching to JSON.
31 * 31 *
32 * @var array current key => legacy key. 32 * @var array current key => legacy key.
33 */ 33 */
diff --git a/application/Updater.php b/application/updater/Updater.php
index ca05ecc2..55251a30 100644
--- a/application/Updater.php
+++ b/application/updater/Updater.php
@@ -1,15 +1,24 @@
1<?php 1<?php
2 2
3namespace Shaarli\Updater;
4
5use ApplicationUtils;
6use Exception;
7use RainTPL;
8use ReflectionClass;
9use ReflectionException;
10use ReflectionMethod;
3use Shaarli\Bookmark\LinkDB; 11use Shaarli\Bookmark\LinkDB;
4use Shaarli\Bookmark\LinkFilter; 12use Shaarli\Bookmark\LinkFilter;
5use Shaarli\Config\ConfigJson; 13use Shaarli\Config\ConfigJson;
6use Shaarli\Config\ConfigPhp;
7use Shaarli\Config\ConfigManager; 14use Shaarli\Config\ConfigManager;
15use Shaarli\Config\ConfigPhp;
8use Shaarli\Exceptions\IOException; 16use Shaarli\Exceptions\IOException;
9use Shaarli\Thumbnailer; 17use Shaarli\Thumbnailer;
18use Shaarli\Updater\Exception\UpdaterException;
10 19
11/** 20/**
12 * Class Updater. 21 * Class updater.
13 * Used to update stuff when a new Shaarli's version is reached. 22 * Used to update stuff when a new Shaarli's version is reached.
14 * Update methods are ran only once, and the stored in a JSON file. 23 * Update methods are ran only once, and the stored in a JSON file.
15 */ 24 */
@@ -87,12 +96,12 @@ class Updater
87 } 96 }
88 97
89 if ($this->methods === null) { 98 if ($this->methods === null) {
90 throw new UpdaterException(t('Couldn\'t retrieve Updater class methods.')); 99 throw new UpdaterException(t('Couldn\'t retrieve updater class methods.'));
91 } 100 }
92 101
93 foreach ($this->methods as $method) { 102 foreach ($this->methods as $method) {
94 // Not an update method or already done, pass. 103 // Not an update method or already done, pass.
95 if (! startsWith($method->getName(), 'updateMethod') 104 if (!startsWith($method->getName(), 'updateMethod')
96 || in_array($method->getName(), $this->doneUpdates) 105 || in_array($method->getName(), $this->doneUpdates)
97 ) { 106 ) {
98 continue; 107 continue;
@@ -143,7 +152,7 @@ class Updater
143 } 152 }
144 } 153 }
145 $this->conf->write($this->isLoggedIn); 154 $this->conf->write($this->isLoggedIn);
146 unlink($this->conf->get('resource.data_dir').'/options.php'); 155 unlink($this->conf->get('resource.data_dir') . '/options.php');
147 } 156 }
148 157
149 return true; 158 return true;
@@ -178,10 +187,10 @@ class Updater
178 $subConfig = array('config', 'plugins'); 187 $subConfig = array('config', 'plugins');
179 foreach ($subConfig as $sub) { 188 foreach ($subConfig as $sub) {
180 foreach ($oldConfig[$sub] as $key => $value) { 189 foreach ($oldConfig[$sub] as $key => $value) {
181 if (isset($legacyMap[$sub .'.'. $key])) { 190 if (isset($legacyMap[$sub . '.' . $key])) {
182 $configKey = $legacyMap[$sub .'.'. $key]; 191 $configKey = $legacyMap[$sub . '.' . $key];
183 } else { 192 } else {
184 $configKey = $sub .'.'. $key; 193 $configKey = $sub . '.' . $key;
185 } 194 }
186 $this->conf->set($configKey, $value); 195 $this->conf->set($configKey, $value);
187 } 196 }
@@ -237,7 +246,7 @@ class Updater
237 return true; 246 return true;
238 } 247 }
239 248
240 $save = $this->conf->get('resource.data_dir') .'/datastore.'. date('YmdHis') .'.php'; 249 $save = $this->conf->get('resource.data_dir') . '/datastore.' . date('YmdHis') . '.php';
241 copy($this->conf->get('resource.datastore'), $save); 250 copy($this->conf->get('resource.datastore'), $save);
242 251
243 $links = array(); 252 $links = array();
@@ -311,7 +320,7 @@ class Updater
311 // We run the update only if this folder still contains the template files. 320 // We run the update only if this folder still contains the template files.
312 $tplDir = $this->conf->get('resource.raintpl_tpl'); 321 $tplDir = $this->conf->get('resource.raintpl_tpl');
313 $tplFile = $tplDir . '/linklist.html'; 322 $tplFile = $tplDir . '/linklist.html';
314 if (! file_exists($tplFile)) { 323 if (!file_exists($tplFile)) {
315 return true; 324 return true;
316 } 325 }
317 326
@@ -335,7 +344,7 @@ class Updater
335 */ 344 */
336 public function updateMethodMoveUserCss() 345 public function updateMethodMoveUserCss()
337 { 346 {
338 if (! is_file('inc/user.css')) { 347 if (!is_file('inc/user.css')) {
339 return true; 348 return true;
340 } 349 }
341 350
@@ -371,11 +380,11 @@ class Updater
371 */ 380 */
372 public function updateMethodPiwikUrl() 381 public function updateMethodPiwikUrl()
373 { 382 {
374 if (! $this->conf->exists('plugins.PIWIK_URL') || startsWith($this->conf->get('plugins.PIWIK_URL'), 'http')) { 383 if (!$this->conf->exists('plugins.PIWIK_URL') || startsWith($this->conf->get('plugins.PIWIK_URL'), 'http')) {
375 return true; 384 return true;
376 } 385 }
377 386
378 $this->conf->set('plugins.PIWIK_URL', 'http://'. $this->conf->get('plugins.PIWIK_URL')); 387 $this->conf->set('plugins.PIWIK_URL', 'http://' . $this->conf->get('plugins.PIWIK_URL'));
379 $this->conf->write($this->isLoggedIn); 388 $this->conf->write($this->isLoggedIn);
380 389
381 return true; 390 return true;
@@ -485,11 +494,11 @@ class Updater
485 return true; 494 return true;
486 } 495 }
487 496
488 if (! $this->conf->exists('general.download_max_size')) { 497 if (!$this->conf->exists('general.download_max_size')) {
489 $this->conf->set('general.download_max_size', 1024*1024*4); 498 $this->conf->set('general.download_max_size', 1024 * 1024 * 4);
490 } 499 }
491 500
492 if (! $this->conf->exists('general.download_timeout')) { 501 if (!$this->conf->exists('general.download_timeout')) {
493 $this->conf->set('general.download_timeout', 30); 502 $this->conf->set('general.download_timeout', 30);
494 } 503 }
495 504
@@ -542,96 +551,3 @@ class Updater
542 return true; 551 return true;
543 } 552 }
544} 553}
545
546/**
547 * Class UpdaterException.
548 */
549class UpdaterException extends Exception
550{
551 /**
552 * @var string Method where the error occurred.
553 */
554 protected $method;
555
556 /**
557 * @var Exception The parent exception.
558 */
559 protected $previous;
560
561 /**
562 * Constructor.
563 *
564 * @param string $message Force the error message if set.
565 * @param string $method Method where the error occurred.
566 * @param Exception|bool $previous Parent exception.
567 */
568 public function __construct($message = '', $method = '', $previous = false)
569 {
570 $this->method = $method;
571 $this->previous = $previous;
572 $this->message = $this->buildMessage($message);
573 }
574
575 /**
576 * Build the exception error message.
577 *
578 * @param string $message Optional given error message.
579 *
580 * @return string The built error message.
581 */
582 private function buildMessage($message)
583 {
584 $out = '';
585 if (! empty($message)) {
586 $out .= $message . PHP_EOL;
587 }
588
589 if (! empty($this->method)) {
590 $out .= t('An error occurred while running the update ') . $this->method . PHP_EOL;
591 }
592
593 if (! empty($this->previous)) {
594 $out .= ' '. $this->previous->getMessage();
595 }
596
597 return $out;
598 }
599}
600
601/**
602 * Read the updates file, and return already done updates.
603 *
604 * @param string $updatesFilepath Updates file path.
605 *
606 * @return array Already done update methods.
607 */
608function read_updates_file($updatesFilepath)
609{
610 if (! empty($updatesFilepath) && is_file($updatesFilepath)) {
611 $content = file_get_contents($updatesFilepath);
612 if (! empty($content)) {
613 return explode(';', $content);
614 }
615 }
616 return array();
617}
618
619/**
620 * Write updates file.
621 *
622 * @param string $updatesFilepath Updates file path.
623 * @param array $updates Updates array to write.
624 *
625 * @throws Exception Couldn't write version number.
626 */
627function write_updates_file($updatesFilepath, $updates)
628{
629 if (empty($updatesFilepath)) {
630 throw new Exception(t('Updates file path is not set, can\'t write updates.'));
631 }
632
633 $res = file_put_contents($updatesFilepath, implode(';', $updates));
634 if ($res === false) {
635 throw new Exception(t('Unable to write updates in '. $updatesFilepath . '.'));
636 }
637}
diff --git a/application/updater/UpdaterUtils.php b/application/updater/UpdaterUtils.php
new file mode 100644
index 00000000..34d4f422
--- /dev/null
+++ b/application/updater/UpdaterUtils.php
@@ -0,0 +1,39 @@
1<?php
2
3/**
4 * Read the updates file, and return already done updates.
5 *
6 * @param string $updatesFilepath Updates file path.
7 *
8 * @return array Already done update methods.
9 */
10function read_updates_file($updatesFilepath)
11{
12 if (! empty($updatesFilepath) && is_file($updatesFilepath)) {
13 $content = file_get_contents($updatesFilepath);
14 if (! empty($content)) {
15 return explode(';', $content);
16 }
17 }
18 return array();
19}
20
21/**
22 * Write updates file.
23 *
24 * @param string $updatesFilepath Updates file path.
25 * @param array $updates Updates array to write.
26 *
27 * @throws Exception Couldn't write version number.
28 */
29function write_updates_file($updatesFilepath, $updates)
30{
31 if (empty($updatesFilepath)) {
32 throw new Exception(t('Updates file path is not set, can\'t write updates.'));
33 }
34
35 $res = file_put_contents($updatesFilepath, implode(';', $updates));
36 if ($res === false) {
37 throw new Exception(t('Unable to write updates in '. $updatesFilepath . '.'));
38 }
39}
diff --git a/application/updater/exception/UpdaterException.php b/application/updater/exception/UpdaterException.php
new file mode 100644
index 00000000..20aceccf
--- /dev/null
+++ b/application/updater/exception/UpdaterException.php
@@ -0,0 +1,60 @@
1<?php
2
3namespace Shaarli\Updater\Exception;
4
5use Exception;
6
7/**
8 * Class UpdaterException.
9 */
10class UpdaterException extends Exception
11{
12 /**
13 * @var string Method where the error occurred.
14 */
15 protected $method;
16
17 /**
18 * @var Exception The parent exception.
19 */
20 protected $previous;
21
22 /**
23 * Constructor.
24 *
25 * @param string $message Force the error message if set.
26 * @param string $method Method where the error occurred.
27 * @param Exception|bool $previous Parent exception.
28 */
29 public function __construct($message = '', $method = '', $previous = false)
30 {
31 $this->method = $method;
32 $this->previous = $previous;
33 $this->message = $this->buildMessage($message);
34 }
35
36 /**
37 * Build the exception error message.
38 *
39 * @param string $message Optional given error message.
40 *
41 * @return string The built error message.
42 */
43 private function buildMessage($message)
44 {
45 $out = '';
46 if (!empty($message)) {
47 $out .= $message . PHP_EOL;
48 }
49
50 if (!empty($this->method)) {
51 $out .= t('An error occurred while running the update ') . $this->method . PHP_EOL;
52 }
53
54 if (!empty($this->previous)) {
55 $out .= ' ' . $this->previous->getMessage();
56 }
57
58 return $out;
59 }
60}