Fix problems with offline writing

This commit is contained in:
danpros 2024-01-05 18:07:31 +07:00
commit b9b8a4e114
2 changed files with 54 additions and 42 deletions

View file

@ -1830,7 +1830,7 @@ get('/admin/categories/:category', function ($category) {
$desc = $desc[0]; $desc = $desc[0];
} }
if (empty($posts) || $page < 1) { if (empty($desc)) {
// a non-existing page // a non-existing page
not_found(); not_found();
} }

View file

@ -98,14 +98,11 @@ function get_draft_posts()
{ {
static $_draft = array(); static $_draft = array();
if (empty($_draft)) { if (empty($_draft)) {
$tmp = array(); $url = 'cache/index/index-draft.txt';
$tmp = glob('content/*/*/*/draft/*.md', GLOB_NOSORT); if (!file_exists($url)) {
if (is_array($tmp)) { rebuilt_cache('all');
foreach ($tmp as $file) {
$_draft[] = pathinfo($file);
} }
} $_draft = unserialize(file_get_contents($url));
usort($_draft, "sortfile_a");
} }
return $_draft; return $_draft;
} }
@ -198,7 +195,7 @@ function get_category_folder()
return $_dfolder; return $_dfolder;
} }
// Get category info files. // Get category slug.
function get_category_slug() function get_category_slug()
{ {
static $_cslug = array(); static $_cslug = array();
@ -254,8 +251,9 @@ function rebuilt_cache($type = null)
$page_cache = array(); $page_cache = array();
$subpage_cache = array(); $subpage_cache = array();
$author_cache = array(); $author_cache = array();
$scheduled = array(); $scheduled_cache = array();
$category_cache = array(); $category_cache = array();
$draft_cache = array();
if (is_dir($dir) === false) { if (is_dir($dir) === false) {
mkdir($dir, 0775, true); mkdir($dir, 0775, true);
@ -278,6 +276,52 @@ function rebuilt_cache($type = null)
$string_posts = serialize($posts_cache); $string_posts = serialize($posts_cache);
file_put_contents('cache/index/index-posts.txt', print_r($string_posts, true)); file_put_contents('cache/index/index-posts.txt', print_r($string_posts, true));
// Rebuilt scheduled posts index
$stmp = array();
$stmp = glob('content/*/*/*/*/scheduled/*.md', GLOB_NOSORT);
if (is_array($stmp)) {
foreach ($stmp as $file) {
$scheduled_cache[] = pathinfo($file);
$ss = explode('/', $file);
$ctmp[] = $ss[3];
}
}
usort($scheduled_cache, "sortfile_d");
$scheduled_string = serialize($scheduled_cache);
file_put_contents('cache/index/index-scheduled.txt', print_r($scheduled_string, true));
// Rebuilt draft posts index
$drf = array();
$drf = glob('content/*/*/*/draft/*.md', GLOB_NOSORT);
if (is_array($drf)) {
foreach ($drf as $file) {
$draft_cache[] = pathinfo($file);
$dd = explode('/', $file);
$ctmp[] = $dd[3];
}
}
usort($draft_cache, "sortfile_d");
$draft_string = serialize($draft_cache);
file_put_contents('cache/index/index-draft.txt', print_r($draft_string, true));
// Rebuilt category files index
$ftmp = array();
$ftmp = glob('content/data/category/*.md', GLOB_NOSORT);
if (is_array($ftmp)) {
foreach ($ftmp as $file) {
$category_cache[] = pathinfo($file);
$ctmp[] = pathinfo($file, PATHINFO_FILENAME);
}
}
usort($category_cache, "sortfile_a");
$category_string = serialize($category_cache);
file_put_contents('cache/index/index-category-files.txt', print_r($category_string, true));
// Rebuilt category slug index
$dirc = array();
$dirc = array_unique($ctmp, SORT_REGULAR);
file_put_contents('cache/index/index-category.txt', print_r(serialize($dirc), true));
// Rebuilt static page index // Rebuilt static page index
$ptmp = array(); $ptmp = array();
$ptmp = glob('content/static/*.md', GLOB_NOSORT); $ptmp = glob('content/static/*.md', GLOB_NOSORT);
@ -318,38 +362,6 @@ function rebuilt_cache($type = null)
$author_string = serialize($author_cache); $author_string = serialize($author_cache);
file_put_contents('cache/index/index-author.txt', print_r($author_string, true)); file_put_contents('cache/index/index-author.txt', print_r($author_string, true));
// Rebuilt category files index
$ftmp = array();
$cf = array();
$dirc = array();
$dirm = array();
$ftmp = glob('content/data/category/*.md', GLOB_NOSORT);
if (is_array($ftmp)) {
foreach ($ftmp as $file) {
$category_cache[] = pathinfo($file);
$cf[] = pathinfo($file, PATHINFO_FILENAME);
}
}
usort($category_cache, "sortfile_a");
$category_string = serialize($category_cache);
file_put_contents('cache/index/index-category-files.txt', print_r($category_string, true));
$dirc = array_unique($ctmp, SORT_REGULAR);
$dirm = array_unique(array_merge($dirc, $cf), SORT_REGULAR);
file_put_contents('cache/index/index-category.txt', print_r(serialize($dirm), true));
// Rebuilt scheduled posts index
$stmp = array();
$stmp = glob('content/*/*/*/*/scheduled/*.md', GLOB_NOSORT);
if (is_array($stmp)) {
foreach ($stmp as $file) {
$scheduled[] = pathinfo($file);
}
}
usort($scheduled, "sortfile_d");
$scheduled_string = serialize($scheduled);
file_put_contents('cache/index/index-scheduled.txt', print_r($scheduled_string, true));
// Remove the widget cache // Remove the widget cache
foreach (glob('cache/widget/*.cache', GLOB_NOSORT) as $file) { foreach (glob('cache/widget/*.cache', GLOB_NOSORT) as $file) {
unlink($file); unlink($file);