Browse Source

Change views ID to slug

Blog post: post_slug
Page: page_slug
Subpage: subpage_parentSlug.slug
pull/674/head
danpros 1 year ago
parent
commit
dc742ea2a1
4 changed files with 136 additions and 140 deletions
  1. +45
    -82
      system/admin/admin.php
  2. +35
    -38
      system/htmly.php
  3. +11
    -1
      system/includes/dispatch.php
  4. +45
    -19
      system/includes/functions.php

+ 45
- 82
system/admin/admin.php View File

@ -445,13 +445,17 @@ function edit_content($title, $tag, $url, $content, $oldfile, $revertPost, $publ
rebuilt_cache('all');
clear_post_cache($dt, $post_tag, $post_url, $oldfile, $category, $type);
if ($oldfile != $newfile) {
$old_filename = pathinfo($oldfile, PATHINFO_FILENAME);
$old_ex = explode('_', $old_filename);
$old_url = $old_ex[2];
if ($old_url != $post_url) {
if (file_exists($viewsFile)) {
$views = json_decode(file_get_contents($viewsFile), true);
$arr = replace_key($views, $oldfile, $newfile);
file_put_contents($viewsFile, json_encode($arr, JSON_UNESCAPED_UNICODE), LOCK_EX);
$views = json_decode(file_get_contents($viewsFile), true);
$arr = replace_key($views, 'post_' . $old_url, 'post_' . $post_url);
save_json_pretty($viewsFile, $arr);
}
}
}
if ($destination == 'post') {
if(!empty($revertPost)) {
@ -674,38 +678,52 @@ function edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft,
} else {
$pu = $post_url;
}
$old_filename = pathinfo($oldfile, PATHINFO_FILENAME);
$old_ex = explode('.', $old_filename);
if (isset($old_ex[1])) {
$old_url = $old_ex[1];
} else {
$old_url = $old_filename;
}
rebuilt_cache('all');
clear_page_cache($post_url);
if (!empty($static)) {
$posturl = site_url() . $static .'/'. $pu;
if ($old_url != $pu) {
if (file_exists($viewsFile)) {
$views = json_decode(file_get_contents($viewsFile), true);
$arr = replace_key($views, 'subpage_' . $static .'.'. $old_url, 'subpage_' . $static .'.'. $pu);
save_json_pretty($viewsFile, $arr);
}
}
} else {
$posturl = site_url() . $pu;
}
rebuilt_cache('all');
clear_page_cache($post_url);
if ($oldfile != $newfile) {
if (file_exists($viewsFile)) {
$views = json_decode(file_get_contents($viewsFile), true);
$arr = replace_key($views, $oldfile, $newfile);
file_put_contents($viewsFile, json_encode($arr, JSON_UNESCAPED_UNICODE), LOCK_EX);
}
if (empty($static)) {
if ($old_url != $pu) {
if (file_exists($viewsFile)) {
$views = json_decode(file_get_contents($viewsFile), true);
$arr = replace_key($views, 'page_' . $old_url, 'page_' . $pu);
save_json_pretty($viewsFile, $arr);
}
$sPage = find_subpage($pu);
$oldSub = 'content/static/' . pathinfo($oldfile, PATHINFO_FILENAME);
$newSub = 'content/static/' . pathinfo($newfile, PATHINFO_FILENAME);
if (!empty($sPage)) {
foreach ($sPage as $sp) {
if (file_exists($viewsFile)) {
$views = json_decode(file_get_contents($viewsFile), true);
$arr = replace_key($views, $oldSub . '/' . $sp->md, $newSub . '/' . $sp->md);
file_put_contents($viewsFile, json_encode($arr, JSON_UNESCAPED_UNICODE), LOCK_EX);
$arr = replace_key($views, 'subpage_' . $old_url . '.' . $sp->slug, 'subpage_' . $pu . '.' . $sp->slug);
save_json_pretty($viewsFile, $arr);
}
}
}
}
}
}
}
if ($destination == 'post') {
if(!empty($revertPage)) {
@ -1012,7 +1030,7 @@ function find_draft_page($static = null)
$post->body = MarkdownExtra::defaultTransform(remove_html_comments($content));
if (config('views.counter') == 'true') {
$post->views = get_views($post->file);
$post->views = get_views('page_' . $post->slug, $post->file);
} else {
$post->views = null;
}
@ -1082,7 +1100,7 @@ function find_draft_subpage($static = null, $sub_static = null)
$post->body = MarkdownExtra::defaultTransform(remove_html_comments($content));
if (config('views.counter') == 'true') {
$post->views = get_views($post->file);
$post->views = get_views('subpage_' . $post->parentSlug .'.'. $post->slug, $post->file);
} else {
$post->views = null;
}
@ -1518,61 +1536,16 @@ function reorder_pages($pages = null)
$num = str_pad($i, 2, 0, STR_PAD_LEFT);
$arr = explode('.' , $fn);
if (isset($arr[1])) {
$oldSub = find_subpage($arr[1]);
rename ($dir . $p, $dir . $num . '.' . $arr[1] . '.md');
if (file_exists($viewsFile)) {
$views = json_decode(file_get_contents($viewsFile), true);
$mod = replace_key($views, $dir . $p, $dir . $num . '.' . $arr[1] . '.md');
file_put_contents($viewsFile, json_encode($mod, JSON_UNESCAPED_UNICODE), LOCK_EX);
}
if (is_dir($dir . $fn)) {
rename($dir . $fn, $dir . $num . '.' . $arr[1]);
if (!empty($oldSub)) {
foreach ($oldSub as $sp) {
if (file_exists($viewsFile)) {
$views = json_decode(file_get_contents($viewsFile), true);
$mod = replace_key($views, $sp->file, $dir . $num . '.' . $arr[1] . '/' . $sp->md);
file_put_contents($viewsFile, json_encode($mod, JSON_UNESCAPED_UNICODE), LOCK_EX);
}
}
}
}
} else {
$oldSub = find_subpage($fn);
rename($dir . $p, $dir . $num . '.' . $fn . '.md');
if (file_exists($viewsFile)) {
$views = json_decode(file_get_contents($viewsFile), true);
$mod = replace_key($views, $dir . $p, $dir . $num . '.' . $fn . '.md');
file_put_contents($viewsFile, json_encode($mod, JSON_UNESCAPED_UNICODE), LOCK_EX);
}
if (is_dir($dir . $fn)) {
rename($dir . $fn, $dir . $num . '.' . $fn);
if (!empty($oldSub)) {
foreach ($oldSub as $sp) {
if (file_exists($viewsFile)) {
$views = json_decode(file_get_contents($viewsFile), true);
$mod = replace_key($views, $sp->file, $dir . $num . '.' . $fn . '/' . $sp->md);
file_put_contents($viewsFile, json_encode($mod, JSON_UNESCAPED_UNICODE), LOCK_EX);
}
}
}
rename($dir . $fn, $dir . $num . '.' . $fn);
}
}
$i++;
}
@ -1593,18 +1566,8 @@ function reorder_subpages($subpages = null)
$arr = explode('.' , $fn);
if (isset($arr[1])) {
rename ($dir . $sp, $dn . $num . '.' . $arr[1] . '.md');
if (file_exists($viewsFile)) {
$views = json_decode(file_get_contents($viewsFile), true);
$mod = replace_key($views, $dir . $sp, $dn . $num . '.' . $arr[1] . '.md');
file_put_contents($viewsFile, json_encode($mod, JSON_UNESCAPED_UNICODE), LOCK_EX);
}
} else {
rename($dir . $sp, $dn . $num . '.' . $fn . '.md');
if (file_exists($viewsFile)) {
$views = json_decode(file_get_contents($viewsFile), true);
$mod = replace_key($views, $dir . $sp, $dn . $num . '.' . $fn . '.md');
file_put_contents($viewsFile, json_encode($mod, JSON_UNESCAPED_UNICODE), LOCK_EX);
}
}
$i++;


+ 35
- 38
system/htmly.php View File

@ -2532,24 +2532,21 @@ get('/post/:name', function ($name) {
if (!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
} else {
add_view('post_' . $name);
if (!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
}
$post = find_post(null, null, $name);
if (is_null($post)) {
not_found();
not_found('post_' . $name);
} else {
$current = $post['current'];
}
if (config("views.counter") == "true") {
add_view($current->file);
if (!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
}
$author = new stdClass;
$author->url = $current->authorUrl;
$author->name = $current->authorName;
@ -3047,12 +3044,17 @@ get('/:static', function ($static) {
if (!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
} else {
add_view('page_' . $static);
if (!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
}
$post = find_page($static);
if (!$post) {
not_found();
not_found('page_' . $static);
}
if (array_key_exists('prev', $post)) {
@ -3068,13 +3070,6 @@ get('/:static', function ($static) {
}
$post = $post['current'];
if (config("views.counter") == "true") {
add_view($post->file);
if (!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
}
$vroot = rtrim(config('views.root'), '/');
@ -3335,13 +3330,28 @@ get('/:static/:sub', function ($static, $sub) {
$redir = site_url();
header("location: $redir", TRUE, 301);
}
if (config("views.counter") != "true") {
if (!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
} else {
add_view('subpage_' . $static.'.'.$sub);
if (!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
}
$parent_post = find_page($static);
if (!$parent_post) {
not_found();
not_found('subpage_' . $static.'.'.$sub);
}
$post = find_subpage($static, $sub);
if (!$post) {
not_found('subpage_' . $static.'.'.$sub);
}
if (array_key_exists('prev', $post)) {
$prev = $post['prev'];
} else {
@ -3354,18 +3364,7 @@ get('/:static/:sub', function ($static, $sub) {
$next = array();
}
if (!$post) {
not_found();
}
$post = $post['current'];
if (config("views.counter") == "true") {
add_view($post->file);
}
if (!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
$vroot = rtrim(config('views.root'), '/');
@ -3400,6 +3399,7 @@ get('/:static/:sub', function ($static, $sub) {
'breadcrumb' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; <a href="' . $parent_post['current']->url . '">' . $parent_post['current']->title . '</a> &#187; ' . $post->title,
'p' => $post,
'static' => $post,
'parent' => $parent_post,
'prev' => static_prev($prev),
'next' => static_next($next),
'type' => 'subPage',
@ -3582,24 +3582,21 @@ get('/:year/:month/:name', function ($year, $month, $name) {
if (!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
} else {
add_view('post_' . $name);
if (!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
}
$post = find_post($year, $month, $name);
if (is_null($post)) {
not_found();
not_found('post_'. $name);
} else {
$current = $post['current'];
}
if (config("views.counter") == "true") {
add_view($current->file);
if (!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
}
$author = new stdClass;
$author->url = $current->authorUrl;
$author->name = $current->authorName;


+ 11
- 1
system/includes/dispatch.php View File

@ -407,7 +407,8 @@ function render($view, $locals = null, $layout = null)
if (config('cache.timestamp') == 'true') {
echo "\n" . '<!-- Cached page generated on '.date('Y-m-d H:i:s').' -->';
}
file_put_contents($cachefile, ob_get_contents(), LOCK_EX);
if (isset($filecache))
file_put_contents($cachefile, ob_get_contents(), LOCK_EX);
}
echo trim(ob_get_clean());
} else {
@ -422,6 +423,15 @@ function json($obj, $code = 200)
exit;
}
function save_json_pretty($filename, $arr)
{
if (defined("JSON_PRETTY_PRINT")) {
file_put_contents($filename, json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), LOCK_EX);
} else {
file_put_contents($filename, json_encode($arr, JSON_UNESCAPED_UNICODE), LOCK_EX);
}
}
function condition()
{
static $cb_map = array();


+ 45
- 19
system/includes/functions.php View File

@ -525,7 +525,7 @@ function get_posts($posts, $page = 1, $perpage = 0)
}
if (config('views.counter') == 'true') {
$post->views = get_views($post->file);
$post->views = get_views('post_' . $post->slug, $post->file);
} else {
$post->views = null;
}
@ -584,7 +584,7 @@ function get_pages($pages, $page = 1, $perpage = 0)
$post->body = MarkdownExtra::defaultTransform(remove_html_comments($content));
if (config('views.counter') == 'true') {
$post->views = get_views($post->file);
$post->views = get_views('page_' . $post->slug, $post->file);
} else {
$post->views = null;
}
@ -652,9 +652,9 @@ function get_subpages($sub_pages, $page = 1, $perpage = 0)
// Get the contents and convert it to HTML
$post->body = MarkdownExtra::defaultTransform(remove_html_comments($content));
if (config('views.counter') == 'true') {
$post->views = get_views($post->file);
$post->views = get_views('subpage_' . $post->parentSlug .'.'. $post->slug, $post->file);
} else {
$post->views = null;
}
@ -1483,7 +1483,6 @@ function recent_type($type, $custom = null, $count = null)
$dir = 'cache/widget';
$filename = 'cache/widget/recent.' . $type . '.cache';
$tmp = array();
$posts = array();
if (!is_dir($dir)) {
mkdir($dir, 0775, true);
@ -1522,6 +1521,7 @@ function popular_posts($custom = null, $count = null)
{
static $_views = array();
$tmp = array();
$posts_list = get_blog_posts();
if (empty($count)) {
$count = config('popular.count');
@ -1539,16 +1539,19 @@ function popular_posts($custom = null, $count = null)
arsort($_views);
$i = 1;
foreach ($_views as $key => $val) {
if (file_exists($key)) {
if (stripos($key, '/blog/') !== false && stripos($key, '/scheduled/') == false && stripos($key, '/draft/') == false) {
$tmp[] = pathinfo($key);
if ($i++ >= $count)
break;
$arr = explode('post_', $key);
if (isset($arr[1])) {
foreach($posts_list as $in => $f) {
if (strpos($f['basename'], $arr[1]) !== false ) {
$tmp[] = $f;
if ($i++ >= $count)
break;
}
}
}
}
}
$dir = "cache/widget";
$filecache = "cache/widget/popular.cache";
$ar = array();
@ -1577,8 +1580,7 @@ function popular_posts($custom = null, $count = null)
echo '<li><a href="' . $post->url . '">' . $post->title . '</a></li>';
}
echo '</ul>';
}
else {
} else {
return $posts;
}
} else {
@ -2630,7 +2632,7 @@ EOF;
}
// The not found error
function not_found()
function not_found($request = null)
{
if (!config('views.root')) die('HTMLy is not installed!');
$vroot = rtrim(config('views.root'), '/');
@ -2640,6 +2642,18 @@ function not_found()
} else {
$layout = '';
}
if (!is_null($request)) {
$filename = "content/data/views.json";
$views = array();
if (file_exists($filename)) {
$views = json_decode(file_get_contents($filename), true);
}
if (isset($views[$request])) {
unset($views[$request]);
}
save_json_pretty($filename, $views);
}
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
render('404', array(
@ -3213,19 +3227,31 @@ function add_view($page)
} else {
$views[$page] = 1;
}
file_put_contents($filename, json_encode($views, JSON_UNESCAPED_UNICODE), LOCK_EX);
save_json_pretty($filename, $views);
}
// Get the page views count
function get_views($page)
function get_views($page, $oldID = null)
{
$_views = array();
$filename = "content/data/views.json";
if (file_exists($filename)) {
$_views = json_decode(file_get_contents($filename), true);
}
if (isset($_views[$page])) {
return $_views[$page];
if (!is_null($oldID)) {
if (isset($_views[$oldID])) {
$arr = replace_key($_views, $oldID, $page);
save_json_pretty($filename, $arr);
return $_views[$oldID];
} else {
if (isset($_views[$page])) {
return $_views[$page];
}
}
} else {
if (isset($_views[$page])) {
return $_views[$page];
}
}
return -1;
}


Loading…
Cancel
Save