Forráskód Böngészése

:sparkles: dhamma term migrate to pg

visuddhinanda@gmail.com 4 éve
szülő
commit
0b578603bd

+ 11 - 0
app/Models/DhammaTerm.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class DhammaTerm extends Model
+{
+    use HasFactory;
+}

+ 50 - 0
database/migrations/2022_02_02_103531_create_dhamma_terms_table.php

@@ -0,0 +1,50 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateDhammaTermsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('dhamma_terms', function (Blueprint $table) {
+			#使用雪花id
+            $table->bigInteger('id')->primary();
+            $table->string('guid',36);
+
+            $table->string('word',1024)->index();
+            $table->string('word_en',1024)->index();
+            $table->string('meaning',1024)->index();
+            $table->string('other_meaning',1024)->nullable();
+            $table->text('note',1024)->nullable();
+            $table->string('tag',1024)->nullable();
+            $table->string('channal',36)->index()->nullable();
+            $table->string('language',16)->default('zh-hans');
+            $table->string('owner',36)->index();
+            $table->bigInteger('editor_id')->index();
+
+            $table->bigInteger('create_time');
+            $table->bigInteger('modify_time');
+
+			$table->timestamp('created_at')->useCurrent()->index();
+			$table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate()->index();
+			$table->timestamp('deleted_at')->nullable();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('dhamma_terms');
+    }
+}

+ 3 - 3
public/app/config.table.php

@@ -292,7 +292,7 @@ define("_SQLITE_DB_TERM_", "sqlite:" . __DIR__ . "/../tmp/user/dhammaterm.db");
 define("_SQLITE_TABLE_TERM_", "term");
 
 define("_PG_DB_TERM_", _PDO_DB_DSN_);
-define("_PG_TABLE_TERM_", "term");
+define("_PG_TABLE_TERM_", "dhamma_terms");
 
 # 协作
 define("_SQLITE_DB_USER_SHARE_", "sqlite:" . __DIR__ . "/../tmp/user/share.db3");
@@ -643,8 +643,8 @@ define("_TABLE_COLLECTION_", _PG_TABLE_COLLECTION_);
 define("_TABLE_ARTICLE_COLLECTION_", _PG_TABLE_ARTICLE_COLLECTION_);
 
 # 术语
-define("_FILE_DB_TERM_", _SQLITE_DB_TERM_);
-define("_TABLE_TERM_", _SQLITE_TABLE_TERM_);
+define("_FILE_DB_TERM_", _PG_DB_TERM_);
+define("_TABLE_TERM_", _PG_TABLE_TERM_);
 
 # 协作
 define("_FILE_DB_USER_SHARE_", _SQLITE_DB_USER_SHARE_);

+ 3 - 3
public/app/term/get_term_index.php

@@ -23,15 +23,15 @@ if (isset($_POST["lang"])) {
 
 if (isset($_POST["word"])) {
     $word = $_POST["word"];
-    $query = "select word,meaning,language,owner,count(*) as co from term where word=? and language=? group by word,meaning order by co DESC";
+    $query = "SELECT word,meaning,language,owner,count(*) as co from "._TABLE_TERM_." where word=? and language=? group by word,meaning order by co DESC";
     $output["data"] = PDO_FetchAll($query, array($word, $lang));
 } else {
-    $query = "select * from (select word,meaning,language,owner,count(*) as co from term where language=? group by word,meaning order by co DESC) where 1 group by word";
+    $query = "SELECT * from (SELECT word,meaning,language,owner,count(*) as co from "._TABLE_TERM_." where language=? group by word,meaning order by co DESC)  group by word";
     $output["data"] = PDO_FetchAll($query, array($lang));
     $pos = mb_strpos($lang, "-", 0, "UTF-8");
     if ($pos) {
         $lang_family = mb_substr($lang, 0, $pos, "UTF-8");
-        $query = "select * from (select word,meaning,language,owner,count(*) as co from term where language like ? group by word,meaning order by co DESC) where 1 group by word";
+        $query = "SELECT * from (SELECT word,meaning,language,owner,count(*) as co from "._TABLE_TERM_." where language like ? group by word,meaning order by co DESC)  group by word";
         $otherlang = PDO_FetchAll($query, array($lang_family . "%"));
         foreach ($otherlang as $key => $value) {
             $output["data"][] = $value;

+ 4 - 4
public/app/term/my_dict_list.php

@@ -78,9 +78,9 @@ $iOnePage = 300;
 
 PDO_Connect(_FILE_DB_TERM_);
 
-$query = "select count(*) as co  from term where owner= ? ";
+$query = "SELECT count(*) as co  from "._TABLE_TERM_." where owner= ? ";
 
-$allWord = PDO_FetchOne($query, array($_COOKIE["userid"]));
+$allWord = PDO_FetchOne($query, array($_COOKIE["user_uid"]));
 $iCountWords = $allWord;
 
 if ($iCountWords == 0) {
@@ -93,8 +93,8 @@ if ($iCountWords == 0) {
     }
     $begin = $iCurrPage * $iOnePage;
 
-    $query = "select *  from term where owner= ? ";
-    $allWords = PDO_FetchAll($query, array($_COOKIE["userid"]));
+    $query = "SELECT *  from "._TABLE_TERM_." where owner= ? ";
+    $allWords = PDO_FetchAll($query, array($_COOKIE["user_uid"]));
 
     echo '<div id="setting_user_dict_nav">';
 

+ 1 - 1
public/app/term/new.php

@@ -9,7 +9,7 @@ require_once '../ucenter/function.php';
 PDO_Connect(_FILE_DB_TERM_);
 $userInfo = new UserInfo();
 
-$query = "select word,meaning , owner from term where 1  order by create_time DESC limit 0,4";
+$query = "SELECT word,meaning , owner from "._TABLE_TERM_."  order by create_time DESC limit 4";
 $Fetch = PDO_FetchAll($query);
 
 foreach ($Fetch as $row) {

+ 40 - 18
public/app/term/term.php

@@ -5,6 +5,8 @@ require_once "../config.php";
 require_once "../public/_pdo.php";
 require_once '../public/load_lang.php';
 require_once '../public/function.php';
+require_once __DIR__."/../public/snowflakeid.php";
+$snowflake = new SnowFlakeId();
 
 //is login
 if (isset($_COOKIE["username"])) {
@@ -41,7 +43,7 @@ if (isset($_GET["username"])) {
 }
 
 global $PDO;
-PDO_Connect("" . _FILE_DB_TERM_);
+PDO_Connect( _FILE_DB_TERM_);
 switch ($op) {
     case "pre": //预查询
         {
@@ -49,10 +51,10 @@ switch ($op) {
 				echo json_encode(array(), JSON_UNESCAPED_UNICODE);
             	break;
 			}
-            $query = "SELECT word,meaning from term where \"word_en\" like " . $PDO->quote($word . '%') . " OR \"word\" like " . $PDO->quote($word . '%') . " group by word limit 0,10";
+            $query = "SELECT word,meaning from "._TABLE_TERM_." where \"word_en\" like " . $PDO->quote($word . '%') . " OR \"word\" like " . $PDO->quote($word . '%') . " group by word limit 0,10";
             $Fetch = PDO_FetchAll($query);
             if (count($Fetch) < 3) {
-                $query = "SELECT word,meaning from term where \"word_en\" like " . $PDO->quote('%' . $word . '%') . " OR \"word\" like " . $PDO->quote('%' . $word . '%') . " group by word limit 0,10";
+                $query = "SELECT word,meaning from "._TABLE_TERM_." where \"word_en\" like " . $PDO->quote('%' . $word . '%') . " OR \"word\" like " . $PDO->quote('%' . $word . '%') . " group by word limit 0,10";
                 $Fetch2 = PDO_FetchAll($query);
                 //去掉重复的
                 foreach ($Fetch2 as $onerow) {
@@ -68,12 +70,12 @@ switch ($op) {
                     }
                 }
                 if (count($Fetch) < 8) {
-                    $query = "SELECT word,meaning from term where \"meaning\" like " . $PDO->quote($word . '%') . " OR \"other_meaning\" like " . $PDO->quote($word . '%') . " group by word limit 0,10";
+                    $query = "SELECT word,meaning from "._TABLE_TERM_." where \"meaning\" like " . $PDO->quote($word . '%') . " OR \"other_meaning\" like " . $PDO->quote($word . '%') . " group by word limit 0,10";
                     $Fetch3 = PDO_FetchAll($query);
 
                     $Fetch = array_merge($Fetch, $Fetch3);
                     if (count($Fetch) < 8) {
-                        $query = "SELECT word,meaning from term where \"meaning\" like " . $PDO->quote('%' . $word . '%') . " OR \"other_meaning\" like " . $PDO->quote('%' . $word . '%') . " group by word limit 0,10";
+                        $query = "SELECT word,meaning from "._TABLE_TERM_." where \"meaning\" like " . $PDO->quote('%' . $word . '%') . " OR \"other_meaning\" like " . $PDO->quote('%' . $word . '%') . " group by word limit 0,10";
                         $Fetch4 = PDO_FetchAll($query);
                         //去掉重复的
                         foreach ($Fetch4 as $onerow) {
@@ -96,7 +98,7 @@ switch ($op) {
         }
     case "my":
         {
-            $query = "select guid,word,meaning,other_meaning,language from term  where owner= ? ";
+            $query = "select guid,word,meaning,other_meaning,language from "._TABLE_TERM_."  where owner= ? ";
             $Fetch = PDO_FetchAll($query, array($_COOKIE["userid"]));
             $iFetch = count($Fetch);
             if ($iFetch > 0) {
@@ -108,7 +110,7 @@ switch ($op) {
         }
     case "allpali":
         {
-            $query = "select word from term  where 1 group by word";
+            $query = "select word from "._TABLE_TERM_."  where 1 group by word";
             $Fetch = PDO_FetchAll($query);
             $iFetch = count($Fetch);
             if ($iFetch > 0) {
@@ -118,7 +120,7 @@ switch ($op) {
         }
     case "allmean":
         {
-            $query = "select meaning from term  where \"word\" = " . $PDO->quote($word) . " group by meaning";
+            $query = "select meaning from "._TABLE_TERM_."  where \"word\" = " . $PDO->quote($word) . " group by meaning";
             $Fetch = PDO_FetchAll($query);
             foreach ($Fetch as $one) {
                 echo "<a>" . $one["meaning"] . "</a> ";
@@ -130,7 +132,7 @@ switch ($op) {
         {
             if (isset($_GET["id"])) {
                 $id = $_GET["id"];
-                $query = "select * from term  where \"guid\" = " . $PDO->quote($id);
+                $query = "select * from "._TABLE_TERM_."  where \"guid\" = " . $PDO->quote($id);
                 $Fetch = PDO_FetchAll($query);
                 echo json_encode($Fetch, JSON_UNESCAPED_UNICODE);
             } else {
@@ -149,7 +151,7 @@ switch ($op) {
             echo "<div class='pali'>{$word}</div>";
             //查本人数据
             echo "<div></div>"; //My Term
-            $query = "select * from term  where word = ? AND  owner = ? limit 0,30";
+            $query = "select * from "._TABLE_TERM_."  where word = ? AND  owner = ? limit 30";
             $Fetch = PDO_FetchAll($query, array($word, $_COOKIE["userid"]));
             $iFetch = count($Fetch);
             if ($iFetch > 0) {
@@ -235,7 +237,7 @@ switch ($op) {
             echo "</div>";
 
             //查他人数据
-            $query = "SELECT * FROM term  WHERE word = ? AND owner <> ? LIMIT 0,30";
+            $query = "SELECT * FROM "._TABLE_TERM_."  WHERE word = ? AND owner <> ? LIMIT 30";
 
             $Fetch = PDO_FetchAll($query, array($word, $_COOKIE["userid"]));
             $iFetch = count($Fetch);
@@ -262,23 +264,43 @@ switch ($op) {
         }
     case "copy": //拷贝到我的字典
         {
-            $query = "select * from term  where \"guid\" = " . $PDO->quote($_GET["wordid"]);
+            $query = "select * from "._TABLE_TERM_."  where \"guid\" = " . $PDO->quote($_GET["wordid"]);
 
             $Fetch = PDO_FetchAll($query);
             $iFetch = count($Fetch);
             if ($iFetch > 0) {
                 /* 开始一个事务,关闭自动提交 */
                 $PDO->beginTransaction();
-                $query = "INSERT INTO term ('id','guid','word','word_en','meaning','other_meaning','note','tag','create_time','owner','hit') VALUES (null,?,?,?,?,?,?,?," . time() . ",'$username',1)";
+                $query = "INSERT INTO "._TABLE_TERM_." 
+                (
+                    'id',
+                    'guid',
+                    'word',
+                    'word_en',
+                    'meaning',
+                    'other_meaning',
+                    'note',
+                    'tag',
+                    'owner',
+                    'editor_id',
+                    'create_time',
+                    'update_time',
+                    ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
                 $stmt = $PDO->prepare($query);
                 {
-                    $stmt->execute(array(UUID::v4,
+                    $stmt->execute(array(
+                        $snowflake->id(),
+                        UUID::v4,
                         $Fetch[0]["word"],
                         $Fetch[0]["word_en"],
                         $Fetch[0]["meaning"],
                         $Fetch[0]["other_meaning"],
                         $Fetch[0]["note"],
                         $Fetch[0]["tag"],
+                        $_COOKIE['user_uid'],
+                        $_COOKIE['user_id'],
+                        mTime(),
+                        mTime()
                     ));
                 }
                 /* 提交更改 */
@@ -301,7 +323,7 @@ switch ($op) {
                 $authors = str_getcsv($_POST["authors"]);
             }
             $queryLang = $currLanguage . "%";
-            $query = "SELECT * from term  where \"word\" in {$words} AND language like ?  limit 0,1000";
+            $query = "SELECT * from "._TABLE_TERM_."  where \"word\" in {$words} AND language like ?  limit 1000";
             $Fetch = PDO_FetchAll($query, array($queryLang));
             $iFetch = count($Fetch);
             echo json_encode($Fetch, JSON_UNESCAPED_UNICODE);
@@ -310,7 +332,7 @@ switch ($op) {
     case "sync":
         {
             $time = $_GET["time"];
-            $query = "SELECT guid,modify_time from term  where receive_time>'{$time}'   limit 0,1000";
+            $query = "SELECT guid,modify_time from "._TABLE_TERM_."  where receive_time>'{$time}'   limit 1000";
             $Fetch = PDO_FetchAll($query);
             $iFetch = count($Fetch);
             echo json_encode($Fetch, JSON_UNESCAPED_UNICODE);
@@ -320,9 +342,9 @@ switch ($op) {
         {
             $Fetch = array();
             if (isset($guid)) {
-                $query = "select * from term  where \"guid\" = '{$guid}'";
+                $query = "select * from "._TABLE_TERM_."  where \"guid\" = '{$guid}'";
             } else if (isset($word)) {
-                $query = "select * from term  where \"word\" = '{$word}'";
+                $query = "select * from "._TABLE_TERM_."  where \"word\" = '{$word}'";
             } else {
                 echo "[]";
                 return;

+ 4 - 4
public/app/term/term_channel_get.php

@@ -10,7 +10,7 @@ require_once '../public/function.php';
 require_once '../ucenter/function.php';
 require_once '../channal/function.php';
 
-PDO_Connect("" . _FILE_DB_TERM_);
+PDO_Connect(_FILE_DB_TERM_);
 
 $output = array();
 if (isset($_POST["words"])) {
@@ -30,7 +30,7 @@ if (isset($_POST["words"])) {
         /*  创建一个填充了和params相同数量占位符的字符串 */
         $place_holders = implode(',', array_fill(0, count($channal), '?'));
         $owner_holders = implode(',', array_fill(0, count($channal_owner), '?'));
-        $query = "SELECT guid,word,meaning,other_meaning,owner,channal,language,tag ,note  FROM term WHERE channal IN ($place_holders) OR owner IN ($owner_holders)";
+        $query = "SELECT guid,word,meaning,other_meaning,owner,channal,language,tag ,note  FROM "._TABLE_TERM_." WHERE channal IN ($place_holders) OR owner IN ($owner_holders)";
         foreach ($channal_owner as $key => $value) {
             # code...
             $channal[] = $key;
@@ -82,9 +82,9 @@ if (isset($_POST["words"])) {
             }
 
             if ($otherCase == "") {
-                $query = "SELECT guid,word,meaning,other_meaning,owner,channal,language,tag ,note FROM term WHERE word = ? ";
+                $query = "SELECT guid,word,meaning,other_meaning,owner,channal,language,tag ,note FROM "._TABLE_TERM_." WHERE word = ? ";
             } else {
-                $query = "SELECT guid,word,meaning,other_meaning,owner,channal,language,tag ,note FROM term WHERE word = ? AND ( $otherCase )";
+                $query = "SELECT guid,word,meaning,other_meaning,owner,channal,language,tag ,note FROM "._TABLE_TERM_." WHERE word = ? AND ( $otherCase )";
             }
 
             $fetch = PDO_FetchAll($query, $parm);

+ 4 - 4
public/app/term/term_get.php

@@ -10,7 +10,7 @@ require_once '../public/function.php';
 require_once '../ucenter/function.php';
 require_once '../channal/function.php';
 
-PDO_Connect("" . _FILE_DB_TERM_);
+PDO_Connect(_FILE_DB_TERM_);
 
 $output = array();
 if (isset($_POST["words"])) {
@@ -29,7 +29,7 @@ if (isset($_POST["words"])) {
         /*  创建一个填充了和params相同数量占位符的字符串 */
         $place_holders = implode(',', array_fill(0, count($channal), '?'));
         $owner_holders = implode(',', array_fill(0, count($channal_owner), '?'));
-        $query = "SELECT guid,word,meaning,other_meaning,owner,channal,language,tag ,note  FROM term WHERE channal IN ($place_holders) OR owner IN ($owner_holders)";
+        $query = "SELECT guid,word,meaning,other_meaning,owner,channal,language,tag ,note  FROM "._TABLE_TERM_." WHERE channal IN ($place_holders) OR owner IN ($owner_holders)";
         foreach ($channal_owner as $key => $value) {
             # code...
             $channal[] = $key;
@@ -80,9 +80,9 @@ if (isset($_POST["words"])) {
             }
 
             if ($otherCase == "") {
-                $query = "SELECT guid,word,meaning,other_meaning,owner,channal,language,tag ,note FROM term WHERE word = ? ";
+                $query = "SELECT guid,word,meaning,other_meaning,owner,channal,language,tag ,note FROM "._TABLE_TERM_." WHERE word = ? ";
             } else {
-                $query = "SELECT guid,word,meaning,other_meaning,owner,channal,language,tag ,note FROM term WHERE word = ? AND ( $otherCase )";
+                $query = "SELECT guid,word,meaning,other_meaning,owner,channal,language,tag ,note FROM "._TABLE_TERM_." WHERE word = ? AND ( $otherCase )";
             }
 
             $fetch = PDO_FetchAll($query, $parm);

+ 3 - 3
public/app/term/term_get_id.php

@@ -8,16 +8,16 @@ require_once "../config.php";
 require_once "../public/_pdo.php";
 require_once '../ucenter/function.php';
 
-PDO_Connect("" . _FILE_DB_TERM_);
+PDO_Connect( _FILE_DB_TERM_);
 
 $fetch = array();
 if (isset($_POST["id"])) {
-    $query = "SELECT * FROM term WHERE guid = ? ";
+    $query = "SELECT * FROM "._TABLE_TERM_." WHERE guid = ? ";
 
     $fetch = PDO_FetchRow($query, array($_POST["id"]));
-    $userinfo = new UserInfo();
     if ($fetch) {
         # code...
+        $userinfo = new UserInfo();
         $fetch["user"] = $userinfo->getName($fetch["owner"]);
     }
 }

+ 55 - 34
public/app/term/term_post.php

@@ -7,6 +7,8 @@ require_once "../public/_pdo.php";
 require_once '../public/function.php';
 require_once "../redis/function.php";
 require_once "../channal/function.php";
+require_once __DIR__."/../public/snowflakeid.php";
+$snowflake = new SnowFlakeId();
 
 $redis = redis_connect();
 
@@ -20,7 +22,7 @@ if (isset($_COOKIE["userid"]) == false) {
 
 
 $respond = array("status" => 0, "message" => "");
-PDO_Connect("" . _FILE_DB_TERM_);
+PDO_Connect( _FILE_DB_TERM_);
 
 
 
@@ -28,7 +30,7 @@ if ($_POST["id"] != "") {
 	#更新
 	#先查询是否有权限
 	#是否这个术语的作者
-	$query = "SELECT id,channal,owner from term where guid= ? ";
+	$query = "SELECT id,channal,owner from "._TABLE_TERM_." where guid= ? ";
 	$stmt = $PDO->prepare($query);
 	$stmt->execute(array($_POST["id"]));
 	if ($stmt) {
@@ -54,7 +56,7 @@ if ($_POST["id"] != "") {
 			exit;				
 		}
 	}
-    $query = "UPDATE term SET meaning= ? ,other_meaning = ? , tag= ? ,channal = ? ,  language = ? , note = ? , receive_time= ?, modify_time= ?   where guid= ? ";
+    $query = "UPDATE "._TABLE_TERM_." SET meaning= ? ,other_meaning = ? , tag= ? ,channal = ? ,  language = ? , note = ? ,  modify_time= ? , updated_at = now()  where guid= ? ";
 	$stmt = @PDO_Execute($query, 
 						array($_POST["mean"],
         					  $_POST["mean2"],
@@ -63,7 +65,6 @@ if ($_POST["id"] != "") {
         					  $_POST["language"],
         					  $_POST["note"],
         					  mTime(),
-        					  mTime(),
         					  $_POST["id"],
     ));
     if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
@@ -107,11 +108,11 @@ if ($_POST["id"] != "") {
 	}
 	#先查询是否有重复数据
 	if($_POST["channal"]==""){
-		$query = "SELECT id from term where word= ? and  language=? and tag=? and owner = ? ";
+		$query = "SELECT id from "._TABLE_TERM_." where word= ? and  language=? and tag=? and owner = ? ";
 		$stmt = $PDO->prepare($query);
 		$stmt->execute(array($_POST["word"],$_POST["language"],$_POST["tag"],$_COOKIE["userid"]));
 	}else{
-		$query = "SELECT id from term where word= ? and channal=?  and tag=? and owner = ? ";
+		$query = "SELECT id from "._TABLE_TERM_." where word= ? and channal=?  and tag=? and owner = ? ";
 		$stmt = $PDO->prepare($query);
 		$stmt->execute(array($_POST["word"],$_POST["channal"],$_POST["tag"],$_COOKIE["userid"]));
 	}
@@ -125,22 +126,40 @@ if ($_POST["id"] != "") {
 			exit;
 		}
 	}
-    $parm[] = UUID::v4();
-    $parm[] = $_POST["word"];
-    $parm[] = pali2english($_POST["word"]);
-    $parm[] = $_POST["mean"];
-    $parm[] = $_POST["mean2"];
-    $parm[] = $_POST["tag"];
-    $parm[] = $_POST["channal"];
-    $parm[] = $_POST["language"];
-    $parm[] = $_POST["note"];
-    $parm[] = $_COOKIE["userid"];
-    $parm[] = 0;
-    $parm[] = mTime();
-    $parm[] = mTime();
-    $parm[] = mTime();
-    $query = "INSERT INTO term (id, guid, word, word_en, meaning, other_meaning, tag, channal, language,note,owner,hit,create_time,modify_time,receive_time )
-	VALUES (NULL, ? , ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
+    $parm = [
+        $snowflake->id(),
+        UUID::v4(),
+        $_POST["word"],
+        pali2english($_POST["word"]),
+        $_POST["mean"],
+        $_POST["mean2"],
+        $_POST["tag"],
+        $_POST["channal"],
+        $_POST["language"],
+        $_POST["note"],
+        $_COOKIE["user_uid"],
+        $_COOKIE["user_id"],
+        mTime(),
+        mTime()
+        ];
+    $query = "INSERT INTO "._TABLE_TERM_." 
+    (
+        id, 
+        guid, 
+        word, 
+        word_en, 
+        meaning, 
+        other_meaning, 
+        tag, 
+        channal, 
+        language,
+        note,
+        owner,
+        editor_id,
+        create_time,
+        modify_time
+    )
+	VALUES (?, ? , ?, ?, ?, ?, ?, ?, ?, ? , ?, ?, ?, ?) ";
     $stmt = @PDO_Execute($query, $parm);
     if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
         $error = PDO_ErrorInfo();
@@ -149,17 +168,19 @@ if ($_POST["id"] != "") {
     } else {
         $respond['status'] = 0;
         $respond['message'] = $_POST["word"];
-        $respond['data'] = ["guid"=>$parm[0],
-							"word"=>$parm[1],
-							"word_en"=>$parm[2],
-							"meaning"=>$parm[3],
-							"other_meaning"=>$parm[4],
-							"tag"=>$parm[5],
-							"channal"=>$parm[6],
-							"language"=>$parm[7],
-							"note"=>$parm[8],
-							"owner"=>$parm[9]
-						];
+        $respond['data'] = [
+            "id"=>$parm[0],
+            "guid"=>$parm[2],
+			"word"=>$parm[3],
+			"word_en"=>$parm[3],
+			"meaning"=>$parm[4],
+			"other_meaning"=>$parm[5],
+			"tag"=>$parm[6],
+			"channal"=>$parm[7],
+			"language"=>$parm[8],
+			"note"=>$parm[9],
+			"owner"=>$parm[10]
+			];
 
     }
 }
@@ -168,7 +189,7 @@ if ($_POST["id"] != "") {
 	if ($redis != false) {
 		{
 			# code...
-			$query = "SELECT id,word,meaning,other_meaning,note,owner,language from term where word = ? ";
+			$query = "SELECT id,word,meaning,other_meaning,note,owner,language from "._TABLE_TERM_." where word = ? ";
 			$stmt = $PDO->prepare($query);
 			$stmt->execute(array($_POST["word"]));
 			if ($stmt) {

+ 3 - 3
public/app/term/update_analytics.php

@@ -5,15 +5,15 @@ require_once '../public/load_lang.php';
 require_once '../public/function.php';
 
 global $PDO;
-PDO_Connect("" . _FILE_DB_TERM_);
+PDO_Connect(_FILE_DB_TERM_);
 
 echo "Day Index,创建,更新\n";
 $end = strtotime("now");
 $begin = strtotime("-1 day");
 for ($i = 0; $i < 100; $i++) {
-    $query = "select count(*) from term where \"create_time\" > " . $PDO->quote($begin) . " AND \"create_time\" < " . $PDO->quote($end);
+    $query = "SELECT count(*) from "._TABLE_TERM_." where \"create_time\" > " . $PDO->quote($begin) . " AND \"create_time\" < " . $PDO->quote($end);
     $create = PDO_FetchOne($query);
-    $query = "select count(*) from term where modify_time <> create_time AND \"modify_time\" > " . $PDO->quote($begin) . " AND \"modify_time\" < " . $PDO->quote($end);
+    $query = "SELECT count(*) from "._TABLE_TERM_." where modify_time <> create_time AND \"modify_time\" > " . $PDO->quote($begin) . " AND \"modify_time\" < " . $PDO->quote($end);
     $modify = PDO_FetchOne($query);
     echo date("m/d/Y", $begin) . ',' . $create . "," . $modify . "\n";
     $end = $begin;

+ 8 - 0
v1/scripts/install3.sh

@@ -0,0 +1,8 @@
+#!/bin/sh
+
+date
+
+php ./migrations/20220202172100_dhamma_terms_copy.php
+
+
+date

+ 190 - 0
v1/scripts/migrations/20220202172100_dhamma_terms_copy.php

@@ -0,0 +1,190 @@
+<?php
+/*
+迁移  article 库
+从旧数据表中提取数据插入到新的表
+插入时用uuid判断是否曾经插入
+曾经插入就不插入了
+*/
+require_once __DIR__."/../../../public/app/config.php";
+require_once __DIR__."/../../../public/app/public/snowflakeid.php";
+
+set_exception_handler(function($e){
+	fwrite(STDERR,"error-msg:".$e->getMessage().PHP_EOL);
+	fwrite(STDERR,"error-file:".$e->getFile().PHP_EOL);
+	fwrite(STDERR,"error-line:".$e->getLine().PHP_EOL);
+	exit;
+});
+$start = time();
+# 雪花id
+$snowflake = new SnowFlakeId();
+
+$fpError = fopen(__DIR__.'/log/'.basename($_SERVER['PHP_SELF'],'.php').".err.data.csv",'w');
+
+#user info
+$user_db=_FILE_DB_USERINFO_;#user数据库
+$user_table=_TABLE_USER_INFO_;#user表名
+
+# 
+$src_db = _SQLITE_DB_TERM_;#源数据库
+$src_table = _SQLITE_TABLE_TERM_;#源表名
+
+$dest_db = _PG_DB_TERM_;#目标数据库
+$dest_table = _PG_TABLE_TERM_;#目标表名
+
+fwrite(STDOUT,"migarate dhammaterm".PHP_EOL);
+#打开user数据库
+$PDO_USER = new PDO($user_db,_DB_USERNAME_,_DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
+$PDO_USER->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+fwrite(STDOUT,"open user table".PHP_EOL);
+
+#打开源数据库
+$PDO_SRC = new PDO($src_db,_DB_USERNAME_,_DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
+$PDO_SRC->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+fwrite(STDOUT,"open src table".PHP_EOL);
+
+#打开目标数据库
+$PDO_DEST = new PDO($dest_db,_DB_USERNAME_,_DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
+$PDO_DEST->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+fwrite(STDOUT,"open dest table".PHP_EOL);
+
+$queryInsert = "INSERT INTO ".$dest_table." 
+								(
+                                    id,
+									guid,
+									word,
+									word_en,
+									meaning,
+									other_meaning,
+									note,
+									tag,
+									channal,
+									language,
+									owner,
+									editor_id,
+									create_time,
+									modify_time,
+									updated_at,
+									created_at) 
+									VALUES (? , ? , ? , ?, ? , ? ,? , ? , ? , ? , ? , ? , ?,? ,?,?)";
+$stmtDEST = $PDO_DEST->prepare($queryInsert);
+
+$commitData = [];
+$allInsertCount = 0;
+$allSrcCount = 0;
+$count = 0;
+
+#从user数据表中读取
+$query = "SELECT id  FROM ".$user_table." WHERE userid = ? ";
+$stmtUser = $PDO_USER->prepare($query);
+
+#从源数据表中读取
+$query = "SELECT *  FROM ".$src_table." WHERE true ";
+$stmtSrc = $PDO_SRC->prepare($query);
+$stmtSrc->execute();
+while($srcData = $stmtSrc->fetch(PDO::FETCH_ASSOC)){
+	$allSrcCount++;
+
+    if($srcData["owner"]=='visuddhinanda'){
+		$srcData["owner"] = 'ba5463f3-72d1-4410-858e-eadd10884713';
+	}
+    if($srcData["owner"]=='test7'){
+		$srcData["owner"] = '6bd2f4d7-d970-419c-8ee5-f4bac42f4bc1';
+	}
+    if($srcData["owner"]=='Dhammadassi'){
+		$srcData["owner"] = 'd8538ebd-d369-4777-b99a-3ccb1aff8bfc';
+	}
+    if($srcData["owner"]=='pannava'){
+		$srcData["owner"] = '4db550c4-bc1b-43f2-a518-2740cb478f37';
+	}
+    if($srcData["owner"]=='NST'){
+		$srcData["owner"] = '5c23e629-56a3-48e9-97c7-2af73b59c3b9';
+	}
+    if($srcData["owner"]=='viranyani'){
+		$srcData["owner"] = 'C1AB2ABF-EAA8-4EEF-B4D9-3854321852B4';
+	}
+    if($srcData["owner"]=='test6'){
+		$srcData["owner"] = 'f81c7140-64b4-4025-b58c-45a3b386324a';
+	}
+	if($srcData["owner"]=='test28'){
+		$srcData["owner"] = 'df0ad9bc-c0cd-4cd9-af05-e43d23ed57f0';
+	}
+	if($srcData["owner"]=='290fd808-2f46-4b8c-b300-0367badd67ed'){
+		$srcData["owner"] = 'f81c7140-64b4-4025-b58c-45a3b386324a';
+	}
+	if($srcData["owner"]=='BA837178-9ABD-4DD4-96A0-D2C21B756DC4'){
+		$srcData["owner"] = 'ba5463f3-72d1-4410-858e-eadd10884713';
+	}
+	$stmtUser->execute(array($srcData["owner"]));
+	$userId = $stmtUser->fetch(PDO::FETCH_ASSOC);
+	if(!$userId){
+		fwrite(STDERR,time()."error,no user id {$srcData["owner"]}".PHP_EOL);
+		continue;
+	}
+	if(strlen($srcData["owner"])>36){
+		fwrite(STDERR,time().",error,user id too long {$srcData["owner"]}".PHP_EOL);
+		continue;	
+	}
+    if(empty($srcData["word"])){
+		fwrite(STDERR,time().",error,word is empty {$srcData["id"]}".PHP_EOL);
+		continue;	
+	}
+    if(empty($srcData["language"]) ){
+        $srcData["language"]='zh-hans';
+    }
+
+    if($srcData["create_time"] < 15987088320){
+        $srcData["create_time"] *= 1000;
+    }
+    if($srcData["modify_time"] < 15987088320){
+        $srcData["modify_time"] *= 1000;
+    }
+	//查询是否已经插入
+	$queryExsit = "SELECT id  FROM ".$dest_table." WHERE guid = ? ";
+	$getExist = $PDO_DEST->prepare($queryExsit);
+	$getExist->execute(array($srcData["id"]));
+	$exist = $getExist->fetch(PDO::FETCH_ASSOC);
+	if($exist){
+		continue;
+	}
+	#插入目标表
+	$created_at = date("Y-m-d H:i:s.",$srcData["create_time"]/1000).($srcData["create_time"]%1000)." UTC";
+	$updated_at = date("Y-m-d H:i:s.",$srcData["modify_time"]/1000).($srcData["modify_time"]%1000)." UTC";
+	$commitData = array(
+            $snowflake->id(),
+			$srcData["guid"],
+			$srcData["word"],
+			$srcData["word_en"],
+			$srcData["meaning"],
+			$srcData["other_meaning"],
+			$srcData["note"],
+			$srcData["tag"],
+			$srcData["channal"],
+			$srcData["language"],
+			$srcData["owner"],
+            $userId["id"],
+			$srcData["create_time"],
+			$srcData["modify_time"],
+			$created_at,
+			$updated_at
+		);
+	$stmtDEST->execute($commitData);
+
+	$count++;	
+	$allInsertCount++;
+
+
+	if($count ==10000){
+		#10000行输出log 一次
+		echo "finished $count".PHP_EOL;
+		$count=0;
+	}	
+}
+
+fwrite(STDOUT,"insert done $allInsertCount in $allSrcCount ".PHP_EOL) ;
+fwrite(STDOUT, "all done in ".(time()-$start)."s".PHP_EOL);
+
+fclose($fpError);
+
+
+
+