upgrade_pali_toc.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. #升级段落完成度数据库
  3. require_once __DIR__.'/../config.php';
  4. require_once __DIR__.'/../redis/function.php';
  5. $redis = redis_connect();
  6. $dns = _FILE_DB_PALI_TOC_;
  7. $dbh_toc = new PDO($dns, _DB_USERNAME_, _DB_PASSWORD_, 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, _DB_USERNAME_, _DB_PASSWORD_, 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, _DB_USERNAME_, _DB_PASSWORD_, 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, _DB_USERNAME_, _DB_PASSWORD_, 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 "._TABLE_PROGRESS_." WHERE true";
  32. $sth_toc = $dbh_toc->prepare($query);
  33. $sth_toc->execute();
  34. $query = "DELETE FROM "._TABLE_PROGRESS_CHAPTER_." WHERE true";
  35. $sth_toc = $dbh_toc->prepare($query);
  36. $sth_toc->execute();
  37. /* 开始一个事务,关闭自动提交 */
  38. $dbh_toc->beginTransaction();
  39. $query = "INSERT INTO "._TABLE_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 "._TABLE_PALI_SENT_." where book = ? and paragraph = ? and word_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($redis){
  75. $redis->hSet("progress_{$para["book"]}-{$para["paragraph"]}", $lang["language"], $para_strlen);
  76. }
  77. {
  78. $sth_toc->execute(array($para["book"], $para["paragraph"], $lang["language"], $para_strlen, 0));
  79. }
  80. }
  81. }
  82. }
  83. $dbh_toc->commit();
  84. if (!$sth_toc || ($sth_toc && $sth_toc->errorCode() != 0)) {
  85. /* 识别错误且回滚更改 */
  86. $sth_toc->rollBack();
  87. $error = $dbh_toc->errorInfo();
  88. echo "error:" . $error[2] . "\n";
  89. }
  90. #第三步生成 段落完成度库
  91. /* 开始一个事务,关闭自动提交 */
  92. $dbh_toc->beginTransaction();
  93. $query = "INSERT INTO "._TABLE_PROGRESS_CHAPTER_." (book, para , lang , all_trans,public) VALUES (?, ?, ? , ? ,? )";
  94. $sth_toc = $dbh_toc->prepare($query);
  95. foreach ($valid_book as $key => $book) {
  96. echo "doing chapter in book " . $book["book"] . "\n";
  97. # code...
  98. $query = "SELECT paragraph , chapter_len from "._TABLE_PALI_TEXT_." where level < 8 and book = ?";
  99. $stmt = $dbh_pali_text->prepare($query);
  100. $stmt->execute(array($book["book"]));
  101. $result_chapter = $stmt->fetchAll(PDO::FETCH_ASSOC);
  102. foreach ($result_chapter as $key => $chapter) {
  103. # 查询巴利字符数
  104. $query = "SELECT sum(strlen) as pali_strlen from "._TABLE_PALI_SENT_INDEX_." where book = ? and para between ? and ? ";
  105. $stmt = $dbh_pali_sent->prepare($query);
  106. $stmt->execute(array($book["book"], $chapter["paragraph"], (int) $chapter["paragraph"] + (int) $chapter["chapter_len"] - 1));
  107. $result_chapter_strlen = $stmt->fetch(PDO::FETCH_ASSOC);
  108. if ($result_chapter_strlen) {
  109. $pali_strlen = (int) $result_chapter_strlen["pali_strlen"];
  110. # 译文等效字符数
  111. foreach ($result_lang as $lang) {
  112. if ($redis) {
  113. $tran_strlen = 0;
  114. for ($i = $chapter["paragraph"]; $i < (int) $chapter["paragraph"] + (int) $chapter["chapter_len"]; $i++) {
  115. # code...
  116. $all_strlen = $redis->hGet("progress_{$book["book"]}-{$i}", $lang["language"]);
  117. if ($all_strlen) {
  118. $tran_strlen += $all_strlen;
  119. }
  120. }
  121. if ($tran_strlen > 0) {
  122. $progress = $tran_strlen / $pali_strlen;
  123. $redis->hSet("progress_chapter_{$book["book"]}_{$chapter["paragraph"]}", $lang["language"], $progress);
  124. }
  125. }
  126. {
  127. $query = "SELECT sum(all_strlen) as all_strlen from "._TABLE_PROGRESS_." where book = ? and (para between ? and ? )and lang = ?";
  128. $stmt = $dbh_toc->prepare($query);
  129. $stmt->execute(array($book["book"], $chapter["paragraph"], (int) $chapter["paragraph"] + (int) $chapter["chapter_len"] - 1, $lang["language"]));
  130. $result_chapter_trans_strlen = $stmt->fetch(PDO::FETCH_ASSOC);
  131. if ($result_chapter_trans_strlen) {
  132. $tran_strlen = (int) $result_chapter_trans_strlen["all_strlen"];
  133. if ($tran_strlen > 0) {
  134. $progress = $tran_strlen / $pali_strlen;
  135. $sth_toc->execute(array($book["book"], $chapter["paragraph"], $lang["language"], $progress, 0));
  136. }
  137. }
  138. }
  139. #插入段落数据
  140. }
  141. }
  142. }
  143. }
  144. $dbh_toc->commit();
  145. if (!$sth_toc || ($sth_toc && $sth_toc->errorCode() != 0)) {
  146. /* 识别错误且回滚更改 */
  147. $sth_toc->rollBack();
  148. $error = $dbh_toc->errorInfo();
  149. echo "error:" . $error[2] . "\n";
  150. }