Skip to content

Commit

Permalink
Adding datastore2json
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Dec 4, 2023
1 parent d5409f6 commit 3ac6380
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
}
},
"scripts": {
"datastore2json": "php -f datastore2json.php"
"datastore2json": "php -f datastore2json.php",
"json2datastore": "php -f json2datastore.php"
}
}
2 changes: 1 addition & 1 deletion datastore2json.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
require 'vendor/autoload.php';

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

use Shaarli\Bookmark\BookmarkArray;

Expand Down
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);
}

0 comments on commit 3ac6380

Please sign in to comment.