Skip to content

Commit

Permalink
Adding datastore2json script
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Dec 4, 2023
1 parent 61a365b commit d5409f6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
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;
}
}
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,8 @@
"Shaarli\\Tests\\": "tests",
"Shaarli\\Tests\\Utils\\": "tests/utils"
}
},
"scripts": {
"datastore2json": "php -f datastore2json.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 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);
}

0 comments on commit d5409f6

Please sign in to comment.