feat(database): ✨ re-add per-post counters to track page views etc.
This commit is contained in:
parent
5ddbae91d2
commit
a3604d21c9
7 changed files with 53 additions and 9 deletions
|
@ -26,6 +26,8 @@ CREATE TABLE dev_posts (
|
|||
post_metadata JSON DEFAULT NULL,
|
||||
post_settings_cache JSON DEFAULT NULL,
|
||||
|
||||
post_counters JSON DEFAULT NULL,
|
||||
|
||||
PRIMARY KEY(post_id),
|
||||
CONSTRAINT unique_post UNIQUE (post_path),
|
||||
|
||||
|
|
|
@ -73,6 +73,8 @@ interface PostdataInterface {
|
|||
public function set_postdata($data);
|
||||
public function set_post_markdown($id, $markdown);
|
||||
|
||||
public function increment_post_counter($path, $counter = 'views', $value = 1);
|
||||
|
||||
public function get_postdata($path);
|
||||
// Returns a key-value pair of child paths => child data
|
||||
public function get_post_children($path,
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
<?php
|
||||
|
||||
require_once 'analytics_interface.php';
|
||||
require_once 'db_interface.php';
|
||||
|
||||
require_once 'mysql_taglist_handling.php';
|
||||
|
@ -116,6 +115,9 @@ class MySQLHandler
|
|||
array_push($post_tags,
|
||||
'path:' . $post_path
|
||||
);
|
||||
if(isset($data['type'])) {
|
||||
$post_tags []= 'type:' . $data['type'];
|
||||
}
|
||||
|
||||
$sql_args = [
|
||||
$post_path,
|
||||
|
@ -168,6 +170,19 @@ class MySQLHandler
|
|||
$this->_exec($qry, "is", $id, $markdown);
|
||||
}
|
||||
|
||||
public function increment_post_counter($path, $counter = 'views', $value = 1) {
|
||||
$path = sanitize_post_path($path);
|
||||
|
||||
$qry =
|
||||
"UPDATE {$this->db_prefix}_posts
|
||||
SET post_counters = JSON_SET(COALESCE(post_counters, \"{}\"), ?, COALESCE(JSON_EXTRACT(post_counters, ?), 0) + ?)
|
||||
WHERE post_path = ?";
|
||||
|
||||
$json_path = "$.{$counter}";
|
||||
|
||||
$this->_exec($qry, "ssds", $json_path, $json_path, $value, $path);
|
||||
}
|
||||
|
||||
private function get_post_settings($post_path) {
|
||||
$post_path = sanitize_post_path($post_path);
|
||||
|
||||
|
@ -252,6 +267,13 @@ class MySQLHandler
|
|||
$post_settings = $this->get_post_settings($data['post_path']);
|
||||
}
|
||||
|
||||
if(isset($data['post_counters'])) {
|
||||
$outdata['counters'] = json_decode($data['post_counters'], true);
|
||||
}
|
||||
else {
|
||||
$outdata['counters'] = [];
|
||||
}
|
||||
|
||||
$outdata = array_merge($post_settings, $post_metadata, $outdata);
|
||||
$outdata['host'] ??= $this->hostname;
|
||||
|
||||
|
|
|
@ -21,7 +21,8 @@ class Post implements ArrayAccess {
|
|||
'title' => '404 Page',
|
||||
'metadata' => [
|
||||
'type' => '404'
|
||||
]
|
||||
],
|
||||
'markdown' => 'Whoops! The dergen could not quite find that...'
|
||||
];
|
||||
|
||||
return $post_data;
|
||||
|
@ -139,6 +140,10 @@ class Post implements ArrayAccess {
|
|||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function increment_counter($type = 'views', $value = 1) {
|
||||
$this->handler->increment_post_counter($this->data['path'], $type, $value);
|
||||
}
|
||||
|
||||
public function __get($name) {
|
||||
if($name == 'html') {
|
||||
return $this->get_html();
|
||||
|
|
|
@ -46,6 +46,10 @@ class PostHandler {
|
|||
return ($this->markdown_engine)($post);
|
||||
}
|
||||
|
||||
public function increment_post_counter(...$opts) {
|
||||
$this->db->increment_post_counter(...$opts);
|
||||
}
|
||||
|
||||
public function get_children_for($post, ...$search_opts) {
|
||||
$child_list = $this->db->get_post_children($post->path, ...$search_opts);
|
||||
|
||||
|
|
|
@ -58,7 +58,7 @@ if($REQUEST_PATH == '/upload') {
|
|||
render_root_template('upload.html');
|
||||
die();
|
||||
}
|
||||
if($REQUEST_PATH == '/search') {
|
||||
if($REQUEST_PATH == '/search/') {
|
||||
$search_results = [];
|
||||
$display_type = 'none';
|
||||
|
||||
|
@ -105,13 +105,22 @@ if($REQUEST_PATH == '/search') {
|
|||
}
|
||||
|
||||
$post = $adapter->get_post($REQUEST_PATH);
|
||||
render_post($post);
|
||||
|
||||
die();
|
||||
|
||||
if(!isset($post)) {
|
||||
render_404();
|
||||
die();
|
||||
$error_page = $SITE_CONFIG['site_defaults'];
|
||||
$error_page['path'] = '/404';
|
||||
$error_page['title'] = '404 oh no';
|
||||
$error_page['basename'] = '404.md';
|
||||
|
||||
render_root_template('derg_error.html', [
|
||||
'page' => $error_page,
|
||||
'error_code' => '404 Hoard not found!',
|
||||
'error_description' => "Looks like we couldn't find `" . $REQUEST_PATH . "` for you. Check in later!"
|
||||
]);
|
||||
}
|
||||
else {
|
||||
$post->increment_counter();
|
||||
render_post($post);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
{%endblock%}
|
||||
|
||||
<span id="content_footer">
|
||||
This page was created on {{ page.created_at }}, last edited {{ page.updated_at }}, and was viewed {{ page.view_count }} times~
|
||||
This page was created on {{ page.created_at }}, last edited {{ page.updated_at }}, and was viewed {{ page.counters.views }} times~
|
||||
</span>
|
||||
</article>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue