ImportDedecms.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2020/2/4
  6. * Time: 12:47
  7. */
  8. namespace app\common\command;
  9. use app\common\model\Article;
  10. use app\common\model\Catalog;
  11. use think\console\Command;
  12. use think\console\Input;
  13. use think\console\input\Argument;
  14. use think\console\Output;
  15. use think\Db;
  16. class ImportDedecms extends Command
  17. {
  18. /**
  19. * 导入dedecms数据
  20. * php think Dedecms
  21. */
  22. protected function configure()
  23. {
  24. $this->setName('ImportDedecms')
  25. ->addArgument('db_name', Argument::OPTIONAL, "db_name", "dedecms")
  26. ->addArgument('db_prefix', Argument::OPTIONAL, "db_prefix", "dede_");
  27. }
  28. protected function execute(Input $input, Output $output)
  29. {
  30. $db_name = $input->getArgument("db_name");
  31. $db_prefix = $input->getArgument("db_prefix");
  32. $dedecms = Db::connect([
  33. // 数据库类型
  34. 'type' => 'mysql',
  35. // 服务器地址
  36. 'hostname' => \think\Env::get("db_host", "localhost"),
  37. // 数据库名
  38. 'database' => $db_name,
  39. // 数据库用户名
  40. 'username' => \think\Env::get("db_username", "root"),
  41. // 数据库密码
  42. 'password' => \think\Env::get("db_password", ""),
  43. // 数据库连接端口
  44. 'hostport' => \think\Env::get("db_port", "3306"),
  45. ]);
  46. //导入栏目
  47. $arctype = $dedecms->query("select * from {$db_prefix}arctype");
  48. foreach ($arctype as $k => $v) {
  49. if (Catalog::get($v['id'])) {
  50. continue;
  51. }
  52. (new Catalog())->save([
  53. 'id' => $v['id'],
  54. 'pid' => $v['reid'],
  55. 'title' => $v['typename'],
  56. 'path' => str_replace('{cmspath}', '', appendEndDS($v['typedir'], '/')) . $v['defaultname'],
  57. 'sort' => $v['sortrank'],
  58. 'type' => $v['ispart'] == 0 ? Catalog::TYPE_ARTICLE_LIST : Catalog::TYPE_CATALOG,
  59. 'catalog_templet' => str_replace(['{style}/', '.htm'], '', $v['ispart'] == 0 ? $v['templist'] : $v['tempindex']),
  60. 'article_templet' => str_replace(['{style}/', '.htm'], '', $v['temparticle']),
  61. 'article_rule' => str_replace('{typedir}', '{cpath}', $v['namerule']),
  62. 'articlelist_rule' => str_replace('{typedir}', '{cpath}', $v['namerule2']),
  63. 'seo_title' => $v['seotitle'],
  64. 'seo_keyword' => $v['keywords'],
  65. 'seo_description' => $v['description'],
  66. ]);
  67. $output->writeln("import Catalog id:" . $v['id']);
  68. }
  69. //导入文章
  70. $archives = $dedecms->query("select * from {$db_prefix}archives where arcrank <> -2");
  71. foreach ($archives as $k => $v) {
  72. if (Article::get($v['id'])) {
  73. continue;
  74. }
  75. //文章内容
  76. $addonarticle = $dedecms->query("select * from {$db_prefix}addonarticle where aid=?", [$v['id']]);
  77. $content = $addonarticle[0]['body'];
  78. //文章标签
  79. $taglist = $dedecms->query("select * from {$db_prefix}taglist where aid=?", [$v['id']]);
  80. $tags = [];
  81. foreach ($taglist as $tag) {
  82. $tags[] = $tag['tag'];
  83. }
  84. (new Article())->save([
  85. 'id' => $v['id'],
  86. 'title' => $v['title'],
  87. 'seo_title' => $v['shorttitle'],
  88. 'seo_keyword' => $v['keywords'],
  89. 'seo_description' => $v['description'],
  90. 'filename' => $v['filename'],
  91. 'page_views' => $v['click'],
  92. 'catalog_id' => $v['typeid'],
  93. 'content' => $content,
  94. 'tag' => implode(',', $tags),
  95. 'status' => $v['arcrank'] == -1 ? Article::STATUS_CLOSE : Article::STATUS_OPEN,
  96. 'istop_time' => ifContain($v['flag'], 'c') ? time() : 0,
  97. 'create_time' => $v['senddate'],
  98. 'update_time' => $v['pubdate']
  99. ]);
  100. $output->writeln("import Article id:" . $v['id']);
  101. }
  102. }
  103. }