使用 ThinkPHP 框架来操作 Typecho 数据库

老虎说测试 脚本开发字数 7457阅读24分51秒阅读模式
摘要在处理比较多的数据的时候,我还是习惯用ThinkPHP轻量级的框架来做,现在有需求,系统是使用的Typecho搭建,现在要经常性的根据接口内容,来更新文章数据,因此便用 Think...

在处理比较多的数据的时候,我还是习惯用ThinkPHP轻量级的框架来做,现在有需求,系统是使用的Typecho搭建,现在要经常性的根据接口内容,来更新文章数据,因此便用 ThinkPHP 框架来实现。

使用 ThinkPHP 框架来操作 Typecho 数据库文章源自陈学虎-https://chenxuehu.com/article/2021/12/7820.html

1、ThinkPHP 新建数据库模型,因为我们主要只是针对文章进行新增、更新操作,所以我们需要建立三个模型,Metas,Relationships,Contents :文章源自陈学虎-https://chenxuehu.com/article/2021/12/7820.html

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
namespace app\model;
use think\Model;
class Metas extends Model
{
protected $name = "typecho_metas";
}
<?php namespace app\model; use think\Model; class Metas extends Model { protected $name = "typecho_metas"; }
  1. <?php
  2. namespace app\model;
  3.  
  4. use think\Model;
  5.  
  6. class Metas extends Model
  7. {
  8. protected $name = "typecho_metas";
  9. }
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
namespace app\model;
use think\Model;
class Relationships extends Model
{
protected $name = "typecho_relationships";
}
<?php namespace app\model; use think\Model; class Relationships extends Model { protected $name = "typecho_relationships"; }
  1. <?php
  2. namespace app\model;
  3.  
  4. use think\Model;
  5.  
  6. class Relationships extends Model
  7. {
  8. protected $name = "typecho_relationships";
  9. }
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
namespace app\model;
use think\Model;
class Contents extends Model
{
protected $name = "typecho_contents";
}
<?php namespace app\model; use think\Model; class Contents extends Model { protected $name = "typecho_contents"; }
  1. <?php
  2. namespace app\model;
  3.  
  4. use think\Model;
  5.  
  6. class Contents extends Model
  7. {
  8. protected $name = "typecho_contents";
  9. }

2、我们主要使用命令行来执行数据的更新,用定时任务执行,因此我们直接建立自定义命令即可,在app\command下建立,基本格式:文章源自陈学虎-https://chenxuehu.com/article/2021/12/7820.html

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?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;
}
}
<?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; } }
  1. <?php
  2. declare (strict_types = 1);
  3.  
  4. namespace app\command;
  5.  
  6. use think\console\Command;
  7. use think\console\Input;
  8. use think\console\input\Argument;
  9. use think\console\input\Option;
  10. use think\console\Output;
  11. use app\model\Contents;
  12. use app\model\Metas;
  13. use app\model\Relationships;
  14. use Overtrue\Pinyin\Pinyin;
  15.  
  16.  
  17.  
  18. class Shopinsert extends Command
  19. {
  20.  
  21.  
  22. protected function configure()
  23. {
  24. // 指令配置
  25. $this->setName('shopinsert')
  26. ->setDescription('导入数据');
  27. }
  28.  
  29. protected function execute(Input $input, Output $output)
  30. {
  31. $this->insertData($title,$text,$slug);
  32. $output->writeln("done ...");
  33. }
  34.  
  35. private function insertData($title,$text,$slug)
  36. {
  37. $insertDataModel = new Contents();
  38. $insertDataModel->title = $title;
  39. $insertDataModel->slug = $this->checkPostSlug($slug);
  40. echo $this->checkPostSlug($slug);
  41. $insertDataModel->created = time();
  42. $insertDataModel->modified = time();
  43. $insertDataModel->text = $text;
  44. $insertDataModel->order=0;
  45. $insertDataModel->authorId=1;
  46. // $insertDataModel->template="";
  47. $insertDataModel->type="post";
  48. $insertDataModel->status="publish";
  49. // $insertDataModel->password="";
  50. // $insertDataModel->commentsNum="";
  51. $insertDataModel->allowComment=1;
  52. $insertDataModel->allowPing=1;
  53. $insertDataModel->allowFeed=1;
  54. $insertDataModel->parent=0;
  55.  
  56. $insertDataModel->save();
  57.  
  58. // 绑定分类,这里就直接指定了,因为没有其他类别的需求
  59.  
  60. $metaName = "默认分类";
  61.  
  62. $insertMetaRelation = new Relationships();
  63. $insertMetaRelation->cid = $insertDataModel->id;
  64. $insertMetaRelation->mid = $this->getCategoryIdByName($metaName);
  65. $insertMetaRelation->save();
  66.  
  67. // 处理标签,没有就新增
  68. $tagName = [$title,$metaName];
  69.  
  70. foreach($tagName as $tagUse)
  71. {
  72. $insertTagRelation = new Relationships();
  73. $insertTagRelation->cid = $insertDataModel->id;
  74. $insertTagRelation->mid = $this->getTagIdByName($tagUse);
  75. $insertTagRelation->save();
  76. }
  77.  
  78. }
  79.  
  80. //获取标签 ID
  81. protected function getTagIdByName($name)
  82. {
  83. $metainfo = Metas::where(["name"=>$name,"type"=>"tag"])->find();
  84. if($metainfo)
  85. {
  86. $metainfo->count += 1;
  87. $metainfo->save();
  88. return $metainfo->mid;
  89. }else{
  90. return $this->insertTag($name);
  91. }
  92. }
  93.  
  94. // 检测文章 slug 是否存在,存在则增加表示
  95. protected function checkPostSlug($slug)
  96. {
  97. $insertDataModel = Contents::where(["slug"=>$slug])->count();
  98. if($insertDataModel >0)
  99. {
  100. return $slug.'-'.time().'-'.mt_rand(10000,99999);
  101. }else{
  102. return $slug;
  103. }
  104. }
  105.  
  106. //新增标签
  107. protected function insertTag($name)
  108. {
  109. $metainfo = new Metas();
  110. $pinyin = new Pinyin();
  111. $metainfo->name = $name;
  112. $metainfo->slug = $pinyin->permalink($name);
  113. $metainfo->count = 1;
  114. $metainfo->type = "tag";
  115. $metainfo->description = $name;
  116. if($metainfo->save())
  117. {
  118. return $metainfo->id;
  119. }else{
  120. return 0;
  121. }
  122. }
  123.  
  124. //获取分类 ID
  125. protected function getCategoryIdByName($name)
  126. {
  127. $metainfo = Metas::where(["name"=>$name,"type"=>"category"])->find();
  128. $metainfo->count += 1;
  129. $metainfo->save();
  130. return $metainfo->mid;
  131. }
  132.  
  133. }

 文章源自陈学虎-https://chenxuehu.com/article/2021/12/7820.html

此处为隐藏的内容
注册登录后,方可查看

3、在配置中增加命令,在 app\config\console.php中配置:文章源自陈学虎-https://chenxuehu.com/article/2021/12/7820.html

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
return [
// 指令定义
'commands' => [
'shopinsert' => 'app\command\Shopinsert',
],
];
<?php // +---------------------------------------------------------------------- // | 控制台配置 // +---------------------------------------------------------------------- return [ // 指令定义 'commands' => [ 'shopinsert' => 'app\command\Shopinsert', ], ];
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 控制台配置
  4. // +----------------------------------------------------------------------
  5. return [
  6. // 指令定义
  7. 'commands' => [
  8. 'shopinsert' => 'app\command\Shopinsert',
  9. ],
  10. ];

 文章源自陈学虎-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

 

 

 

 
  • 版权声明:本文为原创文章,转载请附上原文出处链接及本声明。
  • 转载请注明:使用 ThinkPHP 框架来操作 Typecho 数据库 | https://chenxuehu.com/article/2021/12/7820.html