everything(everything): simply saving work
This commit is contained in:
parent
0f2761cd61
commit
76ca7b9c32
25 changed files with 1330 additions and 561 deletions
205
www/src/db_handler/post.php
Normal file
205
www/src/db_handler/post.php
Normal file
|
@ -0,0 +1,205 @@
|
|||
|
||||
<?php
|
||||
|
||||
class Post implements ArrayAccess {
|
||||
public $handler;
|
||||
|
||||
private $content_html;
|
||||
private $content_markdown;
|
||||
|
||||
public $site_defaults;
|
||||
|
||||
public $data;
|
||||
|
||||
public $html_data;
|
||||
public $raw_data;
|
||||
|
||||
private $_child_posts;
|
||||
|
||||
public static function _generate_404($post_data) {
|
||||
$post_data ??= [
|
||||
'id' => -1,
|
||||
'path' => '/404.md',
|
||||
'title' => '404 Page',
|
||||
'metadata' => [
|
||||
'type' => '404'
|
||||
]
|
||||
];
|
||||
|
||||
return $post_data;
|
||||
}
|
||||
|
||||
public static function _deduce_type($path) {
|
||||
$ext = pathinfo($path, PATHINFO_EXTENSION);
|
||||
|
||||
if(preg_match("/\.(\w+)\.md$/", $path, $ext_match)) {
|
||||
$ext = $ext_match[1];
|
||||
}
|
||||
|
||||
$ext_mapping = [
|
||||
'' => 'directory',
|
||||
'md' => 'text/markdown',
|
||||
'png' => 'image',
|
||||
'jpg' => 'image',
|
||||
'jpeg' => 'image'
|
||||
];
|
||||
|
||||
return $ext_mapping[$ext] ?? '?';
|
||||
}
|
||||
|
||||
public static function _deduce_icon($type) {
|
||||
$icon_mapping = [
|
||||
'' => 'question',
|
||||
'text/markdown' => 'markdown',
|
||||
'blog' => 'markdown',
|
||||
'blog_list' => 'rectangle-list',
|
||||
'directory' => 'folder',
|
||||
'gallery' => 'images',
|
||||
'image' => 'image'
|
||||
];
|
||||
|
||||
return $icon_mapping[$type] ?? 'unknown';
|
||||
}
|
||||
|
||||
function __construct($post_handler, $post_data) {
|
||||
$this->handler = $post_handler;
|
||||
|
||||
$this->content_html = null;
|
||||
$this->content_markdown = null;
|
||||
|
||||
$this->site_defaults = null;
|
||||
|
||||
if(!isset($post_data) or !isset($post_data['id'])) {
|
||||
$post_data = $this->_generate_404($post_data);
|
||||
}
|
||||
|
||||
$data = $post_data;
|
||||
|
||||
$post_data['host'] ??= 'localhost:8081';
|
||||
|
||||
$data['url'] ??= 'https://' . $post_data['host'] . $post_data['path'];
|
||||
|
||||
$data['title'] ??= basename($data['path']);
|
||||
|
||||
$data['tags'] ??= [];
|
||||
|
||||
$data['type'] ??= self::_deduce_type($post_data['path']);
|
||||
|
||||
$data['icon'] ??= self::_deduce_icon($data['type']);
|
||||
|
||||
if(isset($sql_meta['media_url'])) {
|
||||
$data['thumb_url'] ??= $data['media_url'];
|
||||
}
|
||||
|
||||
$data['preview_image'] ??= $data['banners'][0]['src'] ?? null;
|
||||
|
||||
$data['brief'] ??= $data['title'];
|
||||
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function __get($name) {
|
||||
if($name == 'html') {
|
||||
return $this->get_html();
|
||||
}
|
||||
if($name == 'markdown') {
|
||||
return $this->get_markdown();
|
||||
}
|
||||
if($name == 'json') {
|
||||
return $this->to_json();
|
||||
}
|
||||
if($name == 'child_posts') {
|
||||
return $this->get_child_posts();
|
||||
}
|
||||
|
||||
if(isset($this->data[$name])) {
|
||||
return $this->data[$name];
|
||||
}
|
||||
|
||||
if(is_null($this->site_defaults)) {
|
||||
throw new RuntimeException("Post site defaults have not been set properly!");
|
||||
}
|
||||
|
||||
return $this->site_defaults[$name] ?? null;
|
||||
}
|
||||
|
||||
public function offsetGet($offset) : mixed {
|
||||
return $this->__get($offset) ?? null;
|
||||
}
|
||||
public function offsetExists($offset) : bool {
|
||||
if(isset($this->data[$offset])) {
|
||||
return true;
|
||||
}
|
||||
if(isset($this->site_defaults[$offset])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return !is_null($this->offsetGet($offset));
|
||||
}
|
||||
public function offsetSet($offset, $value) : void {
|
||||
throw RuntimeError("Setting of post data is not allowed!");
|
||||
}
|
||||
public function offsetUnset($offset) : void {
|
||||
throw RuntimeError("Unsetting of post data is not allowed!");
|
||||
}
|
||||
|
||||
public function get_html() {
|
||||
$this->content_html ??= $this->handler->render_post($this);
|
||||
|
||||
return $this->content_html;
|
||||
}
|
||||
public function get_markdown() {
|
||||
$this->content_markdown ??=
|
||||
$this->handler->get_markdown_for($this);
|
||||
|
||||
return $this->content_markdown;
|
||||
}
|
||||
|
||||
public function get_child_posts(...$search_args) {
|
||||
var_dump($search_args);
|
||||
|
||||
|
||||
if(count($search_args) == 0) {
|
||||
$this->child_posts ??=
|
||||
$this->handler->get_children_for($this);
|
||||
|
||||
return $this->child_posts;
|
||||
}
|
||||
else {
|
||||
return $this->handler->get_children_for($this, ...$search_args);
|
||||
}
|
||||
}
|
||||
|
||||
public function to_array($options = []) {
|
||||
$out_data = $this->data;
|
||||
|
||||
if(isset($options['markdown'])) {
|
||||
$out_data['markdown'] = $this->get_markdown();
|
||||
}
|
||||
if(isset($options['html'])) {
|
||||
$out_data['html'] = $this->get_html();
|
||||
}
|
||||
if(isset($options['children'])) {
|
||||
die();
|
||||
}
|
||||
|
||||
return $out_data;
|
||||
}
|
||||
public function to_json($options = []) {
|
||||
return json_encode($this->to_array($options));
|
||||
}
|
||||
|
||||
|
||||
public function get_parent_post() {
|
||||
$parent_path = dirname($this->data['path']);
|
||||
if($parent_path == '')
|
||||
return null;
|
||||
|
||||
$this->parent_post ??= new PostData($this->handler,
|
||||
$this->handler->get_post_by_path($parent_path));
|
||||
|
||||
return $this->parent_post;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue