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.

245 lines
11 KiB

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