visuddhinanda 3 лет назад
Родитель
Сommit
f468ff99ec

+ 78 - 0
app/Console/Commands/UpgradeDictDefaultMeaning.php

@@ -0,0 +1,78 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Models\UserDict;
+use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\Log;
+
+class UpgradeDictDefaultMeaning extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'upgrade:dict.default.meaning';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        # 获取字典中所有的语言
+        $langInDict = UserDict::select('language')->groupBy('language')->get();
+        $languages = [];
+        foreach ($langInDict as $lang) {
+            if(!empty($lang["language"])){
+                $languages[] = $lang["language"];
+            }
+        }
+		print_r($languages);
+        foreach ($languages as $thisLang) {
+            Log::info("running $thisLang");
+            $bar = $this->output->createProgressBar(UserDict::where('source','_PAPER_')
+                                                        ->where('language',$thisLang)->count());
+            foreach (UserDict::where('source','_PAPER_')
+                                ->where('language',$thisLang)
+                                ->select('word','note')
+                                ->cursor() as $word) {
+                Cache::put("dict_first_mean/{$thisLang}/{$word['word']}", mb_substr($word['note'],0,50,"UTF-8") ,24*3600);
+                $bar->advance();
+            }
+            $bar->finish();
+        }
+        Log::info("running com");
+        $bar = $this->output->createProgressBar(UserDict::where('source','_PAPER_')->count());
+        foreach (UserDict::where('source','_PAPER_')
+                            ->select('word','note')
+                            ->cursor() as $word) {
+            $key = "dict_first_mean/com/{$word['word']}";
+            Cache::put($key, mb_substr($word['note'],0,50,"UTF-8") ,24*3600);
+            $bar->advance();
+        }
+        $bar->finish();
+
+        return 0;
+    }
+}

+ 56 - 0
app/Console/Commands/UpgradeDictVocabulary.php

@@ -0,0 +1,56 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Models\Vocabulary;
+use App\Models\UserDict;
+use App\Tools\Tools;
+
+class UpgradeDictVocabulary extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'upgrade:dict.vocabulary';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $words = UserDict::where('source','_PAPER_')->selectRaw('word,count(*)')->groupBy('word')->cursor();
+
+		$bar = $this->output->createProgressBar(200000);
+		foreach ($words as $word) {
+			$update = Vocabulary::where('word',$word->word)->updateOrCreate(
+                ['count' => $word->count, 'flag' => 1],
+                ['word' => $word->word, 'word_en'=>Tools::getWordEn($word->word)]
+            );
+            $bar->advance();
+		}
+        $bar->finish();
+        return 0;
+    }
+}

+ 75 - 0
app/Http/Controllers/ArticleMapController.php

@@ -0,0 +1,75 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Models\ArticleCollection;
+use Illuminate\Http\Request;
+use App\Http\Resources\ArticleMapResource;
+
+class ArticleMapController extends Controller
+{
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index(Request $request)
+    {
+        //
+        switch ($request->get('view')) {
+            case 'anthology':
+                $table = ArticleCollection::where('collect_id',$request->get('id'));
+                break;
+            case 'article':
+                $table = ArticleCollection::where('article_id',$request->get('id'));
+                break;
+        }
+        $result = $table->select(['id','collect_id','article_id','level','title','children'])->orderBy('id')->get();
+        return $this->ok(["rows"=>ArticleMapResource::collection($result),"count"=>count($result)]);
+    }
+
+    /**
+     * Store a newly created resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return \Illuminate\Http\Response
+     */
+    public function store(Request $request)
+    {
+        //
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  \App\Models\ArticleCollection  $articleCollection
+     * @return \Illuminate\Http\Response
+     */
+    public function show(ArticleCollection $articleCollection)
+    {
+        //
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \App\Models\ArticleCollection  $articleCollection
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, ArticleCollection $articleCollection)
+    {
+        //
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  \App\Models\ArticleCollection  $articleCollection
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy(ArticleCollection $articleCollection)
+    {
+        //
+    }
+}

+ 19 - 0
app/Http/Resources/ArticleMapResource.php

@@ -0,0 +1,19 @@
+<?php
+
+namespace App\Http\Resources;
+
+use Illuminate\Http\Resources\Json\JsonResource;
+
+class ArticleMapResource extends JsonResource
+{
+    /**
+     * Transform the resource into an array.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
+     */
+    public function toArray($request)
+    {
+        return parent::toArray($request);
+    }
+}

+ 12 - 0
app/Tools/Tools.php

@@ -0,0 +1,12 @@
+<?php
+namespace App\Tools;
+
+class Tools{
+
+    public static function getWordEn($strIn)
+        {
+            $out = str_replace(["ā","ī","ū","ṅ","ñ","ṭ","ḍ","ṇ","ḷ","ṃ"],
+                            ["a","i","u","n","n","t","d","n","l","m"], $strIn);
+            return ($out);
+        }
+}

+ 29 - 0
tests/Feature/ArticleMapTest.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace Tests\Feature;
+
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Foundation\Testing\WithFaker;
+use Tests\TestCase;
+use App\Models\Collection;
+
+
+class ArticleMapTest extends TestCase
+{
+    /**
+     * A basic feature test example.
+     *
+     * @return void
+     */
+    public function test_index()
+    {
+        $collection = Collection::first();
+        $response = $this->get('/api/v2/article-map?view=anthology&id='.$collection->collect_id);
+
+        $response->assertStatus(200);
+
+        $response = $this->get('/api/v2/article-map?view=article&id='.$collection->article_id);
+
+        $response->assertStatus(200);
+    }
+}