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