ExportController.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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->checkItemPermn($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. $data .= htmlspecialchars_decode($Parsedown->text($value['page_content']));
  63. $data .= '</div>';
  64. $parent ++;
  65. }
  66. }
  67. //var_export($catalogs);
  68. if ($catalogs) {
  69. foreach ($catalogs as $key => $value) {
  70. $data .= "<h1>{$parent}、{$value['cat_name']}</h1>";
  71. $data .= '<div style="margin-left:0px;">';
  72. $child = 1 ;
  73. if ($value['pages']) {
  74. foreach ($value['pages'] as $page) {
  75. $data .= "<h2>{$parent}.{$child}、{$page['page_title']}</h2>";
  76. $data .= '<div style="margin-left:0px;">';
  77. $data .= htmlspecialchars_decode($Parsedown->text($page['page_content']));
  78. $data .= '</div>';
  79. $child ++;
  80. }
  81. }
  82. if ($value['catalogs']) {
  83. $parent2 = 1 ;
  84. foreach ($value['catalogs'] as $key3 => $value3) {
  85. $data .= "<h2>{$parent}.{$parent2}、{$value3['cat_name']}</h2>";
  86. $data .= '<div style="margin-left:20px;">';
  87. $child2 = 1 ;
  88. if ($value3['pages']) {
  89. foreach ($value3['pages'] as $page3) {
  90. $data .= "<h3>{$parent}.{$parent2}.{$child2}、{$page3['page_title']}</h3>";
  91. $data .= '<div style="margin-left:0px;">';
  92. $data .= htmlspecialchars_decode($Parsedown->text($page3['page_content']));
  93. $data .= '</div>';
  94. $child2 ++;
  95. }
  96. }
  97. if ($value3['catalogs']) {
  98. $parent3 = 1 ;
  99. foreach ($value3['catalogs'] as $key4 => $value4) {
  100. $data .= "<h2>{$parent}.{$parent2}.{$parent3}、{$value4['cat_name']}</h2>";
  101. $data .= '<div style="margin-left:0px;">';
  102. $child3 = 1 ;
  103. if ($value4['pages']) {
  104. foreach ($value4['pages'] as $page4) {
  105. $data .= "<h3>{$parent}.{$parent2}.{$parent3}.{$child3}、{$page4['page_title']}</h3>";
  106. $data .= '<div style="margin-left:30px;">';
  107. $data .= htmlspecialchars_decode($Parsedown->text($page4['page_content']));
  108. $data .= '</div>';
  109. $child3 ++;
  110. }
  111. }
  112. if ($value4['catalogs']) {
  113. $parent4 = 1 ;
  114. foreach ($value4['catalogs'] as $key5 => $value5) {
  115. $data .= "<h2>{$parent}.{$parent2}.{$parent3}.{$parent4}、{$value5['cat_name']}</h2>";
  116. $data .= '<div style="margin-left:0px;">';
  117. $child4 = 1 ;
  118. if ($value4['pages']) {
  119. foreach ($value4['pages'] as $page5) {
  120. $data .= "<h3>{$parent}.{$parent2}.{$parent3}.{$parent4}.{$child4}、{$page5['page_title']}</h3>";
  121. $data .= '<div style="margin-left:30px;">';
  122. $data .= htmlspecialchars_decode($Parsedown->text($page5['page_content']));
  123. $data .= '</div>';
  124. $child3 ++;
  125. }
  126. }
  127. $data .= '</div>';
  128. $parent3 ++;
  129. }
  130. }
  131. $data .= '</div>';
  132. $parent3 ++;
  133. }
  134. }
  135. $data .= '</div>';
  136. $parent2 ++;
  137. }
  138. }
  139. $data .= '</div>';
  140. $parent ++;
  141. }
  142. }
  143. output_word($data,$item['item_name']);
  144. }
  145. //导出整个项目为markdown压缩包
  146. public function markdown(){
  147. set_time_limit(100);
  148. ini_set('memory_limit','800M');
  149. $item_id = I("item_id/d");
  150. $login_user = $this->checkLogin();
  151. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  152. $this->message(L('no_permissions'));
  153. return;
  154. }
  155. $item = D("Item")->where("item_id = '$item_id' ")->find();
  156. $exportJson = D("Item")->export($item_id , true);
  157. $exportData = json_decode($exportJson , 1 ) ;
  158. $zipArc = new \ZipArchive();
  159. $temp_file = tempnam(sys_get_temp_dir(), 'Tux')."_showdoc_.zip";
  160. $temp_dir = sys_get_temp_dir()."/showdoc_".time().rand();
  161. mkdir($temp_dir) ;
  162. unset($exportData['members']);
  163. file_put_contents($temp_dir.'/'.'info.json', json_encode($exportData));
  164. file_put_contents($temp_dir.'/'.'readme.md', "由于页面标题可能含有特殊字符导致异常,所以markdown文件的命名均为英文(md5串),以下是页面标题和文件的对应关系:".PHP_EOL.PHP_EOL );
  165. $exportData['pages'] = $this->_markdownTofile( $exportData['pages'] , $temp_dir);
  166. $ret = $this->_zip( $temp_dir ,$temp_file );
  167. clear_runtime($temp_dir);
  168. rmdir($temp_dir);
  169. header("Cache-Control: max-age=0");
  170. header("Content-Description: File Transfer");
  171. header('Content-disposition: attachment; filename=showdoc.zip'); // 文件名
  172. header("Content-Type: application/zip"); // zip格式的
  173. header("Content-Transfer-Encoding: binary"); // 告诉浏览器,这是二进制文件
  174. header('Content-Length: ' . filesize($temp_file)); // 告诉浏览器,文件大小
  175. @readfile($temp_file);//输出文件;
  176. unlink($temp_file);
  177. }
  178. private function _markdownTofile( $catalogData , $temp_dir ){
  179. if ($catalogData['pages']) {
  180. foreach ($catalogData['pages'] as $key => $value) {
  181. $t = rand(1000,100000) ;
  182. //把页面内容保存为md文件
  183. $filename = md5($value['page_title'].'_'.$t).".md" ;
  184. file_put_contents($temp_dir.'/'.$filename, htmlspecialchars_decode( $value['page_content']) ) ;
  185. file_put_contents($temp_dir.'/'.'readme.md',$value['page_title']. " —— prefix_". $filename .PHP_EOL, FILE_APPEND );
  186. }
  187. }
  188. if ($catalogData['catalogs']) {
  189. foreach ($catalogData['catalogs'] as $key => $value) {
  190. $catalogData['catalogs'][$key] = $this->_markdownTofile($value , $temp_dir);
  191. }
  192. }
  193. return $catalogData ;
  194. }
  195. private function _zip($temp_dir, $temp_file)
  196. {
  197. $zipArc = new \ZipArchive();
  198. if(!$zipArc->open($temp_file, \ZipArchive::CREATE)){
  199. return FALSE;
  200. }
  201. $dir = opendir( $temp_dir );
  202. while( false != ( $file = readdir( $dir ) ) )
  203. {
  204. if( ( $file != "." ) and ( $file != ".." ) )
  205. {
  206. $res = $zipArc->addFromString ( "prefix_".$file , file_get_contents($temp_dir."/".$file) ) ;
  207. }
  208. }
  209. closedir( $dir );
  210. if(!$res){
  211. $zipArc->close();
  212. return FALSE;
  213. }
  214. return $zipArc->close();
  215. }
  216. }