在处理比较多的数据的时候,我还是习惯用ThinkPHP轻量级的框架来做,现在有需求,系统是使用的Typecho搭建,现在要经常性的根据接口内容,来更新文章数据,因此便用 ThinkPHP 框架来实现。
文章源自陈学虎-https://chenxuehu.com/article/2021/12/7820.html
1、ThinkPHP 新建数据库模型,因为我们主要只是针对文章进行新增、更新操作,所以我们需要建立三个模型,Metas,Relationships,Contents :文章源自陈学虎-https://chenxuehu.com/article/2021/12/7820.html
- <?php
- namespace app\model;
- use think\Model;
- class Metas extends Model
- {
- protected $name = "typecho_metas";
- }
- <?php
- namespace app\model;
- use think\Model;
- class Relationships extends Model
- {
- protected $name = "typecho_relationships";
- }
- <?php
- namespace app\model;
- use think\Model;
- class Contents extends Model
- {
- protected $name = "typecho_contents";
- }
2、我们主要使用命令行来执行数据的更新,用定时任务执行,因此我们直接建立自定义命令即可,在app\command下建立,基本格式:文章源自陈学虎-https://chenxuehu.com/article/2021/12/7820.html
- <?php
- declare (strict_types = 1);
- namespace app\command;
- use think\console\Command;
- use think\console\Input;
- use think\console\input\Argument;
- use think\console\input\Option;
- use think\console\Output;
- use app\model\Contents;
- use app\model\Metas;
- use app\model\Relationships;
- use Overtrue\Pinyin\Pinyin;
- class Shopinsert extends Command
- {
- protected function configure()
- {
- // 指令配置
- $this->setName('shopinsert')
- ->setDescription('导入数据');
- }
- protected function execute(Input $input, Output $output)
- {
- $this->insertData($title,$text,$slug);
- $output->writeln("done ...");
- }
- private function insertData($title,$text,$slug)
- {
- $insertDataModel = new Contents();
- $insertDataModel->title = $title;
- $insertDataModel->slug = $this->checkPostSlug($slug);
- echo $this->checkPostSlug($slug);
- $insertDataModel->created = time();
- $insertDataModel->modified = time();
- $insertDataModel->text = $text;
- $insertDataModel->order=0;
- $insertDataModel->authorId=1;
- // $insertDataModel->template="";
- $insertDataModel->type="post";
- $insertDataModel->status="publish";
- // $insertDataModel->password="";
- // $insertDataModel->commentsNum="";
- $insertDataModel->allowComment=1;
- $insertDataModel->allowPing=1;
- $insertDataModel->allowFeed=1;
- $insertDataModel->parent=0;
- $insertDataModel->save();
- // 绑定分类,这里就直接指定了,因为没有其他类别的需求
- $metaName = "默认分类";
- $insertMetaRelation = new Relationships();
- $insertMetaRelation->cid = $insertDataModel->id;
- $insertMetaRelation->mid = $this->getCategoryIdByName($metaName);
- $insertMetaRelation->save();
- // 处理标签,没有就新增
- $tagName = [$title,$metaName];
- foreach($tagName as $tagUse)
- {
- $insertTagRelation = new Relationships();
- $insertTagRelation->cid = $insertDataModel->id;
- $insertTagRelation->mid = $this->getTagIdByName($tagUse);
- $insertTagRelation->save();
- }
- }
- //获取标签 ID
- protected function getTagIdByName($name)
- {
- $metainfo = Metas::where(["name"=>$name,"type"=>"tag"])->find();
- if($metainfo)
- {
- $metainfo->count += 1;
- $metainfo->save();
- return $metainfo->mid;
- }else{
- return $this->insertTag($name);
- }
- }
- // 检测文章 slug 是否存在,存在则增加表示
- protected function checkPostSlug($slug)
- {
- $insertDataModel = Contents::where(["slug"=>$slug])->count();
- if($insertDataModel >0)
- {
- return $slug.'-'.time().'-'.mt_rand(10000,99999);
- }else{
- return $slug;
- }
- }
- //新增标签
- protected function insertTag($name)
- {
- $metainfo = new Metas();
- $pinyin = new Pinyin();
- $metainfo->name = $name;
- $metainfo->slug = $pinyin->permalink($name);
- $metainfo->count = 1;
- $metainfo->type = "tag";
- $metainfo->description = $name;
- if($metainfo->save())
- {
- return $metainfo->id;
- }else{
- return 0;
- }
- }
- //获取分类 ID
- protected function getCategoryIdByName($name)
- {
- $metainfo = Metas::where(["name"=>$name,"type"=>"category"])->find();
- $metainfo->count += 1;
- $metainfo->save();
- return $metainfo->mid;
- }
- }
文章源自陈学虎-https://chenxuehu.com/article/2021/12/7820.html
3、在配置中增加命令,在 app\config\console.php中配置:文章源自陈学虎-https://chenxuehu.com/article/2021/12/7820.html
- <?php
- // +----------------------------------------------------------------------
- // | 控制台配置
- // +----------------------------------------------------------------------
- return [
- // 指令定义
- 'commands' => [
- 'shopinsert' => 'app\command\Shopinsert',
- ],
- ];
文章源自陈学虎-https://chenxuehu.com/article/2021/12/7820.html
到此,就完成了ThinkPHP操作Typecho数据库,实现内容同步更新了,需要注意的是,Typecho对标签和分类的数量有统计,因此在处理的时候,要特别注意,否则,通过后台操作更新等就会出错。文章源自陈学虎-https://chenxuehu.com/article/2021/12/7820.html
文章源自陈学虎-https://chenxuehu.com/article/2021/12/7820.html
评论