dict_updata_wbw.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. require_once 'checklogin.inc';
  3. require_once "../public/_pdo.php";
  4. require_once "../config.php";
  5. require_once "../redis/function.php";
  6. require_once "../public/function.php";
  7. $return = "";
  8. $serverMsg = "";
  9. $redis = redis_connect();
  10. $input = file_get_contents("php://input");
  11. $xml = simplexml_load_string($input);
  12. $wordsList = $xml->xpath('//word');
  13. PDO_Connect(_FILE_DB_WBW_,_DB_USERNAME_,_DB_PASSWORD_);
  14. //remove the same word
  15. foreach ($wordsList as $ws) {
  16. $combine = $ws->pali . $ws->type . $ws->gramma . $ws->parent . $ws->mean . $ws->factors . $ws->fm;
  17. $word[$combine] = $ws;
  18. }
  19. $arrInserString = array();
  20. $arrExistWords = array();
  21. $updateWord = array();
  22. foreach ($word as $x => $ws) {
  23. $query = "SELECT id,ref_counter FROM "._TABLE_DICT_USER_." WHERE
  24. word = ? AND
  25. type = ? AND
  26. gramma = ? AND
  27. mean = ? AND
  28. base = ? AND
  29. factors = ? AND
  30. factormean = ? AND source = '_USER_DATA_'" ;
  31. $Fetch = PDO_FetchAll($query,array($ws->pali,$ws->type,$ws->gramma,$ws->mean,$ws->parent,$ws->factors,$ws->fm));
  32. $FetchNum = count($Fetch);
  33. if ($FetchNum == 0) {
  34. $updateWord["{$ws->pali}"] = 1;
  35. //没有找到,新建数据
  36. //new recorder
  37. $params = array(
  38. $ws->pali,
  39. $ws->type,
  40. $ws->gramma,
  41. $ws->parent,
  42. $ws->mean,
  43. $ws->note,
  44. $ws->factors,
  45. $ws->fm,
  46. $ws->status,
  47. $ws->language,
  48. '_USER_DATA_',
  49. mTime());
  50. array_push($arrInserString, $params);
  51. } else {
  52. #查询本人是否有此记录
  53. $query = "SELECT id,ref_counter FROM "._TABLE_DICT_USER_." WHERE
  54. word = ? AND
  55. type = ? AND
  56. gramma = ? AND
  57. mean = ? AND
  58. base = ? AND
  59. factors = ? AND
  60. factormean = ? AND user_id = ? " ;
  61. $FetchMy = PDO_FetchAll($query,array($ws->pali,$ws->type,$ws->gramma,$ws->mean,$ws->parent,$ws->factors,$ws->fm,$UID));
  62. $FetchNumMy = count($FetchMy);
  63. if($FetchNumMy==0){
  64. $wordId = $Fetch[0]["id"];
  65. $ref = $Fetch[0]["ref_counter"] + 1;
  66. //更新引用计数
  67. $query = "UPDATE dict SET ref_counter= ? where id = ? ";
  68. $stmt = PDO_Execute($query,array($ref,$wordId));
  69. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  70. $error = PDO_ErrorInfo();
  71. echo "error" . $error[2] . "<br>";
  72. }
  73. #增加我的记录
  74. $params = array(
  75. $ws->pali,
  76. $ws->type,
  77. $ws->gramma,
  78. $ws->parent,
  79. $ws->mean,
  80. $ws->note,
  81. $ws->factors,
  82. $ws->fm,
  83. $ws->status,
  84. $ws->language,
  85. '_WBW_',
  86. mTime());
  87. array_push($arrInserString, $params);
  88. }
  89. // "have a same recorder";
  90. }
  91. }
  92. /* 开始一个事务,关闭自动提交 */
  93. $PDO->beginTransaction();
  94. $query = "INSERT INTO "._TABLE_DICT_USER_." (
  95. 'word',
  96. 'type',
  97. 'gramma',
  98. 'parent',
  99. 'mean',
  100. 'note',
  101. 'factors',
  102. 'factormean',
  103. 'status',
  104. 'source',
  105. 'language',
  106. 'creator_id',
  107. 'create_time')
  108. VALUES (?,?,?,?,?,?,?,?,?,'user',?,?,?)";
  109. $stmt = $PDO->prepare($query);
  110. foreach ($arrInserString as $oneParam) {
  111. $stmt->execute($oneParam);
  112. }
  113. /* 提交更改 */
  114. $PDO->commit();
  115. $lastid = $PDO->lastInsertId();
  116. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  117. $error = PDO_ErrorInfo();
  118. echo "error - $error[2] <br>";
  119. } else {
  120. //成功
  121. #更新 redis
  122. if ($redis != false) {
  123. foreach ($updateWord as $key => $value) {
  124. # code...
  125. $query = "SELECT * from "._TABLE_DICT_USER_." where word = ? ";
  126. $stmt = $PDO->prepare($query);
  127. $stmt->execute(array($key));
  128. if ($stmt) {
  129. $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
  130. $redisWord=array();
  131. foreach ($Fetch as $one) {
  132. # code...
  133. $redisWord[] = array($one["id"],
  134. $one["pali"],
  135. $one["type"],
  136. $one["gramma"],
  137. $one["parent"],
  138. $one["mean"],
  139. $one["note"],
  140. $one["factors"],
  141. $one["factormean"],
  142. $one["status"],
  143. $one["confidence"],
  144. $one["creator"],
  145. $one["dict_name"],
  146. $one["language"]
  147. );
  148. }
  149. $redis->hSet("dict://user",$key,json_encode($redisWord,JSON_UNESCAPED_UNICODE));
  150. }
  151. }
  152. }
  153. #更新redis结束
  154. }