3 namespace Shaarli\Updater
;
5 use Shaarli\Bookmark\BookmarkServiceInterface
;
6 use Shaarli\Config\ConfigManager
;
7 use Shaarli\Updater\Exception\UpdaterException
;
11 * Used to update stuff when a new Shaarli's version is reached.
12 * Update methods are ran only once, and the stored in a TXT file.
17 * @var array Updates which are already done.
19 protected $doneUpdates;
22 * @var BookmarkServiceInterface instance.
24 protected $linkServices;
27 * @var ConfigManager $conf Configuration Manager instance.
32 * @var bool True if the user is logged in, false otherwise.
34 protected $isLoggedIn;
37 * @var \ReflectionMethod[] List of current class methods.
44 * @param array $doneUpdates Updates which are already done.
45 * @param BookmarkServiceInterface $linkDB LinksService instance.
46 * @param ConfigManager $conf Configuration Manager instance.
47 * @param boolean $isLoggedIn True if the user is logged in.
49 public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn)
51 $this->doneUpdates
= $doneUpdates;
52 $this->linkServices
= $linkDB;
54 $this->isLoggedIn
= $isLoggedIn;
56 // Retrieve all update methods.
57 $class = new \
ReflectionClass($this);
58 $this->methods
= $class->getMethods();
62 * Run all new updates.
63 * Update methods have to start with 'updateMethod' and return true (on success).
65 * @return array An array containing ran updates.
67 * @throws UpdaterException If something went wrong.
69 public function update()
71 $updatesRan = array();
73 // If the user isn't logged in, exit without updating.
74 if ($this->isLoggedIn
!== true) {
78 if ($this->methods
=== null) {
79 throw new UpdaterException('Couldn\'t retrieve LegacyUpdater class methods.');
82 foreach ($this->methods
as $method) {
83 // Not an update method or already done, pass.
84 if (! startsWith($method->getName(), 'updateMethod')
85 || in_array($method->getName(), $this->doneUpdates
)
91 $method->setAccessible(true);
92 $res = $method->invoke($this);
93 // Update method must return true to be considered processed.
95 $updatesRan[] = $method->getName();
97 } catch (\Exception
$e) {
98 throw new UpdaterException($method, $e);
102 $this->doneUpdates
= array_merge($this->doneUpdates
, $updatesRan);
108 * @return array Updates methods already processed.
110 public function getDoneUpdates()
112 return $this->doneUpdates
;
116 * With the Slim routing system, default header link should be `./` instead of `?`.
117 * Otherwise you can not go back to the home page. Example: `/picture-wall` -> `/picture-wall?` instead of `/`.
119 public function updateMethodRelativeHomeLink(): bool
121 $link = trim($this->conf
->get('general.header_link'));
122 if ($link[0] === '?') {
123 $link = './'. ltrim($link, '?');
125 $this->conf
->set('general.header_link', $link, true, true);