Skip to content

Commit d5409f6

Browse files
committed
Adding datastore2json script
1 parent 61a365b commit d5409f6

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

application/bookmark/Bookmark.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @package Shaarli\Bookmark
1818
*/
19-
class Bookmark
19+
class Bookmark implements \JsonSerializable
2020
{
2121
/** @var string Date format used in string (former ID format) */
2222
public const LINK_DATE_FORMAT = 'Ymd_His';
@@ -539,4 +539,20 @@ public function deleteTag(string $tag): void
539539
$this->tags = array_values($this->tags);
540540
}
541541
}
542+
543+
public function jsonSerialize()
544+
{
545+
return [
546+
'id' => $this->id,
547+
'shortUrl' => $this->shortUrl,
548+
'url' => $this->url,
549+
'title' => $this->title,
550+
'description' => $this->description,
551+
'tags' => $this->tags,
552+
'sticky' => $this->sticky,
553+
'created' => $this->created,
554+
'updated' => $this->updated,
555+
'private' => $this->private,
556+
];
557+
}
542558
}

application/bookmark/BookmarkArray.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* @package Shaarli\Bookmark
1616
*/
17-
class BookmarkArray implements \Iterator, \Countable, \ArrayAccess
17+
class BookmarkArray implements \Iterator, \Countable, \ArrayAccess, \JsonSerializable
1818
{
1919
/**
2020
* @var Bookmark[]
@@ -261,4 +261,9 @@ public function reorder(string $order = 'DESC', bool $ignoreSticky = false): voi
261261
$this->ids[$bookmark->getId()] = $key;
262262
}
263263
}
264+
265+
public function jsonSerialize()
266+
{
267+
return $this->bookmarks;
268+
}
264269
}

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,8 @@
7878
"Shaarli\\Tests\\": "tests",
7979
"Shaarli\\Tests\\Utils\\": "tests/utils"
8080
}
81+
},
82+
"scripts": {
83+
"datastore2json": "php -f datastore2json.php"
8184
}
8285
}

datastore2json.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
require 'vendor/autoload.php';
3+
4+
// Replicates https://github.com/shaarli/Shaarli/blob/master/application/bookmark/BookmarkIO.php#L72 :
5+
6+
use Shaarli\Bookmark\BookmarkArray;
7+
8+
if ($argv && $argv[0] && realpath($argv[0]) === __FILE__) {
9+
// Code below will only be executed when this script is invoked as a CLI,
10+
// not when served as a web page:
11+
$phpPrefix = '<?php /* ';
12+
$phpSuffix = ' */ ?>';
13+
$datastore_filepath = $argc > 1 ? $argv[1] : "data/datastore.php";
14+
$content = file_get_contents($datastore_filepath);
15+
$links = unserialize(gzinflate(base64_decode(
16+
substr($content, strlen($phpPrefix), -strlen($phpSuffix))
17+
)));
18+
print(json_encode($links).PHP_EOL);
19+
}

0 commit comments

Comments
 (0)