Skip to content
Permalink
Browse files

页面增加父页面功能(页面分层)

* 修改有些重定向导致查询参数丢失的问题
* 把“关于博客”改成了“博客程序”
* 友情链接加上了 nofollow
* 标题字体(h1->h6)在Windows上显示太难看,改成了黑体
  • Loading branch information...
movsb committed Sep 8, 2015
1 parent ba8d434 commit f0e8cf6df04ffc287e08313e3d57e4230eb7b270
Showing with 155 additions and 31 deletions.
  1. +2 −1 admin/canonical.php
  2. +117 −14 admin/db/posts.php
  3. +19 −0 admin/post.php
  4. +13 −13 admin/query.php
  5. +3 −3 theme/header.php
  6. +1 −0 theme/style.css
@@ -4,14 +4,15 @@ function the_link(&$p, $home=true) {
global $tbopt;
global $tbtax;
global $tbopt;
global $tbpost;
$home = $home ? $tbopt->get('home') : '';
$link = '';
if($p->type === 'post') {
$link = $home.'/'.$p->id.'/';
} else if($p->type === 'page') {
$link = $home.'/'.$p->slug;
$link = $home.$tbpost->get_the_parents_string($p->id).'/'.$p->slug;
} else {
$link = '/';
}
@@ -17,6 +17,7 @@ public function update(&$arg){
'content' => '',
'slug' => '',
'taxonomy' => 1,
'page_parents' => '',
];
$arg = tb_parse_args($def, $arg);
@@ -46,6 +47,25 @@ public function update(&$arg){
return false;
}
$type = $this->get_vars('type', 'id='.(int)$arg['id']);
if(!$type) return false;
if($type->type == 'page') {
$parents = $arg['page_parents'];
if($parents) {
$parents = explode(',', $parents);
$pid = $this->get_the_last_parents_id($parents);
if($pid === false) {
$this->error = '父页面不存在';
return false;
} else {
$arg['taxonomy'] = $pid;
}
} else {
$arg['taxonomy'] = 0;
}
}
$modified = &$arg['modified'];
if(!$modified) {
$modified = $tbdate->mysql_datetime_local();
@@ -153,6 +173,7 @@ public function insert(&$arg){
'comment_status' => 1,
'password' => '',
'tags' => '',
'page_parents' => '',
];
$arg = tb_parse_args($def, $arg);
@@ -178,6 +199,23 @@ public function insert(&$arg){
return false;
}
$type = $arg['type'];
if($type == 'page') {
$parents = $arg['page_parents'];
if($parents) {
$parents = explode(',', $parents);
$pid = $this->get_the_last_parents_id($parents);
if($pid === false) {
$this->error = '父页面不存在';
return false;
} else {
$arg['taxonomy'] = $pid;
}
} else {
$arg['taxonomy'] = 0;
}
}
if(!$arg['date']) {
$arg['date'] = $tbdate->mysql_datetime_local();
}
@@ -197,10 +235,6 @@ public function insert(&$arg){
$arg['date_gmt'] = $tbdate->mysql_local_to_gmt($arg['date']);
$arg['modified_gmt'] = $tbdate->mysql_local_to_gmt($arg['modified']);
// 页面无分类
if($arg['type'] == 'page')
$arg['taxonomy'] = 0;
$sql = "INSERT INTO posts (
date,modified,title,content,slug,type,taxonomy,status,comment_status,password)
VALUES (?,?,?,?,?,?,?,?,?,?)";
@@ -236,6 +270,7 @@ public function query(&$arg){
return false;
$defs = ['id' => '', 'tax' => '', 'slug' => '',
'parents' => '', 'page' => '',
'yy' => '', 'mm' => '',
'pageno' => '',
'password' => '', 'status' => '',
@@ -275,13 +310,11 @@ public function query(&$arg){
$tbquery->related_posts = $this->get_related_posts($queried_posts[0]->id);
}
} else if($arg['slug']) {
if($arg['tax']) {
$tbquery->type = 'post';
$queried_posts = $this->query_by_slug($arg);
} else {
$tbquery->type = 'page';
$queried_posts = $this->query_by_page($arg);
}
$tbquery->type = 'post';
$queried_posts = $this->query_by_slug($arg);
} else if($arg['page']) {
$tbquery->type = 'page';
$queried_posts = $this->query_by_page($arg);
} else if($arg['tax']) {
$tbquery->type = 'tax';
$arg['no_content'] = true;
@@ -441,9 +474,15 @@ private function query_by_slug($arg){
private function query_by_page($arg){
global $tbdb;
$slug = $arg['slug'];
$parents = $arg['parents'];
$parents = strlen($parents) ? explode('/', substr($parents, 1)) : [];
$pid = $this->get_the_last_parents_id($parents);
$sql = "SELECT * FROM posts WHERE type='page' AND slug='".$tbdb->real_escape_string($slug)."'";
if($pid === false) return false;
$slug = $arg['page'];
$sql = "SELECT * FROM posts WHERE type='page' AND taxonomy=$pid AND slug='".$tbdb->real_escape_string($slug)."'";
if($arg['modified']) {
$sql .= " AND modified>'".$arg['modified']."'";
}
@@ -636,6 +675,70 @@ public function the_next_id() {
$sql = "SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_name='posts' AND table_schema = DATABASE()";
return $tbdb->query($sql)->fetch_object()->AUTO_INCREMENT;
}
}
// 通过父页面树得到最后一个父页面的id(也就是当前待查询页面的id的父页面)
// 比如:uri -> /aaa/bbb/ccc/ddd
// 则 传入 ['aaa', 'bbb', 'ccc], 传出 ccc 的id
public function get_the_last_parents_id($parents) {
global $tbdb;
if(count($parents) <= 0) return 0;
$sql = "SELECT id FROM posts WHERE slug='".$tbdb->real_escape_string($parents[count($parents)-1])."'";
if(count($parents) == 1) {
$sql .= " AND taxonomy=0 LIMIT 1";
} else {
$sql .= " AND taxonomy IN (";
for($i=count($parents)-2; $i>0; --$i)
$sql .= "SELECT id FROM posts WHERE slug='".$tbdb->real_escape_string($parents[$i])."' AND taxonomy IN (";
$sql .= "SELECT id FROM posts WHERE slug='".$tbdb->real_escape_string($parents[0])."'";
for($i=count($parents)-1; $i > 0; --$i)
$sql .= ")";
}
$rows = $tbdb->query($sql);
if(!$rows || !$rows->num_rows) return false;
return $rows->fetch_object()->id;
}
// 得到父页面uri
// 比如:page -> ddd,其父为 aaa -> bbb -> ccc
// 则返回 /aaa/bbb/ccc,则最终的uri应为:/aaa/bbb/ccc/ddd
public function get_the_parents_string($id) {
global $tbdb;
$id = (int)$id;
$get_id = function ($id) use ($tbdb){
$id = (int)$id;
$sql = "SELECT type,taxonomy,slug FROM posts WHERE id=$id LIMIT 1";
$rows = $tbdb->query($sql);
if(!$rows || !$rows->num_rows) return false;
$o = $rows->fetch_object();
if($o->type != 'page') return false;
return $o;
};
$uri = [];
while($id) {
$t = $get_id($id);
if($t === false) return false;
$uri[] = $t->slug;
$id = $t->taxonomy;
}
// remove this
unset($uri[0]);
$uri = implode('/', array_reverse($uri));
return $uri ? '/'.$uri : '';
}
}
@@ -37,6 +37,25 @@ function post_widget_tax($p=null) {
add_hook('post_widget', 'post_widget_tax');
function post_widget_page_parents($p=null) {
global $tbpost;
if($p) {
$v = $tbpost->get_the_parents_string($p->id);
if($v) {
$v = substr($v, 1);
$v = implode(',', explode('/',$v));
}
}
$content = '<input type="text" name="page_parents" value="'.($p ? $v : '').'" />';
return [
'title' => '父页面',
'content' => $content,
];
}
add_hook('post_widget', 'post_widget_page_parents');
function post_widget_tag($p=null) {
$tag = $p ? join(',', $p->tag_names) : '';
@@ -96,17 +96,17 @@ public function query() {
return false;
$rules = [
'^/(\d+)(/)?$' => 'short=1&id=$1&slash=$2',
'^/archives/(\d+)\.html$' => 'id=$1',
'^/date/((\d{4})/((\d{2})/)?)?(page/(\d+))?$' => 'yy=$2&mm=$4&pageno=$6',
'^/(.+)/([^/]+)\.html$' => 'long=1&tax=$1&slug=$2',
'^/tags/(.+)$' => 'tags=$1',
'^/(feed|rss)(\.xml)?$' => 'feed=1',
'^/sitemap\.xml$' => 'sitemap=1',
'^/([0-9a-zA-Z\-_]+)$' => 'slug=$1',
'^/(.+)/(page/(\d+))?$' => 'tax=$1&pageno=$3',
'^/index\.php$' => '',
'^/$' => '',
'^/(\d+)(/)?$' => 'short=1&id=$1&slash=$2',
'^/archives/(\d+)\.html$' => 'id=$1',
'^/date/((\d{4})/((\d{2})/)?)?(page/(\d+))?$' => 'yy=$2&mm=$4&pageno=$6',
'^/(.+)/([^/]+)\.html$' => 'long=1&tax=$1&slug=$2',
'^/tags/(.+)$' => 'tags=$1',
'^/(feed|rss)(\.xml)?$' => 'feed=1',
'^/sitemap\.xml$' => 'sitemap=1',
'^((/[0-9a-zA-Z\-_]+)*)/([0-9a-zA-Z\-_]+)$' => 'parents=$1&page=$3',
'^/(.+)/(page/(\d+))?$' => 'tax=$1&pageno=$3',
'^/index\.php$' => '',
'^/$' => '',
];
foreach($rules as $rule => $rewrite){
@@ -150,7 +150,7 @@ public function query() {
// 把类似 "/1234" 重定向到 "/12324/"
if(isset($this->internal_query['short']) && !$this->internal_query['slash']) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: /'.$this->internal_query['id'].'/');
header('Location: /'.$this->internal_query['id'].'/?'.$_SERVER['QUERY_STRING']);
die(0);
}
@@ -193,7 +193,7 @@ public function query() {
if($need_redirect && $this->count) {
$link = the_link($this->objs[0]);
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$link);
header('Location: '.$link.'?'.$_SERVER['QUERY_STRING']);
die(0);
}
@@ -70,9 +70,9 @@
<div class="column about">
<h3>ABOUT</h3>
<ul>
<li><a href="/blog">关于博客</a></li>
<li><a href="/echo">建议反馈</a></li>
<li><a href="/about">关于我</a></li>
<li><a href="/blog">博客程序</a></li>
<li><a href="/echo">建议反馈</a></li>
<li><a target="_blank" href="<?php echo $tbopt->get('home'),'/rss'; ?>">订阅博客</a></li>
</ul>
</div>
@@ -88,7 +88,7 @@
$i = 0;
foreach($links as &$link) {
echo '<li><a title="', $link->title, '" ',
'target="_blank" href="', $link->href, '">',
'target="_blank" rel="nofollow" href="', $link->href, '">',
$link->name, '</a></li>', PHP_EOL;
if(++$i >= 5) break;
@@ -60,6 +60,7 @@ input, textarea, button {
h1,h2,h3,h4,h5,h6 {
margin: 1rem 0px;
font-weight: 400;
font-family: Consolas, 黑体;
}

h1 { font-size: 2.0em; }

0 comments on commit f0e8cf6

Please sign in to comment.
You can’t perform that action at this time.