Browse Source

Try to use cache index

Try to use cache index. Any glob results will saved as txt format.
pull/56/head
Danang Probo Sayekti 11 years ago
parent
commit
58d032419f
4 changed files with 207 additions and 60 deletions
  1. +17
    -7
      system/admin/admin.php
  2. +7
    -0
      system/admin/views/rebuilt-cache.html.php
  3. +23
    -7
      system/htmly.php
  4. +160
    -46
      system/includes/functions.php

+ 17
- 7
system/admin/admin.php View File

@ -45,9 +45,7 @@ function edit_post($title, $tag, $url, $content, $oldfile, $destination = null)
$oldurl = explode('_', $oldfile); $oldurl = explode('_', $oldfile);
$post_title = $title; $post_title = $title;
$post_tag = preg_replace('/[^A-Za-z0-9,.-]/u', '', $tag);
$post_tag = str_replace(' ', '-',$post_tag);
$post_tag = rtrim(ltrim($post_tag, ',\.\-'), ',\.\-');
$post_tag = preg_replace(array('/[^a-zA-Z0-9,.-]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($tag));
$post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url))); $post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url)));
$post_content = '<!--t ' . $post_title . ' t-->' . "\n\n" . $content; $post_content = '<!--t ' . $post_title . ' t-->' . "\n\n" . $content;
@ -76,6 +74,8 @@ function edit_post($title, $tag, $url, $content, $oldfile, $destination = null)
// The post URL // The post URL
$posturl = site_url().date('Y/m', $postdate).'/'.$post_url; $posturl = site_url().date('Y/m', $postdate).'/'.$post_url;
rebuilt_cache('all');
if ($destination == 'post') { if ($destination == 'post') {
header("Location: $posturl"); header("Location: $posturl");
} }
@ -112,6 +112,8 @@ function edit_page($title, $url, $content, $oldfile, $destination = null) {
$posturl = site_url() . $post_url; $posturl = site_url() . $post_url;
rebuilt_cache('all');
if ($destination == 'post') { if ($destination == 'post') {
header("Location: $posturl"); header("Location: $posturl");
} }
@ -129,8 +131,7 @@ function add_post($title, $tag, $url, $content, $user) {
$post_date = date('Y-m-d-H-i-s'); $post_date = date('Y-m-d-H-i-s');
$post_title = $title; $post_title = $title;
$post_tag = preg_replace('/[^A-Za-z0-9,.-]/u', '', $tag);
$post_tag = rtrim(ltrim($post_tag, ',\.\-'), ',\.\-');
$post_tag = preg_replace(array('/[^a-zA-Z0-9,.-]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($tag));
$post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url))); $post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url)));
$post_content = '<!--t ' . $post_title . ' t-->' . "\n\n" . $content; $post_content = '<!--t ' . $post_title . ' t-->' . "\n\n" . $content;
@ -147,6 +148,9 @@ function add_post($title, $tag, $url, $content, $user) {
mkdir($dir, 0777, true); mkdir($dir, 0777, true);
file_put_contents($dir . $filename, print_r($post_content, true)); file_put_contents($dir . $filename, print_r($post_content, true));
} }
rebuilt_cache('all');
$redirect = site_url() . 'admin/mine'; $redirect = site_url() . 'admin/mine';
header("Location: $redirect"); header("Location: $redirect");
} }
@ -173,6 +177,9 @@ function add_page($title, $url, $content) {
mkdir($dir, 0777, true); mkdir($dir, 0777, true);
file_put_contents($dir . $filename, print_r($post_content, true)); file_put_contents($dir . $filename, print_r($post_content, true));
} }
rebuilt_cache('all');
$redirect = site_url() . 'admin'; $redirect = site_url() . 'admin';
header("Location: $redirect"); header("Location: $redirect");
} }
@ -184,6 +191,7 @@ function delete_post($file, $destination) {
$deleted_content = $file; $deleted_content = $file;
if(!empty($deleted_content)) { if(!empty($deleted_content)) {
unlink($deleted_content); unlink($deleted_content);
rebuilt_cache('all');
if($destination == 'post') { if($destination == 'post') {
$redirect = site_url(); $redirect = site_url();
header("Location: $redirect"); header("Location: $redirect");
@ -200,6 +208,7 @@ function delete_page($file, $destination) {
$deleted_content = $file; $deleted_content = $file;
if(!empty($deleted_content)) { if(!empty($deleted_content)) {
unlink($deleted_content); unlink($deleted_content);
rebuilt_cache('all');
if($destination == 'post') { if($destination == 'post') {
$redirect = site_url(); $redirect = site_url();
header("Location: $redirect"); header("Location: $redirect");
@ -230,6 +239,7 @@ function edit_profile($title, $content, $user) {
mkdir($dir, 0777, true); mkdir($dir, 0777, true);
file_put_contents($filename, print_r($user_content, true)); file_put_contents($filename, print_r($user_content, true));
} }
rebuilt_cache('all');
$redirect = site_url() . 'author/' . $user; $redirect = site_url() . 'author/' . $user;
header("Location: $redirect"); header("Location: $redirect");
} }
@ -263,7 +273,7 @@ function migrate($title, $time, $tags, $content, $url, $user, $source) {
mkdir($dir, 0777, true); mkdir($dir, 0777, true);
file_put_contents($dir . $filename, print_r($post_content, true)); file_put_contents($dir . $filename, print_r($post_content, true));
} }
rebuilt_cache('all');
$redirect = site_url() . 'admin/mine'; $redirect = site_url() . 'admin/mine';
header("Location: $redirect"); header("Location: $redirect");
} }
@ -290,7 +300,7 @@ function get_feed($feed_url, $credit, $message=null) {
$time = new DateTime($entry->pubDate); $time = new DateTime($entry->pubDate);
$timestamp= $time->format("Y-m-d H:i:s"); $timestamp= $time->format("Y-m-d H:i:s");
$time = strtotime($timestamp); $time = strtotime($timestamp);
$tags = strip_tags(preg_replace('/[^A-Za-z0-9,.-]/u', '', $entry->category));
$tags = strip_tags(preg_replace(array('/[^a-zA-Z0-9,.-]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($entry->category)));
$title = rtrim($entry->title, ' \,\.\-'); $title = rtrim($entry->title, ' \,\.\-');
$title = ltrim($title, ' \,\.\-'); $title = ltrim($title, ' \,\.\-');
$user = $_SESSION['user']; $user = $_SESSION['user'];


+ 7
- 0
system/admin/views/rebuilt-cache.html.php View File

@ -0,0 +1,7 @@
<?php
rebuilt_cache('all');
echo 'All cache has been rebuilt!';
?>

+ 23
- 7
system/htmly.php View File

@ -145,10 +145,10 @@ get('/:year/:month/:name', function($year, $month, $name){
// Edit blog post // Edit blog post
get('/:year/:month/:name/edit', function($year, $month, $name){ get('/:year/:month/:name/edit', function($year, $month, $name){
$user = $_SESSION['user'];
$role = user('role', $user);
if(login()) { if(login()) {
$user = $_SESSION['user'];
$role = user('role', $user);
config('views.root', 'system/admin/views'); config('views.root', 'system/admin/views');
$post = find_post($year, $month, $name); $post = find_post($year, $month, $name);
@ -231,12 +231,11 @@ post('/:year/:month/:name/edit', function() {
// Delete blog post // Delete blog post
get('/:year/:month/:name/delete', function($year, $month, $name){ get('/:year/:month/:name/delete', function($year, $month, $name){
$user = $_SESSION['user'];
$role = user('role', $user);
if(login()) { if(login()) {
$user = $_SESSION['user'];
$role = user('role', $user);
config('views.root', 'system/admin/views'); config('views.root', 'system/admin/views');
$post = find_post($year, $month, $name); $post = find_post($year, $month, $name);
@ -896,6 +895,23 @@ get('/admin/backup-start',function(){
die; die;
}); });
// Create Zip file
get('/admin/rebuilt-cache',function(){
if(login()) {
config('views.root', 'system/admin/views');
render('rebuilt-cache', array(
'head_contents' => head_contents('Rebuilt cache started - ' . blog_title(), blog_description(), site_url()),
'bodyclass' => 'rebuiltcache',
'breadcrumb' => '<a href="' . site_url() . '">' .config('breadcrumb.home'). '</a> &#187; Rebuilt cache started'
));
}
else {
$login = site_url() . 'login';
header("location: $login");
}
die;
});
// The tag page // The tag page
get('/tag/:tag',function($tag){ get('/tag/:tag',function($tag){


+ 160
- 46
system/includes/functions.php View File

@ -11,90 +11,127 @@ use \Suin\RSSWriter\Item;
// Get blog post path. Unsorted. Mostly used on widget. // Get blog post path. Unsorted. Mostly used on widget.
function get_post_unsorted(){ function get_post_unsorted(){
static $_cache = array();
if(empty($_cache)){
// Get the names of all the posts
$_cache = glob('content/*/blog/*.md', GLOB_NOSORT);
static $_unsorted = array();
if(empty($_unsorted)){
$url = 'cache/index/index-unsorted.txt';
if (file_exists($url)) {
$_unsorted = unserialize(file_get_contents($url));
}
else {
rebuilt_cache('all');
$_unsorted = unserialize(file_get_contents($url));
}
if(empty($_unsorted)){
$_unsorted = glob('content/*/blog/*.md', GLOB_NOSORT);
}
} }
return $_cache;
return $_unsorted;
} }
// Get blog post with more info about the path. Sorted by filename. // Get blog post with more info about the path. Sorted by filename.
function get_post_sorted(){ function get_post_sorted(){
static $tmp= array();
static $_sorted = array();
static $_cache = array();
if(empty($_cache)){
// Get the names of all the posts
$url = 'cache/index/index-sorted.txt';
if (file_exists($url)) {
$_sorted = unserialize(file_get_contents($url));
}
else {
rebuilt_cache('all');
$_sorted = unserialize(file_get_contents($url));
}
$tmp = glob('content/*/blog/*.md', GLOB_NOSORT);
if(empty($_sorted)){
$url = 'cache/index/index-sorted.txt';
if (file_exists($url)) {
$_sorted = unserialize(file_get_contents($url));
}
else {
rebuilt_cache('all');
$_sorted = unserialize(file_get_contents($url));
}
if (is_array($tmp)) {
foreach($tmp as $file) {
$_cache[] = pathinfo($file);
if(empty($_sorted)){
$tmp = array();
$tmp = glob('content/*/blog/*.md', GLOB_NOSORT);
if (is_array($tmp)) {
foreach($tmp as $file) {
$_sorted[] = pathinfo($file);
}
} }
usort($_sorted, "sortfile");
} }
} }
usort($_cache, "sortfile");
return $_cache;
return $_sorted;
} }
// Get static page path. Unsorted. // Get static page path. Unsorted.
function get_static_pages(){ function get_static_pages(){
static $_cache = array();
if(empty($_cache)){
// Get the names of all the
// static page.
$_cache = glob('content/static/*.md', GLOB_NOSORT);
static $_page = array();
if(empty($_page)){
$url = 'cache/index/index-page.txt';
if (file_exists($url)) {
$_page = unserialize(file_get_contents($url));
}
else {
rebuilt_cache('all');
$_page = unserialize(file_get_contents($url));
}
if(empty($_page)){
$_page = glob('content/static/*.md', GLOB_NOSORT);
}
} }
return $_cache;
return $_page;
} }
// Get author bio path. Unsorted. // Get author bio path. Unsorted.
function get_author_names(){ function get_author_names(){
static $_cache = array();
if(empty($_cache)){
// Get the names of all the
// author.
static $_author = array();
$_cache = glob('content/*/author.md', GLOB_NOSORT);
if(empty($_author)){
$url = 'cache/index/index-author.txt';
if (file_exists($url)) {
$_author = unserialize(file_get_contents($url));
}
else {
rebuilt_cache('all');
$_author = unserialize(file_get_contents($url));
}
if(empty($_author)){
$_author = glob('content/*/author.md', GLOB_NOSORT);
}
} }
return $_cache;
return $_author;
} }
// Get backup file. // Get backup file.
function get_zip_files(){ function get_zip_files(){
static $_cache = array();
static $_zip = array();
if(empty($_cache)){
if(empty($_zip)){
// Get the names of all the // Get the names of all the
// zip files. // zip files.
$_cache = glob('backup/*.zip');
$_zip = glob('backup/*.zip');
} }
return $_cache;
return $_zip;
} }
// usort function. Sort by filename. // usort function. Sort by filename.
@ -107,6 +144,82 @@ function sortdate($a, $b) {
return $a->date == $b->date ? 0 : ( $a->date < $b->date ) ? 1 : -1; return $a->date == $b->date ? 0 : ( $a->date < $b->date ) ? 1 : -1;
} }
// Rebuilt cache index
function rebuilt_cache($type) {
$dir = 'cache/index';
$posts_cache_sorted = array();
$posts_cache_unsorted = array();
$page_cache = array();
$author_cache = array();
if(is_dir($dir) === false) {
mkdir($dir, 0777, true);
}
if($type === 'posts') {
$posts_cache_unsorted = glob('content/*/blog/*.md', GLOB_NOSORT);
$string = serialize($posts_cache_unsorted);
file_put_contents('cache/index/index-unsorted.txt', print_r($string, true));
$tmp= array();
$tmp = glob('content/*/blog/*.md', GLOB_NOSORT);
if (is_array($tmp)) {
foreach($tmp as $file) {
$posts_cache_sorted[] = pathinfo($file);
}
}
usort($posts_cache_sorted, "sortfile");
$string = serialize($posts_cache_sorted);
file_put_contents('cache/index/index-sorted.txt', print_r($string, true));
}
elseif ($type === 'page') {
$page_cache = glob('content/static/*.md', GLOB_NOSORT);
$string = serialize($page_cache);
file_put_contents('cache/index/index-page.txt', print_r($string, true));
}
elseif ($type === 'author') {
$author_cache = glob('content/*/author.md', GLOB_NOSORT);
$string = serialize($author_cache);
file_put_contents('cache/index/index-author.txt', print_r($string, true));
}
elseif ($type === 'all') {
$posts_cache_unsorted = glob('content/*/blog/*.md', GLOB_NOSORT);
$string = serialize($posts_cache_unsorted);
file_put_contents('cache/index/index-unsorted.txt', print_r($string, true));
$tmp= array();
$tmp = glob('content/*/blog/*.md', GLOB_NOSORT);
if (is_array($tmp)) {
foreach($tmp as $file) {
$posts_cache_sorted[] = pathinfo($file);
}
}
usort($posts_cache_sorted, "sortfile");
$string = serialize($posts_cache_sorted);
file_put_contents('cache/index/index-sorted.txt', print_r($string, true));
$page_cache = glob('content/static/*.md', GLOB_NOSORT);
$string = serialize($page_cache);
file_put_contents('cache/index/index-page.txt', print_r($string, true));
$author_cache = glob('content/*/author.md', GLOB_NOSORT);
$string = serialize($author_cache);
file_put_contents('cache/index/index-author.txt', print_r($string, true));
}
}
// Return blog posts. // Return blog posts.
function get_posts($posts, $page = 1, $perpage = 0){ function get_posts($posts, $page = 1, $perpage = 0){
@ -1161,7 +1274,7 @@ function generate_rss($posts){
echo $feed; echo $feed;
} }
// Return post, archive url.
// Return post, archive url for sitemap
function get_path(){ function get_path(){
$posts = get_post_sorted(); $posts = get_post_sorted();
@ -1212,7 +1325,7 @@ function get_path(){
return $tmp; return $tmp;
} }
// Return static page path.
// Return static page path for sitemap
function get_static_path(){ function get_static_path(){
$posts = get_static_pages(); $posts = get_static_pages();
@ -1572,6 +1685,7 @@ EOF;
echo '<li><a href="'.$base.'edit/profile">Edit profile</a></li>'; echo '<li><a href="'.$base.'edit/profile">Edit profile</a></li>';
echo '<li><a href="'.$base.'admin/import">Import</a></li>'; echo '<li><a href="'.$base.'admin/import">Import</a></li>';
echo '<li><a href="'.$base.'admin/backup">Backup</a></li>'; echo '<li><a href="'.$base.'admin/backup">Backup</a></li>';
echo '<li><a href="'.$base.'admin/rebuilt-cache">Rebuilt cache</a></li>';
echo '<li><a href="'.$base.'logout">Logout</a></li>'; echo '<li><a href="'.$base.'logout">Logout</a></li>';
echo '</ul></div>'; echo '</ul></div>';

Loading…
Cancel
Save