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

co_export.php 11KB

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