db_update_palitext_cli.php 6.2 KB

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