feat: ✨ added first draft of Database based website rendering
Some checks failed
/ phplint (push) Failing after 3s
Some checks failed
/ phplint (push) Failing after 3s
This commit is contained in:
parent
5bcba0b689
commit
fcc42bde20
4 changed files with 171 additions and 23 deletions
|
@ -9,40 +9,103 @@ CREATE TABLE posts (
|
||||||
post_path VARCHAR(255) NOT NULL,
|
post_path VARCHAR(255) NOT NULL,
|
||||||
post_path_depth INTEGER NOT NULL DEFAULT 0,
|
post_path_depth INTEGER NOT NULL DEFAULT 0,
|
||||||
|
|
||||||
post_title TEXT,
|
post_create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
post_description TEXT,
|
post_update_time DATETIME NOT NULL
|
||||||
post_brief TEXT,
|
DEFAULT CURRENT_TIMESTAMP
|
||||||
|
ON UPDATE CURRENT_TIMESTAMP,
|
||||||
|
|
||||||
|
post_metadata JSON NOT NULL,
|
||||||
|
|
||||||
post_content MEDIUMTEXT,
|
post_content MEDIUMTEXT,
|
||||||
post_content_type TEXT,
|
|
||||||
|
|
||||||
PRIMARY KEY(post_id),
|
PRIMARY KEY(post_id),
|
||||||
CONSTRAINT unique_post_path UNIQUE (post_path),
|
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)
|
INDEX(post_path_depth, post_path)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE post_tags (
|
|
||||||
post_id INTEGER,
|
|
||||||
tag VARCHAR(255),
|
|
||||||
|
|
||||||
CONSTRAINT post_fkey
|
INSERT INTO posts (post_path, post_path_depth, post_metadata, post_content)
|
||||||
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)
|
|
||||||
VALUES (
|
VALUES (
|
||||||
'/about',
|
'/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
|
# About the dergs indeed
|
||||||
|
|
||||||
This is just a simple test. Might be nice, though!
|
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
|
||||||
|
'
|
||||||
);
|
);
|
|
@ -5,18 +5,59 @@ require_once 'vendor/autoload.php';
|
||||||
|
|
||||||
$sql = mysqli_connect('mysql', 'root', 'example', 'dragon_fire');
|
$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) {
|
function get_post_by_path($path) {
|
||||||
global $sql;
|
global $sql;
|
||||||
|
|
||||||
|
$path = chop($path, '/');
|
||||||
|
|
||||||
$qry = "SELECT * FROM posts WHERE post_path = ?";
|
$qry = "SELECT * FROM posts WHERE post_path = ?";
|
||||||
|
|
||||||
$stmt = $sql->prepare($qry);
|
$stmt = $sql->prepare($qry);
|
||||||
$stmt->bind_param("s", $path);
|
$stmt->bind_param("s", $path);
|
||||||
$stmt->execute();
|
$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)
|
//if (!$sql)
|
||||||
|
@ -51,6 +92,19 @@ if($_SERVER['REQUEST_URI'] == '/') {
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
echo json_encode(get_post_by_path($match[1]));
|
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'])) {
|
} elseif(preg_match('/^\/about(.html)?$/', $_SERVER['REQUEST_URI'])) {
|
||||||
echo $twig->render('about.html');
|
echo $twig->render('about.html');
|
||||||
} elseif(preg_match('/^\/gallery\/([^\?]+)/', $_SERVER['REQUEST_URI'])) {
|
} elseif(preg_match('/^\/gallery\/([^\?]+)/', $_SERVER['REQUEST_URI'])) {
|
||||||
|
|
|
@ -3,11 +3,23 @@
|
||||||
{% extends "root.html" %}
|
{% extends "root.html" %}
|
||||||
|
|
||||||
{% block second_title %}
|
{% block second_title %}
|
||||||
<h2> Abouts </h2>
|
<h2> {{ post.post_metadata.title }} </h2>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{%block main_content%}
|
{%block main_content%}
|
||||||
<article>
|
<article>
|
||||||
{{ include('about.md')|markdown_to_html }}
|
{{ post['post_content']|markdown_to_html }}
|
||||||
</article>
|
</article>
|
||||||
|
|
||||||
|
{% if subposts|length > 0 %}
|
||||||
|
<article>
|
||||||
|
<h3>Related:</h3>
|
||||||
|
<ul>
|
||||||
|
{% for subpost in subposts %}
|
||||||
|
<li><a href={{subpost.post_path}}>{{ subpost.post_metadata.title }}</a></li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
</article>
|
||||||
|
{%endif%}
|
||||||
|
|
||||||
{%endblock%}
|
{%endblock%}
|
||||||
|
|
19
www/templates/generic_markdown_post.html
Normal file
19
www/templates/generic_markdown_post.html
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
|
||||||
|
{% extends "root.html" %}
|
||||||
|
|
||||||
|
{% block second_title %}
|
||||||
|
<h2> Abouts </h2>
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{%block main_content%}
|
||||||
|
<article>
|
||||||
|
{{ include('about.md')|markdown_to_html }}
|
||||||
|
</article>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
{% for subpost in subposts %}
|
||||||
|
<li> {{ subpost.title }} </li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
{%endblock%}
|
Loading…
Add table
Add a link
Reference in a new issue