dict_updata_wbw.php 4.2 KB

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