Explorar o código

导出到sqlite

visuddhinanda %!s(int64=2) %!d(string=hai) anos
pai
achega
d473d8f125

+ 28 - 16
app/Console/Commands/ExportChannel.php

@@ -41,28 +41,40 @@ class ExportChannel extends Command
      */
     public function handle()
     {
-        $filename = "public/export/offline/channel.csv";
-        Storage::disk('local')->put($filename, "");
-        $file = fopen(storage_path("app/{$filename}"),"w");
-        fputcsv($file,['id','name','type','language','summary','owner_id','setting','created_at']);
+        $exportFile = storage_path('app/public/export/offline/sentence-'.date("Y-m-d").'.db3');
+        $dbh = new \PDO('sqlite:'.$exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
+        $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
+        $dbh->beginTransaction();
+
+        $query = "INSERT INTO channel ( id , name , type , language ,
+                                    summary , owner_id , setting,created_at )
+                                    VALUES ( ? , ? , ? , ? , ? , ? , ? , ?  )";
+        try{
+            $stmt = $dbh->prepare($query);
+        }catch(PDOException $e){
+            Log::info($e);
+            return 1;
+        }
+
         $bar = $this->output->createProgressBar(Channel::where('status',30)->count());
         foreach (Channel::where('status',30)
                 ->select(['uid','name','type','lang',
                           'summary','owner_uid','setting','created_at'])
-                          ->cursor() as $chapter) {
-            fputcsv($file,[
-                            $chapter->uid,
-                            $chapter->name,
-                            $chapter->type,
-                            $chapter->lang,
-                            $chapter->summary,
-                            $chapter->owner_uid,
-                            $chapter->setting,
-                            $chapter->created_at,
-                            ]);
+                          ->cursor() as $row) {
+                $currData = array(
+                            $row->uid,
+                            $row->name,
+                            $row->type,
+                            $row->lang,
+                            $row->summary,
+                            $row->owner_uid,
+                            $row->setting,
+                            $row->created_at,
+                            );
+            $stmt->execute($currData);
             $bar->advance();
         }
-        fclose($file);
+        $dbh->commit();
         $bar->finish();
         return 0;
     }

+ 29 - 16
app/Console/Commands/ExportChapterIndex.php

@@ -39,25 +39,38 @@ class ExportChapterIndex extends Command
      */
     public function handle()
     {
-        $filename = "public/export/offline/chapter.csv";
-        Storage::disk('local')->put($filename, "");
-        $file = fopen(storage_path("app/{$filename}"),"w");
-        fputcsv($file,['id','book','paragraph','language','title','channel_id','progress','updated_at']);
+        $exportFile = storage_path('app/public/export/offline/sentence-'.date("Y-m-d").'.db3');
+        $dbh = new \PDO('sqlite:'.$exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
+        $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
+        $dbh->beginTransaction();
+
+        $query = "INSERT INTO chapter ( id , book , paragraph,
+                                    language , title , channel_id , progress,updated_at  )
+                                    VALUES ( ? , ? , ? , ? , ? , ? , ? , ?  )";
+        try{
+            $stmt = $dbh->prepare($query);
+        }catch(PDOException $e){
+            Log::info($e);
+            return 1;
+        }
+
         $bar = $this->output->createProgressBar(ProgressChapter::count());
-        foreach (ProgressChapter::select(['uid','book','para','lang','title','channel_id','progress','updated_at'])->cursor() as $chapter) {
-            fputcsv($file,[
-                            $chapter->uid,
-                            $chapter->book,
-                            $chapter->para,
-                            $chapter->lang,
-                            $chapter->title,
-                            $chapter->channel_id,
-                            $chapter->progress,
-                            $chapter->updated_at,
-                            ]);
+        foreach (ProgressChapter::select(['uid','book','para',
+                    'lang','title','channel_id','progress','updated_at'])->cursor() as $row) {
+            $currData = array(
+                            $row->uid,
+                            $row->book,
+                            $row->para,
+                            $row->lang,
+                            $row->title,
+                            $row->channel_id,
+                            $row->progress,
+                            $row->updated_at,
+                            );
+            $stmt->execute($currData);
             $bar->advance();
         }
-        fclose($file);
+        $dbh->commit();
         $bar->finish();
         return 0;
     }

+ 15 - 2
app/Console/Commands/ExportOffline.php

@@ -37,19 +37,32 @@ class ExportOffline extends Command
      */
     public function handle()
     {
+        //建表
+        $this->info('create db');
+        $this->call('export:create.db');
         //导出channel
+        $this->info('channel');
         $this->call('export:channel');
-        //导出channel
+        //tag
+        $this->info('tag');
         $this->call('export:tag');
         $this->call('export:tag.map');
+        $this->info('pali text');
         $this->call('export:pali.text');
         //导出章节索引
+        $this->info('chapter');
         $this->call('export:chapter.index');
         //导出译文
+        $this->info('sentence');
         $this->call('export:sentence');
         //导出原文
         $this->call('export:sentence',['--type'=>'original']);
-        shell_exec("XZ_OPT=-9 tar jcvf ".storage_path("app/public/export/offline.tar.xz")." ".storage_path("app/public/export/offline"));
+
+        $this->info('zip');
+        $exportFile = storage_path('app/public/export/offline/sentence-'.date("Y-m-d").'.db3');
+        shell_exec("tar jcf ".
+                    storage_path("app/public/export/offline-".date("Y-m-d").".tar.xz")." ".
+                    $exportFile);
         return 0;
     }
 }

+ 22 - 12
app/Console/Commands/ExportPalitext.php

@@ -39,31 +39,41 @@ class ExportPalitext extends Command
      */
     public function handle()
     {
-        $filename = "public/export/offline/pali_text.csv";
-        Storage::disk('local')->put($filename, "");
-        $file = fopen(storage_path("app/{$filename}"),"w");
-        fputcsv($file,['id','book','paragraph','level','toc','length','chapter_len','next_chapter','prev_chapter','parent','chapter_strlen']);
+        $exportFile = storage_path('app/public/export/offline/sentence-'.date("Y-m-d").'.db3');
+        $dbh = new \PDO('sqlite:'.$exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
+        $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
+        $dbh->beginTransaction();
+
+        $query = "INSERT INTO pali_text ( id , book , paragraph, level, toc,
+                                    chapter_len , parent   )
+                                    VALUES ( ? , ? , ? , ? , ? , ? , ? )";
+        try{
+            $stmt = $dbh->prepare($query);
+        }catch(PDOException $e){
+            Log::info($e);
+            return 1;
+        }
+
         $bar = $this->output->createProgressBar(PaliText::count());
-        foreach (PaliText::select(['uid','book','paragraph','level','toc','lenght','chapter_len','next_chapter','prev_chapter','parent','chapter_strlen'])
+        foreach (PaliText::select(['uid','book','paragraph',
+                    'level','toc','lenght','chapter_len',
+                    'next_chapter','prev_chapter','parent','chapter_strlen'])
                     ->orderBy('book')
                     ->orderBy('paragraph')
                     ->cursor() as $chapter) {
-            fputcsv($file,[
+            $currData = array(
                             $chapter->uid,
                             $chapter->book,
                             $chapter->paragraph,
                             $chapter->level,
                             $chapter->toc,
-                            $chapter->lenght,
                             $chapter->chapter_len,
-                            $chapter->next_chapter,
-                            $chapter->prev_chapter,
                             $chapter->parent,
-                            $chapter->chapter_strlen,
-                            ]);
+                            );
+            $stmt->execute($currData);
             $bar->advance();
         }
-        fclose($file);
+        $dbh->commit();
         $bar->finish();
 
         return 0;

+ 39 - 21
app/Console/Commands/ExportSentence.php

@@ -7,6 +7,7 @@ use Illuminate\Support\Facades\Storage;
 use App\Models\Sentence;
 use App\Models\Channel;
 use App\Http\Api\ChannelApi;
+use Illuminate\Support\Facades\Log;
 
 class ExportSentence extends Command
 {
@@ -63,31 +64,48 @@ class ExportSentence extends Command
                 }
             }
         }
+
+
+        $exportFile = storage_path('app/public/export/offline/sentence-'.date("Y-m-d").'.db3');
+        $dbh = new \PDO('sqlite:'.$exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
+        $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
+        $dbh->beginTransaction();
+
+        if($channel_type === "original"){
+            $table = 'sentence';
+        }else{
+            $table = 'sentence_translation';
+        }
+
+        $query = "INSERT INTO {$table} ( book , paragraph ,
+                                    word_start , word_end , content , channel_id  )
+                                    VALUES ( ? , ? , ? , ? , ? , ? )";
+        try{
+            $stmt = $dbh->prepare($query);
+        }catch(PDOException $e){
+            Log::info($e);
+            return 1;
+        }
+
         $db = Sentence::whereIn('channel_uid',$channels);
-        $file_name = "public/export/offline/sentence_{$file_suf}.csv";
-        Storage::disk('local')->put($file_name, "");
-        $file = fopen(storage_path("app/{$file_name}"),"w");
-        fputcsv($file,['id','book','paragraph','word_start','word_end','content','content_type','html','channel_id','editor_id','language','updated_at']);
         $bar = $this->output->createProgressBar($db->count());
-        foreach ($db->select(['uid','book_id','paragraph','word_start','word_end','content','content_type','channel_uid','editor_uid','language','updated_at'])->cursor() as $chapter) {
-            $content = str_replace("\n","<br />",$chapter->content);
-            fputcsv($file,[
-                            $chapter->uid,
-                            $chapter->book_id,
-                            $chapter->paragraph,
-                            $chapter->word_start,
-                            $chapter->word_end,
-                            $content,
-                            $chapter->content_type,
-                            $content,
-                            $chapter->channel_uid,
-                            $chapter->editor_uid,
-                            $chapter->language,
-                            $chapter->updated_at,
-                            ]);
+        $srcDb = $db->select(['uid','book_id','paragraph',
+                                'word_start','word_end',
+                                'content','content_type','channel_uid',
+                                'editor_uid','language','updated_at'])->cursor();
+        foreach ($srcDb as $sent) {
+            $currData = array(
+                    $sent->book_id,
+                    $sent->paragraph,
+                    $sent->word_start,
+                    $sent->word_end,
+                    $sent->content,
+                    $sent->channel_uid,
+                );
+            $stmt->execute($currData);
             $bar->advance();
         }
-        fclose($file);
+        $dbh->commit();
         $bar->finish();
         return 0;
     }

+ 25 - 13
app/Console/Commands/ExportTag.php

@@ -39,22 +39,34 @@ class ExportTag extends Command
      */
     public function handle()
     {
-        $filename = "public/export/offline/tag.csv";
-        Storage::disk('local')->put($filename, "");
-        $file = fopen(storage_path("app/{$filename}"),"w");
-        fputcsv($file,['id','name','description','color','owner_id']);
+        $exportFile = storage_path('app/public/export/offline/sentence-'.date("Y-m-d").'.db3');
+        $dbh = new \PDO('sqlite:'.$exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
+        $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
+        $dbh->beginTransaction();
+
+        $query = "INSERT INTO tag ( id , name ,
+                                    description , color , owner_id  )
+                                    VALUES ( ? , ? , ? , ? , ?  )";
+        try{
+            $stmt = $dbh->prepare($query);
+        }catch(PDOException $e){
+            Log::info($e);
+            return 1;
+        }
+
         $bar = $this->output->createProgressBar(Tag::count());
-        foreach (Tag::select(['id','name','description','color','owner_id'])->cursor() as $chapter) {
-            fputcsv($file,[
-                            $chapter->id,
-                            $chapter->name,
-                            $chapter->description,
-                            $chapter->color,
-                            $chapter->owner_id,
-                            ]);
+        foreach (Tag::select(['id','name','description','color','owner_id'])->cursor() as $row) {
+            $currData = array(
+                $row->id,
+                $row->name,
+                $row->description,
+                $row->color,
+                $row->owner_id,
+            );
+            $stmt->execute($currData);
             $bar->advance();
         }
-        fclose($file);
+        $dbh->commit();
         $bar->finish();
         return 0;
     }

+ 22 - 12
app/Console/Commands/ExportTagmap.php

@@ -5,6 +5,7 @@ namespace App\Console\Commands;
 use Illuminate\Console\Command;
 use Illuminate\Support\Facades\Storage;
 use App\Models\TagMap;
+
 class ExportTagmap extends Command
 {
     /**
@@ -38,21 +39,30 @@ class ExportTagmap extends Command
      */
     public function handle()
     {
-        $filename = "public/export/offline/tag_map.csv";
-        Storage::disk('local')->put($filename, "");
-        $file = fopen(storage_path("app/{$filename}"),"w");
-        fputcsv($file,['id','table_name','anchor_id','tag_id']);
+        $exportFile = storage_path('app/public/export/offline/sentence-'.date("Y-m-d").'.db3');
+        $dbh = new \PDO('sqlite:'.$exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
+        $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
+        $dbh->beginTransaction();
+
+        $query = "INSERT INTO tag_map ( anchor_id , tag_id )
+                                    VALUES ( ? , ? )";
+        try{
+            $stmt = $dbh->prepare($query);
+        }catch(PDOException $e){
+            Log::info($e);
+            return 1;
+        }
+
         $bar = $this->output->createProgressBar(TagMap::count());
-        foreach (TagMap::select(['id','table_name','anchor_id','tag_id'])->cursor() as $chapter) {
-            fputcsv($file,[
-                            $chapter->id,
-                            $chapter->table_name,
-                            $chapter->anchor_id,
-                            $chapter->tag_id,
-                            ]);
+        foreach (TagMap::select(['id','table_name','anchor_id','tag_id'])->cursor() as $row) {
+            $currData = array(
+                            $row->anchor_id,
+                            $row->tag_id,
+                            );
+            $stmt->execute($currData);
             $bar->advance();
         }
-        fclose($file);
+        $dbh->commit();
         $bar->finish();
         return 0;
     }