upgrade_pali_toc.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. #升级段落完成度数据库
  3. require_once '../path.php';
  4. $redis = new redis();
  5. $r_conn = $redis->connect('127.0.0.1', 6379);
  6. $dns = "" . _FILE_DB_PALI_TOC_;
  7. $dbh_toc = new PDO($dns, "", "", array(PDO::ATTR_PERSISTENT => true));
  8. $dbh_toc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  9. $dns = "" . _FILE_DB_SENTENCE_;
  10. $dbh_sent = new PDO($dns, "", "", array(PDO::ATTR_PERSISTENT => true));
  11. $dbh_sent->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  12. $dns = "" . _FILE_DB_PALI_SENTENCE_;
  13. $dbh_pali_sent = new PDO($dns, "", "", array(PDO::ATTR_PERSISTENT => true));
  14. $dbh_pali_sent->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  15. $dns = "" . _FILE_DB_PALITEXT_;
  16. $dbh_pali_text = new PDO($dns, "", "", array(PDO::ATTR_PERSISTENT => true));
  17. $dbh_pali_text->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  18. $valid_book = array();
  19. #第一步 查询有多少书有译文
  20. $query = "SELECT book from sentence where strlen>0 and begin<>'' and language<>'' and book <1000 group by book";
  21. $stmt = $dbh_sent->prepare($query);
  22. $stmt->execute();
  23. $valid_book = $stmt->fetchAll(PDO::FETCH_ASSOC);
  24. echo "book:" . count($valid_book) . "<br>\n";
  25. #第一步 查询语言
  26. $query = "SELECT language from sentence where strlen>0 and begin<>'' and language<>'' and book <1000 group by language";
  27. $stmt = $dbh_sent->prepare($query);
  28. $stmt->execute();
  29. $result_lang = $stmt->fetchAll(PDO::FETCH_ASSOC);
  30. echo "lang:" . count($result_lang) . "<br>\n";
  31. $query = "DELETE FROM progress WHERE 1";
  32. $sth_toc = $dbh_toc->prepare($query);
  33. $sth_toc->execute();
  34. $query = "DELETE FROM progress_chapter WHERE 1";
  35. $sth_toc = $dbh_toc->prepare($query);
  36. $sth_toc->execute();
  37. /* 开始一个事务,关闭自动提交 */
  38. $dbh_toc->beginTransaction();
  39. $query = "INSERT INTO progress (book, para , lang , all_strlen , public_strlen) VALUES (?, ?, ? , ? ,? )";
  40. $sth_toc = $dbh_toc->prepare($query);
  41. foreach ($result_lang as $lang) {
  42. # 第二步 生成para progress 1,2,15,zh-tw
  43. #查询该语言有多少段
  44. $query = "select book,paragraph from sentence where strlen>0 and language= ? and book<1000 group by book,paragraph";
  45. $stmt = $dbh_sent->prepare($query);
  46. $stmt->execute(array($lang["language"]));
  47. $result_para = $stmt->fetchAll(PDO::FETCH_ASSOC);
  48. foreach ($result_para as $para) {
  49. # 查询每个段落的等效巴利语字符数
  50. $query = "select begin from sentence where strlen>0 and language= ? and book = ? and paragraph = ? and begin<>'' group by begin,end";
  51. $stmt = $dbh_sent->prepare($query);
  52. $stmt->execute(array($lang["language"], $para["book"], $para["paragraph"]));
  53. $result_sent = $stmt->fetchAll(PDO::FETCH_ASSOC);
  54. if (count($result_sent) > 0) {
  55. echo "book:{$para["book"]} para: {$para["paragraph"]}\n";
  56. #查询这些句子的总共等效巴利语字符数
  57. $place_holders = implode(',', array_fill(0, count($result_sent), '?'));
  58. $query = "select sum(length) as strlen from pali_sent where book = ? and paragraph = ? and begin in ($place_holders)";
  59. $sth = $dbh_pali_sent->prepare($query);
  60. $param = array();
  61. $param[] = $para["book"];
  62. $param[] = $para["paragraph"];
  63. foreach ($result_sent as $sent) {
  64. # code...
  65. $param[] = (int) $sent["begin"];
  66. }
  67. $sth->execute($param);
  68. $result_strlen = $sth->fetch(PDO::FETCH_ASSOC);
  69. if ($result_strlen) {
  70. $para_strlen = $result_strlen["strlen"];
  71. } else {
  72. $para_strlen = 0;
  73. }
  74. if ($r_conn) {
  75. $redis->hSet("progress_{$para["book"]}-{$para["paragraph"]}", $lang["language"], $para_strlen);
  76. } else {
  77. $sth_toc->execute(array($para["book"], $para["paragraph"], $lang["language"], $para_strlen, 0));
  78. }
  79. }
  80. }
  81. }
  82. $dbh_toc->commit();
  83. if (!$sth_toc || ($sth_toc && $sth_toc->errorCode() != 0)) {
  84. /* 识别错误且回滚更改 */
  85. $sth_toc->rollBack();
  86. $error = $dbh_toc->errorInfo();
  87. echo "error:" . $error[2] . "\n";
  88. }
  89. #第三步生成 段落完成度库
  90. /* 开始一个事务,关闭自动提交 */
  91. $dbh_toc->beginTransaction();
  92. $query = "INSERT INTO progress_chapter (book, para , lang , all_trans,public) VALUES (?, ?, ? , ? ,? )";
  93. $sth_toc = $dbh_toc->prepare($query);
  94. foreach ($valid_book as $key => $book) {
  95. echo "doing chapter in book " . $book["book"] . "\n";
  96. # code...
  97. $query = "SELECT paragraph , chapter_len from pali_text where level < 8 and book = ?";
  98. $stmt = $dbh_pali_text->prepare($query);
  99. $stmt->execute(array($book["book"]));
  100. $result_chapter = $stmt->fetchAll(PDO::FETCH_ASSOC);
  101. foreach ($result_chapter as $key => $chapter) {
  102. # 查询巴利字符数
  103. $query = "SELECT sum(strlen) as pali_strlen from pali_sent_index where book = ? and para between ? and ? ";
  104. $stmt = $dbh_pali_sent->prepare($query);
  105. $stmt->execute(array($book["book"], $chapter["paragraph"], (int) $chapter["paragraph"] + (int) $chapter["chapter_len"] - 1));
  106. $result_chapter_strlen = $stmt->fetch(PDO::FETCH_ASSOC);
  107. if ($result_chapter_strlen) {
  108. $pali_strlen = (int) $result_chapter_strlen["pali_strlen"];
  109. # 译文等效字符数
  110. foreach ($result_lang as $lang) {
  111. if ($r_conn) {
  112. $tran_strlen = 0;
  113. for ($i = $chapter["paragraph"]; $i < (int) $chapter["paragraph"] + (int) $chapter["chapter_len"]; $i++) {
  114. # code...
  115. $all_strlen = $redis->hGet("progress_{$book["book"]}-{$i}", $lang["language"]);
  116. if ($all_strlen) {
  117. $tran_strlen += $all_strlen;
  118. }
  119. }
  120. if ($tran_strlen > 0) {
  121. $progress = $tran_strlen / $pali_strlen;
  122. $redis->hSet("progress_chapter_{$book["book"]}_{$chapter["paragraph"]}", $lang["language"], $progress);
  123. }
  124. } else {
  125. $query = "SELECT sum(all_strlen) as all_strlen from progress where book = ? and (para between ? and ? )and lang = ?";
  126. $stmt = $dbh_toc->prepare($query);
  127. $stmt->execute(array($book["book"], $chapter["paragraph"], (int) $chapter["paragraph"] + (int) $chapter["chapter_len"] - 1, $lang["language"]));
  128. $result_chapter_trans_strlen = $stmt->fetch(PDO::FETCH_ASSOC);
  129. if ($result_chapter_trans_strlen) {
  130. $tran_strlen = (int) $result_chapter_trans_strlen["all_strlen"];
  131. if ($tran_strlen > 0) {
  132. $progress = $tran_strlen / $pali_strlen;
  133. $sth_toc->execute(array($book["book"], $chapter["paragraph"], $lang["language"], $progress, 0));
  134. }
  135. }
  136. }
  137. #插入段落数据
  138. }
  139. }
  140. }
  141. }
  142. $dbh_toc->commit();
  143. if (!$sth_toc || ($sth_toc && $sth_toc->errorCode() != 0)) {
  144. /* 识别错误且回滚更改 */
  145. $sth_toc->rollBack();
  146. $error = $dbh_toc->errorInfo();
  147. echo "error:" . $error[2] . "\n";
  148. }