国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
11KB

  1. <?php
  2. /**
  3. * 导出采集规则
  4. *
  5. * @version $Id: co_edit_text.php 1 14:31 2010年7月12日Z tianya $
  6. * @package DedeBIZ.Administrator
  7. * @copyright Copyright (c) 2020, DedeBIZ.COM
  8. * @license https://www.dedebiz.com/license
  9. * @link https://www.dedebiz.com
  10. */
  11. require_once(dirname(__FILE__) . "/config.php");
  12. CheckPurview('co_Export');
  13. if (empty($dopost)) $dopost = '';
  14. if ($dopost != 'done') {
  15. require_once(DEDEADMIN . "/inc/inc_catalog_options.php");
  16. $totalcc = $channelid = $usemore = 0;
  17. if (!empty($nid)) {
  18. $mrow = $dsql->GetOne("SELECT COUNT(*) AS dd FROM `#@__co_htmls` WHERE nid='$nid' AND isdown='1' ");
  19. $totalcc = $mrow['dd'];
  20. $rrow = $dsql->GetOne("SELECT channelid,usemore FROM `#@__co_note` WHERE nid='$nid' ");
  21. $channelid = $rrow['channelid'];
  22. $usemore = $rrow['usemore'];
  23. } else {
  24. $mrow = $dsql->GetOne("SELECT COUNT(*) AS dd FROM `#@__co_htmls` WHERE isdown='1' ");
  25. $totalcc = $mrow['dd'];
  26. }
  27. include DedeInclude("templets/co_export.htm");
  28. exit();
  29. } else {
  30. require_once(DEDEINC . '/dedecollection.class.php');
  31. $channelid = isset($channelid) && is_numeric($channelid) ? $channelid : 0;
  32. $typeid = isset($typeid) && is_numeric($typeid) ? $typeid : 0;
  33. $pageno = isset($pageno) && is_numeric($pageno) ? $pageno : 1;
  34. $startid = isset($startid) && is_numeric($startid) ? $startid : 0;
  35. $endid = isset($endid) && is_numeric($endid) ? $endid : 0;
  36. if (!isset($makehtml)) $makehtml = 0;
  37. if (!isset($onlytitle)) $onlytitle = 0;
  38. if (!isset($usetitle)) $usetitle = 0;
  39. if (!isset($autotype)) $autotype = 0;
  40. $co = new DedeCollection();
  41. $co->LoadNote($nid);
  42. $orderway = (($co->noteInfos['cosort'] == 'desc' || $co->noteInfos['cosort'] == 'asc') ? $co->noteInfos['cosort'] : 'desc');
  43. if ($channelid == 0 && $typeid == 0) {
  44. ShowMsg('请指定默认导出栏目或频道ID!', 'javascript:;');
  45. exit();
  46. }
  47. if ($channelid == 0) {
  48. $row = $dsql->GetOne("SELECT ch.* FROM `#@__arctype` tp LEFT JOIN `#@__channeltype` ch ON ch.id=tp.channeltype WHERE tp.id='$typeid'; ");
  49. } else {
  50. $row = $dsql->GetOne("SELECT * FROM `#@__channeltype` WHERE id='$channelid'; ");
  51. }
  52. if (!is_array($row)) {
  53. echo "找不到频道内容模型信息,无法完成操作!";
  54. exit();
  55. }
  56. //分析规则,并生成临时的SQL语句
  57. $channelid = $row['id'];
  58. $maintable = $row['maintable'];
  59. $addtable = $row['addtable'];
  60. if (empty($maintable)) $maintable = '#@__archives';
  61. if (empty($addtable)) {
  62. echo "找不主表配置信息,无法完成操作!";
  63. exit();
  64. }
  65. $adminid = $cuserLogin->getUserID();
  66. //微索引表
  67. $indexSqlTemplate = "INSERT INTO `#@__arctiny`(`arcrank`,`typeid`,`channel`,`senddate`,`sortrank`) VALUES ('$arcrank','@typeid@' ,'$channelid','@senddate@', '@sortrank@'); ";
  68. //基本信息主表
  69. $mainSqlTemplate = "INSERT INTO `$maintable`(id,typeid,sortrank,flag,ismake,channel,arcrank,click,money,title,shorttitle,color,writer,source,litpic,pubdate,senddate,mid,description,keywords)
  70. VALUES ('@aid@','@typeid@','@sortrank@','@flag@','0','$channelid','$arcrank','0','0','@title@','','','@writer@','@source@','@litpic@','@pubdate@','@senddate@','$adminid','@description@','@keywords@'); ";
  71. //生成附加表插入的SQL语句
  72. $inadd_f = $inadd_v = '';
  73. $dtp = new DedeTagParse();
  74. $dtp->SetNameSpace('field', '<', '>');
  75. $dtp->LoadString($row['fieldset']);
  76. foreach ($dtp->CTags as $ctag) {
  77. $tname = $ctag->GetTagName();
  78. $inadd_f .= ",`$tname`";
  79. $notsend = $ctag->GetAtt('notsend');
  80. $fieldtype = $ctag->GetAtt('type');
  81. if ($notsend == 1) {
  82. //对不同类型设置默认值
  83. if ($ctag->GetAtt('default') != '') {
  84. $dfvalue = $ctag->GetAtt('default');
  85. } else if ($fieldtype == 'int' || $fieldtype == 'float' || $fieldtype == 'number') {
  86. $dfvalue = '0';
  87. } else if ($fieldtype == 'dtime') {
  88. $dfvalue = time();
  89. } else {
  90. $dfvalue = '';
  91. }
  92. $inadd_v .= ",'$dfvalue'";
  93. } else {
  94. $inadd_v .= ",'@$tname@'";
  95. }
  96. }
  97. $addSqlTemplate = "INSERT INTO `{$addtable}`(`aid`,`typeid`{$inadd_f}) Values('@aid@','@typeid@'{$inadd_v})";
  98. //导出数据的SQL操作
  99. $dtp = new DedeTagParse();
  100. $totalpage = $totalcc / $pagesize;
  101. $startdd = ($pageno - 1) * $pagesize;
  102. if (!empty($nid)) {
  103. $dsql->SetQuery("SELECT * FROM `#@__co_htmls` WHERE nid='$nid' AND isdown='1' ORDER BY aid $orderway LIMIT $startdd,$pagesize");
  104. } else {
  105. $dsql->SetQuery("SELECT * FROM `#@__co_htmls` WHERE isdown='1' ORDER BY aid $orderway LIMIT $startdd,$pagesize");
  106. }
  107. $dsql->Execute();
  108. while ($row = $dsql->GetObject()) {
  109. if (trim($row->result == '')) continue;
  110. //$addSqlTemplate,$mainSqlTemplate,$indexSqlTemplate
  111. $ntypeid = ($autotype == 1 && $row->typeid != 0) ? $row->typeid : $typeid;
  112. $indexSql = str_replace('@typeid@', $ntypeid, $indexSqlTemplate);
  113. $mainSql = str_replace('@typeid@', $ntypeid, $mainSqlTemplate);
  114. $addSql = str_replace('@typeid@', $ntypeid, $addSqlTemplate);
  115. $dtp->LoadString($row->result);
  116. $exid = $row->aid;
  117. if (!is_array($dtp->CTags)) continue;
  118. //获取时间和标题
  119. $pubdate = $sortrank = time();
  120. $title = $row->title;
  121. $litpic = '';
  122. foreach ($dtp->CTags as $ctag) {
  123. $itemName = $ctag->GetAtt('name');
  124. if ($itemName == 'title' && $usetitle == 0) {
  125. $title = trim($ctag->GetInnerText());
  126. if ($title == '') {
  127. $title = $row->title;
  128. }
  129. } else if ($itemName == 'pubdate') {
  130. $pubdate = trim($ctag->GetInnerText());
  131. if (preg_match("#[^0-9]#", $pubdate)) {
  132. $pubdate = $sortrank = GetMkTime($pubdate);
  133. } else {
  134. $pubdate = $sortrank = time();
  135. }
  136. } else if ($itemName == 'litpic') {
  137. $litpic = trim($ctag->GetInnerText());
  138. }
  139. }
  140. //检测重复标题
  141. $title = addslashes($title);
  142. if ($onlytitle) {
  143. $testrow = $dsql->GetOne("SELECT COUNT(ID) AS dd FROM `$maintable` WHERE title LIKE '$title'");
  144. if ($testrow['dd'] > 0) {
  145. echo "数据库已存在标题为: {$title} 的文档,程序阻止了此本条内容导入<br />\r\n";
  146. continue;
  147. }
  148. }
  149. //替换固定的项目
  150. $senddate = time();
  151. $flag = '';
  152. if ($litpic != '') $flag = 'p';
  153. //随机推荐
  154. if ($randcc > 0) {
  155. $rflag = mt_rand(1, $randcc);
  156. if ($rflag == $randcc) {
  157. $flag = ($flag == '' ? 'c' : $flag . ',c');
  158. }
  159. }
  160. $indexSql = str_replace('@senddate@', $senddate, $indexSql);
  161. $indexSql = str_replace('@sortrank@', $sortrank, $indexSql);
  162. $mainSql = str_replace('@flag@', $flag, $mainSql);
  163. $mainSql = str_replace('@sortrank@', $sortrank, $mainSql);
  164. $mainSql = str_replace('@pubdate@', $pubdate, $mainSql);
  165. $mainSql = str_replace('@senddate@', $senddate, $mainSql);
  166. $mainSql = str_replace('@title@', cn_substr($title, $cfg_title_maxlen), $mainSql);
  167. //$mainSql = str_replace('@title@', cn_substr($title, 60), $mainSql); 原来的语句,采集的文章导出到栏目后标题不全
  168. $addSql = str_replace('@sortrank@', $sortrank, $addSql);
  169. $addSql = str_replace('@senddate@', $senddate, $addSql);
  170. //替换模型里的其它字段
  171. foreach ($dtp->CTags as $ctag) {
  172. if ($ctag->GetName() != 'field') {
  173. continue;
  174. }
  175. $itemname = $ctag->GetAtt('name');
  176. $itemvalue = addslashes(trim($ctag->GetInnerText()));
  177. $mainSql = str_replace("@$itemname@", $itemvalue, $mainSql);
  178. $addSql = str_replace("@$itemname@", $itemvalue, $addSql);
  179. }
  180. //插入数据库
  181. $rs = $dsql->ExecuteNoneQuery($indexSql);
  182. if ($rs) {
  183. $aid = $dsql->GetLastID();
  184. $mainSql = str_replace('@aid@', $aid, $mainSql);
  185. $addSql = str_replace('@aid@', $aid, $addSql);
  186. $mainSql = preg_replace("#@([a-z0-9]{1,})@#", '', $mainSql);
  187. $addSql = preg_replace("#@([a-z0-9]{1,})@#", '', $addSql);
  188. $rs = $dsql->ExecuteNoneQuery($mainSql);
  189. if (!$rs) {
  190. echo "导入 '$title' 时错误:" . $dsql->GetError() . "<br />";
  191. $dsql->ExecuteNoneQuery("DELETE FROM `#@__arctiny` WHERE id='$aid' ");
  192. } else {
  193. $rs = $dsql->ExecuteNoneQuery($addSql);
  194. if (!$rs) {
  195. echo "导入 '$title' 时错误:" . $dsql->GetError() . "<br />";
  196. $dsql->ExecuteNoneQuery("DELETE FROM `#@__arctiny` WHERE id='$aid' ");
  197. $dsql->ExecuteNoneQuery("DELETE FROM `$maintable` WHERE id='$aid' ");
  198. }
  199. }
  200. }
  201. $dsql->ExecuteNoneQuery("UPDATE `#@__co_htmls` SET isexport=1 WHERE aid='$exid' ");
  202. }
  203. //检测是否完成或后续操作
  204. if ($totalpage <= $pageno) {
  205. if ($channelid > 0 && $makehtml == 1) {
  206. if ($autotype == 0 && !empty($nid)) {
  207. $mhtml = "makehtml_archives_action.php?typeid=$typeid&startid=$startid&endid=$endid&pagesize=20";
  208. ShowMsg("完成数据导入,准备生成文档HTML...", $mhtml);
  209. exit();
  210. } else {
  211. ShowMsg("完成所有数据导入,请手工更新HTML!", "javascript:;");
  212. exit();
  213. }
  214. } else {
  215. ShowMsg("完成所有数据导入!", "javascript:;");
  216. exit();
  217. }
  218. } else {
  219. if ($totalpage > 0) {
  220. $rs = substr(($pageno / $totalpage * 100), 0, 2);
  221. } else {
  222. $rs = 100;
  223. }
  224. $pageno++;
  225. $gourl = "co_export.php?dopost=done&nid=$nid&totalcc=$totalcc&channelid=$channelid&pageno=$pageno";
  226. $gourl .= "&nid=$nid&typeid=$typeid&autotype=$autotype&arcrank=$arcrank&pagesize=$pagesize&randcc=$randcc";
  227. $gourl .= "&startid=$startid&endid=$endid&onlytitle=$onlytitle&usetitle=$usetitle&makehtml=$makehtml";
  228. ShowMsg("完成 {$rs}% 导入,继续执行操作...", $gourl, '', 500);
  229. exit();
  230. }
  231. }