]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/updater/Updater.php
Process main page (linklist) through Slim controller
[github/shaarli/Shaarli.git] / application / updater / Updater.php
1 <?php
2
3 namespace Shaarli\Updater;
4
5 use Shaarli\Bookmark\BookmarkServiceInterface;
6 use Shaarli\Config\ConfigManager;
7 use Shaarli\Updater\Exception\UpdaterException;
8
9 /**
10 * Class Updater.
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.
13 */
14 class Updater
15 {
16 /**
17 * @var array Updates which are already done.
18 */
19 protected $doneUpdates;
20
21 /**
22 * @var BookmarkServiceInterface instance.
23 */
24 protected $bookmarkService;
25
26 /**
27 * @var ConfigManager $conf Configuration Manager instance.
28 */
29 protected $conf;
30
31 /**
32 * @var bool True if the user is logged in, false otherwise.
33 */
34 protected $isLoggedIn;
35
36 /**
37 * @var \ReflectionMethod[] List of current class methods.
38 */
39 protected $methods;
40
41 /**
42 * Object constructor.
43 *
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.
48 */
49 public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn)
50 {
51 $this->doneUpdates = $doneUpdates;
52 $this->bookmarkService = $linkDB;
53 $this->conf = $conf;
54 $this->isLoggedIn = $isLoggedIn;
55
56 // Retrieve all update methods.
57 $class = new \ReflectionClass($this);
58 $this->methods = $class->getMethods();
59 }
60
61 /**
62 * Run all new updates.
63 * Update methods have to start with 'updateMethod' and return true (on success).
64 *
65 * @return array An array containing ran updates.
66 *
67 * @throws UpdaterException If something went wrong.
68 */
69 public function update()
70 {
71 $updatesRan = [];
72
73 // If the user isn't logged in, exit without updating.
74 if ($this->isLoggedIn !== true) {
75 return $updatesRan;
76 }
77
78 if ($this->methods === null) {
79 throw new UpdaterException('Couldn\'t retrieve LegacyUpdater class methods.');
80 }
81
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)
86 ) {
87 continue;
88 }
89
90 try {
91 $method->setAccessible(true);
92 $res = $method->invoke($this);
93 // Update method must return true to be considered processed.
94 if ($res === true) {
95 $updatesRan[] = $method->getName();
96 }
97 } catch (\Exception $e) {
98 throw new UpdaterException($method, $e);
99 }
100 }
101
102 $this->doneUpdates = array_merge($this->doneUpdates, $updatesRan);
103
104 return $updatesRan;
105 }
106
107 /**
108 * @return array Updates methods already processed.
109 */
110 public function getDoneUpdates()
111 {
112 return $this->doneUpdates;
113 }
114
115 public function readUpdates(string $updatesFilepath): array
116 {
117 return UpdaterUtils::read_updates_file($updatesFilepath);
118 }
119
120 public function writeUpdates(string $updatesFilepath, array $updates): void
121 {
122 UpdaterUtils::write_updates_file($updatesFilepath, $updates);
123 }
124
125 /**
126 * With the Slim routing system, default header link should be `./` instead of `?`.
127 * Otherwise you can not go back to the home page. Example: `/picture-wall` -> `/picture-wall?` instead of `/`.
128 */
129 public function updateMethodRelativeHomeLink(): bool
130 {
131 $link = trim($this->conf->get('general.header_link'));
132 if ($link[0] === '?') {
133 $link = './'. ltrim($link, '?');
134
135 $this->conf->set('general.header_link', $link, true, true);
136 }
137
138 return true;
139 }
140
141 /**
142 * With the Slim routing system, note bookmarks URL formatted `?abcdef`
143 * should be replaced with `/shaare/abcdef`
144 */
145 public function updateMethodMigrateExistingNotesUrl(): bool
146 {
147 $updated = false;
148
149 foreach ($this->bookmarkService->search() as $bookmark) {
150 if ($bookmark->isNote()
151 && startsWith($bookmark->getUrl(), '?')
152 && 1 === preg_match('/^\?([a-zA-Z0-9-_@]{6})($|&|#)/', $bookmark->getUrl(), $match)
153 ) {
154 $updated = true;
155 $bookmark = $bookmark->setUrl('/shaare/' . $match[1]);
156
157 $this->bookmarkService->set($bookmark, false);
158 }
159 }
160
161 if ($updated) {
162 $this->bookmarkService->save();
163 }
164
165 return true;
166 }
167 }