ExportController.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class ExportController extends BaseController {
  5. //导出整个项目为word
  6. public function word(){
  7. set_time_limit(100);
  8. ini_set('memory_limit','800M');
  9. import("Vendor.Parsedown.Parsedown");
  10. $Parsedown = new \Parsedown();
  11. $item_id = I("item_id/d");
  12. $cat_id = I("cat_id/d");
  13. $page_id = I("page_id/d");
  14. $login_user = $this->checkLogin();
  15. if (!$this->checkItemEdit($login_user['uid'] , $item_id)) {
  16. $this->message(L('no_permissions'));
  17. return;
  18. }
  19. $item = D("Item")->where("item_id = '$item_id' ")->find();
  20. $menu = D("Item")->getContent($item_id,"*","*",1);
  21. if($page_id > 0 ){
  22. $pages[] = D("Page")->where(" page_id = '$page_id' ")->find();
  23. }
  24. else if ($cat_id) {
  25. foreach ($menu['catalogs'] as $key => $value) {
  26. if ($cat_id == $value['cat_id']) {
  27. $pages = $value['pages'] ;
  28. $catalogs = $value['catalogs'] ;
  29. }else{
  30. if ($value['catalogs']) {
  31. foreach ($value['catalogs'] as $key2 => $value2) {
  32. if ($cat_id == $value2['cat_id']) {
  33. $pages = $value2['pages'] ;
  34. $catalogs = $value2['catalogs'] ;
  35. }
  36. }
  37. if ($value2['catalogs']) {
  38. foreach ($value2['catalogs'] as $key3 => $value3) {
  39. if ($cat_id == $value3['cat_id']) {
  40. $pages = $value3['pages'] ;
  41. $catalogs = $value3['catalogs'] ;
  42. }
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }else{
  49. $pages = $menu['pages'] ;
  50. $catalogs = $menu['catalogs'] ;
  51. }
  52. $data = '';
  53. $parent = 1;
  54. if ($pages) {
  55. foreach ($pages as $key => $value) {
  56. if(count($pages) > 1){
  57. $data .= "<h1>{$parent}、{$value['page_title']}</h1>";
  58. }else{
  59. $data .= "<h1>{$value['page_title']}</h1>";
  60. }
  61. $data .= '<div style="margin-left:20px;">';
  62. $tmp_content = D("Page")->runapiToMd($value['page_content']) ;
  63. $value['page_content'] = $tmp_content ? $tmp_content : $value['page_content'] ;
  64. $data .= htmlspecialchars_decode($Parsedown->text($value['page_content']));
  65. $data .= '</div>';
  66. $parent ++;
  67. }
  68. }
  69. //var_export($catalogs);
  70. if ($catalogs) {
  71. foreach ($catalogs as $key => $value) {
  72. $data .= "<h1>{$parent}、{$value['cat_name']}</h1>";
  73. $data .= '<div style="margin-left:0px;">';
  74. $child = 1 ;
  75. if ($value['pages']) {
  76. foreach ($value['pages'] as $page) {
  77. $data .= "<h2>{$parent}.{$child}、{$page['page_title']}</h2>";
  78. $data .= '<div style="margin-left:0px;">';
  79. $tmp_content = D("Page")->runapiToMd($page['page_content']) ;
  80. $page['page_content'] = $tmp_content ? $tmp_content : $page['page_content'] ;
  81. $data .= htmlspecialchars_decode($Parsedown->text($page['page_content']));
  82. $data .= '</div>';
  83. $child ++;
  84. }
  85. }
  86. if ($value['catalogs']) {
  87. $parent2 = 1 ;
  88. foreach ($value['catalogs'] as $key3 => $value3) {
  89. $data .= "<h2>{$parent}.{$parent2}、{$value3['cat_name']}</h2>";
  90. $data .= '<div style="margin-left:20px;">';
  91. $child2 = 1 ;
  92. if ($value3['pages']) {
  93. foreach ($value3['pages'] as $page3) {
  94. $data .= "<h3>{$parent}.{$parent2}.{$child2}、{$page3['page_title']}</h3>";
  95. $data .= '<div style="margin-left:0px;">';
  96. $tmp_content = D("Page")->runapiToMd($page3['page_content']) ;
  97. $page3['page_content'] = $tmp_content ? $tmp_content : $page3['page_content'] ;
  98. $data .= htmlspecialchars_decode($Parsedown->text($page3['page_content']));
  99. $data .= '</div>';
  100. $child2 ++;
  101. }
  102. }
  103. if ($value3['catalogs']) {
  104. $parent3 = 1 ;
  105. foreach ($value3['catalogs'] as $key4 => $value4) {
  106. $data .= "<h2>{$parent}.{$parent2}.{$parent3}、{$value4['cat_name']}</h2>";
  107. $data .= '<div style="margin-left:0px;">';
  108. $child3 = 1 ;
  109. if ($value4['pages']) {
  110. foreach ($value4['pages'] as $page4) {
  111. $data .= "<h3>{$parent}.{$parent2}.{$parent3}.{$child3}、{$page4['page_title']}</h3>";
  112. $data .= '<div style="margin-left:30px;">';
  113. $tmp_content = D("Page")->runapiToMd($page4['page_content']) ;
  114. $page4['page_content'] = $tmp_content ? $tmp_content : $page4['page_content'] ;
  115. $data .= htmlspecialchars_decode($Parsedown->text($page4['page_content']));
  116. $data .= '</div>';
  117. $child3 ++;
  118. }
  119. }
  120. if ($value4['catalogs']) {
  121. $parent4 = 1 ;
  122. foreach ($value4['catalogs'] as $key5 => $value5) {
  123. $data .= "<h2>{$parent}.{$parent2}.{$parent3}.{$parent4}、{$value5['cat_name']}</h2>";
  124. $data .= '<div style="margin-left:0px;">';
  125. $child4 = 1 ;
  126. if ($value4['pages']) {
  127. foreach ($value4['pages'] as $page5) {
  128. $data .= "<h3>{$parent}.{$parent2}.{$parent3}.{$parent4}.{$child4}、{$page5['page_title']}</h3>";
  129. $data .= '<div style="margin-left:30px;">';
  130. $tmp_content = D("Page")->runapiToMd($page5['page_content']) ;
  131. $page5['page_content'] = $tmp_content ? $tmp_content : $page5['page_content'] ;
  132. $data .= htmlspecialchars_decode($Parsedown->text($page5['page_content']));
  133. $data .= '</div>';
  134. $child3 ++;
  135. }
  136. }
  137. $data .= '</div>';
  138. $parent3 ++;
  139. }
  140. }
  141. $data .= '</div>';
  142. $parent3 ++;
  143. }
  144. }
  145. $data .= '</div>';
  146. $parent2 ++;
  147. }
  148. }
  149. $data .= '</div>';
  150. $parent ++;
  151. }
  152. }
  153. output_word($data,$item['item_name']);
  154. }
  155. //导出整个项目为markdown压缩包
  156. public function markdown(){
  157. set_time_limit(100);
  158. ini_set('memory_limit','800M');
  159. $item_id = I("item_id/d");
  160. $login_user = $this->checkLogin();
  161. if (!$this->checkItemEdit($login_user['uid'] , $item_id)) {
  162. $this->message(L('no_permissions'));
  163. return;
  164. }
  165. $item = D("Item")->where("item_id = '$item_id' ")->find();
  166. $exportJson = D("Item")->export($item_id , true);
  167. $exportData = json_decode($exportJson , 1 ) ;
  168. $zipArc = new \ZipArchive();
  169. $temp_file = tempnam(sys_get_temp_dir(), 'Tux')."_showdoc_.zip";
  170. $temp_dir = sys_get_temp_dir()."/showdoc_".time().rand();
  171. mkdir($temp_dir) ;
  172. unset($exportData['members']);
  173. file_put_contents($temp_dir.'/'.'info.json', json_encode($exportData));
  174. file_put_contents($temp_dir.'/'.'readme.md', "由于页面标题可能含有特殊字符导致异常,所以markdown文件的命名均为英文(md5串),以下是页面标题和文件的对应关系:".PHP_EOL.PHP_EOL );
  175. $exportData['pages'] = $this->_markdownTofile( $exportData['pages'] , $temp_dir);
  176. $ret = $this->_zip( $temp_dir ,$temp_file );
  177. clear_runtime($temp_dir);
  178. rmdir($temp_dir);
  179. header("Cache-Control: max-age=0");
  180. header("Content-Description: File Transfer");
  181. header('Content-disposition: attachment; filename=showdoc.zip'); // 文件名
  182. header("Content-Type: application/zip"); // zip格式的
  183. header("Content-Transfer-Encoding: binary"); // 告诉浏览器,这是二进制文件
  184. header('Content-Length: ' . filesize($temp_file)); // 告诉浏览器,文件大小
  185. @readfile($temp_file);//输出文件;
  186. unlink($temp_file);
  187. }
  188. private function _markdownTofile( $catalogData , $temp_dir ){
  189. if ($catalogData['pages']) {
  190. foreach ($catalogData['pages'] as $key => $value) {
  191. $t = rand(1000,100000) ;
  192. //把页面内容保存为md文件
  193. $filename = md5($value['page_title'].'_'.$t).".md" ;
  194. file_put_contents($temp_dir.'/'.$filename, htmlspecialchars_decode( $value['page_content']) ) ;
  195. file_put_contents($temp_dir.'/'.'readme.md',$value['page_title']. " —— prefix_". $filename .PHP_EOL, FILE_APPEND );
  196. }
  197. }
  198. if ($catalogData['catalogs']) {
  199. foreach ($catalogData['catalogs'] as $key => $value) {
  200. $catalogData['catalogs'][$key] = $this->_markdownTofile($value , $temp_dir);
  201. }
  202. }
  203. return $catalogData ;
  204. }
  205. private function _zip($temp_dir, $temp_file)
  206. {
  207. $zipArc = new \ZipArchive();
  208. if(!$zipArc->open($temp_file, \ZipArchive::CREATE)){
  209. return FALSE;
  210. }
  211. $dir = opendir( $temp_dir );
  212. while( false != ( $file = readdir( $dir ) ) )
  213. {
  214. if( ( $file != "." ) and ( $file != ".." ) )
  215. {
  216. $res = $zipArc->addFromString ( "prefix_".$file , file_get_contents($temp_dir."/".$file) ) ;
  217. }
  218. }
  219. closedir( $dir );
  220. if(!$res){
  221. $zipArc->close();
  222. return FALSE;
  223. }
  224. return $zipArc->close();
  225. }
  226. }