From fcc42bde20813f5099a5c5fc51f887dbcf33f9c1 Mon Sep 17 00:00:00 2001 From: xaseiresh Date: Sun, 22 Oct 2023 12:59:20 +0200 Subject: [PATCH] feat: :sparkles: added first draft of Database based website rendering --- docker_dev/mysql_schema.sql | 101 ++++++++++++++++++----- www/router.php | 58 ++++++++++++- www/templates/about.html | 16 +++- www/templates/generic_markdown_post.html | 19 +++++ 4 files changed, 171 insertions(+), 23 deletions(-) create mode 100644 www/templates/generic_markdown_post.html diff --git a/docker_dev/mysql_schema.sql b/docker_dev/mysql_schema.sql index 6b954b7..e3c1b58 100644 --- a/docker_dev/mysql_schema.sql +++ b/docker_dev/mysql_schema.sql @@ -9,40 +9,103 @@ CREATE TABLE posts ( post_path VARCHAR(255) NOT NULL, post_path_depth INTEGER NOT NULL DEFAULT 0, - post_title TEXT, - post_description TEXT, - post_brief TEXT, + post_create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + post_update_time DATETIME NOT NULL + DEFAULT CURRENT_TIMESTAMP + ON UPDATE CURRENT_TIMESTAMP, + + post_metadata JSON NOT NULL, post_content MEDIUMTEXT, - post_content_type TEXT, PRIMARY KEY(post_id), CONSTRAINT unique_post_path UNIQUE (post_path), - INDEX(post_path), + INDEX(post_path, post_update_time), + INDEX(post_path, post_create_time), INDEX(post_path_depth, post_path) ); -CREATE TABLE post_tags ( - post_id INTEGER, - tag VARCHAR(255), - CONSTRAINT post_fkey - FOREIGN KEY(post_id) REFERENCES posts(post_id) - ON DELETE CASCADE, - - INDEX(post_id), - INDEX(tag) -); - - -INSERT INTO posts (post_path, post_title, post_content) +INSERT INTO posts (post_path, post_path_depth, post_metadata, post_content) VALUES ( '/about', - 'About the Dergs', + 0, +' +{ + "tags": ["test", "test2", "hellorld"], + "brief": "This is a simple test indeed", + "type": "text/markdown", + "title": "About the dergen" +} +', ' # About the dergs indeed This is just a simple test. Might be nice, though! ' +), ( + '/about/neira', + 1, +' +{ + "tags": ["test", "test2", "hellorld", "neira"], + "brief": "This is a soft grab of Neira", + "type": "text/markdown", + "title": "About her" +} +', +' +# Nothing here yet! + +Sorry for this. She is working hard :> +' +), ( + '/about/xasin', + 1, +' +{ + "tags": ["test", "test2", "hellorld", "xasin"], + "brief": "This is a soft grab of Xasin", + "type": "text/markdown", + "title": "About her" +} +', +' +# Nothing here yet! + +Sorry for this. He is working hard :> +' +), ( + '/about/mesh', + 1, +' +{ + "tags": ["test", "test2", "hellorld", "mesh"], + "brief": "This is a soft grab of Mesh", + "type": "text/markdown", + "title": "About her" +} +', +' +# Nothing here yet! + +Sorry for this. Shi is working hard :> +' +), ( + '/about/alviere', + 1, +' +{ + "tags": ["test", "test2", "hellorld", "mesh"], + "brief": "SHE GRABS", + "type": "text/markdown", + "title": "SHE GRABS" +} +', +' +# Nothing here yet! + +Sorry for this. She GRABS A LOT +' ); \ No newline at end of file diff --git a/www/router.php b/www/router.php index 7d76a83..091897e 100644 --- a/www/router.php +++ b/www/router.php @@ -5,18 +5,59 @@ require_once 'vendor/autoload.php'; $sql = mysqli_connect('mysql', 'root', 'example', 'dragon_fire'); +function process_post_data($post_data) { + if($post_data == null) { + $post_data = [ + "found" => false + ]; + } + else { + $post_data["found"] = true; + $post_data["post_metadata"] = json_decode($post_data["post_metadata"]); + } + + return $post_data; +} + function get_post_by_path($path) { global $sql; + $path = chop($path, '/'); + $qry = "SELECT * FROM posts WHERE post_path = ?"; $stmt = $sql->prepare($qry); $stmt->bind_param("s", $path); $stmt->execute(); - // $result = $stmt->get_result(); + $post_data = $stmt->get_result()->fetch_assoc(); - return $stmt->get_result()->fetch_assoc(); + return process_post_data($post_data); +} + +function get_subposts($path) { + global $sql; + + $path = chop($path, '/'); + + $path_depth = substr_count($path, "/"); + + $qry = "SELECT post_path, post_metadata + FROM posts + WHERE (post_path LIKE CONCAT(?,'/%')) + AND post_path_depth = ? + ORDER BY post_create_time DESC + LIMIT 10"; + + $stmt = $sql->prepare($qry); + $stmt->bind_param("si", $path, $path_depth); + $stmt->execute(); + + $post_data = $stmt->get_result()->fetch_all(MYSQLI_ASSOC); + + $post_data = array_map('process_post_data', $post_data); + + return $post_data; } //if (!$sql) @@ -51,6 +92,19 @@ if($_SERVER['REQUEST_URI'] == '/') { header('Content-Type: application/json'); echo json_encode(get_post_by_path($match[1])); +} elseif(preg_match('/^\/api\/subposts(.*)$/', $_SERVER['REQUEST_URI'], $match)) { + + header('Content-Type: application/json'); + echo json_encode(get_subposts($match[1])); + +} elseif(true) { + $post = get_post_by_path($_SERVER['REQUEST_URI']); + + echo $twig->render('about.html', [ + "post" => $post, + "subposts" => get_subposts($_SERVER['REQUEST_URI']) + ]); + } elseif(preg_match('/^\/about(.html)?$/', $_SERVER['REQUEST_URI'])) { echo $twig->render('about.html'); } elseif(preg_match('/^\/gallery\/([^\?]+)/', $_SERVER['REQUEST_URI'])) { diff --git a/www/templates/about.html b/www/templates/about.html index 16352aa..2b8619d 100644 --- a/www/templates/about.html +++ b/www/templates/about.html @@ -3,11 +3,23 @@ {% extends "root.html" %} {% block second_title %} -

Abouts

+

{{ post.post_metadata.title }}

{% endblock %} {%block main_content%}
- {{ include('about.md')|markdown_to_html }} + {{ post['post_content']|markdown_to_html }}
+ + {% if subposts|length > 0 %} +
+

Related:

+ +
+ {%endif%} + {%endblock%} diff --git a/www/templates/generic_markdown_post.html b/www/templates/generic_markdown_post.html new file mode 100644 index 0000000..6a0170f --- /dev/null +++ b/www/templates/generic_markdown_post.html @@ -0,0 +1,19 @@ + +{% extends "root.html" %} + +{% block second_title %} +

Abouts

+{% endblock %} + +{%block main_content%} +
+ {{ include('about.md')|markdown_to_html }} +
+ + + +{%endblock%}