wordpress后台mysql表结构分析

有很多万年不用的表我就不写了,主要分析常用的。

表名 描述
wp_commentmeta 评论的元数据
wp_comments 评论内容
wp_links 友情链接
wp_options 系统选项和插件、主题配置
wp_postmeta 文章(页面、上传文件、修改)的元数据
wp_posts 文章内容
wp_terms 目录、标签
wp_term_relationships 文章和对应分类的关系
wp_term_taxonomy 目录、标签所对应的分类
wp_usermeta 用户的元数据
wp_users 用户信息

这里面最常用到的就是这三个表

表名 描述
wp_posts 文章内容
wp_term_relationships 文章和对应分类的关系
wp_term_taxonomy 目录、标签所对应的分类

首先看wp_posts表,这个是最常用的,存储数据最多,也是操作最复杂的。
这里只展示新建和修改文章要用到的字段。

属性 描述
ID 主键
post_author 作者id
post_date 当前时间
post_date_gmt 当前格林威治时间
post_content 文章内容
post_title 文章标题
post_status 文章状态(私密,公开或密码保护)
comment_status 是否开放评论
post_password 文章密码(如果设置了密码保护)
post_name 文章版本
post_modified 修改时间
post_modified_gmt 修改时的格林威治时间
post_parent 父文章(原始版本id)
guid 文章链接
menu_order 排序id
post_type 文章类型(post文章/page页面等)
comment_count 评论数量

下面看wp_term_relationships表

属性 描述
object_id 文章id
term_taxonomy_id 分类方法id
term_order 排序id

再看wp_term_taxonomy表

属性 描述
term_taxonomy_id 分类方法id
term_id 类别id
taxonomy 分类方法
parent 父级类别id
count 所属分类文章数量

新建和修改文章的时候主要就是用到这些。具体怎么修改我就直接贴代码吧,懒得再分析一遍了,中间有很多坑,模拟了很久才成功。

代码肯定不能直接拿来用,还要仔细看一下,修改一些私人的东西。

第一次写php,并不是很规范,我只是满足功能需求,安全性什么的也没有考虑,毕竟一个博客也没有什么风险,我也有数据备份。

responseMsg();
        }
        // 异常访问,返回error
        else {
            echo 'error';
        }
    }
    // 接收消息
    public function responseMsg(){
        // 获取xml数据
        $postArr = file_get_contents("php://input");
        // 解析xml数据
        // file_put_contents(realpath(LOG_PATH).'/16_10_19.log', $postArr, FILE_APPEND);
        $postObj = simplexml_load_string( $postArr );
        $toUser = $postObj->FromUserName;
        $fromUser = $postObj->ToUserName;
        $type = $postObj->MsgType;
        $time = time();
        // 格式化返回消息类型
        $template = "
                     
                     
                     %s
                     
                     
                     ";
        $text = $postObj->Content;
        // 图片处理
        if ($type == "image"){
            $type = "text";
            $text = $postObj->PicUrl;
        }
        // 实例化业务逻辑
        $test = new wpMysql();
        // 执行业务后返回的结果
        $Content = $test->index($text);
        // 组装返回消息
        $info = sprintf($template, $toUser, $fromUser, $time, $type, $Content);
        echo $info;
    }
}
class wpMysql{
    // 初始化连接数据库
    public function __construct()
    {
        // 初始化连接参数
        $this->serverName = "****";
        $this->userName = "****";
        $this->passWord = "****";
        $this->dbName = "****";
        $this->conn = new mysqli($this->serverName, $this->userName, $this->passWord, $this->dbName);
        $this->conn->set_charset('utf8'); // 设置utf8字符集
        // 获取各种时间格式
        $this->today = date("D M d Y");//当前日期
        $this->timeNow = date("Y-m-d H:i:s");//当前时间,年月日,时分秒
        $this->gmTimeNow = gmdate("Y-m-d H:i:s");//当前格林威治时间
        $this->shortTimeNow = substr($this->timeNow, 0, 10);//当前,年月日
        $this->time = date("H:i:s");//当前时间,时分秒
        // 读取模式
        $this->sql = "select post_status from wp_status where id='1'";//我自己新建了一张表来存储当前私密和公开模式的信息
        $this->status = array_values($this->conn->query($this->sql)->fetch_assoc())[0];

    }
    // 判断操作类型
    public function index($text){
        if ($this->conn->connect_error){
            return $Content = "连接失败:" . $this->conn->connect_error;
        }
        //数据预处理,将单引号 ' 加上转义字符 \'
        $text = $this->prase($text);

        //模式切换
        if ($text=='pu'){
            $sql = "update wp_status set post_status='publish' where id='1'";
            if ($this->conn->query($sql) === TRUE){
                $this->conn->close();
                return "已切换至公开模式";
            }
            else{
                return $this->conn->error;
            }
            
        }else if($text == 'pr'){
            $sql = "update wp_status set post_status='private' where id='1'";
            if ($this->conn->query($sql) === TRUE){
                $this->conn->close();
                return "已切换至私密模式";
            }
            else{
                return $this->conn->error;
            }
        }

        // 业务逻辑
        $sql = "select * from wp_posts where post_title='$this->today' and post_status='$this->status'";
        $result = count($this->conn->query($sql)->fetch_assoc());
        if ($result != 0){
            // 删除操作
            if ($text == 'del')
                return $this->delete();
            // 删除所有
            else if ($text == 'delall')
                return $this->deleteAll();
            // 返回所有
            else if ($text == 'all')
                return $this->all();
            // 更新操作
            else 
                return $this->update($text);
        }
        // 新建操作
        else {
            return $this->publish($text);
        }

    }
    // 返回文章所有内容
    public function all(){
        $sql = "select post_content from wp_posts where post_title='$this->today' and post_status='$this->status'";
        $content = array_values($this->conn->query($sql)->fetch_assoc())[0];
        $this->conn->close();
        // $content = preg_replace("/<[^>]+>\n/", "                 [图片]", $content);
        // 超过1200字符,返回最后1200字符
        if (strlen($content) > 1200){
            $content = substr($content, strlen($content)-1200);
            $first = strpos($content, "\n");
            $content = substr($content, $first);
        }
        return $content;
    }
    // 删除上一条语句
    public function delete(){
        $sql = "select post_content from wp_posts where post_title='$this->today' and post_status='$this->status'";
        $content = array_values($this->conn->query($sql)->fetch_assoc())[0];
        $last = strrpos($content, "\n");
        $content = substr($content, 0, $last);
        $content = $this->prase($content);
        $sql = "update wp_posts set post_content='$content' where post_title='$this->today' and post_status='$this->status'";
        if ($this->conn->query($sql) === TRUE){
            return $this->all();
        }
        else{
            return $this->conn->error;
        }
    }
    // 删除所有
    public function deleteAll(){
        $sql = "update wp_posts set post_content='' where post_title='$this->today' and post_status='$this->status'";
        if ($this->conn->query($sql) === TRUE){
            $this->conn->close();
            return "删除成功";
        }
        else{
            return $this->conn->error;
        }
    }
    // 更新文章
    public function update($content){
        $msg = "更新成功";
        if ($content == '#'){
            $content = '\n\n---- ' . $this->time . ' ----';
            $msg = "换行成功";
            $sql = "update wp_posts set post_content=concat(post_content,'$content') where post_title='$this->today' and post_status='$this->status'";
        }
        // 图片操作
        else if (substr($content, 0, 4) == "http"){
            $name = time();
            $this->getImage($content, "****".$name.".jpg"); //这个路径我就不展示了
            $content ='\n';
            $content .= '![](*****'.$name.'.jpg)';
            $msg = "图片保存成功";
            $sql = "update wp_posts set post_content=concat(post_content,'$content') where post_title='$this->today' and post_status='$this->status'";
        }
        else{
            // 先判断当前文章是否为空
            $sql = "select post_content from wp_posts where post_title='$this->today' and post_status='$this->status'";
            $contentOld = array_values($this->conn->query($sql)->fetch_assoc())[0];
            if ($contentOld == "")
                $content = '-- ' . $this->time . ' --\n' . $content;
            else
                $content = '\n'.$content;
            $sql = "update wp_posts set post_content=concat(post_content,'$content') where post_title='$this->today' and post_status='$this->status'";
        }  
        if ($this->conn->query($sql) === TRUE){
            return $this->all();
        }
        else{
            return $this->conn->error;
        }
    }
    // 新建文章
    public function publish($text){
        // 取当前表中最后的ID值
        $sql = "select max(ID) from wp_posts";
        $result = array_values($this->conn->query($sql)->fetch_assoc())[0];
        $count = intval($result, 10) + 1;

        // 组装需要拼接的字段
        $content = '---- ' . $this->time . ' ----\n';
        $content .= $text;
        $title = $this->today;
        $status = $this->status;
        $msg = "新建成功";
        if (substr($text, 0, 1) == '*'){
            $content = '';
            $title = substr($text, 1);
            $status = "publish";
            $msg = "说说成功";
        }
        $url = 'http://****.cn/?p='.strval($count);
        $url1 = 'http://****.cn/archives/'.strval($count);
        $revision = strval($count).'-revision-v1';
        // post语句
        $sql_post = "insert into wp_posts(post_author, post_date, post_date_gmt, post_content, post_title, 
                        post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping,
                        pinged, post_modified, post_modified_gmt, post_content_filtered, post_parent, guid, 
                        menu_order, post_type, post_mime_type, comment_count) values 
                        (1, '$this->timeNow', '$this->gmTimeNow', '$content', '$title', '', '$status', 'open', 'open', '', '$this->shortTimeNow',
                        '', '', '$this->timeNow', '$this->gmTimeNow', '', 0, '$url', 0, 'post', '', 0);";
        // 复述post语句
        $sql_post .= "insert into wp_posts(post_author, post_date, post_date_gmt, post_content, post_title, 
                        post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping,
                        pinged, post_modified, post_modified_gmt, post_content_filtered, post_parent, guid, 
                        menu_order, post_type, post_mime_type, comment_count) values 
                        (1, '$this->timeNow', '$this->gmTimeNow', '$content', '$title', '', 'inherit', 'closed', 'closed', '', 
                        '$revision',
                        '', '', '$this->timeNow', '$this->gmTimeNow', '', '$count', '$url1', 0, 'revision', '', 0);";
        // 同时更新文章分类关系表
        $sql_post .= "insert into wp_term_relationships (object_id, term_taxonomy_id, term_order) values ('$count', 5, 0);";
        // 文章分类统计表数据自增
        $sql_post .= "update wp_term_taxonomy set count=count+1 where term_id=5";
        // 执行多条语句
        if ($this->conn->multi_query($sql_post) === TRUE){
            // $this->conn->close();
            // return $msg;
            return $this->all();
        }
        else{
            return $this->conn->error;
        }

    }
    // 下载图片并保存
    public function getImage($url, $filename='', $type=0){
        if($url==''){return false;}
        if($filename==''){
            $ext=strrchr($url,'.');
            if($ext!='.gif' && $ext!='.jpg'){return false;}
            $filename=time().$ext;
        }
        //文件保存路径
        if($type){
            $ch=curl_init();
            $timeout=5;
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
            $img=curl_exec($ch);
            curl_close($ch);
        }else{
            ob_start();
            readfile($url);
            $img=ob_get_contents();
            ob_end_clean();
        }
        $fp2=@fopen($filename,'a');
        fwrite($fp2,$img);
        fclose($fp2);
        return $filename;
    }

    //信息 预处理
    public function prase($text){
        return str_replace("'", "\'", $text);
    }
}

你可能感兴趣的:(wordpress后台mysql表结构分析)