db_insert_bold.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. require_once "./_pdo.php";
  16. require_once "../config.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 = (int)$_GET["from"];
  28. $to = (int)$_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. // 打开文件并读取数据
  76. if (($fp = fopen($dirXmlBase . $dirXml . $outputFileNameHead . ".csv", "r")) !== false) {
  77. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  78. $params = $data;
  79. $arrInserString[count($arrInserString)] = $params;
  80. }
  81. fclose($fp);
  82. echo "单词表load:" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv<br>";
  83. PDO_Connect(_FILE_DB_BOLD_);
  84. $query = "DELETE FROM "._TABLE_WORD_BOLD_." WHERE book=?";
  85. PDO_Execute($query,array($from+1));
  86. // 开始一个事务,关闭自动提交
  87. $PDO->beginTransaction();
  88. $query = "INSERT INTO "._TABLE_WORD_BOLD_." (book , paragraph , word , word2 , word_en ) VALUES (?,?,?,?,?)";
  89. $stmt = $PDO->prepare($query);
  90. $allcount = 1;
  91. $count = 0;
  92. $count1 = 0;
  93. $sen = "";
  94. $sen1 = "";
  95. $sen_en = "";
  96. $sen_count = 0;
  97. $book = "";
  98. $paragraph = "";
  99. foreach ($arrInserString as $oneParam) {
  100. if ($oneParam[15] == "bld") {
  101. if ($oneParam[5] != "") {
  102. $sen_count++;
  103. }
  104. $sen .= $oneParam[4] . " ";
  105. $sen1 .= $oneParam[5] . " ";
  106. $book = substr($oneParam[2], 1);
  107. $paragraph = $oneParam[3];
  108. if ($oneParam[5] != "") {
  109. $newWord = array($book, $paragraph, $oneParam[4], $oneParam[5], getWordEn($oneParam[5]));
  110. $stmt->execute($newWord);
  111. $count++;
  112. $allcount++;
  113. }
  114. } else {
  115. if ($sen_count > 1) {
  116. $sen = rtrim($sen);
  117. $sen1 = rtrim($sen1);
  118. $sen_en = getWordEn($sen1);
  119. $newWord = array($book, $paragraph, $sen, $sen1, $sen_en);
  120. $stmt->execute($newWord);
  121. $count1++;
  122. $allcount++;
  123. $sen = "";
  124. $sen1 = "";
  125. $sen_en = "";
  126. $sen_count = 0;
  127. } else {
  128. $sen = "";
  129. $sen1 = "";
  130. $sen_en = "";
  131. $sen_count = 0;
  132. }
  133. }
  134. }
  135. // 提交更改
  136. $PDO->commit();
  137. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  138. $error = PDO_ErrorInfo();
  139. echo "error - $error[2] <br>";
  140. $log = $log . "$from, $FileName, error, $error[2] \r\n";
  141. } else {
  142. echo "updata $count-$count1 recorders.";
  143. }
  144. $myLogFile = fopen($dirLog . "insert_bold.log", "a");
  145. fwrite($myLogFile, $log);
  146. fclose($myLogFile);
  147. } else {
  148. echo "can not open csv file. filename=" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv";
  149. }
  150. ?>
  151. <?php
  152. if ($from == $to) {
  153. echo "<h2>齐活!功德无量!all done!</h2>";
  154. } else {
  155. echo "<script>";
  156. echo "window.location.assign(\"db_insert_bold.php?from=" . ($from + 1) . "&to=" . $to . "\")";
  157. echo "</script>";
  158. echo "正在载入:" . ($from + 1) . "——" . $filelist[$from + 1][0];
  159. }
  160. ?>
  161. </body>
  162. </html>