feat: preliminary work on revised Post system
Some checks reported warnings
/ phplint (push) Has been cancelled

This commit is contained in:
David Bailey 2024-02-12 23:20:43 +01:00
parent b8e449006b
commit 0f2761cd61
4 changed files with 58 additions and 8 deletions

View file

@ -386,7 +386,8 @@ class MySQLAdapter {
$post_data ??= ['found' => false];
$post_data['post_path'] = $post_path;
$post_data['parent_path'] = dirname($post_path);
$post_data = $this->_normalize_post_data($post_data);
if(!$post_data['found']) {
@ -403,6 +404,19 @@ class MySQLAdapter {
return $post_data;
}
function get_markdown_for_id($id) {
$qry = "SELECT post_content
FROM posts
WHERE post_id = ? AND host = ?
";
$post_data = $this->_exec($qry, "is", $id,
$this->SITE_CONFIG['HTTP_HOST']
)->fetch_assoc();
return $post_data['post_content'];
}
function get_subposts_by_path($path) {
global $sql;

View file

@ -43,6 +43,8 @@ class PostData {
$icon_mapping = [
'' => 'question',
'text/markdown' => 'markdown',
'blog' => 'markdown',
'blog_list' => 'rectangle-list',
'directory' => 'folder',
'gallery' => 'images',
'image' => 'image'
@ -52,17 +54,18 @@ class PostData {
}
function __construct($post_handler, $sql_row) {
$this->post_handler = $post_handler;
$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);
@ -72,7 +75,9 @@ class PostData {
$this->sql_row = $sql_row;
$this->sql_meta = $sql_meta ?? [];
$sql_settings = json_decode($sql_row['post_settings_cache'], true) ?? [];
$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'],
@ -133,7 +138,7 @@ class PostData {
return $this->get_html();
}
if($name == 'markdown') {
return $this->sql_row['post_content'];
return $this->get_markdown();
}
if($name == 'json') {
return $this->to_json();
@ -148,12 +153,18 @@ class PostData {
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->sql_row['post_content'];
$out_data['markdown'] = $this->get_markdown();
}
if($with_html) {
$out_data['html'] = $this->get_html();
@ -161,6 +172,29 @@ class PostData {
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);
}
}
?>

View file

@ -36,8 +36,10 @@ class PostHandler extends MySQLAdapter {
$icon_mapping = [
'' => 'question',
'text/markdown' => 'markdown',
'blog' => 'markdown',
'directory' => 'folder',
'gallery' => 'images',
'blog_list' => 'rectangle-list',
'image' => 'image'
];

View file

@ -225,7 +225,7 @@ function try_render_post($SURI) {
$search_result = $adapter->perform_post_search($search_query);
$search_result = array_map(function($key) {
$post = new PostData(null, $key);
$post = new PostData($adapter, $key);
return $post->data;
}, $search_result['results']);
@ -285,7 +285,7 @@ function generate_website($SURI) {
header('Content-Type: application/json');
$post = new PostData(null, $adapter->get_post_by_path($match[1]));
$post = new PostData($adapter, $adapter->get_post_by_path($match[1]));
echo $post->to_json(with_markdown: true, with_html: true);
} elseif(preg_match('/^\/api\/subposts(.*)$/', $SURI, $match)) {