db_update_palitext.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. #根据pali_text 里面的*_title.csv数据更新数据库
  3. require_once "install_head.php";
  4. require_once "../public/_pdo.php";
  5. ?>
  6. <!DOCTYPE html>
  7. <html>
  8. <head>
  9. </head>
  10. <body>
  11. <h2>Update Pali Text Use CSV</h2>
  12. <p><a href="index.php">Home</a></p>
  13. <?php
  14. if (isset($_GET["from"]) == false) {
  15. ?>
  16. <form action="db_update_palitext.php" method="get">
  17. From: <input type="text" name="from" value="0"><br>
  18. To: <input type="text" name="to" value="216"><br>
  19. <input type="submit">
  20. </form>
  21. <?php
  22. return;
  23. }
  24. $from = $_GET["from"];
  25. $to = $_GET["to"];
  26. $filelist = array();
  27. $fileNums = 0;
  28. $log = "";
  29. echo "<h2>$from</h2>";
  30. if (($handle = fopen("filelist.csv", 'r')) !== false) {
  31. while (($filelist[$fileNums] = fgetcsv($handle, 0, ',')) !== false) {
  32. $fileNums++;
  33. }
  34. }
  35. if ($to == 0 || $to >= $fileNums) {
  36. $to = $fileNums - 1;
  37. }
  38. $FileName = $filelist[$from][1] . ".htm";
  39. $fileId = $filelist[$from][0];
  40. $fileId = $filelist[$from][0];
  41. $dirLog = _DIR_LOG_ . "/";
  42. $dirDb = "/";
  43. $inputFileName = $FileName;
  44. $outputFileNameHead = $filelist[$from][1];
  45. $bookId = $filelist[$from][2];
  46. $vriParNum = 0;
  47. $wordOrder = 1;
  48. $dirXmlBase = _DIR_PALI_CSV_ . "/";
  49. $dirPaliTextBase = _DIR_PALI_HTML_ . "/";
  50. $dirXml = $outputFileNameHead . "/";
  51. $xmlfile = $inputFileName;
  52. echo "doing:" . $xmlfile . "<br>";
  53. $log = $log . date("Y-m-d h:i:sa") . ",$from,$FileName,open\r\n";
  54. $arrInserString = array();
  55. // 打开vri html文件并读取数据
  56. $pali_text_array = array(); //vri text
  57. if (($fpPaliText = fopen($dirPaliTextBase . $xmlfile, "r")) !== false) {
  58. while (($data = fgets($fpPaliText)) !== false) {
  59. array_push($pali_text_array, $data);
  60. }
  61. fclose($fpPaliText);
  62. echo "pali text load:" . $dirPaliTextBase . $xmlfile . "<br>";
  63. } else {
  64. echo "can not pali text file. filename=" . $dirPaliTextBase . $xmlfile;
  65. }
  66. // 打开csv文件并读取数据
  67. $inputRow = 0;
  68. if (($fp = fopen(_DIR_PALI_TITLE_ . "/" . ($from + 1) . "_title.csv", "r")) !== false) {
  69. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  70. if ($inputRow > 0) {
  71. $params = $data;
  72. array_push($arrInserString, $params);
  73. }
  74. $inputRow++;
  75. }
  76. fclose($fp);
  77. echo "单词表load:" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv<br>";
  78. } else {
  79. echo "can not open csv file. filename=" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv";
  80. }
  81. if ((count($arrInserString)) != count($pali_text_array) - 2) {
  82. $log = $log . "$from, $FileName,error,文件行数不匹配 csv = " . (count($arrInserString) - 1) . " pali_text_array=" . (count($pali_text_array) - 2) . " \r\n";
  83. }
  84. $book = $from + 1;
  85. //计算段落信息,如上一段
  86. PDO_Connect(_FILE_DB_PALITEXT_,_DB_USERNAME_,_DB_PASSWORD_);
  87. $query = "SELECT * from "._TABLE_PALI_TEXT_." where book = '$book' order by paragraph asc";
  88. $title_data = PDO_FetchAll($query);
  89. echo "Paragraph Count:" . count($title_data) . " arrInserString:".count($arrInserString)." <br>";
  90. $paragraph_count = count($title_data);
  91. // 开始一个事务,关闭自动提交
  92. $PDO->beginTransaction();
  93. $query = "UPDATE "._TABLE_PALI_TEXT_." SET level = ? , toc = ? , chapter_len = ? , next_chapter = ?, prev_chapter=? , parent= ? , chapter_strlen = ? WHERE book=? and paragraph=?";
  94. $stmt = $PDO->prepare($query);
  95. $paragraph_info = array();
  96. array_push($paragraph_info, array($from, -1, $paragraph_count, -1, -1, -1));
  97. for ($iPar = 0; $iPar < count($title_data); $iPar++) {
  98. $title_data[$iPar]["level"] = $arrInserString[$iPar][3];
  99. }
  100. for ($iPar = 0; $iPar < count($title_data); $iPar++) {
  101. $book = $from + 1;
  102. $paragraph = $title_data[$iPar]["paragraph"];
  103. if ((int) $title_data[$iPar]["level"] == 8) {
  104. $title_data[$iPar]["level"] = 100;
  105. }
  106. $curr_level = (int) $title_data[$iPar]["level"];
  107. # 计算这个chapter的段落数量
  108. $length = -1;
  109. for ($iPar1 = $iPar + 1; $iPar1 < count($title_data); $iPar1++) {
  110. $thislevel = (int) $title_data[$iPar1]["level"];
  111. if ($thislevel <= $curr_level) {
  112. $length = (int) $title_data[$iPar1]["paragraph"] - $paragraph;
  113. break;
  114. }
  115. }
  116. if ($length == -1) {
  117. $length = $paragraph_count - $paragraph + 1;
  118. }
  119. $prev = -1;
  120. if ($iPar > 0) {
  121. for ($iPar1 = $iPar - 1; $iPar1 >= 0; $iPar1--) {
  122. if ($title_data[$iPar1]["level"] == $curr_level) {
  123. $prev = $title_data[$iPar1]["paragraph"];
  124. break;
  125. }
  126. }
  127. }
  128. $next = -1;
  129. if ($iPar < count($title_data) - 1) {
  130. for ($iPar1 = $iPar + 1; $iPar1 < count($title_data); $iPar1++) {
  131. if ($title_data[$iPar1]["level"] == $curr_level) {
  132. $next = $title_data[$iPar1]["paragraph"];
  133. break;
  134. }
  135. }
  136. }
  137. $parent = -1;
  138. if ($iPar > 0) {
  139. for ($iPar1 = $iPar - 1; $iPar1 >= 0; $iPar1--) {
  140. if ($title_data[$iPar1]["level"] < $curr_level) {
  141. $parent = $title_data[$iPar1]["paragraph"];
  142. break;
  143. }
  144. }
  145. }
  146. //计算章节包含总字符数
  147. $iChapter_strlen = 0;
  148. for ($i = $iPar; $i < $iPar + $length; $i++) {
  149. $iChapter_strlen += $title_data[$i]["lenght"];
  150. }
  151. $newData = array(
  152. $arrInserString[$iPar][3],
  153. $arrInserString[$iPar][5],
  154. $length,
  155. $next,
  156. $prev,
  157. $parent,
  158. $iChapter_strlen,
  159. $book,
  160. $paragraph,
  161. );
  162. $stmt->execute($newData);
  163. if ($curr_level > 0 && $curr_level < 8) {
  164. array_push($paragraph_info, array($book, $paragraph, $length, $prev, $next, $parent));
  165. }
  166. }
  167. // 提交更改
  168. $PDO->commit();
  169. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  170. $error = PDO_ErrorInfo();
  171. echo "error - $error[2] <br>";
  172. $log = $log . "$from, $FileName, error, $error[2] \r\n";
  173. } else {
  174. $count = count($title_data);
  175. echo "updata $count paragraph info recorders.<br>";
  176. echo count($paragraph_info) . " Heading<br>";
  177. }
  178. //段落信息结束
  179. $myLogFile = fopen(_DIR_LOG_ . "/db_update_palitext.log", "a");
  180. fwrite($myLogFile, $log);
  181. fclose($myLogFile);
  182. ?>
  183. <?php
  184. if ($from == $to) {
  185. echo "<h2>齐活!功德无量!all done!</h2>";
  186. } else {
  187. echo "<script>";
  188. echo "window.location.assign(\"db_update_palitext.php?from=" . ($from + 1) . "&to=" . $to . "\")";
  189. echo "</script>";
  190. echo "正在载入:" . ($from + 1) . "——" . $filelist[$from + 1][0];
  191. }
  192. ?>
  193. </body>
  194. </html>