+ -
当前位置:首页 → 问答吧 → 晒晒程序 [原创]wordpress 快速入库的程序

晒晒程序 [原创]wordpress 快速入库的程序

时间:2008-05-01

来源:互联网

请高手多指教指教哦
来源:http://www.zeusion.com
复制PHP内容到剪贴板
PHP代码:

<?php
/**
* **************wordpress 2.5.0快速入库的程序*******************

* **********************主要功能*******************************
* 1.批量添加分类
* 2.批量添加标签
* 3.批量处理分类标签urlslug

* ********************后期功能预览*****************************
* 1.url 对搜索引擎友好话处理
* 2.文章内容 对搜索引擎友好话处理
* 3.增加自定义标签处理

* ***********************关于*********************************
* 作者  zeusion           [url=http://www.zeusion.com]www.zeusion.com[/url]
* 转载请注明出处      [url=http://www.zeusion.com]http://www.zeusion.com[/url]

* ***********************版本*********************************
* 2008.4.20  0.1   zeusion

*/
include_once('common.inc.php');
class Wordpress {
var  $siteurl = 'http://127.0.0.1';   //设置网站的域名
private $_db;
function __construct() {
  global $db;
  $this->_db = $db;
}
// 批量转换slug
private function name_slug(){
  
  $sql = "SELECT * FROM `wp_terms`";
  $rs = $this->_db->query($sql);
  while ($rw = $this->_db->fetch_array($rs)) {
   $name = urlencode($rw['name']);
   $sql2 = "update `wp_terms` set `slug` = '".$name."' where `term_id` = ".$rw['term_id'];
   $rs2 = $this->_db->query($sql2);
  }
}
/**
  * 批量添加分类
  *
  * @param array $name
  * @return array $re
  */
function add_cat($name){
  
  $re =array();
  if (is_array($name)) {
   $i = 0;
   foreach ($name as $v) {
    if (!$this->is_here($v)) {
     $sql = "INSERT INTO `wp_terms` (`term_id`, `name`, `slug`, `term_group`) VALUES (NULL, '{$v}', '".urlencode($v)."', 0);";
     $rs = $this->_db->query($sql);
     $num = $this->_db->insert_id();
     $re[$i] = $num;
     $sql1 = "INSERT INTO `wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES
  (NULL, {$num}, 'category', '', 0, 0)";
     $rs = $this->_db->query($sql1);
     $i++;
    }
   }
   return $re;
  }else {
   if (!$this->is_here($name)) {
    $sql = "INSERT INTO `wp_terms` (`term_id`, `name`, `slug`, `term_group`) VALUES (NULL, '{$name}', '".urlencode($name)."', 0);";
    $rs = $this->_db->query($sql);
    $num = $this->_db->insert_id();
    $sql1 = "INSERT INTO `wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES
  (NULL, {$num}, 'category', '', 0, 0)";
    $rs = $this->_db->query($sql1);
    return $num;
   }
  }
  return $num;
}
/**
  * 批量添加标签
  *
  * @param array $name
  * @return array $re
  */
function add_tags($name){
  
  $re =array();
  if (is_array($name)) {
   $i = 0;
   foreach ($name as $v) {
    if (!$this->is_here($v,'post_tag')) {
     $sql = "INSERT INTO `wp_terms` (`term_id`, `name`, `slug`, `term_group`) VALUES (NULL, '{$v}', '".urlencode($v)."', 0);";
     $rs = $this->_db->query($sql);
     $num = $this->_db->insert_id();
     $re[$i] = $num;
     $sql1 = "INSERT INTO `wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES
  (NULL, {$num}, 'post_tag', '', 0, 0)";
     $rs = $this->_db->query($sql1);
     $i++;
    }
   }
   return $re;
  }else {
   if (!$this->is_here($name,'post_tag')) {
    $sql = "INSERT INTO `wp_terms` (`term_id`, `name`, `slug`, `term_group`) VALUES (NULL, '{$name}', '".urlencode($name)."', 0);";
    $rs = $this->_db->query($sql);
    $num = $this->_db->insert_id();
    $sql1 = "INSERT INTO `wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES
  (NULL, {$num}, 'post_tag', '', 0, 0)";
    $rs = $this->_db->query($sql1);
    return $num;
   }
  }
}
/**
  * 验证一个标签 分类是否存在
  *
  * @param string $name
  * @param string $type
  * @return bool
  */
function is_here($name,$type = 'category'){
  
  $sql ="SELECT s.`term_id`  FROM `wp_terms` s, `wp_term_taxonomy` y
  WHERE s.`name` = '{$name}'
  AND y.`taxonomy` = '{$type}'
  AND s.`term_id` = y.`term_id`";
  $rs = $this->_db->query($sql);
  if ($num = $this->_db->affected_rows()) {
   return true;
  }else {
   return false;
  }
}
/**
  * 添加一个新文章
  *
  * 示例
  * $rw['title'] = 'title';
  * $rw['content'] = 'content';
  * $rw['tags'] = array('黑色','美丽');
  * $rw['category'] = array('黑色1','美丽2');
  *
  * @param unknown_type $row
  */
function add_post($row){
  
  $time = date("Y-m-d H:i:s",time());
  $sql = "INSERT INTO `wp_posts`
  (`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_category`, `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
  (NULL, 1, '{$time}', '{$time}',
  '{$row['content']}', '{$row['title']}', 0, '', 'publish', 'open', 'open', '',
  '".urlencode($row['title'])."', '', '', '{$time}', '{$time}',
  '', 0, '{$siteurl}/wp/?p=3', 0, 'post', '', 0)";
  $rs = $this->_db->query($sql);
  if ($rs) {
   $id = $this->_db->insert_id();
   $sql_u = "UPDATE `wp_posts` SET `guid` = '".$siteurl."/wp/?p=".$id."' WHERE `wp_posts`.`ID` ={$id} LIMIT 1 ;";
   $this->_db->query($sql_u);
   //建立分类关联
   $cate = $this->add_cat($row['category']);
   if (!empty($cate)) {
    $sql = "INSERT INTO `wp_term_relationships` (`object_id`, `term_taxonomy_id`) VALUES ";
    if (is_array($cate)) {
     foreach ($cate as $v) {
      $sql .="({$id}, $v),";
     }
    }else {
     $sql .= "({$id}, $cate),";
    }
   }
   //建立标签关联
   $tags = $this->add_tags($row['tags']);
   if (!empty($tags)) {
    if (is_array($tags)) {
     foreach ($tags as $s) {
      $sql .="({$id}, $s),";
     }
    }else {
     $sql .= "({$id}, $tags),";
    }
    $sql = substr($sql,0,-1).";";
    $rs = $this->_db->query($sql);
   }
   return true;
  }
}
}
$wp = new Wordpress();
$rw['title'] = 'title';
$rw['content'] = 'content';
$rw['tags'] = array('黑色','美丽');
$rw['category'] = array('黑色1','美丽2');
$wp->add_post($rw);
//$wp->name_slug();

[ 本帖最后由 zeusion 于 2008-5-1 09:33 编辑 ]

作者: zeusion   发布时间: 2008-05-01

请高手多帮忙哦
指点指点

作者: zeusion   发布时间: 2008-05-01

这个程序有啥用啊???

作者: panney   发布时间: 2008-05-01

看起来写得很乱

作者: qingqiang   发布时间: 2008-05-01

自己平时写给自己用的,
呵呵
是乱

快速入库啊
数据转换的时候用得到

作者: zeusion   发布时间: 2008-05-01

大家给点意见啊

作者: zeusion   发布时间: 2008-05-05

没有一点作用。。

作者: guanle   发布时间: 2008-05-07

作者: zeusion   发布时间: 2008-05-12

热门下载

更多