2016-01-12 19:50:48 +01:00
|
|
|
<?php
|
2018-12-03 01:10:39 +01:00
|
|
|
|
2018-12-03 23:49:20 +01:00
|
|
|
namespace Shaarli\Updater;
|
|
|
|
|
2019-05-25 15:52:27 +02:00
|
|
|
use Shaarli\Bookmark\BookmarkServiceInterface;
|
2020-01-26 11:15:15 +01:00
|
|
|
use Shaarli\Config\ConfigManager;
|
2018-12-03 23:49:20 +01:00
|
|
|
use Shaarli\Updater\Exception\UpdaterException;
|
2016-01-12 19:50:48 +01:00
|
|
|
|
|
|
|
/**
|
2019-05-25 15:52:27 +02:00
|
|
|
* Class Updater.
|
2016-01-12 19:50:48 +01:00
|
|
|
* Used to update stuff when a new Shaarli's version is reached.
|
2019-05-25 15:52:27 +02:00
|
|
|
* Update methods are ran only once, and the stored in a TXT file.
|
2016-01-12 19:50:48 +01:00
|
|
|
*/
|
|
|
|
class Updater
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array Updates which are already done.
|
|
|
|
*/
|
|
|
|
protected $doneUpdates;
|
|
|
|
|
|
|
|
/**
|
2019-05-25 15:52:27 +02:00
|
|
|
* @var BookmarkServiceInterface instance.
|
2016-01-12 19:50:48 +01:00
|
|
|
*/
|
2020-07-06 08:04:35 +02:00
|
|
|
protected $bookmarkService;
|
2016-01-12 19:50:48 +01:00
|
|
|
|
2016-06-09 20:04:02 +02:00
|
|
|
/**
|
|
|
|
* @var ConfigManager $conf Configuration Manager instance.
|
|
|
|
*/
|
|
|
|
protected $conf;
|
|
|
|
|
2016-01-12 19:50:48 +01:00
|
|
|
/**
|
|
|
|
* @var bool True if the user is logged in, false otherwise.
|
|
|
|
*/
|
|
|
|
protected $isLoggedIn;
|
|
|
|
|
2018-06-08 12:50:49 +02:00
|
|
|
/**
|
2019-05-25 15:52:27 +02:00
|
|
|
* @var \ReflectionMethod[] List of current class methods.
|
2016-01-12 19:50:48 +01:00
|
|
|
*/
|
|
|
|
protected $methods;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Object constructor.
|
|
|
|
*
|
2019-05-25 15:52:27 +02:00
|
|
|
* @param array $doneUpdates Updates which are already done.
|
|
|
|
* @param BookmarkServiceInterface $linkDB LinksService instance.
|
|
|
|
* @param ConfigManager $conf Configuration Manager instance.
|
|
|
|
* @param boolean $isLoggedIn True if the user is logged in.
|
2016-01-12 19:50:48 +01:00
|
|
|
*/
|
2019-05-25 15:52:27 +02:00
|
|
|
public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn)
|
2016-01-12 19:50:48 +01:00
|
|
|
{
|
|
|
|
$this->doneUpdates = $doneUpdates;
|
2020-07-06 08:04:35 +02:00
|
|
|
$this->bookmarkService = $linkDB;
|
2016-06-09 20:04:02 +02:00
|
|
|
$this->conf = $conf;
|
2016-01-12 19:50:48 +01:00
|
|
|
$this->isLoggedIn = $isLoggedIn;
|
|
|
|
|
|
|
|
// Retrieve all update methods.
|
2019-05-25 15:52:27 +02:00
|
|
|
$class = new \ReflectionClass($this);
|
2016-01-12 19:50:48 +01:00
|
|
|
$this->methods = $class->getMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run all new updates.
|
|
|
|
* Update methods have to start with 'updateMethod' and return true (on success).
|
|
|
|
*
|
|
|
|
* @return array An array containing ran updates.
|
|
|
|
*
|
|
|
|
* @throws UpdaterException If something went wrong.
|
|
|
|
*/
|
|
|
|
public function update()
|
|
|
|
{
|
2020-07-06 08:04:35 +02:00
|
|
|
$updatesRan = [];
|
2016-01-12 19:50:48 +01:00
|
|
|
|
|
|
|
// If the user isn't logged in, exit without updating.
|
|
|
|
if ($this->isLoggedIn !== true) {
|
|
|
|
return $updatesRan;
|
|
|
|
}
|
|
|
|
|
2017-01-06 18:54:29 +01:00
|
|
|
if ($this->methods === null) {
|
2019-05-25 15:52:27 +02:00
|
|
|
throw new UpdaterException('Couldn\'t retrieve LegacyUpdater class methods.');
|
2016-01-12 19:50:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->methods as $method) {
|
|
|
|
// Not an update method or already done, pass.
|
2019-05-25 15:52:27 +02:00
|
|
|
if (! startsWith($method->getName(), 'updateMethod')
|
2016-01-12 19:50:48 +01:00
|
|
|
|| in_array($method->getName(), $this->doneUpdates)
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$method->setAccessible(true);
|
|
|
|
$res = $method->invoke($this);
|
|
|
|
// Update method must return true to be considered processed.
|
|
|
|
if ($res === true) {
|
|
|
|
$updatesRan[] = $method->getName();
|
|
|
|
}
|
2019-05-25 15:52:27 +02:00
|
|
|
} catch (\Exception $e) {
|
2016-01-12 19:50:48 +01:00
|
|
|
throw new UpdaterException($method, $e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->doneUpdates = array_merge($this->doneUpdates, $updatesRan);
|
|
|
|
|
|
|
|
return $updatesRan;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array Updates methods already processed.
|
|
|
|
*/
|
|
|
|
public function getDoneUpdates()
|
|
|
|
{
|
|
|
|
return $this->doneUpdates;
|
|
|
|
}
|
2020-01-26 11:15:15 +01:00
|
|
|
|
2020-07-06 08:04:35 +02:00
|
|
|
public function readUpdates(string $updatesFilepath): array
|
|
|
|
{
|
|
|
|
return UpdaterUtils::read_updates_file($updatesFilepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function writeUpdates(string $updatesFilepath, array $updates): void
|
|
|
|
{
|
|
|
|
UpdaterUtils::write_updates_file($updatesFilepath, $updates);
|
|
|
|
}
|
|
|
|
|
2020-01-26 11:15:15 +01:00
|
|
|
/**
|
|
|
|
* With the Slim routing system, default header link should be `./` instead of `?`.
|
|
|
|
* Otherwise you can not go back to the home page. Example: `/picture-wall` -> `/picture-wall?` instead of `/`.
|
|
|
|
*/
|
|
|
|
public function updateMethodRelativeHomeLink(): bool
|
|
|
|
{
|
|
|
|
$link = trim($this->conf->get('general.header_link'));
|
|
|
|
if ($link[0] === '?') {
|
|
|
|
$link = './'. ltrim($link, '?');
|
|
|
|
|
|
|
|
$this->conf->set('general.header_link', $link, true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-07-06 08:04:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* With the Slim routing system, note bookmarks URL formatted `?abcdef`
|
|
|
|
* should be replaced with `/shaare/abcdef`
|
|
|
|
*/
|
|
|
|
public function updateMethodMigrateExistingNotesUrl(): bool
|
|
|
|
{
|
|
|
|
$updated = false;
|
|
|
|
|
|
|
|
foreach ($this->bookmarkService->search() as $bookmark) {
|
|
|
|
if ($bookmark->isNote()
|
|
|
|
&& startsWith($bookmark->getUrl(), '?')
|
|
|
|
&& 1 === preg_match('/^\?([a-zA-Z0-9-_@]{6})($|&|#)/', $bookmark->getUrl(), $match)
|
|
|
|
) {
|
|
|
|
$updated = true;
|
|
|
|
$bookmark = $bookmark->setUrl('/shaare/' . $match[1]);
|
|
|
|
|
|
|
|
$this->bookmarkService->set($bookmark, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($updated) {
|
|
|
|
$this->bookmarkService->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-01-12 19:50:48 +01:00
|
|
|
}
|