Просмотр исходного кода

Merge pull request #1577 from visuddhinanda/laravel

有术语句子不用cache
visuddhinanda 2 лет назад
Родитель
Сommit
b4db5c5814

+ 4 - 1
app/Console/Commands/TestMdRender.php

@@ -75,13 +75,16 @@ class TestMdRender extends Command
         bla **content**
         ```
         md;
+
         $data['note'] = '`bla **bold** _em_ bla`';
         $data['noteTpl'] = <<<md
         {{note|trigger=kacayana|text=bla **bold** _em_ bla}}
         md;
+
         $data['noteTpl2'] = <<<md
         {{note|trigger=kacayana|text={{99-556-8-12}}}}
         md;
+
         $data['trigger'] = <<<md
         ## heading
         ddd
@@ -112,7 +115,7 @@ class TestMdRender extends Command
         title=第一章 戒律概说(Vinaya)|
         style=modal}}
         md;
-
+        $data['empty'] = '';
         //$wiki = MdRender::markdown2wiki($data['noteMulti']);
         //$xml = MdRender::wiki2xml($wiki,['00ae2c48-c204-4082-ae79-79ba2740d506']);
         //$this->info($xml);

+ 2 - 1
app/Console/Commands/UpgradeCommunityTerm.php

@@ -57,7 +57,8 @@ class UpgradeCommunityTerm extends Command
             $this->error('no channel');
             return 1;
         }
-        $table = DhammaTerm::select('word')->whereIn('language',[$this->argument('lang'),$lang,$langFamily])
+        $table = DhammaTerm::select('word')
+                            ->whereIn('language',[$this->argument('lang'),$lang,$langFamily])
                             ->groupBy('word');
 
 

+ 37 - 36
app/Console/Commands/UpgradeCompound.php

@@ -57,22 +57,7 @@ class UpgradeCompound extends Command
 
 		$start = \microtime(true);
 
-		$_word = $this->argument('word');
-		if(!empty($_word)){
-			$ts = new TurboSplit();
-            if($this->option('debug')){
-                $ts->debug(true);
-            }
-			$results = $ts->splitA($_word);
-			Storage::disk('local')->put("tmp/compound1.csv", "word,type,grammar,parent,factors");
-			foreach ($results as $key => $value) {
-				# code...
-                $output = "{$value['word']},{$value['type']},{$value['grammar']},{$value['parent']},{$value['factors']},{$value['confidence']}";
-                $this->info($output);
-				Storage::disk('local')->append("tmp/compound1.csv", $output);
-			}
-			return 0;
-		}
+
 
 		//
 		if($this->option('test')){
@@ -108,7 +93,12 @@ class UpgradeCompound extends Command
 			return 0;
 		}
 
-        if($this->option('book')){
+
+		$_word = $this->argument('word');
+		if(!empty($_word)){
+            $words = array((object)array('real'=>$_word));
+            $count[] = (object)array('count'=>1);
+		}else if($this->option('book')){
             $words = WbwTemplate::select('real')
                             ->where('book',$this->option('book'))
                             ->where('type','<>','.ctl.')
@@ -148,42 +138,53 @@ class UpgradeCompound extends Command
                     ->delete();
 
 			$ts = new TurboSplit();
-
+            if($this->option('debug')){
+                $ts->debug(true);
+            }
             $parts = $ts->splitA($word->real);
+            if(!empty($_word)){
+                Storage::disk('local')->put("tmp/compound1.csv", "word,type,grammar,parent,factors");
+            }
+
+            $count = 0;
             foreach ($parts as $part) {
                 if(isset($part['type']) && $part['type'] === ".v."){
                     continue;
                 }
-                $new = UserDict::firstOrNew(
-                    [
-                        'word' => $part['word'],
-                        'factors' => $part['factors'],
-                        'dict_id' => $dict_id,
-                    ],
-                    [
-                        'id' => app('snowflake')->id(),
-                        'source' => '_ROBOT_',
-                        'create_time'=>(int)(microtime(true)*1000),
-                    ]
-                );
+                $count++;
+                $new = new UserDict;
+                $new->id = app('snowflake')->id();
+                $new->word = $part['word'];
+                $new->factors = $part['factors'];
+                $new->dict_id = $dict_id;
+                $new->source = '_ROBOT_';
+                $new->create_time = (int)(microtime(true)*1000);
+
                 if(isset($part['type'])){
                     $new->type = $part['type'];
                 }else{
                     $new->type = ".cp.";
                 }
-                if(isset($part['grammar'])) $new->grammar = $part['grammar'];
-                if(isset($part['parent'])) $new->parent = $part['parent'];
+                if(isset($part['grammar'])){
+                    $new->grammar = $part['grammar'];
+                }
+                if(isset($part['parent'])){
+                    $new->parent = $part['parent'];
+                }
                 $new->confidence = 50*$part['confidence'];
                 $new->note = $part['confidence'];
                 $new->language = 'cm';
                 $new->creator_id = 1;
                 $new->flag = 1;//标记为维护状态
                 $new->save();
-            }
-            if(env('APP_ENV','local') !== 'local'){
-                usleep(500);
-            }
 
+                if(!empty($_word)){
+                    $output = "{$part['word']},{$part['type']},{$part['grammar']},{$part['parent']},{$part['factors']},{$part['confidence']}";
+                    $this->info($count);
+                    $this->info($output);
+                    Storage::disk('local')->append("tmp/compound1.csv", $output);
+                }
+            }
 		}
 		//维护状态数据改为正常状态
 		UserDict::where('dict_id',$dict_id)->where('flag',1)->update(['flag'=>0]);

+ 103 - 80
app/Http/Api/MdRender.php

@@ -14,11 +14,29 @@ use App\Tools\Markdown;
 define("STACK_DEEP",8);
 
 class MdRender{
+    /**
+     * 文字渲染模式
+     * read 阅读模式
+     * edit 编辑模式
+     */
+    protected $options = ['mode' => 'read',
+                'channelType'=>'translation',
+                'contentType'=>"markdown",
+                'format'=>'react',
+                'debug'=>[],
+                ];
+
+    public function __construct($options=[])
+    {
+        foreach ($options as $key => $value) {
+            $this->options[$key] = $value;
+        }
+    }
 
     /**
      * 按照{{}}把字符串切分成三个部分。模版之前的,模版,和模版之后的
      */
-    public static function tplSplit($tpl){
+    private function tplSplit($tpl){
         $before = strpos($tpl,'{{');
         if($before === FALSE){
             //未找到
@@ -75,14 +93,14 @@ class MdRender{
         }
     }
 
-    public static function wiki2xml(string $wiki,$channelId=[],$mode='read',$format='react'):string{
+    private function wiki2xml(string $wiki,$channelId=[]):string{
         /**
          * 把模版转换为xml
          */
         $remain = $wiki;
         $buffer = array();
         do {
-            $arrWiki = MdRender::tplSplit($remain);
+            $arrWiki = $this->tplSplit($remain);
             $buffer[] = $arrWiki['data'][0];
             $tpl = $arrWiki['data'][1];
             if(!empty($tpl)){
@@ -104,7 +122,7 @@ class MdRender{
                 //tpl to react
                 $tpl = str_replace('<param','<span class="param"',$tpl);
                 $tpl = str_replace('</param>','</span>',$tpl);
-                $tpl = MdRender::xml2tpl($tpl,$channelId,$mode,$format);
+                $tpl = $this->xml2tpl($tpl,$channelId);
                 $buffer[] = $tpl;
             }
             $remain = $arrWiki['data'][2];
@@ -114,7 +132,7 @@ class MdRender{
 
         return $html;
     }
-    public static function xmlQueryId(string $xml, string $id):string{
+    private function xmlQueryId(string $xml, string $id):string{
         try{
             $dom = simplexml_load_string($xml);
         }catch(\Exception $e){
@@ -168,7 +186,7 @@ class MdRender{
         }
         return $output;
     }
-    public static function xml2tpl(string $xml, $channelId=[],$mode='read',$format='react'):string{
+    private function xml2tpl(string $xml, $channelId=[]):string{
         /**
          * 解析xml
          * 获取模版参数
@@ -189,13 +207,12 @@ class MdRender{
         if(!$ok){
             return "<span>xml解析错误</span>";
         }
-/*
+        /*
         if(!$dom){
             Log::error($xml);
             return "<span>xml解析错误</span>";
         }
-*/
-
+         */
 
         //$tpl_list = $dom->xpath('//MdTpl');
         $tpl_list = $doc->getElementsByTagName('dfn');
@@ -244,9 +261,9 @@ class MdRender{
              */
             //TODO 判断$channelId里面的是否都是uuid
             $channelInfo = Channel::whereIn('uid',$channelId)->get();
-            $tplRender = new TemplateRender($props,$channelInfo,$mode,$format);
+            $tplRender = new TemplateRender($props,$channelInfo,$this->options['mode'],$this->options['format'],$this->options['debug']);
             $tplProps = $tplRender->render($tpl_name);
-            if($format==='react' && $tplProps){
+            if($this->options['format']==='react' && $tplProps){
                 $props = $doc->createAttribute("props");
                 $props->nodeValue = $tplProps['props'];
                 $tpl->appendChild($props);
@@ -260,7 +277,7 @@ class MdRender{
         }
         $html = $doc->saveHTML();
         $html = str_replace(['<dfn','</dfn>'],['<MdTpl','</MdTpl>'],$html);
-        switch ($format) {
+        switch ($this->options['format']) {
             case 'react':
                 return trim($html);
                 break;
@@ -291,57 +308,8 @@ class MdRender{
         }
     }
 
-    public static function render2($markdown,$channelId=[],$queryId=null,$mode='read',$channelType,$contentType="markdown",$format='react'){
-        if(empty($markdown)){
-            switch ($format) {
-                case 'react':
-                    return "<span></span>";
-                    break;
-                default:
-                    return "";
-                    break;
-            }
-        }
-        $wiki = MdRender::markdown2wiki($markdown,$channelType,$contentType);
-        $html = MdRender::wiki2xml($wiki,$channelId,$mode,$format);
-        if(!is_null($queryId)){
-            $html = MdRender::xmlQueryId($html, $queryId);
-        }
-        $html = MdRender::markdownToHtml($html,$format);
-        //$tpl = MdRender::xml2tpl($html,$channelId,$mode);
-
-        //后期处理
-        $output = '';
-        switch ($format) {
-            case 'react':
-                //生成可展开组件
-                $html = str_replace("<div/>","<div></div>",$html);
-                $pattern = '/<li><div>(.+?)<\/div><\/li>/';
-                $replacement = '<li><MdTpl name="toggle" tpl="toggle" props=""><div>$1</div></MdTpl></li>';
-                $output = preg_replace($pattern,$replacement,$html);
-                break;
-            case 'text':
-                $html = strip_tags($html);
-                $output = htmlspecialchars_decode($html,ENT_QUOTES);
-                //$output = html_entity_decode($html);
-                break;
-            case 'tex':
-                $html = strip_tags($html);
-                $output = htmlspecialchars_decode($html,ENT_QUOTES);
-                //$output = html_entity_decode($html);
-                break;
-            case 'unity':
-                $html = str_replace(['<strong>','</strong>','<em>','</em>'],['[%b%]','[%/b%]','[%i%]','[%/i%]'],$html);
-                $html = strip_tags($html);
-                $html = str_replace(['[%b%]','[%/b%]','[%i%]','[%/i%]'],['<b>','</b>','<i>','</i>'],$html);
-                $output = htmlspecialchars_decode($html,ENT_QUOTES);
-                break;
-        }
-        return $output;
-    }
-
 
-    public static function markdown2wiki(string $markdown,$channelType=null,$contentType=null): string{
+    private function markdown2wiki(string $markdown): string{
         //$markdown = mb_convert_encoding($markdown,'UTF-8','UTF-8');
         $markdown = iconv('UTF-8','UTF-8//IGNORE',$markdown);
         /**
@@ -349,8 +317,8 @@ class MdRender{
          * aaa=bbb\n
          * {{nissaya|aaa|bbb}}
          */
-        if($channelType==='nissaya'){
-            if($contentType === "json"){
+        if($this->options['channelType']==='nissaya'){
+            if($this->options['contentType'] === "json"){
                 $json = json_decode($markdown);
                 $nissayaWord = [];
                 foreach ($json as $word) {
@@ -369,7 +337,7 @@ class MdRender{
                     }
                 }
                 $markdown = implode('',$nissayaWord);
-            }else if($contentType === "markdown"){
+            }else if($this->options['contentType'] === "markdown"){
                 $lines = explode("\n",$markdown);
                 $newLines = array();
                 foreach ($lines as  $line) {
@@ -427,7 +395,7 @@ class MdRender{
         /*
 
         $html = MdRender::fixHtml($html);
-*/
+        */
 
         #替换术语
         $pattern = "/\[\[(.+?)\]\]/";
@@ -492,13 +460,13 @@ class MdRender{
         return $markdown;
     }
 
-    public static function markdownToHtml($markdown,$format='react'){
+    private function markdownToHtml($markdown){
         $markdown = str_replace('MdTpl','mdtpl',$markdown);
         $markdown = str_replace(['<param','</param>'],['<span','</span>'],$markdown);
 
         $html = Markdown::render($markdown);
-        if($format==='react'){
-            $html = MdRender::fixHtml($html);
+        if($this->options['format']==='react'){
+            $html = $this->fixHtml($html);
         }
         $html = str_replace('<hr>','<hr />',$html);
         //给H1-6 添加uuid
@@ -521,10 +489,68 @@ class MdRender{
         $html = str_replace('mdtpl','MdTpl',$html);
         return $html;
     }
-
+    private function  fixHtml($html) {
+        $doc = new \DOMDocument();
+        libxml_use_internal_errors(true);
+        $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
+        $doc->loadHTML('<span>'.$html.'</span>',LIBXML_NOERROR  | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
+        $fixed = $doc->saveHTML();
+        $fixed = mb_convert_encoding($fixed, "UTF-8", 'HTML-ENTITIES');
+        return $fixed;
+    }
     public static function init(){
         $GLOBALS["MdRenderStack"] = 0;
     }
+
+    public function convert($markdown,$channelId=[],$queryId=null){
+        if(empty($markdown)){
+            switch ($this->options['format']) {
+                case 'react':
+                    return "<span></span>";
+                    break;
+                default:
+                    return "";
+                    break;
+            }
+        }
+        $wiki = $this->markdown2wiki($markdown);
+        $html = $this->wiki2xml($wiki,$channelId);
+        if(!is_null($queryId)){
+            $html = $this->xmlQueryId($html, $queryId);
+        }
+        $html = $this->markdownToHtml($html);
+
+        //后期处理
+        $output = '';
+        switch ($this->options['format']) {
+            case 'react':
+                //生成可展开组件
+                $html = str_replace("<div/>","<div></div>",$html);
+                $pattern = '/<li><div>(.+?)<\/div><\/li>/';
+                $replacement = '<li><MdTpl name="toggle" tpl="toggle" props=""><div>$1</div></MdTpl></li>';
+                $output = preg_replace($pattern,$replacement,$html);
+                break;
+            case 'text':
+                $html = strip_tags($html);
+                $output = htmlspecialchars_decode($html,ENT_QUOTES);
+                //$output = html_entity_decode($html);
+                break;
+            case 'tex':
+                $html = strip_tags($html);
+                $output = htmlspecialchars_decode($html,ENT_QUOTES);
+                //$output = html_entity_decode($html);
+                break;
+            case 'unity':
+                $html = str_replace(['<strong>','</strong>','<em>','</em>'],['[%b%]','[%/b%]','[%i%]','[%/i%]'],$html);
+                $html = strip_tags($html);
+                $html = str_replace(['[%b%]','[%/b%]','[%i%]','[%/i%]'],['<b>','</b>','<i>','</i>'],$html);
+                $output = htmlspecialchars_decode($html,ENT_QUOTES);
+                break;
+        }
+        return $output;
+    }
+
+
     /**
      * string[] $channelId
      */
@@ -535,7 +561,12 @@ class MdRender{
             $GLOBALS["MdRenderStack"] = 1;
         }
         if($GLOBALS["MdRenderStack"]<3){
-            $output  = MdRender::render2($markdown,$channelId,$queryId,$mode,$channelType,$contentType,$format);
+            $mdRender = new MdRender(['mode'=>$mode,
+                                    'channelType'=>$channelType,
+                                    'contentType'=>$contentType,
+                                    'format'=>$format]);
+
+            $output  = $mdRender->convert($markdown,$channelId,$queryId);
         }else{
             $output  = $markdown;
         }
@@ -543,13 +574,5 @@ class MdRender{
         return $output;
     }
 
-    public static function  fixHtml($html) {
-        $doc = new \DOMDocument();
-        libxml_use_internal_errors(true);
-        $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
-        $doc->loadHTML('<span>'.$html.'</span>',LIBXML_NOERROR  | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
-        $fixed = $doc->saveHTML();
-        $fixed = mb_convert_encoding($fixed, "UTF-8", 'HTML-ENTITIES');
-        return $fixed;
-    }
+
 }

+ 19 - 5
app/Http/Api/TemplateRender.php

@@ -14,6 +14,7 @@ class TemplateRender{
     protected $param = [];
     protected $mode = "read";
     protected $channel_id = [];
+    protected $debug = [];
     protected $format = 'react';
 
 
@@ -23,7 +24,7 @@ class TemplateRender{
      * string $format  'react' | 'text' | 'tex' | 'unity'
      * @return void
      */
-    public function __construct($param, $channelInfo, $mode,$format='react')
+    public function __construct($param, $channelInfo, $mode,$format='react',$debug=[])
     {
         $this->param = $param;
         foreach ($channelInfo as $value) {
@@ -32,8 +33,18 @@ class TemplateRender{
         $this->channelInfo = $channelInfo;
         $this->mode = $mode;
         $this->format = $format;
+        $this->debug = $debug;
+    }
+    private function info($message,$debug){
+        if(in_array($debug,$this->debug)){
+            Log::info($message);
+        }
+    }
+    private function error($message,$debug){
+        if(in_array($debug,$this->debug)){
+            Log::error($message);
+        }
     }
-
     public function render($tpl_name){
         switch ($tpl_name) {
             case 'term':
@@ -81,7 +92,8 @@ class TemplateRender{
         }else{
             $langFamily = 'zh';
         }
-        //先查属于这个channel 的
+        $this->info("term:{$word} 先查属于这个channel 的",'term');
+        $this->info('channel id'.$channelId,'term');
         $tplParam = DhammaTerm::where("word",$word)
                               ->where('channal',$channelId)
                               ->orderBy('updated_at','desc')
@@ -93,6 +105,7 @@ class TemplateRender{
              * 完全匹配的优先
              * 语族匹配也行
              */
+            $this->info("没有-再查这个studio的",'term');
             $termsInStudio = DhammaTerm::where("word",$word)
                                   ->where('owner',$channelInfo->owner_uid)
                                   ->orderBy('updated_at','desc')
@@ -123,14 +136,15 @@ class TemplateRender{
             }
         }
         if(!$tplParam){
-            //没有,再查社区
-
+            $this->info("没有,再查社区",'term');
             $community_channel = ChannelApi::getSysChannel("_community_term_zh-hans_");
             $tplParam = DhammaTerm::where("word",$word)
                                   ->where('channal',$community_channel)
                                   ->first();
             if($tplParam){
                 $isCommunity = true;
+            }else{
+                $this->info("查社区没有",'term');
             }
         }
         $output = [

+ 11 - 2
app/Http/Controllers/CorpusController.php

@@ -65,6 +65,8 @@ class CorpusController extends Controller
 
     protected $userUuid=null;
 
+    protected $debug=[];
+
     public function __construct()
     {
 
@@ -240,6 +242,9 @@ class CorpusController extends Controller
      */
     public function showPara(Request $request)
     {
+        if($request->has('debug')){
+            $this->debug = explode(',',$request->get('debug'));
+        }
         $user = AuthApi::current($request);
         if($user){
             $this->userUuid = $user['user_uid'];
@@ -340,6 +345,9 @@ class CorpusController extends Controller
      */
     public function showChapter(Request $request, string $id)
     {
+        if($request->has('debug')){
+            $this->debug = explode(',',$request->get('debug'));
+        }
         $user = AuthApi::current($request);
         if($user){
             $this->userUuid = $user['user_uid'];
@@ -667,13 +675,14 @@ class CorpusController extends Controller
                          * 包涵术语的不用cache
                          */
                             if(strpos($row->content,'[[')===false){
-                                $newSent['html'] = MdRender::render($row->content,[$row->channel_uid]);
-                            }else{
                                 $newSent['html'] = Cache::remember("/sent/{$channelId}/{$currSentId}",
                                                 env('CACHE_EXPIRE',3600*24),
                                                 function() use($row){
                                                     return MdRender::render($row->content,[$row->channel_uid]);
                                                 });
+                            }else{
+                                $mdRender = new MdRender(['debug'=>$this->debug]);
+                                $newSent['html'] = $mdRender->convert($row->content,[$row->channel_uid]);
                             }
                             break;
                     }

+ 2 - 5
app/Http/Resources/ArticleResource.php

@@ -165,11 +165,8 @@ class ArticleResource extends JsonResource
                     $channels = [$channelId];
                 }
             }
-            if($request->has('mode')){
-                $mode = $request->get('mode');
-            }else{
-                $mode = 'read';
-            }
+
+            $mode = $request->get('mode','read');
             $data["html"] = MdRender::render($this->content,$channels,$query_id,$mode);
             if(empty($this->summary)){
                 $data["_summary"] = MdRender::render($this->content,$channels,$query_id,$mode,'translation','markdown','text');

+ 18 - 8
app/Http/Resources/TermResource.php

@@ -38,17 +38,27 @@ class TermResource extends JsonResource
             "updated_at"=> $this->updated_at,
         ];
 
-        if(!empty($this->channal)){
-            $channelId = $this->channal;
-            $data["channel"] = ChannelApi::getById($this->channal);
+
+        if($request->has('channel')){
+            $channels = explode('_',$request->get('channel')) ;
         }else{
-            $channelId = ChannelApi::getSysChannel('_community_translation_'.$this->language.'_');
-            if(empty($channelId)){
-                $channelId = ChannelApi::getSysChannel('_community_translation_zh-hans_');
+            if(!empty($this->channal)){
+                $channelId = $this->channal;
+                $data["channel"] = ChannelApi::getById($this->channal);
+            }else{
+                $channelId = ChannelApi::getSysChannel('_community_translation_'.$this->language.'_');
+                if(empty($channelId)){
+                    $channelId = ChannelApi::getSysChannel('_community_translation_zh-hans_');
+                }
+            }
+            if(!empty($channelId)){
+                $channels = [$channelId];
+            }else{
+               $channels = [];
             }
         }
-        if(!empty($this->note) && !empty($channelId)){
-            $data["html"] = MdRender::render($this->note,[$channelId],null,'read');
+        if(!empty($this->note)){
+            $data["html"] = MdRender::render($this->note,$channels,null,$request->get('mode','read'));
         }
         $user = AuthApi::current($request);
         if(!$user){