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..dc93f62ff 100644 --- a/composer.json +++ b/composer.json @@ -78,5 +78,9 @@ "Shaarli\\Tests\\": "tests", "Shaarli\\Tests\\Utils\\": "tests/utils" } + }, + "scripts": { + "datastore2json": "php -f datastore2json.php", + "json2datastore": "php -f json2datastore.php" } } diff --git a/datastore2json.php b/datastore2json.php new file mode 100644 index 000000000..3703faf33 --- /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); +} diff --git a/json2datastore.php b/json2datastore.php new file mode 100644 index 000000000..be60c6ffa --- /dev/null +++ b/json2datastore.php @@ -0,0 +1,33 @@ +'; + $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); +}