update.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /*
  3. get xml doc from db
  4. */
  5. require_once "../path.php";
  6. require_once "../public/_pdo.php";
  7. require_once "../public/function.php";
  8. require_once "../ucenter/active.php";
  9. require_once "../redis/function.php";
  10. $respond['status'] = 0;
  11. $respond['message'] = "";
  12. if (isset($_POST["data"])) {
  13. $aData = json_decode($_POST["data"]);
  14. } else {
  15. $respond['status'] = 1;
  16. $respond['message'] = "no data";
  17. echo json_encode(array(), JSON_UNESCAPED_UNICODE);
  18. exit;
  19. }
  20. if (count($aData) > 0) {
  21. add_edit_event(_WBW_EDIT_, "{$aData[0]->book}-{$aData[0]->para}-{$aData[0]->word_id}");
  22. PDO_Connect(_FILE_DB_USER_WBW_);
  23. /* 开始一个事务,关闭自动提交 */
  24. $PDO->beginTransaction();
  25. $query = "UPDATE wbw SET data= ? , receive_time= ? , modify_time= ? where block_id= ? and wid= ? ";
  26. $sth = $PDO->prepare($query);
  27. foreach ($aData as $data) {
  28. $sth->execute(array($data->data, mTime(), $data->time, $data->block_id, $data->word_id));
  29. }
  30. $PDO->commit();
  31. $respond = array("status" => 0, "message" => "");
  32. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  33. /* 识别错误且回滚更改 */
  34. $PDO->rollBack();
  35. $error = PDO_ErrorInfo();
  36. $respond['status'] = 1;
  37. $respond['message'] = $error[2];
  38. } else {
  39. $respond['status'] = 0;
  40. $respond['message'] = "成功";
  41. }
  42. if (count($aData) ==1){
  43. $redis = redis_connect();
  44. try {
  45. if($redis){
  46. $xmlString = "<root>" . $data->data . "</root>";
  47. $xmlWord = simplexml_load_string($xmlString);
  48. $wordsList = $xmlWord->xpath('//word');
  49. foreach ($wordsList as $word) {
  50. $pali = $word->real->__toString();
  51. $newword[]=array( "0",
  52. $pali,
  53. $word->type->__toString(),
  54. $word->gramma->__toString(),
  55. $word->parent->__toString(),
  56. $word->mean->__toString(),
  57. "",
  58. $word->org->__toString(),
  59. $word->om->__toString(),
  60. "7",
  61. "100",
  62. "my",
  63. "none"
  64. );
  65. $redis->hSet("wbwdict://new/".$_COOKIE["userid"],$pali,json_encode($newword, JSON_UNESCAPED_UNICODE));
  66. }
  67. }
  68. } catch (Throwable $e) {
  69. echo "Captured Throwable: " . $e->getMessage();
  70. }
  71. }
  72. echo json_encode($respond, JSON_UNESCAPED_UNICODE);
  73. } else {
  74. $respond['status'] = 1;
  75. $respond['message'] = "no data";
  76. echo json_encode($respond, JSON_UNESCAPED_UNICODE);
  77. }