dict_updata_wbw.php 3.6 KB

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