dict_updata_wbw.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. $redis = redis_connect();
  7. $input = file_get_contents("php://input");
  8. $return = "";
  9. $serverMsg = "";
  10. $xml = simplexml_load_string($input);
  11. PDO_Connect(_FILE_DB_WBW_);
  12. PDO_Execute("PRAGMA synchronous = OFF");
  13. PDO_Execute("PRAGMA journal_mode = WAL");
  14. PDO_Execute("PRAGMA foreign_keys = ON");
  15. PDO_Execute("PRAGMA busy_timeout = 5000");
  16. $wordsList = $xml->xpath('//word');
  17. //$serverMsg+= "word count:".count($wordsList)."<br>";
  18. //remove the same word
  19. foreach ($wordsList as $ws) {
  20. $combine = $ws->pali . $ws->guid . $ws->type . $ws->gramma . $ws->parent . $ws->parent_id . $ws->mean . $ws->factors . $ws->fm . $ws->part_id;
  21. $word[$combine] = $ws;
  22. }
  23. $arrInserString = array();
  24. $arrExistWords = array();
  25. $updateWord = array();
  26. foreach ($word as $x => $ws) {
  27. $query = "SELECT id,ref_counter FROM dict WHERE
  28. \"guid\"= ? AND
  29. \"pali\"= ? AND
  30. \"type\"= ? AND
  31. \"gramma\"= ? AND
  32. \"mean\"= ? AND
  33. \"parent\"= ? AND
  34. \"parent_id\"= ? AND
  35. \"factors\"= ? AND
  36. \"factormean\"= ? AND
  37. \"part_id\"= ?" ;
  38. $Fetch = PDO_FetchAll($query,array($ws->guid,$ws->pali,$ws->type,$ws->gramma,$ws->mean,$ws->parent,$ws->parent_id,$ws->factors,$ws->fm,$ws->part_id));
  39. $FetchNum = count($Fetch);
  40. if ($FetchNum == 0) {
  41. $updateWord["{$ws->pali}"] = 1;
  42. //没有找到,新建数据
  43. //new recorder
  44. $params = array($ws->guid,
  45. $ws->pali,
  46. $ws->type,
  47. $ws->gramma,
  48. $ws->parent,
  49. $ws->parent_id,
  50. $ws->mean,
  51. $ws->note,
  52. $ws->factors,
  53. $ws->fm,
  54. $ws->part_id,
  55. $ws->status,
  56. $ws->language,
  57. $UID,
  58. time());
  59. array_push($arrInserString, $params);
  60. } else {
  61. // "have a same recorder";
  62. $wordId = $Fetch[0]["id"];
  63. $ref = $Fetch[0]["ref_counter"] + 1;
  64. //更新引用计数
  65. $query = "UPDATE dict SET ref_counter='$ref' where id=" . $PDO->quote($wordId);
  66. $stmt = @PDO_Execute($query);
  67. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  68. $error = PDO_ErrorInfo();
  69. echo "error" . $error[2] . "<br>";
  70. }
  71. //去掉已经有的索引
  72. $query = "select count(*) from user_index where word_index={$wordId} and user_id={$UID}";
  73. $num = PDO_FetchOne($query);
  74. if ($num == 0) {
  75. array_push($arrExistWords, $Fetch[0]["id"]);
  76. }
  77. }
  78. }
  79. /* 开始一个事务,关闭自动提交 */
  80. $PDO->beginTransaction();
  81. $query = "INSERT INTO dict ('id',
  82. 'guid',
  83. 'pali',
  84. 'type',
  85. 'gramma',
  86. 'parent',
  87. 'parent_id',
  88. 'mean',
  89. 'note',
  90. 'factors',
  91. 'factormean',
  92. 'part_id',
  93. 'status',
  94. 'dict_name',
  95. 'language',
  96. 'creator',
  97. 'time')
  98. VALUES (null,?,?,?,?,?,?,?,?,?,?,?,?,'user',?,?,?)";
  99. $stmt = $PDO->prepare($query);
  100. foreach ($arrInserString as $oneParam) {
  101. $stmt->execute($oneParam);
  102. }
  103. /* 提交更改 */
  104. $PDO->commit();
  105. $lastid = $PDO->lastInsertId();
  106. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  107. $error = PDO_ErrorInfo();
  108. echo "error - $error[2] <br>";
  109. } else {
  110. //成功
  111. $count = count($arrInserString);
  112. echo "updata $count recorders.";
  113. //更新索引表
  114. $iFirst = $lastid - $count + 1;
  115. for ($i = 0; $i < $count; $i++) {
  116. array_push($arrExistWords, $iFirst + $i);
  117. }
  118. if (count($arrExistWords) > 0) {
  119. /* 开始一个事务,关闭自动提交 */
  120. $PDO->beginTransaction();
  121. $query = "INSERT INTO user_index ('id','word_index','user_id','create_time')
  122. VALUES (null,?,{$UID},?)";
  123. $stmt = $PDO->prepare($query);
  124. foreach ($arrExistWords as $oneId) {
  125. $stmt->execute(array($oneId, time()));
  126. }
  127. /* 提交更改 */
  128. $PDO->commit();
  129. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  130. $error = PDO_ErrorInfo();
  131. echo "error - $error[2] <br>";
  132. } else {
  133. echo "updata index " . count($arrExistWords) . " recorders.";
  134. }
  135. } else {
  136. echo "updata index 0";
  137. }
  138. #更新 redis
  139. if ($redis != false) {
  140. foreach ($updateWord as $key => $value) {
  141. # code...
  142. $query = "SELECT * from dict where pali = ? ";
  143. $stmt = $PDO->prepare($query);
  144. $stmt->execute(array($key));
  145. if ($stmt) {
  146. $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
  147. $redisWord=array();
  148. foreach ($Fetch as $one) {
  149. # code...
  150. $redisWord[] = array($one["id"],
  151. $one["pali"],
  152. $one["type"],
  153. $one["gramma"],
  154. $one["parent"],
  155. $one["mean"],
  156. $one["note"],
  157. $one["factors"],
  158. $one["factormean"],
  159. $one["status"],
  160. $one["confidence"],
  161. $one["creator"],
  162. $one["dict_name"],
  163. $one["language"]
  164. );
  165. }
  166. $redis->hSet("dict://user",$key,json_encode($redisWord,JSON_UNESCAPED_UNICODE));
  167. }
  168. }
  169. }
  170. #更新redis结束
  171. }