Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding datastore2json & json2datastore scripts #2053

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion application/bookmark/Bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
];
}
}
7 changes: 6 additions & 1 deletion application/bookmark/BookmarkArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @package Shaarli\Bookmark
*/
class BookmarkArray implements \Iterator, \Countable, \ArrayAccess
class BookmarkArray implements \Iterator, \Countable, \ArrayAccess, \JsonSerializable
{
/**
* @var Bookmark[]
Expand Down Expand Up @@ -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;
}
}
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,9 @@
"Shaarli\\Tests\\": "tests",
"Shaarli\\Tests\\Utils\\": "tests/utils"
}
},
"scripts": {
"datastore2json": "php -f datastore2json.php",
"json2datastore": "php -f json2datastore.php"
}
}
19 changes: 19 additions & 0 deletions datastore2json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
require 'vendor/autoload.php';

// Replicates BookmarkIO.read() at https://github.com/shaarli/Shaarli/blob/master/application/bookmark/BookmarkIO.php#L72 :

use Shaarli\Bookmark\BookmarkArray;

if ($argv && $argv[0] && realpath($argv[0]) === __FILE__) {
// Code below will only be executed when this script is invoked as a CLI,
// not when served as a web page:
$phpPrefix = '<?php /* ';
$phpSuffix = ' */ ?>';
$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);
}
33 changes: 33 additions & 0 deletions json2datastore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
require 'vendor/autoload.php';

// Replicates BookmarkIO.write() at https://github.com/shaarli/Shaarli/blob/master/application/bookmark/BookmarkIO.php#L114
// and BookmarkFileService.add() at https://github.com/shaarli/Shaarli/blob/master/application/bookmark/BookmarkFileService.php

require_once 'application/bookmark/LinkUtils.php'; // imports tags_str2array()
use Shaarli\Bookmark\Bookmark;
use Shaarli\Bookmark\BookmarkArray;

if ($argv && $argv[0] && realpath($argv[0]) === __FILE__) {
// Code below will only be executed when this script is invoked as a CLI,
// not when served as a web page:
$phpPrefix = '<?php /* ';
$phpSuffix = ' */ ?>';
$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);
}
Loading