日志

加入自动站外链接与标签(tag)系统

发布者:星野天河  发布时间:08-06-27  查看次数:8 评论数:0
标签:标签(8)   tag(1)   站外链(1)  

要加自动站外链接主要是考虑到音乐模块大多引用了朋友的作品,不加个链接过意不去,但也不想每次都手工加链接,所以做了个简单的自动链接系统。管理链接采用全AJAX方式,可以看看我以前写的演示

TAG系统在去年七月就做过,不过当时核心类都是老大写的,我只负责调用而己,不过还是对这玩意儿头痛不己,看起来简单出错率却很高。

这次在修改前台页面前先写好TAG类,把所有可能用到的功能都写出来,一个一个方法调试,未想投入使用时“健壮性”却令人欣喜,基本上没发生什么错误,标签的统计还是采用+1,-1的方式,还准备好了“刷新全部标签数据”,“修改无效标签”方法,不过没出什么问题所以没有用上。实在是爽啊,遥想当年(好像就一年)花十倍的时间也达不到这种稳定性。

既然逻辑代码简单,那便在前面表现层提高效果,于是把之前写的“仿thickbox插件"功能做点扩充,做成鼠标移动(或点击)到标签,自动在当前位置弹出使用此标签的相关内容窗口的形式,用起来该算舒服。

标签系统可以考虑的扩展方向

1. 自动站内链接:其实是自动标签链接,在保存正文时,扫描其中的TAG,自动添加标签链接。

2. 写个FCK插件——注释系统:对一些可能需要更深入解释的词句添加注释块,这些词句形如普通链接,但鼠标移动到会弹出注释框,这样即保持了主体内容简炼清晰,又可以对相关内容更进一步说明。当然这个系统需要新的表,但前面表现可借鉴标签部分。

最后老规矩,扔代码跑人

PHP代码
  1. <?php  
  2. class tag extends model   
  3. {  
  4.     protected $_table='xy_tagContent';  
  5.     private $tb = 'xy_tag';  
  6.       
  7.     public function __construct()  
  8.     {  
  9.         parent::__construct();  
  10.     }  
  11.     //新建内容时加入标签  
  12.     public function saveTag($tagStr$mid$cid)  
  13.     {  
  14.         $mid = intval($mid); $cid = intval($cid);  
  15.         if(!$mid || !$cidreturn false;  
  16.         if($this->getField("SELECT id FROM $this->_table WHERE mid = $mid AND cid = $cid")) return $this->updateTag($tagStr$mid$cid);  
  17.         $tags = $this->format($tagStr);  
  18.         return $this->addTags($tags$mid$cid);  
  19.     }  
  20.     //更新标签,分为两步。1。删除不再使用的标签($delArr) 2。添加新标签($addArr)  
  21.     public function updateTag($tagStr$mid$cid)  
  22.     {  
  23.         $mid = intval($mid); $cid = intval($cid);  
  24.         $newTags = $this->format($tagStr);  
  25.         $oldTags = $this->getAll("SELECT title FROM $this->_table JOIN $this->tb on xy_tag.id = tid WHERE mid = $mid AND cid = $cid");  
  26.         foreach ($oldTags as &$tag) {  
  27.             $tag = $tag['title'];  
  28.         }  
  29.         foreach ($oldTags as $item) {  
  30.             if(!in_array($item$newTags)) $delArr[] = $item;  
  31.         }  
  32.         foreach ($newTags as $item) {  
  33.             if(!in_array($item$oldTags)) $addArr[] = $item;  
  34.         }  
  35.         $this->delTags($delArr$mid$cid);  
  36.         $this->addTags($addArr$mid$cid);  
  37.     }  
  38.     //取标签字符串 $link = 是否添加链接  
  39.     public function getStr($mid=0, $cid=0, $link=true, $num=20)  
  40.     {  
  41.         $tags = $this->getTags($mid$cid$num);  
  42.         foreach ($tags as $tag) {  
  43.             if($link) {  
  44.                 $str .= '<a class="tag" href="'.ROOT.'?m=tag&id='.$tag['tid'].'">'.$tag['title'].'<span class="error">('.$tag['num'].')</span></a>   ';  
  45.             }else{  
  46.                 $str .= $tag['title'].' ';  
  47.             }  
  48.         }  
  49.         return trim($str);  
  50.     }  
  51.     //取使用某标签的内容的id,title,mid,num  
  52.     public function getContent($tag$mid=0)  
  53.     {  
  54.         if (is_numeric($tag)) $tid = $tag;  
  55.         else $tid = $this->getField("SELECT id FROM $this->tb WHERE title = '$tag'");  
  56.         if($mid$and = "AND mid = $mid";  
  57.         $sql = "SELECT mid,cid FROM $this->_table WHERE tid = $tid $and";  
  58.         $rs = $this->getAll($sql);  
  59.         $table = array('',PREV.'blog', PREV.'photo', PREV.'music');  
  60.         foreach ($rs as &$item) {  
  61.             $item['title'] = $this->getField("SELECT title FROM {$table[$item['mid']]} WHERE id = {$item['cid']}");  
  62.         }  
  63.         return $rs;  
  64.     }  
  65.     //取标签数组  
  66.     private function getTags($mid=0, $cid=0, $num=20)  
  67.     {  
  68.         $mid = intval($mid); $cid = intval($cid);  
  69.         if($mid && $cid$where = "WHERE mid = $mid AND cid = $cid";  
  70.         elseif ($mid$where = "WHERE mid = $mid";  
  71.           
  72.         $sql = "SELECT title, tid, num FROM $this->tb JOIN $this->_table ON $this->tb.id = tid $where GROUP BY tid ORDER BY num DESC LIMIT $num";  
  73.         return $this->getAll($sql);  
  74.     }  
  75.     //标签字符串切成数组,去掉空白和重复项  
  76.     private function format($tagStr)  
  77.     {  
  78.         $tagStr = str_replace(array(' '','',',' '), ' 'strip_tags($tagStr));  
  79.         $tags = explode(' '$tagStr);  
  80.         $temp = array();  
  81.         foreach ($tags as $tag){  
  82.             $tag = trim($tag);  
  83.             if(!emptyempty($tag) && !in_array($tag$temp)) $temp[] = $tag;  
  84.         }  
  85.         return $temp ? $temp : array();  
  86.     }  
  87.     //删除标签数组  
  88.     private function delTags($delArr$mid$cid)  
  89.     {  
  90.         if(!$delArrreturn ;  
  91.         foreach ($delArr as $tag) {  
  92.             $rs = $this->getOne("SELECT id,num FROM $this->tb WHERE title = '$tag'");  
  93.             $this->exec("DELETE FROM $this->_table WHERE mid = $mid AND cid = $cid AND tid = ".intval($rs['id']));  
  94.             if($rs['num'] <= 1) $this->exec("DELETE FROM $this->tb WHERE title = '$tag'");  
  95.             else $this->exec("UPDATE $this->tb SET num = num - 1 WHERE title = '$tag'");  
  96.         }  
  97.     }  
  98.     //添加标签数组  
  99.     private function addTags($addArr$mid$cid)  
  100.     {  
  101.         if(!$addArrreturn ;  
  102.         foreach ($addArr as $tag) {  
  103.             //添加标签表,顺便返回标签id  
  104.             if($tid = $this->getField("SELECT id FROM $this->tb WHERE title = '$tag'")) {  
  105.                 $this->exec("UPDATE $this->tb SET num = num + 1 WHERE id = $tid");  
  106.             }else{  
  107.                 $tid = $this->exec("INSERT INTO $this->tb SET title = '$tag', num = 1");  
  108.             }  
  109.             //添加关系表  
  110.             $this->exec("INSERT INTO $this->_table SET mid = $mid, cid = $cid, tid = $tid");  
  111.         }  
  112.     }  
  113.     /** 
  114.      * 重计tag使用数目.1.只刷新一个tag 2.只刷新一个内容的所有tag 
  115.      * @param $tag; tag名称或id 
  116.      */  
  117.     public function count($tag=''$mid=0, $cid=0)  
  118.     {  
  119.         $mid = intval($mid); $cid = intval($cid);  
  120.         //$idArr 是需要重计的tag id  
  121.         if($tag) {  
  122.             if (is_numeric($tag)) $tid = $tag;  
  123.             else $tid = $this->getField("SELECT id FROM $this->tb WHERE title = '$tag'");  
  124.             $idArr = array($tid);  
  125.         }else{  
  126.             if($cid && $mid) {  
  127.                 $rs = $this->getAll("SELECT id FROM $this->tb WHERE mid = $mid AND cid = $cid");  
  128.             }else{  
  129.                 $rs = $this->getAll("SELECT id FROM $this->tb");  
  130.             }  
  131.             $idArr = array();  
  132.             foreach ($rs as $item) {  
  133.                 $idArr[] = $item['id'];  
  134.             }  
  135.         }  
  136.         foreach ($idArr as $tid) {  
  137.             $num = $this->getField("SELECT COUNT(id) FROM $this->_table WHERE tid = $tid");  
  138.             if($num$this->exec("UPDATE $this->tb SET num = $num WHERE id = $tid");  
  139.             else $this->exec("DELETE FROM $this->tb WHERE id = $tid");  
  140.         }  
  141.     }  
  142. }  

评论列表

评论表单

妮称