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.

232 lines
11 KiB

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