2023-10-23 22:38:14 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class MySQLAdapter {
|
|
|
|
public $raw;
|
|
|
|
|
|
|
|
function __construct() {
|
2023-12-11 23:30:39 +01:00
|
|
|
$db_params = json_decode(file_get_contents('secrets/db.json'), true);
|
|
|
|
|
2023-12-12 22:39:06 +01:00
|
|
|
try {
|
|
|
|
if(false !== getenv('MYSQL_HOST')) {
|
|
|
|
$this->raw = mysqli_connect(getenv('MYSQL_HOST'),
|
|
|
|
getenv('MYSQL_USER'), getenv('MYSQL_PASSWORD'),
|
|
|
|
getenv('MYSQL_DATABASE'),
|
|
|
|
getenv('MYSQL_PORT'));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->raw = mysqli_connect($db_params['MYSQL_HOST'],
|
|
|
|
$db_params['MYSQL_USER'], $db_params['MYSQL_PASSWORD'],
|
|
|
|
$db_params['MYSQL_DATABASE'],
|
|
|
|
$db_params['MYSQL_PORT']);
|
|
|
|
}
|
|
|
|
} catch (\Throwable $th) {
|
2023-12-11 16:20:27 +01:00
|
|
|
echo 'Connection failed<br>';
|
|
|
|
echo 'Error number: ' . mysqli_connect_errno() . '<br>';
|
|
|
|
echo 'Error message: ' . mysqli_connect_error() . '<br>';
|
|
|
|
die();
|
2023-12-12 22:39:06 +01:00
|
|
|
|
|
|
|
//throw $th;
|
2023-10-23 22:38:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _exec($qery, $argtypes, ...$args) {
|
|
|
|
$stmt = $this->raw->prepare($qery);
|
|
|
|
$stmt->bind_param($argtypes, ...$args);
|
|
|
|
$stmt->execute();
|
|
|
|
|
|
|
|
return $stmt->get_result();
|
|
|
|
}
|
2023-12-13 10:19:00 +01:00
|
|
|
|
|
|
|
function _normalize_post_data($post_data) {
|
2023-12-11 16:20:27 +01:00
|
|
|
if($post_data == null) {
|
|
|
|
return [
|
|
|
|
"found" => false
|
2023-10-23 22:38:14 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-12-11 16:20:27 +01:00
|
|
|
$post_data["found"] = true;
|
|
|
|
|
2023-12-13 10:19:00 +01:00
|
|
|
$post_data['post_metadata'] = json_decode($post_data["post_metadata"], true) ?? [];
|
2023-12-11 16:20:27 +01:00
|
|
|
$post_data["post_content"] ??= '';
|
|
|
|
|
|
|
|
return $post_data;
|
|
|
|
}
|
|
|
|
|
2023-12-13 10:19:00 +01:00
|
|
|
function bump_post($post_path, $post_metadata = [], $create_dirs = true) {
|
2023-10-25 09:12:03 +02:00
|
|
|
$post_path = chop($post_path, '/');
|
|
|
|
$path_depth = substr_count($post_path, "/");
|
|
|
|
|
2023-12-13 10:19:00 +01:00
|
|
|
if($create_dirs) {
|
|
|
|
$this->make_post_directory(dirname($post_path));
|
|
|
|
}
|
2023-10-25 09:12:03 +02:00
|
|
|
|
2023-12-11 16:20:27 +01:00
|
|
|
$qry = "
|
|
|
|
INSERT INTO posts
|
|
|
|
(post_path, post_path_depth, post_metadata, post_content)
|
|
|
|
VALUES
|
|
|
|
( ?, ?, ?, ?) AS new
|
|
|
|
ON DUPLICATE KEY UPDATE post_path=new.post_path;";
|
|
|
|
|
|
|
|
$this->_exec($qry, "siss",
|
|
|
|
$post_path,
|
|
|
|
$path_depth,
|
|
|
|
json_encode($post_metadata),
|
|
|
|
'');
|
|
|
|
}
|
|
|
|
|
2023-12-13 10:19:00 +01:00
|
|
|
function make_post_directory($directory) {
|
|
|
|
$json_metadata = ["type" => 'directory'];
|
|
|
|
|
|
|
|
while(strlen($directory) > 1) {
|
|
|
|
try {
|
|
|
|
$this->bump_post($directory, $json_metadata, false);
|
|
|
|
}
|
|
|
|
catch(Exception $e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
$directory = dirname($directory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function update_or_create_post($post_path, $post_metadata, $post_content) {
|
2023-12-11 16:20:27 +01:00
|
|
|
$post_path = chop($post_path, '/');
|
|
|
|
$path_depth = substr_count($post_path, "/");
|
|
|
|
|
2023-12-13 10:19:00 +01:00
|
|
|
$this->make_post_directory(dirname($post_path));
|
2023-10-25 09:12:03 +02:00
|
|
|
|
|
|
|
$qry = "
|
2023-12-11 16:20:27 +01:00
|
|
|
INSERT INTO posts
|
|
|
|
(post_path, post_path_depth, post_metadata, post_content)
|
|
|
|
VALUES
|
|
|
|
( ?, ?, ?, ?) AS new
|
|
|
|
ON DUPLICATE KEY UPDATE post_metadata=new.post_metadata, post_content=new.post_content;";
|
2023-10-25 09:12:03 +02:00
|
|
|
|
|
|
|
$this->_exec($qry, "siss",
|
|
|
|
$post_path,
|
|
|
|
$path_depth,
|
|
|
|
json_encode($post_metadata),
|
|
|
|
$post_content);
|
|
|
|
}
|
|
|
|
|
2023-12-19 10:14:14 +01:00
|
|
|
function get_settings_for_path($post_path) {
|
|
|
|
|
|
|
|
$qry = "
|
|
|
|
WITH RECURSIVE settings_data (post_path, post_depth, json_settings) AS (
|
|
|
|
SELECT post_path, post_path_depth, post_metadata
|
|
|
|
FROM posts
|
|
|
|
WHERE post_path = ?
|
|
|
|
|
|
|
|
UNION ALL
|
|
|
|
|
|
|
|
SELECT posts.post_path, posts.post_path_depth, posts.post_metadata
|
|
|
|
FROM posts, settings_data
|
|
|
|
WHERE posts.post_path = SUBSTRING_INDEX(settings_data.post_path, '/', settings_data.post_depth)
|
|
|
|
AND posts.post_path_depth = settings_data.post_depth - 1
|
|
|
|
)
|
|
|
|
SELECT post_depth, json_settings
|
|
|
|
FROM settings_data
|
|
|
|
ORDER BY post_depth ASC;
|
|
|
|
";
|
2023-10-23 22:38:14 +02:00
|
|
|
|
2023-12-19 10:14:14 +01:00
|
|
|
$out_settings = [];
|
|
|
|
|
|
|
|
$settings_list = $this->_exec($qry, "s", $post_path)->fetch_all(MYSQLI_ASSOC);
|
|
|
|
|
|
|
|
foreach($settings_list AS $setting) {
|
|
|
|
$setting = json_decode($setting['json_settings'], true);
|
|
|
|
|
|
|
|
if(!isset($setting) || !isset($setting['settings'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$out_settings = array_merge($out_settings, $setting['settings']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out_settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_post_by_path($post_path,
|
|
|
|
$with_subposts = true, $with_settings = true) {
|
|
|
|
|
|
|
|
$qry = "SELECT * FROM posts WHERE post_path = ?";
|
2023-10-23 22:38:14 +02:00
|
|
|
$post_path = chop($post_path, '/');
|
|
|
|
|
2023-12-11 16:20:27 +01:00
|
|
|
$post_data = $this->_exec($qry, "s", $post_path)->fetch_assoc();
|
2023-12-13 10:19:00 +01:00
|
|
|
$post_data = $this->_normalize_post_data($post_data);
|
2023-10-23 22:38:14 +02:00
|
|
|
|
2023-12-18 16:02:31 +01:00
|
|
|
$post_data['post_path'] = $post_path;
|
|
|
|
|
2023-10-23 22:38:14 +02:00
|
|
|
if($with_subposts) {
|
|
|
|
$post_data['subposts'] = $this->get_subposts_by_path($post_path);
|
|
|
|
}
|
2023-12-19 10:14:14 +01:00
|
|
|
if($with_settings) {
|
|
|
|
$post_data['settings'] = $this->get_settings_for_path($post_path);
|
|
|
|
}
|
2023-10-23 22:38:14 +02:00
|
|
|
|
|
|
|
return $post_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_subposts_by_path($path) {
|
|
|
|
global $sql;
|
|
|
|
|
|
|
|
$path = chop($path, '/');
|
|
|
|
|
|
|
|
$path_depth = substr_count($path, "/");
|
|
|
|
|
2023-12-11 16:20:27 +01:00
|
|
|
$qry = "SELECT post_path, post_metadata, post_update_time
|
2023-10-23 22:38:14 +02:00
|
|
|
FROM posts
|
|
|
|
WHERE (post_path LIKE CONCAT(?,'/%'))
|
|
|
|
AND post_path_depth = ?
|
|
|
|
ORDER BY post_create_time DESC
|
|
|
|
LIMIT 10";
|
|
|
|
|
2023-10-25 09:12:03 +02:00
|
|
|
$post_data = $this->_exec($qry, "si", $path, $path_depth+1)->fetch_all(MYSQLI_ASSOC);
|
2023-10-23 22:38:14 +02:00
|
|
|
|
|
|
|
$fn = function($data) {
|
2023-12-13 10:19:00 +01:00
|
|
|
return $this->_normalize_post_data($data);
|
2023-10-23 22:38:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
$post_data = array_map($fn, $post_data);
|
|
|
|
|
|
|
|
return $post_data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|