Răsfoiți Sursa

支持导出巴利原文 & 多个channel

visuddhinanda 2 ani în urmă
părinte
comite
d6d7e45523
1 a modificat fișierele cu 159 adăugiri și 98 ștergeri
  1. 159 98
      app/Console/Commands/ExportChapter.php

+ 159 - 98
app/Console/Commands/ExportChapter.php

@@ -22,10 +22,11 @@ class ExportChapter extends Command
     /**
      * The name and signature of the console command.
      * php artisan export:chapter 213 1913 a19eaf75-c63f-4b84-8125-1bce18311e23 213-1913 --format=html
-     * php artisan export:chapter 168 915 19f53a65-81db-4b7d-8144-ac33f1217d34 168-915 --format=html
+     * php artisan export:chapter 168 915 19f53a65-81db-4b7d-8144-ac33f1217d34 168-915.html --format=html
+     * php artisan export:chapter 168 915 19f53a65-81db-4b7d-8144-ac33f1217d34 168-915.html --format=html --origin=true
      * @var string
      */
-    protected $signature = 'export:chapter {book} {para} {channel} {filename} {--debug} {--format=tex} ';
+    protected $signature = 'export:chapter {book} {para} {channel} {filename} {--origin=false} {--translation=true} {--debug} {--format=tex} ';
 
     /**
      * The console command description.
@@ -55,6 +56,8 @@ class ExportChapter extends Command
                                 'message'=>$message,
                             ],
                             $this->exportStatusExpiry);
+        $percent = (int)($progress * 100);
+        $this->info("[{$percent}%]".$message);
     }
     public function getStatus($filename){
         return RedisClusters::get($this->exportStatusKey.'/'.$filename);
@@ -71,6 +74,12 @@ class ExportChapter extends Command
         if(\App\Tools\Tools::isStop()){
             return 0;
         }
+        $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES,
+                                        'delimiters' => '[[ ]]',
+                                        'escape'=>function ($value){
+                                            return $value;
+                                        }));
+        $tplParagraph = file_get_contents(resource_path("mustache/".$this->option('format')."/paragraph.".$this->option('format')));
 
         MdRender::init();
 
@@ -89,9 +98,29 @@ class ExportChapter extends Command
         }
         $book = $this->argument('book');
         $para = $this->argument('para');
-        $channelId = $this->argument('channel');
-        $channel = ChannelApi::getById($channelId);
-        $chapter = PaliText::where('book',$book)->where('paragraph',$para)->first();
+
+        //获取原文channel
+        $orgChannelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
+
+        $tranChannelsId = explode('_',$this->argument('channel'));
+
+        $channelsId = array_merge([$orgChannelId],$tranChannelsId);
+
+        $channels = array();
+        $channelsIndex = array();
+        foreach ($channelsId as $key => $id) {
+            $channels[] = ChannelApi::getById($id);
+            $channelsIndex[$id] = ChannelApi::getById($id);
+        }
+
+        $bookMeta = array();
+        $bookMeta['book_author'] = "";
+        foreach ($channels as $key => $channel) {
+            $bookMeta['book_author'] .= $channel['name'] . ' ';
+        }
+
+        $chapter = PaliText::where('book',$book)
+                           ->where('paragraph',$para)->first();
         if(!$chapter){
             return $this->error("no data");
         }
@@ -99,19 +128,19 @@ class ExportChapter extends Command
         $currProgress = 0;
         $this->setStatus($currProgress,'start');
 
-        $bookMeta = array();
+
         if(empty($chapter->toc)){
             $bookMeta['title'] = "unknown";
         }else{
-            $title = ProgressChapter::where('book',$book)->where('para',$para)
-                                    ->where('channel_id',$channelId)
-                                    ->value('title');
-            $bookMeta['book_title'] = $title?$title:$chapter->toc;
+            $bookMeta['book_title'] = '';
+            foreach ($channelsId as $key => $id) {
+                $title = ProgressChapter::where('book',$book)->where('para',$para)
+                        ->where('channel_id',$id)
+                        ->value('title');
+                $bookMeta['book_title'] .= $title;
+            }
             $bookMeta['sub_title'] = $chapter->toc;
         }
-        if($channel){
-            $bookMeta['book_author'] = $channel['name'];
-        }
 
         $subChapter = PaliText::where('book',$book)->where('parent',$para)
                               ->where('level','<',8)
@@ -125,112 +154,144 @@ class ExportChapter extends Command
                               ->get();
         }
 
+        $chapterParagraph = PaliText::where('book',$book)->where('paragraph',$para)->value('chapter_len');
+        if($chapterParagraph >0 ){
+            $step = 0.9 / $chapterParagraph;
+        }else{
+            $step = 0.9;
+            Log::error('段落长度不能为0',['book'=>$book,'para'=>$para]);
+        }
 
-        $step = 1 / PaliText::where('book',$book)->where('paragraph',$para)->value('chapter_len');
+        $outputChannelsId = [];
+        if($this->option('origin') === 'true'){
+            $outputChannelsId[] = $orgChannelId;
+        }
+        if($this->option('translation') === 'true'){
+            $outputChannelsId = array_merge($outputChannelsId,$tranChannelsId);
+        }
 
         $sections = array();
         foreach ($subChapter as $key => $sub) {
-            # code...
-            $chapter = ProgressChapter::where('book',$book)->where('para',$sub->paragraph)
-                                    ->where('channel_id',$channelId)
-                                    ->first();
-            if($chapter){
-                $filename = "{$sub->paragraph}.".$this->option('format');
-                $bookMeta['sections'][] = ['filename'=>$filename];
-                $paliTitle = PaliText::where('book',$book)->where('paragraph',$sub->paragraph)->value('toc');
-                $title = $chapter->title?$chapter->title:$paliTitle;
-                $content = array();
-
-                $chapterStart = $sub->paragraph+1;
-                $chapterEnd = $sub->paragraph + $sub->chapter_len;
-                $chapterBody = PaliText::where('book',$book)
-                                          ->whereBetween('paragraph',[$chapterStart,$chapterEnd])
-                                          ->orderBy('paragraph')->get();
-                foreach ($chapterBody as $body) {
-                    $currProgress += $step;
-                    $this->setStatus($currProgress,'export chapter '.$body->paragraph);
-                    # code...
+            # 看这个章节是否存在译文
+            $hasChapter = false;
+            if($this->option('origin') === 'true'){
+                $hasChapter = true;
+            }
+            if($this->option('translation') === 'true'){
+                foreach ($tranChannelsId as $id) {
+                    if(ProgressChapter::where('book',$book)->where('para',$sub->paragraph)
+                        ->where('channel_id',$id)
+                        ->exists()){
+                            $hasChapter = true;
+                    }
+                }
+            }
+            if(!$hasChapter){
+                //不存在需要导出的数据
+                continue;
+            }
+            $filename = "{$sub->paragraph}.".$this->option('format');
+            $bookMeta['sections'][] = ['filename'=>$filename];
+            $paliTitle = PaliText::where('book',$book)
+                                 ->where('paragraph',$sub->paragraph)
+                                 ->value('toc');
+            $sectionTitle = $paliTitle;
+            if($this->option('translation') === 'true'){
+                $chapter = ProgressChapter::where('book',$book)->where('para',$sub->paragraph)
+                                        ->where('channel_id',$tranChannelsId[0])
+                                        ->first();
+                if($chapter && !empty($chapter->title)){
+                    $sectionTitle = $chapter->title;
+                }
+            }
+
+
+            $content = array();
+
+            $chapterStart = $sub->paragraph+1;
+            $chapterEnd = $sub->paragraph + $sub->chapter_len;
+            $chapterBody = PaliText::where('book',$book)
+                                    ->whereBetween('paragraph',[$chapterStart,$chapterEnd])
+                                    ->orderBy('paragraph')->get();
+
+
+
+            foreach ($chapterBody as $body) {
+                $currProgress += $step;
+                $this->setStatus($currProgress,'export chapter '.$body->paragraph);
+                $paraData = array();
+                $paraData['translations'] = array();
+                foreach ($outputChannelsId as $key => $channelId) {
                     $translationData = Sentence::where('book_id',$book)
-                                    ->where('paragraph',$body->paragraph)
-                                    ->where('channel_uid',$channelId)
-                                    ->orderBy('word_start')->get();
+                                        ->where('paragraph',$body->paragraph)
+                                        ->where('channel_uid',$channelId)
+                                        ->orderBy('word_start')->get();
                     $sentContent = array();
                     foreach ($translationData as $sent) {
                         $texText = MdRender::render($sent->content,
-                                                        [$sent->channel_uid],
-                                                        null,
-                                                        'read',
-                                                        $channel['type'],
-                                                        $sent->content_type,
-                                                        $renderFormat
-                                                        );
+                                                    [$sent->channel_uid],
+                                                    null,
+                                                    'read',
+                                                    $channelsIndex[$channelId]['type'],
+                                                    $sent->content_type,
+                                                    $renderFormat
+                                                    );
                         $sentContent[] = trim($texText);
                     }
                     $paraContent = implode(' ',$sentContent);
-                    if($body->level > 7){
-                        switch ($this->option('format')) {
-                            case 'tex':
-                                $content[] = '\par '.$paraContent;
-                                break;
-                            case 'html':
-                                $content[] = '<p>'.$paraContent.'</p>';
-                                break;
-                            case 'md':
-                                $content[] = "\n\n".$paraContent;
-                                break;
-                        }
-
+                    if($channelsIndex[$channelId]['type'] === 'original'){
+                        $paraData['origin'] = $paraContent;
                     }else{
-                        $currLevel = $body->level - $sub->level;
-                        if($currLevel>0){
-                            if(empty($paraContent)){
-                                $subSessionTitle = PaliText::where('book',$book)
-                                                     ->where('paragraph',$body->paragraph)
-                                                     ->value('toc');
-                            }else{
-                                $subSessionTitle = $paraContent;
-                            }
-                            switch ($this->option('format')) {
-                                case 'tex':
-                                    $subStr = array_fill(0,$currLevel,'sub');
-                                    $content[] = '\\'. implode('',$subStr) . "section{".$subSessionTitle.'}';
-                                    break;
-                                case 'md':
-                                    $subStr = array_fill(0,$currLevel,'#');
-                                    $content[] = implode('',$subStr) . " ".$subSessionTitle;
-                                    break;
-                                case 'html':
-                                    $level = $currLevel+2;
-                                    $content[] = "<h{$currLevel}".$subSessionTitle."</h{$currLevel}";
-                                    break;
-                            }
-
-                        }else{
-                            $content[] = '\par '.$paraContent;
-                        }
+                        $paraData['translations'][] = ['content'=>$paraContent];
                     }
-                    $content[] = "\n\n";
                 }
+                if($body->level > 7){
+                    $content[] = $m->render($tplParagraph,$paraData);
+                }else{
+                    $currLevel = $body->level - $sub->level;
+                    if($currLevel<=0){
+                        $currLevel = 1;
+                    }
 
-                $sections[] = [
-                                'name'=>$filename,
-                                'body'=>[
-                                    'title'=>$title,
-                                    'content'=>implode('',$content)
-                                ]
-                            ];
+                    if(count($paraData['translations'])===0){
+                        $subSessionTitle = PaliText::where('book',$book)
+                                            ->where('paragraph',$body->paragraph)
+                                            ->value('toc');
+                    }else{
+                        $subSessionTitle = $paraData['translations'][0]['content'];
+                    }
+                    switch ($this->option('format')) {
+                        case 'tex':
+                            $subStr = array_fill(0,$currLevel,'sub');
+                            $content[] = '\\'. implode('',$subStr) . "section{".$subSessionTitle.'}';
+                            break;
+                        case 'md':
+                            $subStr = array_fill(0,$currLevel,'#');
+                            $content[] = implode('',$subStr) . " ".$subSessionTitle;
+                            break;
+                        case 'html':
+                            $level = $currLevel+2;
+                            $content[] = "<h{$currLevel}".$subSessionTitle."</h{$currLevel}";
+                            break;
+                    }
+                }
+                $content[] = "\n\n";
             }
+
+            $sections[] = [
+                    'name'=>$filename,
+                    'body'=>[
+                        'title'=>$sectionTitle,
+                        'content'=>implode('',$content)
+                    ]
+                ];
         }
 
         $this->setStatus(0.9,'export content done');
         Log::debug('导出结束');
 
         $tex = array();
-        $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES,
-                                        'delimiters' => '[[ ]]',
-                                        'escape'=>function ($value){
-                                            return $value;
-                                        }));
+
         $tpl = file_get_contents(resource_path("mustache/".$this->option('format')."/main.".$this->option('format')));
         $texContent = $m->render($tpl,$bookMeta);
         $tex[] = ['name'=>'main.'.$this->option('format'),
@@ -289,7 +350,7 @@ class ExportChapter extends Command
                 Storage::put($filename, implode('',$file));
                 break;
         }
-        $this->setStatus(1,'export done');
+        $this->setStatus(1,'export done filename='.$filename);
         Log::debug('task export offline chapter-table finished');
         return 0;
     }