db_insert_bold.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. require_once "install_head.php";
  3. ?>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. </head>
  8. <body>
  9. <h2>Insert to bold</h2>
  10. <p><a href="index.php">Home</a></p>
  11. <div>
  12. 生成黑体字数据库。黑体字多数是义注复注里的单词(尤其是专有名词)解释。
  13. </div>
  14. <?php
  15. include "./_pdo.php";
  16. include "../path.php";
  17. if (isset($_GET["from"]) == false) {
  18. ?>
  19. <form action="db_insert_bold.php" method="get">
  20. From: <input type="text" name="from" value="0"><br>
  21. To: <input type="text" name="to" value="216"><br>
  22. <input type="submit">
  23. </form>
  24. <?php
  25. return;
  26. }
  27. $from = $_GET["from"];
  28. $to = $_GET["to"];
  29. $filelist = array();
  30. $fileNums = 0;
  31. $log = "";
  32. echo "<h2>$from</h2>";
  33. function getWordEn($strIn)
  34. {
  35. $search = array('ā', 'ī', 'ū', 'ṅ', 'ñ', 'ṭ', 'ḍ', 'ṇ', 'ḷ', 'ṃ');
  36. $replace = array('a', 'i', 'u', 'n', 'n', 't', 'd', 'n', 'l', 'm');
  37. return (str_replace($search, $replace, $strIn));
  38. }
  39. if (($handle = fopen("filelist.csv", 'r')) !== false) {
  40. while (($filelist[$fileNums] = fgetcsv($handle, 0, ',')) !== false) {
  41. $fileNums++;
  42. }
  43. }
  44. if ($to == 0 || $to >= $fileNums) {
  45. $to = $fileNums - 1;
  46. }
  47. $FileName = $filelist[$from][1] . ".htm";
  48. $fileId = $filelist[$from][0];
  49. $fileId = $filelist[$from][0];
  50. $dirLog = _DIR_LOG_;
  51. //输出文件夹
  52. $dirDb = _DIR_PALICANON_ . "/";
  53. $inputFileName = $FileName;
  54. $outputFileNameHead = $filelist[$from][1];
  55. $bookId = $filelist[$from][2];
  56. $vriParNum = 0;
  57. $wordOrder = 1;
  58. $dirXmlBase = _DIR_PALI_CSV_ . "/";
  59. $dirXml = $outputFileNameHead . "/";
  60. $currChapter = "";
  61. $currParNum = "";
  62. $arrAllWords[0] = array("id", "wid", "book", "paragraph", "word", "real", "type", "gramma", "mean", "note", "part", "partmean", "bmc", "bmt", "un", "style", "vri", "sya", "si", "ka", "pi", "pa", "kam");
  63. $g_wordCounter = 0;
  64. $arrUnPart[0] = "word";
  65. $g_unPartCounter = -1;
  66. /*去掉标点符号的统计*/
  67. $arrAllPaliWordsCount = array();
  68. $g_paliWordCounter = 0;
  69. $g_wordCounterInSutta = 0;
  70. $g_paliWordCountCounter = 0;
  71. $xmlfile = $inputFileName;
  72. echo "doing:" . $xmlfile . "<br>";
  73. $log = $log . "$from,$FileName,open\r\n";
  74. $arrInserString = array();
  75. $db_file = _FILE_DB_BOLD_;
  76. PDO_Connect("$db_file");
  77. // 打开文件并读取数据
  78. if (($fp = fopen($dirXmlBase . $dirXml . $outputFileNameHead . ".csv", "r")) !== false) {
  79. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  80. $params = $data;
  81. $arrInserString[count($arrInserString)] = $params;
  82. }
  83. fclose($fp);
  84. echo "单词表load:" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv<br>";
  85. } else {
  86. echo "can not open csv file. filename=" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv";
  87. }
  88. // 开始一个事务,关闭自动提交
  89. $PDO->beginTransaction();
  90. $query = "INSERT INTO bold ('id','book','paragraph','word','word2','word_en') VALUES (NULL,?,?,?,?,?)";
  91. $stmt = $PDO->prepare($query);
  92. $allcount = 1;
  93. $count = 0;
  94. $count1 = 0;
  95. $sen = "";
  96. $sen1 = "";
  97. $sen_en = "";
  98. $sen_count = 0;
  99. $book = "";
  100. $paragraph = "";
  101. foreach ($arrInserString as $oneParam) {
  102. if ($oneParam[15] == "bld") {
  103. if ($oneParam[5] != "") {
  104. $sen_count++;
  105. }
  106. $sen .= $oneParam[4] . " ";
  107. $sen1 .= $oneParam[5] . " ";
  108. $book = substr($oneParam[2], 1);
  109. $paragraph = $oneParam[3];
  110. if ($oneParam[5] != "") {
  111. $newWord = array($book, $paragraph, $oneParam[4], $oneParam[5], getWordEn($oneParam[5]));
  112. $stmt->execute($newWord);
  113. $count++;
  114. $allcount++;
  115. }
  116. } else {
  117. if ($sen_count > 1) {
  118. $sen = rtrim($sen);
  119. $sen1 = rtrim($sen1);
  120. $sen_en = getWordEn($sen1);
  121. $newWord = array($book, $paragraph, $sen, $sen1, $sen_en);
  122. $stmt->execute($newWord);
  123. $count1++;
  124. $allcount++;
  125. $sen = "";
  126. $sen1 = "";
  127. $sen_en = "";
  128. $sen_count = 0;
  129. } else {
  130. $sen = "";
  131. $sen1 = "";
  132. $sen_en = "";
  133. $sen_count = 0;
  134. }
  135. }
  136. }
  137. // 提交更改
  138. $PDO->commit();
  139. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  140. $error = PDO_ErrorInfo();
  141. echo "error - $error[2] <br>";
  142. $log = $log . "$from, $FileName, error, $error[2] \r\n";
  143. } else {
  144. echo "updata $count-$count1 recorders.";
  145. }
  146. $myLogFile = fopen($dirLog . "insert_bold.log", "a");
  147. fwrite($myLogFile, $log);
  148. fclose($myLogFile);
  149. ?>
  150. <?php
  151. if ($from == $to) {
  152. echo "<h2>齐活!功德无量!all done!</h2>";
  153. } else {
  154. echo "<script>";
  155. echo "window.location.assign(\"db_insert_bold.php?from=" . ($from + 1) . "&to=" . $to . "\")";
  156. echo "</script>";
  157. echo "正在载入:" . ($from + 1) . "——" . $filelist[$from + 1][0];
  158. }
  159. ?>
  160. </body>
  161. </html>