dict_updata_wbw.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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("sqlite:$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. }
  57. else{
  58. // "have a same recorder";
  59. $wordId=$Fetch[0]["id"];
  60. $ref=$Fetch[0]["ref_counter"]+1;
  61. //更新引用计数
  62. $query="UPDATE dict SET ref_counter='$ref' where id=".$PDO->quote($wordId);
  63. $stmt = @PDO_Execute($query);
  64. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  65. $error = PDO_ErrorInfo();
  66. echo "error".$error[2]."<br>";
  67. }
  68. //去掉已经有的索引
  69. $query = "select count(*) from user_index where word_index={$wordId} and user_id={$UID}";
  70. $num = PDO_FetchOne($query);
  71. if($num==0){
  72. array_push($arrExistWords,$Fetch[0]["id"]);
  73. }
  74. }
  75. }
  76. /* 开始一个事务,关闭自动提交 */
  77. $PDO->beginTransaction();
  78. $query="INSERT INTO dict ('id',
  79. 'guid',
  80. 'pali',
  81. 'type',
  82. 'gramma',
  83. 'parent',
  84. 'parent_id',
  85. 'mean',
  86. 'note',
  87. 'factors',
  88. 'factormean',
  89. 'part_id',
  90. 'status',
  91. 'dict_name',
  92. 'language',
  93. 'creator',
  94. 'time')
  95. VALUES (null,?,?,?,?,?,?,?,?,?,?,?,?,'user',?,?,?)";
  96. $stmt = $PDO->prepare($query);
  97. foreach($arrInserString as $oneParam){
  98. $stmt->execute($oneParam);
  99. }
  100. /* 提交更改 */
  101. $PDO->commit();
  102. $lastid=$PDO->lastInsertId();
  103. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  104. $error = PDO_ErrorInfo();
  105. echo "error - $error[2] <br>";
  106. }
  107. else{
  108. $count=count($arrInserString);
  109. echo "updata $count recorders.";
  110. //更新索引表
  111. $iFirst=$lastid-$count+1;
  112. for($i=0;$i<$count;$i++){
  113. array_push($arrExistWords,$iFirst+$i);
  114. }
  115. if(count($arrExistWords)>0){
  116. /* 开始一个事务,关闭自动提交 */
  117. $PDO->beginTransaction();
  118. $query="INSERT INTO user_index ('id','word_index','user_id','create_time')
  119. VALUES (null,?,{$UID},?)";
  120. $stmt = $PDO->prepare($query);
  121. foreach($arrExistWords as $oneId){
  122. $stmt->execute(array($oneId,time()));
  123. }
  124. /* 提交更改 */
  125. $PDO->commit();
  126. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  127. $error = PDO_ErrorInfo();
  128. echo "error - $error[2] <br>";
  129. }
  130. else{
  131. echo "updata index ".count($arrExistWords)." recorders.";
  132. }
  133. }
  134. else{
  135. echo "updata index 0";
  136. }
  137. }
  138. ?>