dict_updata_wbw.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. require 'checklogin.inc';
  3. include "../public/_pdo.php";
  4. include "../path.php";
  5. $input = file_get_contents("php://input");
  6. $return = "";
  7. $serverMsg = "";
  8. $xml = simplexml_load_string($input);
  9. PDO_Connect(_FILE_DB_WBW_);
  10. PDO_Execute("PRAGMA synchronous = OFF");
  11. PDO_Execute("PRAGMA journal_mode = WAL");
  12. PDO_Execute("PRAGMA foreign_keys = ON");
  13. PDO_Execute("PRAGMA busy_timeout = 5000");
  14. $wordsList = $xml->xpath('//word');
  15. //$serverMsg+= "word count:".count($wordsList)."<br>";
  16. //remove the same word
  17. foreach ($wordsList as $ws) {
  18. $combine = $ws->pali . $ws->guid . $ws->type . $ws->gramma . $ws->parent . $ws->parent_id . $ws->mean . $ws->factors . $ws->fm . $ws->part_id;
  19. $word[$combine] = $ws;
  20. }
  21. $arrInserString = array();
  22. $arrExistWords = array();
  23. foreach ($word as $x => $ws) {
  24. $query = "select id,ref_counter from dict where
  25. \"guid\"=" . $PDO->quote($ws->guid) . " AND
  26. \"pali\"=" . $PDO->quote($ws->pali) . " AND
  27. \"type\"=" . $PDO->quote($ws->type) . " AND
  28. \"gramma\"=" . $PDO->quote($ws->gramma) . " AND
  29. \"mean\"=" . $PDO->quote($ws->mean) . " AND
  30. \"parent\"=" . $PDO->quote($ws->parent) . " AND
  31. \"parent_id\"=" . $PDO->quote($ws->parent_id) . " AND
  32. \"factors\"=" . $PDO->quote($ws->factors) . " AND
  33. \"factormean\"=" . $PDO->quote($ws->fm) . " AND
  34. \"part_id\"=" . $PDO->quote($ws->part_id);
  35. $Fetch = PDO_FetchAll($query);
  36. $FetchNum = count($Fetch);
  37. if ($FetchNum == 0) {
  38. //new recorder
  39. $params = array($ws->guid,
  40. $ws->pali,
  41. $ws->type,
  42. $ws->gramma,
  43. $ws->parent,
  44. $ws->parent_id,
  45. $ws->mean,
  46. $ws->note,
  47. $ws->factors,
  48. $ws->fm,
  49. $ws->part_id,
  50. $ws->status,
  51. $ws->language,
  52. $UID,
  53. time());
  54. array_push($arrInserString, $params);
  55. } else {
  56. // "have a same recorder";
  57. $wordId = $Fetch[0]["id"];
  58. $ref = $Fetch[0]["ref_counter"] + 1;
  59. //更新引用计数
  60. $query = "UPDATE dict SET ref_counter='$ref' where id=" . $PDO->quote($wordId);
  61. $stmt = @PDO_Execute($query);
  62. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  63. $error = PDO_ErrorInfo();
  64. echo "error" . $error[2] . "<br>";
  65. }
  66. //去掉已经有的索引
  67. $query = "select count(*) from user_index where word_index={$wordId} and user_id={$UID}";
  68. $num = PDO_FetchOne($query);
  69. if ($num == 0) {
  70. array_push($arrExistWords, $Fetch[0]["id"]);
  71. }
  72. }
  73. }
  74. /* 开始一个事务,关闭自动提交 */
  75. $PDO->beginTransaction();
  76. $query = "INSERT INTO dict ('id',
  77. 'guid',
  78. 'pali',
  79. 'type',
  80. 'gramma',
  81. 'parent',
  82. 'parent_id',
  83. 'mean',
  84. 'note',
  85. 'factors',
  86. 'factormean',
  87. 'part_id',
  88. 'status',
  89. 'dict_name',
  90. 'language',
  91. 'creator',
  92. 'time')
  93. VALUES (null,?,?,?,?,?,?,?,?,?,?,?,?,'user',?,?,?)";
  94. $stmt = $PDO->prepare($query);
  95. foreach ($arrInserString as $oneParam) {
  96. $stmt->execute($oneParam);
  97. }
  98. /* 提交更改 */
  99. $PDO->commit();
  100. $lastid = $PDO->lastInsertId();
  101. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  102. $error = PDO_ErrorInfo();
  103. echo "error - $error[2] <br>";
  104. } else {
  105. $count = count($arrInserString);
  106. echo "updata $count recorders.";
  107. //更新索引表
  108. $iFirst = $lastid - $count + 1;
  109. for ($i = 0; $i < $count; $i++) {
  110. array_push($arrExistWords, $iFirst + $i);
  111. }
  112. if (count($arrExistWords) > 0) {
  113. /* 开始一个事务,关闭自动提交 */
  114. $PDO->beginTransaction();
  115. $query = "INSERT INTO user_index ('id','word_index','user_id','create_time')
  116. VALUES (null,?,{$UID},?)";
  117. $stmt = $PDO->prepare($query);
  118. foreach ($arrExistWords as $oneId) {
  119. $stmt->execute(array($oneId, time()));
  120. }
  121. /* 提交更改 */
  122. $PDO->commit();
  123. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  124. $error = PDO_ErrorInfo();
  125. echo "error - $error[2] <br>";
  126. } else {
  127. echo "updata index " . count($arrExistWords) . " recorders.";
  128. }
  129. } else {
  130. echo "updata index 0";
  131. }
  132. }