]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/Updater.php
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.
11 * @var array Updates which are already done.
13 protected $doneUpdates;
16 * @var LinkDB instance.
21 * @var ConfigManager $conf Configuration Manager instance.
26 * @var bool True if the user is logged in, false otherwise.
28 protected $isLoggedIn;
31 * @var ReflectionMethod[] List of current class methods.
38 * @param array $doneUpdates Updates which are already done.
39 * @param LinkDB $linkDB LinkDB instance.
40 * @oaram ConfigManager $conf Configuration Manager instance.
41 * @param boolean $isLoggedIn True if the user is logged in.
43 public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn)
45 $this->doneUpdates
= $doneUpdates;
46 $this->linkDB
= $linkDB;
48 $this->isLoggedIn
= $isLoggedIn;
50 // Retrieve all update methods.
51 $class = new ReflectionClass($this);
52 $this->methods
= $class->getMethods();
56 * Run all new updates.
57 * Update methods have to start with 'updateMethod' and return true (on success).
59 * @return array An array containing ran updates.
61 * @throws UpdaterException If something went wrong.
63 public function update()
65 $updatesRan = array();
67 // If the user isn't logged in, exit without updating.
68 if ($this->isLoggedIn
!== true) {
72 if ($this->methods
== null) {
73 throw new UpdaterException('Couldn\'t retrieve Updater class methods.');
76 foreach ($this->methods
as $method) {
77 // Not an update method or already done, pass.
78 if (! startsWith($method->getName(), 'updateMethod')
79 || in_array($method->getName(), $this->doneUpdates
)
85 $method->setAccessible(true);
86 $res = $method->invoke($this);
87 // Update method must return true to be considered processed.
89 $updatesRan[] = $method->getName();
91 } catch (Exception
$e) {
92 throw new UpdaterException($method, $e);
96 $this->doneUpdates
= array_merge($this->doneUpdates
, $updatesRan);
102 * @return array Updates methods already processed.
104 public function getDoneUpdates()
106 return $this->doneUpdates
;
110 * Move deprecated options.php to config.php.
112 * Milestone 0.9 (old versioning) - shaarli/Shaarli#41:
113 * options.php is not supported anymore.
115 public function updateMethodMergeDeprecatedConfigFile()
117 if (is_file($this->conf
->get('resource.data_dir') . '/options.php')) {
118 include $this->conf
->get('resource.data_dir') . '/options.php';
120 // Load GLOBALS into config
121 $allowedKeys = array_merge(ConfigPhp
::$ROOT_KEYS);
122 $allowedKeys[] = 'config';
123 foreach ($GLOBALS as $key => $value) {
124 if (in_array($key, $allowedKeys)) {
125 $this->conf
->set($key, $value);
128 $this->conf
->write($this->isLoggedIn
);
129 unlink($this->conf
->get('resource.data_dir').'/options.php');
136 * Rename tags starting with a '-' to work with tag exclusion search.
138 public function updateMethodRenameDashTags()
140 $linklist = $this->linkDB
->filterSearch();
141 foreach ($linklist as $link) {
142 $link['tags'] = preg_replace('/(^| )\-/', '$1', $link['tags
']);
143 $link['tags
'] = implode(' ', array_unique(LinkFilter::tagsStrToArray($link['tags
'], true)));
144 $this->linkDB[$link['linkdate
']] = $link;
146 $this->linkDB->savedb($this->conf->get('resource.page_cache
'));
151 * Move old configuration in PHP to the new config system in JSON format.
153 * Will rename 'config
.php
' into 'config
.save
.php
' and create 'config
.json
.php
'.
154 * It will also convert legacy setting keys to the new ones.
156 public function updateMethodConfigToJson()
158 // JSON config already exists, nothing to do.
159 if ($this->conf->getConfigIO() instanceof ConfigJson) {
163 $configPhp = new ConfigPhp();
164 $configJson = new ConfigJson();
165 $oldConfig = $configPhp->read($this->conf->getConfigFile() . '.php
');
166 rename($this->conf->getConfigFileExt(), $this->conf->getConfigFile() . '.save
.php
');
167 $this->conf->setConfigIO($configJson);
168 $this->conf->reload();
170 $legacyMap = array_flip(ConfigPhp::$LEGACY_KEYS_MAPPING);
171 foreach (ConfigPhp::$ROOT_KEYS as $key) {
172 $this->conf->set($legacyMap[$key], $oldConfig[$key]);
175 // Set sub config keys (config and plugins)
176 $subConfig = array('config
', 'plugins
');
177 foreach ($subConfig as $sub) {
178 foreach ($oldConfig[$sub] as $key => $value) {
179 if (isset($legacyMap[$sub .'.'. $key])) {
180 $configKey = $legacyMap[$sub .'.'. $key];
182 $configKey = $sub .'.'. $key;
184 $this->conf->set($configKey, $value);
189 $this->conf->write($this->isLoggedIn);
191 } catch (IOException $e) {
192 error_log($e->getMessage());
198 * Escape settings which have been manually escaped in every request in previous versions:
200 * - general.header_link
203 * @return bool true if the update is successful, false otherwise.
205 public function updateMethodEscapeUnescapedConfig()
208 $this->conf->set('general
.title
', escape($this->conf->get('general
.title
')));
209 $this->conf->set('general
.header_link
', escape($this->conf->get('general
.header_link
')));
210 $this->conf->set('redirector
.url
', escape($this->conf->get('redirector
.url
')));
211 $this->conf->write($this->isLoggedIn);
212 } catch (Exception $e) {
213 error_log($e->getMessage());
221 * Class UpdaterException.
223 class UpdaterException extends Exception
226 * @var string Method where the error occurred.
231 * @var Exception The parent exception.
238 * @param string $message Force the error message if set.
239 * @param string $method Method where the error occurred.
240 * @param Exception|bool $previous Parent exception.
242 public function __construct($message = '', $method = '', $previous = false)
244 $this->method = $method;
245 $this->previous = $previous;
246 $this->message = $this->buildMessage($message);
250 * Build the exception error message.
252 * @param string $message Optional given error message.
254 * @return string The built error message.
256 private function buildMessage($message)
259 if (! empty($message)) {
260 $out .= $message . PHP_EOL;
263 if (! empty($this->method)) {
264 $out .= 'An error occurred
while running the update
'. $this->method . PHP_EOL;
267 if (! empty($this->previous)) {
268 $out .= ' '. $this->previous->getMessage();
276 * Read the updates file, and return already done updates.
278 * @param string $updatesFilepath Updates file path.
280 * @return array Already done update methods.
282 function read_updates_file($updatesFilepath)
284 if (! empty($updatesFilepath) && is_file($updatesFilepath)) {
285 $content = file_get_contents($updatesFilepath);
286 if (! empty($content)) {
287 return explode(';', $content);
294 * Write updates file.
296 * @param string $updatesFilepath Updates file path.
297 * @param array $updates Updates array to write.
299 * @throws Exception Couldn't write version number
.
301 function write_updates_file($updatesFilepath, $updates)
303 if (empty($updatesFilepath)) {
304 throw new Exception('Updates file path is not set, can\'t write updates.');
307 $res = file_put_contents($updatesFilepath, implode(';', $updates));
308 if ($res === false) {
309 throw new Exception('Unable to write updates in '. $updatesFilepath . '.');