Fix critical bugs

Fix critical bugs.
This commit is contained in:
Danang Probo Sayekti 2014-01-07 07:07:56 +07:00
commit e666497cf0
2 changed files with 66 additions and 52 deletions

View file

@ -21,6 +21,9 @@ get('/index', function () {
$posts = get_posts($page); $posts = get_posts($page);
// Extract a specific page with results
$posts = array_slice($posts, ($page-1) * $perpage, $perpage);
$total = ''; $total = '';
if(empty($posts) || $page < 1){ if(empty($posts) || $page < 1){
@ -122,13 +125,16 @@ get('/archive/:req',function($req){
// The blog post page // The blog post page
get('/:year/:month/:name', function($year, $month, $name){ get('/:year/:month/:name', function($year, $month, $name){
$page = from($_GET, 'page');
$page = $page ? (int)$page : 1;
$perpage = 1;
$post = find_post($year, $month, $name); $post = find_post($year, $month, $name);
$current = $post['current']; // Extract a specific page with results
$post = array_slice($post, 0, $perpage);
if(!$current){ $current = $post[0];
not_found();
}
if (array_key_exists('prev', $post)) { if (array_key_exists('prev', $post)) {
$prev = $post['prev']; $prev = $post['prev'];

View file

@ -56,17 +56,14 @@ function get_author_names(){
return $_cache; return $_cache;
} }
function cmp($a, $b) {
return $a->date == $b->date ? 0 : ( $a->date < $b->date ) ? 1 : -1;
}
// Return blog post // Return blog post
function get_posts($page = 1, $perpage = 0){ function get_posts($page = 1, $perpage = 0){
if($perpage == 0){
$perpage = config('posts.perpage');
}
$posts = get_post_names(); $posts = get_post_names();
// Extract a specific page with results
$posts = array_slice($posts, ($page-1) * $perpage, $perpage);
$tmp = array(); $tmp = array();
@ -116,8 +113,8 @@ function get_posts($page = 1, $perpage = 0){
$tmp[] = $post; $tmp[] = $post;
} }
krsort($tmp); usort($tmp,'cmp');
return $tmp; return $tmp;
} }
@ -125,47 +122,59 @@ function get_posts($page = 1, $perpage = 0){
function find_post($year, $month, $name){ function find_post($year, $month, $name){
$posts = get_post_names(); $posts = get_post_names();
$tmp = array();
// Create a new instance of the markdown parser
$md = new MarkdownParser();
foreach($posts as $index => $v){ foreach($posts as $index => $v){
if( strpos($v, "$year-$month") !== false && strpos($v, $name.'.md') !== false){ if( strpos($v, "$year-$month") !== false && strpos($v, $name.'.md') !== false){
// Use the get_posts method to return $post = new stdClass;
// a properly parsed object
$ar = get_posts($index+1,1); // Extract the date
$nx = get_posts($index,1); $arr = explode('_', $v);
$pr = get_posts($index+2,1);
if ($index == 0) { // Replaced string
if(isset($pr[0])) { $replaced = substr($arr[0], 0,strrpos($arr[0], '/')) . '/';
return array(
'current'=> $ar[0], // Author string
'prev'=> $pr[0] $str = explode('/', $replaced);
); $author = $str[count($str)-3];
}
else { // The post author + author url
return array( $post->author = $author;
'current'=> $ar[0], $post->authorurl = site_url() . 'author/' . $author;
'prev'=> null
); // The post date
} $post->date = strtotime(str_replace($replaced,'',$arr[0]));
}
elseif (count($posts) == $index+1) { // The archive per day
return array( $post->archive = site_url(). 'archive/' . date('Y-m-d', $post->date) ;
'current'=> $ar[0],
'next'=> $nx[0] // The post URL
); $post->url = site_url().date('Y/m', $post->date).'/'.str_replace('.md','',$arr[2]);
}
else { // The post tag
return array( $post->tag = str_replace($replaced,'',$arr[1]);
'current'=> $ar[0],
'next'=> $nx[0], // The post tag url
'prev'=> $pr[0] $post->tagurl = site_url(). 'tag/' . $arr[1];
);
} // Get the contents and convert it to HTML
$content = $md->transformMarkdown(file_get_contents($v));
// Extract the title and body
$arr = explode('</h1>', $content);
$post->title = str_replace('<h1>','',$arr[0]);
$post->body = $arr[1];
$tmp[] = $post;
} }
} }
return false;
usort($tmp,'cmp');
return $tmp;
} }
// Return tag page // Return tag page
@ -227,7 +236,7 @@ function get_tag($tag){
} }
} }
krsort($tmp); usort($tmp,'cmp');
return $tmp; return $tmp;
} }
@ -283,7 +292,7 @@ function get_archive($req){
} }
} }
krsort($tmp); usort($tmp,'cmp');
return $tmp; return $tmp;
} }
@ -403,8 +412,7 @@ function get_spage($posts, $spage){
$tmp[] = $post; $tmp[] = $post;
} }
} }
krsort($tmp);
return $tmp; return $tmp;
} }
@ -484,7 +492,7 @@ function get_profile($profile){
} }
} }
krsort($tmp); usort($tmp,'cmp');
return $tmp; return $tmp;
} }
@ -602,7 +610,7 @@ function get_keyword($keyword){
} }
} }
krsort($tmp); usort($tmp,'cmp');
return $tmp; return $tmp;
} }