ExportController.class.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. $login_user = $this->checkLogin();
  14. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  15. $this->message(L('no_permissions'));
  16. return;
  17. }
  18. $item = D("Item")->where("item_id = '$item_id' ")->find();
  19. $menu = D("Item")->getContent($item_id,"*","*",1);
  20. if ($cat_id) {
  21. foreach ($menu['catalogs'] as $key => $value) {
  22. if ($cat_id == $value['cat_id']) {
  23. $pages = $value['pages'] ;
  24. $catalogs = $value['catalogs'] ;
  25. }else{
  26. if ($value['catalogs']) {
  27. foreach ($value['catalogs'] as $key2 => $value2) {
  28. if ($cat_id == $value2['cat_id']) {
  29. $pages = $value2['pages'] ;
  30. $catalogs = $value2['catalogs'] ;
  31. }
  32. }
  33. if ($value2['catalogs']) {
  34. foreach ($value2['catalogs'] as $key3 => $value3) {
  35. if ($cat_id == $value3['cat_id']) {
  36. $pages = $value3['pages'] ;
  37. $catalogs = $value3['catalogs'] ;
  38. }
  39. }
  40. }
  41. }
  42. }
  43. }
  44. }else{
  45. $pages = $menu['pages'] ;
  46. $catalogs = $menu['catalogs'] ;
  47. }
  48. $data = '';
  49. $parent = 1;
  50. if ($pages) {
  51. foreach ($pages as $key => $value) {
  52. $data .= "<h1>{$parent}、{$value['page_title']}</h1>";
  53. $data .= '<div style="margin-left:20px;">';
  54. $data .= htmlspecialchars_decode($Parsedown->text($value['page_content']));
  55. $data .= '</div>';
  56. $parent ++;
  57. }
  58. }
  59. //var_export($catalogs);
  60. if ($catalogs) {
  61. foreach ($catalogs as $key => $value) {
  62. $data .= "<h1>{$parent}、{$value['cat_name']}</h1>";
  63. $data .= '<div style="margin-left:0px;">';
  64. $child = 1 ;
  65. if ($value['pages']) {
  66. foreach ($value['pages'] as $page) {
  67. $data .= "<h2>{$parent}.{$child}、{$page['page_title']}</h2>";
  68. $data .= '<div style="margin-left:0px;">';
  69. $data .= htmlspecialchars_decode($Parsedown->text($page['page_content']));
  70. $data .= '</div>';
  71. $child ++;
  72. }
  73. }
  74. if ($value['catalogs']) {
  75. $parent2 = 1 ;
  76. foreach ($value['catalogs'] as $key3 => $value3) {
  77. $data .= "<h2>{$parent}.{$parent2}、{$value3['cat_name']}</h2>";
  78. $data .= '<div style="margin-left:20px;">';
  79. $child2 = 1 ;
  80. if ($value3['pages']) {
  81. foreach ($value3['pages'] as $page3) {
  82. $data .= "<h3>{$parent}.{$parent2}.{$child2}、{$page3['page_title']}</h3>";
  83. $data .= '<div style="margin-left:0px;">';
  84. $data .= htmlspecialchars_decode($Parsedown->text($page3['page_content']));
  85. $data .= '</div>';
  86. $child2 ++;
  87. }
  88. }
  89. if ($value3['catalogs']) {
  90. $parent3 = 1 ;
  91. foreach ($value3['catalogs'] as $key4 => $value4) {
  92. $data .= "<h2>{$parent}.{$parent2}.{$parent3}、{$value4['cat_name']}</h2>";
  93. $data .= '<div style="margin-left:0px;">';
  94. $child3 = 1 ;
  95. if ($value4['pages']) {
  96. foreach ($value4['pages'] as $page4) {
  97. $data .= "<h3>{$parent}.{$parent2}.{$parent3}.{$child3}、{$page4['page_title']}</h3>";
  98. $data .= '<div style="margin-left:30px;">';
  99. $data .= htmlspecialchars_decode($Parsedown->text($page4['page_content']));
  100. $data .= '</div>';
  101. $child3 ++;
  102. }
  103. }
  104. $data .= '</div>';
  105. $parent3 ++;
  106. }
  107. }
  108. $data .= '</div>';
  109. $parent2 ++;
  110. }
  111. }
  112. $data .= '</div>';
  113. $parent ++;
  114. }
  115. }
  116. output_word($data,$item['item_name']);
  117. }
  118. //导出整个项目为markdown压缩包
  119. public function markdown(){
  120. set_time_limit(100);
  121. ini_set('memory_limit','800M');
  122. $item_id = I("item_id/d");
  123. $login_user = $this->checkLogin();
  124. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  125. $this->message(L('no_permissions'));
  126. return;
  127. }
  128. $item = D("Item")->where("item_id = '$item_id' ")->find();
  129. $exportJson = D("Item")->export($item_id , true);
  130. $exportData = json_decode($exportJson , 1 ) ;
  131. $zipArc = new \ZipArchive();
  132. $temp_file = tempnam(sys_get_temp_dir(), 'Tux')."_showdoc_.zip";
  133. $temp_dir = sys_get_temp_dir()."/showdoc_".time().rand();
  134. mkdir($temp_dir) ;
  135. file_put_contents($temp_dir.'/'.'info.json', json_encode($exportData));
  136. file_put_contents($temp_dir.'/'.'readme.md', "由于页面标题可能含有特殊字符导致异常,所以markdown文件的命令均为英文(md5串),以下是页面标题和文件的对应关系:".PHP_EOL.PHP_EOL );
  137. $exportData['pages'] = $this->_markdownTofile( $exportData['pages'] , $temp_dir);
  138. $ret = $this->_zip( $temp_dir ,$temp_file );
  139. clear_runtime($temp_dir);
  140. header("Cache-Control: max-age=0");
  141. header("Content-Description: File Transfer");
  142. header('Content-disposition: attachment; filename=showdoc.zip'); // 文件名
  143. header("Content-Type: application/zip"); // zip格式的
  144. header("Content-Transfer-Encoding: binary"); // 告诉浏览器,这是二进制文件
  145. header('Content-Length: ' . filesize($temp_file)); // 告诉浏览器,文件大小
  146. @readfile($temp_file);//输出文件;
  147. unlink($temp_file);
  148. }
  149. private function _markdownTofile( $catalogData , $temp_dir ){
  150. if ($catalogData['pages']) {
  151. foreach ($catalogData['pages'] as $key => $value) {
  152. $t = rand(1000,100000) ;
  153. //把页面内容保存为md文件
  154. $filename = md5($value['page_title'].'_'.$t).".md" ;
  155. file_put_contents($temp_dir.'/'.$filename, $value['page_content']);
  156. file_put_contents($temp_dir.'/'.'readme.md',$value['page_title']. " —— ". $filename .PHP_EOL, FILE_APPEND );
  157. }
  158. }
  159. if ($catalogData['catalogs']) {
  160. foreach ($catalogData['catalogs'] as $key => $value) {
  161. $catalogData['catalogs'][$key] = $this->_markdownTofile($value , $temp_dir);
  162. }
  163. }
  164. return $catalogData ;
  165. }
  166. /**
  167. * 使用ZIP压缩文件或目录
  168. * @param [string] $fromName 被压缩的文件或目录名
  169. * @param [string] $toName 压缩后的文件名
  170. * @return [bool] 成功返回TRUE, 失败返回FALSE
  171. */
  172. private function _zip($fromName, $toName)
  173. {
  174. if(!file_exists($fromName) && !is_dir($fromName)){
  175. return FALSE;
  176. }
  177. $zipArc = new \ZipArchive();
  178. if(!$zipArc->open($toName, \ZipArchive::CREATE)){
  179. return FALSE;
  180. }
  181. $res = is_dir($fromName) ? $zipArc->addGlob("{$fromName}/*" , 0 , array('add_path' => "prefix_", 'remove_all_path' => TRUE) ) : $zipArc->addFile($fromName);
  182. if(!$res){
  183. $zipArc->close();
  184. return FALSE;
  185. }
  186. return $zipArc->close();
  187. }
  188. }