国内流行的内容管理系统(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.

308 lines
11KB

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