From d5409f6b8d2c70366d9a5fe0964696ab48d15261 Mon Sep 17 00:00:00 2001 From: Lucas Cimon <925560+Lucas-C@users.noreply.github.com> Date: Wed, 12 Jan 2022 22:38:40 +0100 Subject: [PATCH 1/2] Adding datastore2json script --- application/bookmark/Bookmark.php | 18 +++++++++++++++++- application/bookmark/BookmarkArray.php | 7 ++++++- composer.json | 3 +++ datastore2json.php | 19 +++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 datastore2json.php diff --git a/application/bookmark/Bookmark.php b/application/bookmark/Bookmark.php index 56751e15d..fdc22c6f6 100644 --- a/application/bookmark/Bookmark.php +++ b/application/bookmark/Bookmark.php @@ -16,7 +16,7 @@ * * @package Shaarli\Bookmark */ -class Bookmark +class Bookmark implements \JsonSerializable { /** @var string Date format used in string (former ID format) */ public const LINK_DATE_FORMAT = 'Ymd_His'; @@ -539,4 +539,20 @@ public function deleteTag(string $tag): void $this->tags = array_values($this->tags); } } + + public function jsonSerialize() + { + return [ + 'id' => $this->id, + 'shortUrl' => $this->shortUrl, + 'url' => $this->url, + 'title' => $this->title, + 'description' => $this->description, + 'tags' => $this->tags, + 'sticky' => $this->sticky, + 'created' => $this->created, + 'updated' => $this->updated, + 'private' => $this->private, + ]; + } } diff --git a/application/bookmark/BookmarkArray.php b/application/bookmark/BookmarkArray.php index 0c1a6ecab..392bc5167 100644 --- a/application/bookmark/BookmarkArray.php +++ b/application/bookmark/BookmarkArray.php @@ -14,7 +14,7 @@ * * @package Shaarli\Bookmark */ -class BookmarkArray implements \Iterator, \Countable, \ArrayAccess +class BookmarkArray implements \Iterator, \Countable, \ArrayAccess, \JsonSerializable { /** * @var Bookmark[] @@ -261,4 +261,9 @@ public function reorder(string $order = 'DESC', bool $ignoreSticky = false): voi $this->ids[$bookmark->getId()] = $key; } } + + public function jsonSerialize() + { + return $this->bookmarks; + } } diff --git a/composer.json b/composer.json index a8690dbea..393af1be5 100644 --- a/composer.json +++ b/composer.json @@ -78,5 +78,8 @@ "Shaarli\\Tests\\": "tests", "Shaarli\\Tests\\Utils\\": "tests/utils" } + }, + "scripts": { + "datastore2json": "php -f datastore2json.php" } } diff --git a/datastore2json.php b/datastore2json.php new file mode 100644 index 000000000..5d1be93df --- /dev/null +++ b/datastore2json.php @@ -0,0 +1,19 @@ +'; + $datastore_filepath = $argc > 1 ? $argv[1] : "data/datastore.php"; + $content = file_get_contents($datastore_filepath); + $links = unserialize(gzinflate(base64_decode( + substr($content, strlen($phpPrefix), -strlen($phpSuffix)) + ))); + print(json_encode($links).PHP_EOL); +} From 3ac63804d14336329396df92952797737d66e6ea Mon Sep 17 00:00:00 2001 From: Lucas Cimon <925560+Lucas-C@users.noreply.github.com> Date: Mon, 4 Dec 2023 11:55:27 +0100 Subject: [PATCH 2/2] Adding datastore2json --- composer.json | 3 ++- datastore2json.php | 2 +- json2datastore.php | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 json2datastore.php diff --git a/composer.json b/composer.json index 393af1be5..dc93f62ff 100644 --- a/composer.json +++ b/composer.json @@ -80,6 +80,7 @@ } }, "scripts": { - "datastore2json": "php -f datastore2json.php" + "datastore2json": "php -f datastore2json.php", + "json2datastore": "php -f json2datastore.php" } } diff --git a/datastore2json.php b/datastore2json.php index 5d1be93df..3703faf33 100644 --- a/datastore2json.php +++ b/datastore2json.php @@ -1,7 +1,7 @@ '; + $json_filepath = $argv[1]; + $json_links = json_decode(file_get_contents($json_filepath), true); + $bookmarks = new BookmarkArray(); + foreach($json_links as &$json_link) { + $json_link['created'] = DateTime::createFromFormat(DateTime::ISO8601, $json_link['created']); + if ($json_link['updated']) { + $json_link['updated'] = DateTime::createFromFormat(DateTime::ISO8601, $json_link['updated']); + } + $bookmark = new Bookmark(); + $bookmark->fromArray($json_link, ' '); + $bookmark->setId($bookmarks->getNextId()); + $bookmark->validate(); + $bookmarks[$bookmark->getId()] = $bookmark; + } + $bookmarks->reorder(); + $data = base64_encode(gzdeflate(serialize($bookmarks))); + print($data = $phpPrefix . $data . $phpSuffix); +}