Skip to content

Commit 463f3b6

Browse files
committed
Adding datastore2json
1 parent d5409f6 commit 463f3b6

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
}
8181
},
8282
"scripts": {
83-
"datastore2json": "php -f datastore2json.php"
83+
"datastore2json": "php -f datastore2json.php",
84+
"json2datastore": "php -f json2datastore.php"
8485
}
8586
}

datastore2json.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
require 'vendor/autoload.php';
33

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

66
use Shaarli\Bookmark\BookmarkArray;
77

json2datastore.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
require 'vendor/autoload.php';
3+
4+
// Replicates BookmarkIO.write() at https://github.com/shaarli/Shaarli/blob/master/application/bookmark/BookmarkIO.php#L114
5+
// and BookmarkFileService.add() at https://github.com/shaarli/Shaarli/blob/master/application/bookmark/BookmarkFileService.php
6+
7+
require_once 'application/bookmark/LinkUtils.php'; // imports tags_str2array()
8+
use Shaarli\Bookmark\Bookmark;
9+
use Shaarli\Bookmark\BookmarkArray;
10+
11+
if ($argv && $argv[0] && realpath($argv[0]) === __FILE__) {
12+
// Code below will only be executed when this script is invoked as a CLI,
13+
// not when served as a web page:
14+
$phpPrefix = '<?php /* ';
15+
$phpSuffix = ' */ ?>';
16+
$json_filepath = $argv[1];
17+
$json_links = json_decode(file_get_contents($json_filepath), true);
18+
$bookmarks = new BookmarkArray();
19+
foreach($json_links as &$json_link) {
20+
$json_link['created'] = DateTime::createFromFormat(DateTime::ISO8601, $json_link['created']);
21+
if ($json_link['updated']) {
22+
$json_link['updated'] = DateTime::createFromFormat(DateTime::ISO8601, $json_link['updated']);
23+
}
24+
$bookmark = new Bookmark();
25+
$bookmark->fromArray($json_link, ' ');
26+
$bookmark->setId($bookmarks->getNextId());
27+
$bookmark->validate();
28+
$bookmarks[$bookmark->getId()] = $bookmark;
29+
}
30+
$data = base64_encode(gzdeflate(serialize($bookmarks)));
31+
print($data = $phpPrefix . $data . $phpSuffix);
32+
}

0 commit comments

Comments
 (0)