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

306 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 - 2019, DesDev, Inc.
  8. * @license http://help.dedecms.com/usersguide/license.html
  9. * @link http://www.dedecms.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, 60), $mainSql);
  208. $addSql = str_replace('@sortrank@', $sortrank, $addSql);
  209. $addSql = str_replace('@senddate@', $senddate, $addSql);
  210. //替换模型里的其它字段
  211. foreach($dtp->CTags as $ctag)
  212. {
  213. if($ctag->GetName()!='field')
  214. {
  215. continue;
  216. }
  217. $itemname = $ctag->GetAtt('name');
  218. $itemvalue = addslashes(trim($ctag->GetInnerText()));
  219. $mainSql = str_replace("@$itemname@", $itemvalue, $mainSql);
  220. $addSql = str_replace("@$itemname@", $itemvalue, $addSql);
  221. }
  222. //插入数据库
  223. $rs = $dsql->ExecuteNoneQuery($indexSql);
  224. if($rs)
  225. {
  226. $aid = $dsql->GetLastID();
  227. $mainSql = str_replace('@aid@', $aid, $mainSql);
  228. $addSql = str_replace('@aid@', $aid, $addSql);
  229. $mainSql = preg_replace("#@([a-z0-9]{1,})@#", '', $mainSql);
  230. $addSql = preg_replace("#@([a-z0-9]{1,})@#", '', $addSql);
  231. $rs = $dsql->ExecuteNoneQuery($mainSql);
  232. if(!$rs)
  233. {
  234. echo "导入 '$title' 时错误:".$dsql->GetError()."<br />";
  235. $dsql->ExecuteNoneQuery("DELETE FROM `#@__arctiny` WHERE id='$aid' ");
  236. }
  237. else
  238. {
  239. $rs = $dsql->ExecuteNoneQuery($addSql);
  240. if(!$rs)
  241. {
  242. echo "导入 '$title' 时错误:".$dsql->GetError()."<br />";
  243. $dsql->ExecuteNoneQuery("DELETE FROM `#@__arctiny` WHERE id='$aid' ");
  244. $dsql->ExecuteNoneQuery("DELETE FROM `$maintable` WHERE id='$aid' ");
  245. }
  246. }
  247. }
  248. $dsql->ExecuteNoneQuery("UPDATE `#@__co_htmls` SET isexport=1 WHERE aid='$exid' ");
  249. }
  250. //检测是否完成或后续操作
  251. if($totalpage <= $pageno)
  252. {
  253. if($channelid>0 && $makehtml==1)
  254. {
  255. if( $autotype==0 && !empty($nid) )
  256. {
  257. $mhtml = "makehtml_archives_action.php?typeid=$typeid&startid=$startid&endid=$endid&pagesize=20";
  258. ShowMsg("完成数据导入,准备生成文档HTML...",$mhtml);
  259. exit();
  260. }
  261. else
  262. {
  263. ShowMsg("完成所有数据导入,请手工更新HTML!","javascript:;");
  264. exit();
  265. }
  266. }
  267. else
  268. {
  269. ShowMsg("完成所有数据导入!","javascript:;");
  270. exit();
  271. }
  272. }
  273. else
  274. {
  275. if($totalpage>0)
  276. {
  277. $rs = substr(($pageno / $totalpage * 100), 0, 2);
  278. }
  279. else
  280. {
  281. $rs = 100;
  282. }
  283. $pageno++;
  284. $gourl = "co_export.php?dopost=done&nid=$nid&totalcc=$totalcc&channelid=$channelid&pageno=$pageno";
  285. $gourl .= "&nid=$nid&typeid=$typeid&autotype=$autotype&arcrank=$arcrank&pagesize=$pagesize&randcc=$randcc";
  286. $gourl .= "&startid=$startid&endid=$endid&onlytitle=$onlytitle&usetitle=$usetitle&makehtml=$makehtml";
  287. ShowMsg("完成 {$rs}% 导入,继续执行操作...",$gourl,'',500);
  288. exit();
  289. }
  290. }