]>
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 array Shaarli's configuration array.
21 * @var LinkDB 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 array $config Shaarli's configuration array.
40 * @param LinkDB $linkDB LinkDB instance.
41 * @param boolean $isLoggedIn True if the user is logged in.
43 public function __construct($doneUpdates, $config, $linkDB, $isLoggedIn)
45 $this->doneUpdates
= $doneUpdates;
46 $this->config
= $config;
47 $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 $config_file = $this->config
['config']['CONFIG_FILE'];
119 if (is_file($this->config
['config']['DATADIR'].'/options.php')) {
120 include $this->config
['config']['DATADIR'].'/options.php';
122 // Load GLOBALS into config
123 foreach ($GLOBALS as $key => $value) {
124 $this->config
[$key] = $value;
126 $this->config
['config']['CONFIG_FILE'] = $config_file;
127 writeConfig($this->config
, $this->isLoggedIn
);
129 unlink($this->config
['config']['DATADIR'].'/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->config['config
']['PAGECACHE
']);
152 * Class UpdaterException.
154 class UpdaterException extends Exception
157 * @var string Method where the error occurred.
162 * @var Exception The parent exception.
169 * @param string $message Force the error message if set.
170 * @param string $method Method where the error occurred.
171 * @param Exception|bool $previous Parent exception.
173 public function __construct($message = '', $method = '', $previous = false)
175 $this->method = $method;
176 $this->previous = $previous;
177 $this->message = $this->buildMessage($message);
181 * Build the exception error message.
183 * @param string $message Optional given error message.
185 * @return string The built error message.
187 private function buildMessage($message)
190 if (! empty($message)) {
191 $out .= $message . PHP_EOL;
194 if (! empty($this->method)) {
195 $out .= 'An error occurred
while running the update
'. $this->method . PHP_EOL;
198 if (! empty($this->previous)) {
199 $out .= ' '. $this->previous->getMessage();
208 * Read the updates file, and return already done updates.
210 * @param string $updatesFilepath Updates file path.
212 * @return array Already done update methods.
214 function read_updates_file($updatesFilepath)
216 if (! empty($updatesFilepath) && is_file($updatesFilepath)) {
217 $content = file_get_contents($updatesFilepath);
218 if (! empty($content)) {
219 return explode(';', $content);
226 * Write updates file.
228 * @param string $updatesFilepath Updates file path.
229 * @param array $updates Updates array to write.
231 * @throws Exception Couldn't write version number
.
233 function write_updates_file($updatesFilepath, $updates)
235 if (empty($updatesFilepath)) {
236 throw new Exception('Updates file path is not set, can\'t write updates.');
239 $res = file_put_contents($updatesFilepath, implode(';', $updates));
240 if ($res === false) {
241 throw new Exception('Unable to write updates in '. $updatesFilepath . '.');