1.代码懒得细看,先打上sql日志。
2.经过分析主要操作了posts,terms,term_taxonomy,term_relationships, postmeta,options这几个表,首先去掉postmeta,options这两个表的日志,主要记录后台手动编辑记录,用处不大,不需要处理。
3.posts是文章主表,terms是标签表(也包括菜单栏目等),term_taxonomy是标签详情表,term_relationships是文章,标签,栏目id关联表
4.更新逻辑是:
1)向文章主表posts插入文章,返回文章id
2)查询items表标签是否存在,存在返回标签id,不存在插入标签,返回标签id
3)向term_taxonomy插入分类,统计等记录
4)向term_relationships插入文章,栏目关联记录和文章标签关联记录
下面是代码,已经过自测
<?php
class db{private static $db;
public function __construct(){}
public static function getInstance(){
if(!self::$db){ self::$db = self::connectdb(); }return self::$db;
} private static function connectdb(){ $config = include COLLECTOR_DIR.'/conf/db.config.php'; $db = new mysqli($config['host'], $config['user'], $config['password'], $config['db']); !$db && die('Error,cannot connect database!'); $db->query("set names utf8"); return $db; }}
<?php
class postInterface{private $db;
private static $term_taxonomy_id;public function __construct(){
$this->db = db::getInstance(); self::$term_taxonomy_id = []; }public function getPost($post_title){
$sql = "SELECT ID FROM `pn_posts` WHERE `post_title`='{$post_title}' LIMIT 0, 1"; DEBUG && print($sql)."\n";$result = $this->db->query($sql);
$row = $result->fetch_assoc();
return $row['ID'];
} public function insertPost($post_title, $post_name, $post_content, $post_date){ $post_id =$this->getPost($post_title); if($post_id){ return $post_id; } $sql = "INSERT IGNORE INTO `pn_posts` (`post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_content_filtered`, `post_title`, `post_excerpt`, `post_status`, `post_type`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_parent`, `menu_order`, `post_mime_type`, `guid`) VALUES (1, '{$post_date}', '{$post_date}', '{$post_content}', '', '{$post_title}', '', 'publish', 'post', 'open', 'open', '', '{$post_name}', '', '', '{$post_date}', '{$post_date}', 0, 0, '', '');";DEBUG && print($sql)."\n";
$this->db->query($sql);
$post_id = $this->db->insert_id;
$sql = "UPDATE `pn_posts` SET `guid`='/?p={$this->db->insert_id}' WHERE `ID`='{$this->db->insert_id}' LIMIT 1";
DEBUG && print($sql."\n"); $this->db->query($sql); return $post_id; } public function getTagInfo($tag){ $sql = "SELECT term_taxonomy_id,term_id FROM `pn_term_taxonomy` WHERE term_id IN (SELECT term_id FROM `pn_terms` WHERE name = '{$tag}') AND taxonomy = 'post_tag' LIMIT 0, 1"; DEBUG && print($sql."\n"); $result = $this->db->query($sql); $row = $result->fetch_assoc(); return $row; }public function insertTag($post_id, $cateid, $tag, $slug){ $tag = trim($tag); $tagInfo = $this->getTagInfo($tag);
if(empty($tagInfo)){
$tagInfo = []; $sql = "INSERT INTO `pn_terms` (`name`, `slug`, `term_group`) VALUES ('{$tag}', '{$slug}', 0);"; DEBUG && print($sql."\n"); $this->db->query($sql);$tagInfo['term_id'] = $this->db->insert_id;
$sql="INSERT IGNORE INTO `pn_term_taxonomy` (`term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES ({$tagInfo['term_id']}, 'post_tag', '', 0, 0);"; DEBUG && print($sql."\n"); $this->db->query($sql);$tagInfo['term_taxonomy_id'] = $this->db->insert_id;
}
$sql = "INSERT IGNORE INTO `pn_term_relationships` (`object_id`, `term_taxonomy_id`) VALUES ({$post_id}, {$tagInfo['term_taxonomy_id']});";
DEBUG && print($sql."\n"); $this->db->query($sql);$term_taxonomy_id = $this->getTermTaxonomyId($cateid);
$sql = "INSERT IGNORE INTO `pn_term_relationships` (`object_id`, `term_taxonomy_id`) VALUES ({$post_id}, {$term_taxonomy_id});"; DEBUG && print($sql."\n"); $this->db->query($sql); $num = $this->getTagPostNum($tagInfo['term_id']); $sql = "UPDATE `pn_term_taxonomy` SET `count`={$num} WHERE `term_id` = {$tagInfo['term_id']}"; DEBUG && print($sql."\n"); $this->db->query($sql);$num = $this->getCategoryPostNum($cateid);
$sql ="UPDATE `pn_term_taxonomy` SET `count` = {$num} WHERE `term_id` = {$cateid};"; DEBUG && print($sql."\n"); $this->db->query($sql);}
public function getCategoryPostNum($cateid){
$term_taxonomy_id = $this->getTermTaxonomyId($cateid);$sql = "SELECT COUNT(*) AS num FROM `pn_term_relationships` WHERE `term_taxonomy_id` = {$term_taxonomy_id} AND `object_id` != {$cateid}";
DEBUG && print($sql."\n"); $result = $this->db->query($sql);$row = $result->fetch_assoc();
return (int)$row['num'];
}
public function getTagPostNum($tagid){
$term_taxonomy_id = $this->getTermTaxonomyId($tagid);$sql = "SELECT COUNT(*) AS num FROM `pn_term_relationships` WHERE `term_taxonomy_id` = {$term_taxonomy_id} AND `object_id` != {$tagid}";
DEBUG && print($sql."\n"); $result = $this->db->query($sql);$row = $result->fetch_assoc();
return (int)$row['num'];
}
public function getTermTaxonomyId($termid){
if(isset(self::$term_taxonomy_id[$termid])){ return self::$term_taxonomy_id[$termid]; }$sql = "SELECT `term_taxonomy_id` FROM `pn_term_taxonomy` WHERE `term_id` = {$termid} LIMIT 0,1";
DEBUG && print($sql."\n"); $result = $this->db->query($sql);$row = $result->fetch_assoc();
self::$term_taxonomy_id[$termid] = $row['term_taxonomy_id']; return $row['term_taxonomy_id']; } }/*
include 'db.class.php';$pi = new postInterface();
$cateid = 1;
$post_title = 'AAAAAABBBBB'; $post_content = 'AAAAAA,BBBBBB'; $post_tag1 = 'AA'; $post_tag2 = 'BB'; $post_date = date("Y-m-d H:i:s");$post_id = $pi->insertPost($post_title, $post_content, $post_date);
$pi->insertTag($post_id, $cateid, $post_tag1); $pi->insertTag($post_id, $cateid, $post_tag2); $post_title = 'AAAAAAABBBBBB'; $post_content = 'AAAAAAA,BBBBBBB'; $post_tag1 = 'AA'; $post_tag2 = 'BB'; $post_date = date("Y-m-d H:i:s");$post_id = $pi->insertPost($post_title, $post_content, $post_date);
$pi->insertTag($post_id, $cateid, $post_tag1); $pi->insertTag($post_id, $cateid, $post_tag2); */
注:原版的有bug,代码已更新。db类其实就是一个单列。