示例:typecho文章 

类型
发布插件 » CMS程序
标识
TypechoDemoSkycaiji
下载
877次
更新
2021-11-07
实现typecho文章入库功能,您可参考该代码开发更多typecho应用,使用时需在数据绑定»CMS路径结尾加上@typecho

推荐使用typecho远程发布接口插件

<?php
/**
 * 示例:typecho文章
 * 您可参考代码自行开发typecho的更多功能
 * 您可以使用 thinkphp5的函数
 */
namespace plugin\release\cms;
class TypechoDemoSkycaiji extends BaseCms{
	public $siteurl;//cms站点网址
	public function cms_db_typecho($cmsPath){
		$configFile=realpath($cmsPath.'/config.inc.php');
		$cmsDb=array();
		if(file_exists($configFile)){
			$configFile=file_get_contents($configFile);
			if($configFile){
				if(preg_match('/\s*new\s*Typecho_Db\s*\([^,\(\)]+,\s*[\'\"](?P<pre>[^\'\"]+)[\'\"]/i',$configFile,$prefix)){
					//匹配前缀
					$cmsDb['db_prefix']=$prefix['pre'];
				}
				if(preg_match('/\$db->addServer\s*\((?P<db>[\s\S]+?)\)\s*,/i',$configFile,$db)){
					//匹配数组
					$db=$db['db'];
					$dbKeys=array('db_host'=>'host','db_user'=>'user','db_pwd'=>'password','db_charset'=>'charset','db_port'=>'port','db_name'=>'database');
					foreach($dbKeys as $k=>$v){
						if(preg_match('/[\'\"]'.$v.'[\'\"]\s*=\s*>\s*[\'\"](?P<val>[^\'\"]+)[\'\"]/i',$db,$val)){
							$cmsDb[$k]=$val['val'];
						}
					}
				}
			}
		}
		return $cmsDb;
	}
	/*初始化扩展*/
	public function init_extend(){
		$siteUrl=$this->db()->table('__OPTIONS__')->where("name='siteUrl'")->find();
		$this->siteurl=rtrim($siteUrl['value'],'\/\\').'/';
	}
	//参数
	public $_params = array (
		'author' => array (
			'name' => '作者账号',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_author',
		),
		'category' => array (
			'name' => '分类',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_category',
		),
		'title' => array (
			'name' => '文章标题',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
		'content' => array (
			'name' => '文章内容',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
	);

	/*
	 * 导入数据
	* 必须以数组形式返回:
	* id(必填)表示入库返回的自增id或状态
	* target(可选)记录入库的数据位置(发布的网址等)
	* desc(可选)记录入库的数据位置附加信息
	* error(可选)记录入库失败的错误信息
	* 入库的信息可在“已采集数据”中查看
	* return array('id'=>0,'target'=>'','desc'=>'','error'=>'');
	*/
	public function runImport($params){
		$newPost=array(
	        'title'=>$params['title'],
	        'created'=>time(),
	        'modified'=>time(),
	        'text'=>$params['content'],
	        'order'=>0,
	        'authorId'=>$params['author'],
	        'type'=>'post',
	        'status'=>'publish',
	        'allowComment'=>1,
	        'allowPing'=>1,
	        'allowFeed'=>1,
	        'parent'=>0
		);
		
		$postId=$this->db()->table('__CONTENTS__')->insert($newPost,false,true);//添加文章并返回id
		if($postId>0){
			$this->db()->table('__CONTENTS__')->where('cid',$postId)->update(array('slug'=>$postId));
			$this->db()->table('__RELATIONSHIPS__')->insert(array('cid'=>$postId,'mid'=>$params['category']),true);
			
			$target=$this->siteurl.'/index.php/archives/'.$postId.'/';
			return array('id'=>$postId,'target'=>$target);
		}else{
			return array('id'=>0,'error'=>'文章入库失败');
		}
	}
	/*
	 * 参数选项:作者
	 * 必须返回键值对形式的数组
	 */
	public function param_option_author(){
		$usersDb=$this->db()->table('__USERS__')->where('group','administrator')->select();
		$userList=array();
		foreach ($usersDb as $user){
			$userList[$user['uid']]=$user['name'];
		}
		return $userList;
	}
	
	/*
	 * 参数选项:分类
	 * 必须返回键值对形式的数组
	 */
	public function param_option_category(){
		$catsDb=$this->db()->table('__METAS__')->select();//文章分类
		$catList=array();
		foreach ($catsDb as $cat){
			$catList[$cat['mid']]=$cat['name'];
		}
		return $catList;
	}
}
?>
非常抱歉!该插件已停止更新仅供示例参考,cms程序更新可能会导致插件功能失效,请自行修改代码以适应您的需求