refactor: ♻️ refactor out post-type specific code
This commit is contained in:
parent
9e855fba36
commit
38a5056ff0
3 changed files with 129 additions and 139 deletions
|
@ -1,10 +1,7 @@
|
|||
<?php
|
||||
|
||||
use Spatie\YamlFrontMatter\YamlFrontMatter;
|
||||
|
||||
class MySQLAdapter {
|
||||
public $raw;
|
||||
public $data_directory;
|
||||
|
||||
function __construct() {
|
||||
$db_params = json_decode(file_get_contents('secrets/db.json'), true);
|
||||
|
@ -30,8 +27,6 @@ class MySQLAdapter {
|
|||
|
||||
//throw $th;
|
||||
}
|
||||
|
||||
$this->data_directory = 'raw';
|
||||
}
|
||||
|
||||
function _exec($qery, $argtypes, ...$args) {
|
||||
|
@ -41,8 +36,8 @@ class MySQLAdapter {
|
|||
|
||||
return $stmt->get_result();
|
||||
}
|
||||
|
||||
function _prepare_post_data($post_data) {
|
||||
|
||||
function _normalize_post_data($post_data) {
|
||||
if($post_data == null) {
|
||||
return [
|
||||
"found" => false
|
||||
|
@ -50,91 +45,20 @@ class MySQLAdapter {
|
|||
}
|
||||
|
||||
$post_data["found"] = true;
|
||||
$post_data["post_basename"] = basename($post_data["post_path"]);
|
||||
|
||||
$post_meta = json_decode($post_data["post_metadata"], true) ?? [];
|
||||
|
||||
$post_meta["title"] ??= basename($post_data["post_path"]);
|
||||
|
||||
if(!isset($post_meta['type'])) {
|
||||
$type = null;
|
||||
|
||||
$ext = pathinfo($post_data['post_basename'], PATHINFO_EXTENSION);
|
||||
|
||||
if($ext == '') {
|
||||
$type = 'directory';
|
||||
} elseif($ext == 'md') {
|
||||
$type = 'text/markdown';
|
||||
}
|
||||
|
||||
$post_meta['type'] = $type;
|
||||
}
|
||||
|
||||
$post_data['post_metadata'] = json_decode($post_data["post_metadata"], true) ?? [];
|
||||
$post_data["post_content"] ??= '';
|
||||
$post_data["post_file_dir"] = '/' . $this->data_directory . $post_data["post_path"];
|
||||
|
||||
$post_data['post_metadata'] = $post_meta;
|
||||
|
||||
return $post_data;
|
||||
}
|
||||
|
||||
function _fill_post_descriptors($post_data) {
|
||||
|
||||
switch(($post_data['post_metadata']['type'] ?? '')) {
|
||||
case 'directory':
|
||||
$readme = $this->get_post_by_path($post_data['post_path'] . '/README.md', false);
|
||||
$post_data['readme'] = $readme;
|
||||
|
||||
if($readme['found']) {
|
||||
$post_data['post_metadata']['title'] = $readme['post_metadata']['title'];
|
||||
}
|
||||
break;
|
||||
|
||||
case 'file':
|
||||
$post_data['descriptor'] = $this->get_post_by_path($post_data['post_path'] . '.md', false);
|
||||
break;
|
||||
}
|
||||
|
||||
return $post_data;
|
||||
}
|
||||
|
||||
function ensure_directory($directory) {
|
||||
$data_directory = $this->data_directory . $directory;
|
||||
|
||||
is_dir($data_directory) || mkdir($data_directory, 0777, true);
|
||||
|
||||
$insert_statement = "
|
||||
INSERT INTO posts
|
||||
(post_path, post_path_depth, post_metadata, post_content)
|
||||
VALUES
|
||||
(?, ?, ?, '') AS new
|
||||
ON DUPLICATE KEY UPDATE post_path=new.post_path
|
||||
";
|
||||
|
||||
$json_metadata = json_encode(["type" => 'directory']);
|
||||
|
||||
while(strlen($directory) > 1) {
|
||||
try {
|
||||
echo "Inserting dir " . $directory . "\n";
|
||||
|
||||
$this->_exec($insert_statement, "sis",
|
||||
$directory, substr_count($directory, '/'),
|
||||
$json_metadata);
|
||||
|
||||
}
|
||||
catch(Exception $e) {
|
||||
}
|
||||
$directory = dirname($directory);
|
||||
}
|
||||
}
|
||||
|
||||
function ensure_post_placeholder($post_path, $post_metadata) {
|
||||
function bump_post($post_path, $post_metadata = [], $create_dirs = true) {
|
||||
$post_path = chop($post_path, '/');
|
||||
$path_depth = substr_count($post_path, "/");
|
||||
|
||||
$this->ensure_directory(dirname($post_path));
|
||||
|
||||
echo "Bumping post placeholder " . $post_path . "\n";
|
||||
if($create_dirs) {
|
||||
$this->make_post_directory(dirname($post_path));
|
||||
}
|
||||
|
||||
$qry = "
|
||||
INSERT INTO posts
|
||||
|
@ -150,13 +74,25 @@ class MySQLAdapter {
|
|||
'');
|
||||
}
|
||||
|
||||
function create_post_entry($post_path, $post_metadata, $post_content) {
|
||||
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) {
|
||||
$post_path = chop($post_path, '/');
|
||||
$path_depth = substr_count($post_path, "/");
|
||||
|
||||
$this->ensure_directory(dirname($post_path));
|
||||
|
||||
echo "Creating post entry " . $post_path . "\n";
|
||||
$this->make_post_directory(dirname($post_path));
|
||||
|
||||
$qry = "
|
||||
INSERT INTO posts
|
||||
|
@ -172,60 +108,13 @@ class MySQLAdapter {
|
|||
$post_content);
|
||||
}
|
||||
|
||||
function save_markdown_post($post_path, $post_data) {
|
||||
$frontmatter_post = YamlFrontMatter::parse($post_data);
|
||||
$post_path = chop($post_path, '/');
|
||||
|
||||
$post_content = $frontmatter_post->body();
|
||||
$post_metadata = $frontmatter_post->matter();
|
||||
|
||||
if(basename($post_path) == "README.md") {
|
||||
$readme_metadata = $frontmatter_post->matter();
|
||||
$readme_metadata['type'] = $post_metadata['readme_type'] ?? 'directory';
|
||||
|
||||
$this->create_post_entry(dirname($post_path), $readme_metadata, '');
|
||||
|
||||
$post_metadata['type'] = 'text/markdown';
|
||||
}
|
||||
|
||||
$post_metadata['type'] ??= 'text/markdown';
|
||||
|
||||
$this->create_post_entry($post_path, $post_metadata, $post_content);
|
||||
file_put_contents($this->data_directory . $post_path,
|
||||
$post_data);
|
||||
|
||||
}
|
||||
|
||||
function save_file($post_path, $file_path) {
|
||||
$post_metadata = ["type" => 'file'];
|
||||
$post_path = chop($post_path, '/');
|
||||
|
||||
$this->ensure_post_placeholder($post_path, $post_metadata, '');
|
||||
|
||||
copy($file_path, $this->data_directory . $post_path);
|
||||
}
|
||||
|
||||
function handle_upload($post_path, $file_path) {
|
||||
$ext = pathinfo($post_path, PATHINFO_EXTENSION);
|
||||
|
||||
switch($ext) {
|
||||
case "md":
|
||||
$this->save_markdown_post($post_path, file_get_contents($file_path));
|
||||
break;
|
||||
default:
|
||||
$this->save_file($post_path, $file_path);
|
||||
}
|
||||
}
|
||||
|
||||
function get_post_by_path($post_path, $with_subposts = true) {
|
||||
$qry = "SELECT * FROM posts WHERE post_path = ?";
|
||||
|
||||
$post_path = chop($post_path, '/');
|
||||
|
||||
$post_data = $this->_exec($qry, "s", $post_path)->fetch_assoc();
|
||||
$post_data = $this->_prepare_post_data($post_data);
|
||||
|
||||
$post_data = $this->_fill_post_descriptors($post_data);
|
||||
$post_data = $this->_normalize_post_data($post_data);
|
||||
|
||||
if($with_subposts) {
|
||||
$post_data['subposts'] = $this->get_subposts_by_path($post_path);
|
||||
|
@ -251,7 +140,7 @@ class MySQLAdapter {
|
|||
$post_data = $this->_exec($qry, "si", $path, $path_depth+1)->fetch_all(MYSQLI_ASSOC);
|
||||
|
||||
$fn = function($data) {
|
||||
return $this->_prepare_post_data($data);
|
||||
return $this->_normalize_post_data($data);
|
||||
};
|
||||
|
||||
$post_data = array_map($fn, $post_data);
|
||||
|
|
101
www/post_adapter.php
Normal file
101
www/post_adapter.php
Normal file
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
require_once 'mysql_adapter.php';
|
||||
|
||||
use Spatie\YamlFrontMatter\YamlFrontMatter;
|
||||
|
||||
class PostHandler extends MySQLAdapter {
|
||||
public $data_directory;
|
||||
|
||||
function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
$this->data_directory = 'raw';
|
||||
}
|
||||
|
||||
function _normalize_post_data($post_data) {
|
||||
$post_data = parent::_normalize_post_data($post_data);
|
||||
|
||||
$post_data["post_basename"] = basename($post_data["post_path"]);
|
||||
|
||||
$post_meta = $post_data['post_metadata'];
|
||||
|
||||
$post_meta["title"] ??= basename($post_data["post_path"]);
|
||||
|
||||
if(!isset($post_meta['type'])) {
|
||||
$type = null;
|
||||
|
||||
$ext = pathinfo($post_data['post_basename'], PATHINFO_EXTENSION);
|
||||
|
||||
$ext_mapping = [
|
||||
'' => 'directory',
|
||||
'md' => 'text/markdown',
|
||||
'png' => 'image',
|
||||
];
|
||||
|
||||
if(isset($ext_mapping[$ext])) {
|
||||
$post_meta['type'] = $ext_mapping[$ext];
|
||||
}
|
||||
}
|
||||
|
||||
$post_data["post_file_dir"] = '/' . $this->data_directory . $post_data["post_path"];
|
||||
|
||||
$post_data['post_metadata'] = $post_meta;
|
||||
|
||||
return $post_data;
|
||||
}
|
||||
|
||||
function make_post_directory($directory) {
|
||||
$data_directory = $this->data_directory . $directory;
|
||||
|
||||
is_dir($data_directory) || mkdir($data_directory, 0777, true);
|
||||
|
||||
parent::make_post_directory($directory);
|
||||
}
|
||||
|
||||
function save_file($post_path, $file_path) {
|
||||
$this->bump_post($post_path);
|
||||
copy($file_path, $this->data_directory . $post_path);
|
||||
}
|
||||
|
||||
function save_markdown_post($post_path, $post_data) {
|
||||
$frontmatter_post = YamlFrontMatter::parse($post_data);
|
||||
$post_path = chop($post_path, '/');
|
||||
|
||||
$post_content = $frontmatter_post->body();
|
||||
$post_metadata = $frontmatter_post->matter();
|
||||
|
||||
if(basename($post_path) == "README.md") {
|
||||
|
||||
$readme_metadata = [];
|
||||
if(isset($post_metadata['directory_data'])) {
|
||||
$readme_metadata = $post_metadata['directory_data'];
|
||||
}
|
||||
|
||||
$this->update_or_create_post(dirname($post_path),
|
||||
$readme_metadata, $post_content);
|
||||
}
|
||||
|
||||
$this->update_or_create_post($post_path, $post_metadata, $post_content);
|
||||
}
|
||||
|
||||
function handle_upload($post_path, $file_path) {
|
||||
$ext = pathinfo($post_path, PATHINFO_EXTENSION);
|
||||
|
||||
switch($ext) {
|
||||
case "md":
|
||||
$this->save_markdown_post($post_path, file_get_contents($file_path));
|
||||
|
||||
file_put_contents($this->data_directory . $post_path,
|
||||
$post_data);
|
||||
break;
|
||||
case "mddesc":
|
||||
$this->save_markdown_post(chop($post_path, '.mddesc'), file_get_contents($file_path));
|
||||
break;
|
||||
default:
|
||||
$this->save_file($post_path, $file_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,11 +1,11 @@
|
|||
<?php
|
||||
|
||||
require_once 'vendor/autoload.php';
|
||||
require_once 'mysql_adapter.php';
|
||||
require_once 'post_adapter.php';
|
||||
|
||||
// $sql = mysqli_connect('mysql', 'root', 'example', 'dragon_fire');
|
||||
|
||||
$adapter = new MySQLAdapter();
|
||||
$adapter = new PostHandler();
|
||||
|
||||
//if (!$sql)
|
||||
// {
|
||||
|
@ -77,7 +77,7 @@ if($SURI == '/') {
|
|||
"subposts" => $post['subposts']
|
||||
]);
|
||||
}
|
||||
elseif($post['post_metadata']['type'] == 'file') {
|
||||
elseif($post['post_metadata']['type'] == 'image') {
|
||||
echo $twig->render('post_types/image.html', [
|
||||
"post" => $post
|
||||
]);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue