200 lines
No EOL
5.4 KiB
PHP
200 lines
No EOL
5.4 KiB
PHP
|
|
<?php
|
|
|
|
class PostData {
|
|
public static $default_banners = [];
|
|
public static $markdown_engine = null;
|
|
|
|
public $data;
|
|
|
|
public $html_data;
|
|
public $raw_data;
|
|
|
|
public static function _generate_404($sql_row) {
|
|
$sql_row ??= [
|
|
'post_id' => -1,
|
|
'post_path' => '/404.md',
|
|
'post_title' => '404 Page',
|
|
'post_metadata' => [
|
|
'type' => '404'
|
|
]
|
|
];
|
|
}
|
|
|
|
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, $sql_row) {
|
|
$this->handler = $post_handler;
|
|
|
|
$this->content_html = null;
|
|
$this->content_markdown = null;
|
|
|
|
$sql_meta = null;
|
|
|
|
if(!isset($sql_row) or !$sql_row['found']) {
|
|
$sql_row = $this->_generate_404($sql_row);
|
|
$sql_meta = $sql_row['post_metadata'];
|
|
}
|
|
|
|
$sql_meta = $sql_row['post_metadata'];
|
|
if(is_string($sql_meta)) {
|
|
$sql_meta = json_decode($sql_meta, true);
|
|
}
|
|
|
|
unset($sql_meta['settings']);
|
|
|
|
$this->sql_row = $sql_row;
|
|
$this->sql_meta = $sql_meta ?? [];
|
|
|
|
$sql_settings = json_decode($sql_row['post_settings_cache'], true)
|
|
?? $this->handler->get_settings_for_path($sql_row['post_path']);
|
|
|
|
$data = [
|
|
'id' => $sql_row['post_id'],
|
|
'path' => $sql_row['post_path'],
|
|
'url' => 'https://' . $sql_row['host'] . $sql_row['post_path'],
|
|
'created_at' => $sql_row['post_create_time'] ?? '',
|
|
'updated_at' => $sql_row['post_update_time'] ?? '',
|
|
'view_count' => $sql_row['post_access_count'] ?? 0
|
|
];
|
|
|
|
$data['title'] = $sql_meta['title'] ?? $sql_row['title'];
|
|
unset($sql_meta['title']);
|
|
|
|
$data['tags'] = $sql_meta['tags'] ?? [];
|
|
unset($sql_meta['tags']);
|
|
|
|
$data['type'] = $sql_meta['type']
|
|
?? self::_deduce_type($sql_row['post_path']);
|
|
unset($sql_meta['type']);
|
|
|
|
$data['icon'] = $sql_meta['icon']
|
|
?? self::_deduce_icon($data['type']);
|
|
unset($sql_meta['icon']);
|
|
|
|
if(isset($sql_meta['media_url'])) {
|
|
$data['media_url'] = $sql_meta['media_url'];
|
|
$data['thumb_url'] = $sql_meta['thumb_url'] ?? $data['media_url'];
|
|
|
|
unset($sql_meta['media_url']);
|
|
unset($sql_meta['thumb_url']);
|
|
}
|
|
|
|
$data['banners'] = $sql_meta['banners']
|
|
?? $sql_settings['banners']
|
|
?? self::$default_banners;
|
|
|
|
unset($sql_meta['banners']);
|
|
unset($sql_settings['banners']);
|
|
|
|
$data['preview_image'] = $sql_meta['preview_image'] ?? $data['banners'][0]['src'] ?? null;
|
|
unset($sql_meta['preview_image']);
|
|
|
|
$data['brief'] = $sql_meta['brief']
|
|
?? $data['title'];
|
|
unset($sql_meta['brief']);
|
|
|
|
$data['excerpt'] = $sql_meta['excerpt']
|
|
?? substr($sql_row['post_content'], 0, 256);
|
|
|
|
$data['metadata'] = $sql_meta;
|
|
$data['settings'] = $sql_settings;
|
|
|
|
$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();
|
|
}
|
|
|
|
return $this->data[$name];
|
|
}
|
|
|
|
public function get_html() {
|
|
$fn = self::$markdown_engine;
|
|
$this->content_html ??= $fn($this);
|
|
|
|
return $this->content_html;
|
|
}
|
|
public function get_markdown() {
|
|
$this->content_markdown ??=
|
|
$this->handler->get_markdown_for_id($this->data['id']);
|
|
|
|
return $this->content_markdown;
|
|
}
|
|
|
|
public function to_json($with_markdown = false, $with_html = false) {
|
|
$out_data = $this->data;
|
|
|
|
if($with_markdown) {
|
|
$out_data['markdown'] = $this->get_markdown();
|
|
}
|
|
if($with_html) {
|
|
$out_data['html'] = $this->get_html();
|
|
}
|
|
|
|
return json_encode($out_data);
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
public function get_child_posts() {
|
|
if(isset($this->child_posts))
|
|
return $this->child_posts;
|
|
|
|
$child_data = $this->handler->get_subposts_by_path($this->data['path']);
|
|
|
|
$this->child_posts = array_map(function($data) {
|
|
return new PostData($this->handler, $data);
|
|
}, $child_data);
|
|
}
|
|
}
|
|
|
|
?>
|