db_update_palitext_cli.php 6.0 KB

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