From f70ce4e9506a4b60f431a182a3016a5ac3a0c382 Mon Sep 17 00:00:00 2001 From: Kanti Date: Tue, 29 Jul 2014 17:44:13 +0200 Subject: [PATCH] clean up the cache functions --- system/includes/functions.php | 2707 +++++++++++++++++++---------------------- 1 file changed, 1266 insertions(+), 1441 deletions(-) diff --git a/system/includes/functions.php b/system/includes/functions.php index 1ed6b1d..7595454 100644 --- a/system/includes/functions.php +++ b/system/includes/functions.php @@ -9,960 +9,840 @@ use \Suin\RSSWriter\Channel; use \Suin\RSSWriter\Item; // Get blog post path. Unsorted. Mostly used on widget. -function get_post_unsorted(){ - - 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 $_unsorted; +function get_post_unsorted() { + + static $_unsorted = array(); + + if (empty($_unsorted)) { + + $url = 'cache/index/index-unsorted.txt'; + if(! file_exists($url)) { + rebuilt_cache('all'); + } + $_unsorted = unserialize(file_get_contents($url)); + } + return $_unsorted; } // Get blog post with more info about the path. Sorted by filename. -function get_post_sorted(){ - - static $_sorted = array(); - - $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(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(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"); - } - } - - return $_sorted; +function get_post_sorted() { + + static $_sorted = array(); + + if (empty($_sorted)) { + $url = 'cache/index/index-sorted.txt'; + if(! file_exists($url)) { + rebuilt_cache('all'); + } + $_sorted = unserialize(file_get_contents($url)); + } + return $_sorted; } // Get static page path. Unsorted. -function get_static_pages(){ - - 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 $_page; +function get_static_pages() { + + static $_page = array(); + + if (empty($_page)) { + $url = 'cache/index/index-page.txt'; + if(! file_exists($url)) { + rebuilt_cache('all'); + } + $_page = unserialize(file_get_contents($url)); + } + return $_page; } // Get author bio path. Unsorted. -function get_author_names(){ - - static $_author = array(); - - 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 $_author; +function get_author_names() { + + static $_author = array(); + + if (empty($_author)) { + $url = 'cache/index/index-author.txt'; + if(! file_exists($url)) { + rebuilt_cache('all'); + } + $_author = unserialize(file_get_contents($url)); + } + + return $_author; } // Get backup file. -function get_zip_files(){ +function get_zip_files() { - static $_zip = array(); + static $_zip = array(); - if(empty($_zip)){ + if (empty($_zip)) { - // Get the names of all the - // zip files. + // Get the names of all the + // zip files. - $_zip = glob('backup/*.zip'); - } + $_zip = glob('backup/*.zip'); + } - return $_zip; + return $_zip; } // usort function. Sort by filename. function sortfile($a, $b) { - return $a['filename'] == $b['filename'] ? 0 : ( $a['filename'] < $b['filename'] ) ? 1 : -1; + return $a['filename'] == $b['filename'] ? 0 : ( $a['filename'] < $b['filename'] ) ? 1 : -1; } // usort function. Sort by date. 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)); - - } + $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') { + rebuilt_cache('posts'); + rebuilt_cache('page'); + rebuilt_cache('author'); + } } // Return blog posts. -function get_posts($posts, $page = 1, $perpage = 0){ - - if(empty($posts)) { - $posts = get_post_sorted(); - } - - $tmp = array(); - - // Extract a specific page with results - $posts = array_slice($posts, ($page-1) * $perpage, $perpage); - - foreach($posts as $index => $v){ - - $post = new stdClass; - - $filepath = $v['dirname'] . '/' . $v['basename']; - - // Extract the date - $arr = explode('_', $filepath); - - // Replaced string - $replaced = substr($arr[0], 0,strrpos($arr[0], '/')) . '/'; - - // Author string - $str = explode('/', $replaced); - $author = $str[count($str)-3]; - - // The post author + author url - $post->author = $author; - $post->authorurl = site_url() . 'author/' . $author; - - $dt = str_replace($replaced,'',$arr[0]); - $t = str_replace('-', '', $dt); - $time = new DateTime($t); - $timestamp= $time->format("Y-m-d H:i:s"); - - // The post date - $post->date = strtotime($timestamp); - - // The archive per day - $post->archive = site_url(). 'archive/' . date('Y-m', $post->date) ; - - // The post URL - $post->url = site_url().date('Y/m', $post->date).'/'.str_replace('.md','',$arr[2]); - - $tag = array(); - $url = array(); - $bc = array(); - - $t = explode(',', $arr[1]); - foreach($t as $tt) { - $tag[] = array($tt, site_url(). 'tag/' . $tt); - } - - foreach($tag as $a) { - $url[] = ''. $a[0] .''; - $bc[] = ''. $a[0] .''; - } - - $post->tag = implode(', ', $url); - - $post->tagb = implode(' » ', $bc); - - $post->file = $filepath; - - // Get the contents and convert it to HTML - $content = MarkdownExtra::defaultTransform(file_get_contents($filepath)); - - // Extract the title and body - $arr = explode('t-->', $content); - if(isset($arr[1])) { - $title = str_replace('', $content); + if (isset($arr[1])) { + $title = str_replace('', $content); - if(isset($arr[1])) { - $title = str_replace('', $content); + if (isset($arr[1])) { + $title = str_replace('', $content); - if(isset($arr[1])) { - $title = str_replace('', $content); + if (isset($arr[1])) { + $title = str_replace('', $content); - if(isset($arr[1])) { - $title = str_replace('', $content); + if (isset($arr[1])) { + $title = str_replace('