db_insert_index_once.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. require_once "install_head.php";
  3. include "./_pdo.php";
  4. if (PHP_SAPI == "cli") {
  5. echo $argc;
  6. if ($argc >= 3) {
  7. $from = $argv[1];
  8. $to = $argv[2];
  9. echo "From: {$from} To:{$to}";
  10. } else if ($argc >= 1) {
  11. $from = 0;
  12. $to = 216;
  13. echo "生成全部217本书";
  14. } else {
  15. echo "参数错误";
  16. exit;
  17. }
  18. } else {
  19. echo "<!DOCTYPE html><html><head></head>";
  20. echo "<body><h2>Insert to Index</h2>";
  21. if (isset($_GET["from"]) == false) {
  22. echo '<form action="db_insert_index_once.php" method="get">';
  23. echo 'From: <input type="text" name="from" value="0"><br>';
  24. echo 'To: <input type="text" name="to" value="216"><br>';
  25. echo '<input type="submit">';
  26. echo '</form>';
  27. exit;
  28. } else {
  29. $from = $_GET["from"];
  30. $to = $_GET["to"];
  31. }
  32. }
  33. $g_wordCounter = 0;
  34. $g_wordIndexCounter = 0;
  35. $iAllWordIndex = array();
  36. $sAllWord = array();
  37. $dirLog = _DIR_LOG_ . "/";
  38. $dirXmlBase = _DIR_PALI_CSV_ . "/";
  39. $filelist = array();
  40. $fileNums = 0;
  41. $log = "";
  42. echo "<h2>$from</h2>";
  43. function getWordEn($strIn)
  44. {
  45. $out = $strIn;
  46. $out = str_replace("ā", "a", $out);
  47. $out = str_replace("ī", "i", $out);
  48. $out = str_replace("ū", "u", $out);
  49. $out = str_replace("ṅ", "n", $out);
  50. $out = str_replace("ñ", "n", $out);
  51. $out = str_replace("ṭ", "t", $out);
  52. $out = str_replace("ḍ", "d", $out);
  53. $out = str_replace("ṇ", "n", $out);
  54. $out = str_replace("ḷ", "l", $out);
  55. $out = str_replace("ṃ", "m", $out);
  56. return ($out);
  57. }
  58. if (($handle = fopen("filelist.csv", 'r')) !== false) {
  59. while (($filelist[$fileNums] = fgetcsv($handle, 0, ',')) !== false) {
  60. $fileNums++;
  61. }
  62. }
  63. if ($to == 0 || $to >= $fileNums) {
  64. $to = $fileNums - 1;
  65. }
  66. $db_file = _FILE_DB_INDEX_;
  67. PDO_Connect("$db_file");
  68. for ($iFile = $from; $iFile <= $to; $iFile++) {
  69. echo "<h3>{$iFile}</h3>";
  70. $FileName = $filelist[$iFile][1] . ".htm";
  71. $fileId = $filelist[$iFile][0];
  72. $inputFileName = $FileName;
  73. $outputFileNameHead = $filelist[$iFile][1];
  74. $bookId = $filelist[$iFile][2];
  75. $dirXml = $outputFileNameHead . "/";
  76. $xmlfile = $inputFileName;
  77. echo "doing:" . $xmlfile . "<br>";
  78. $log = $log . "$iFile,$FileName,open\r\n";
  79. $arrInserString = array();
  80. // 打开文件并读取数据
  81. $irow = 0;
  82. if (($fp = fopen($dirXmlBase . $dirXml . $outputFileNameHead . ".csv", "r")) !== false) {
  83. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  84. $irow++;
  85. if ($irow > 1) {
  86. $params = $data;
  87. $arrInserString[count($arrInserString)] = $params;
  88. }
  89. }
  90. fclose($fp);
  91. echo "单词表load:" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv<br>";
  92. } else {
  93. echo "can not open csv file. filename=" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv";
  94. }
  95. // 开始一个事务,关闭自动提交
  96. $PDO->beginTransaction();
  97. $query = "INSERT INTO word ('id','book','paragraph','wordindex','bold') VALUES (?,?,?,?,?)";
  98. $stmt = $PDO->prepare($query);
  99. $count = 0;
  100. $count1 = 0;
  101. $sen = "";
  102. $sen1 = "";
  103. $sen_en = "";
  104. $sen_count = 0;
  105. $book = "";
  106. $paragraph = "";
  107. foreach ($arrInserString as $oneParam) {
  108. if ($oneParam[5] != "") {
  109. $g_wordCounter++;
  110. $book = substr($oneParam[2], 1);
  111. $paragraph = $oneParam[3];
  112. $word = $oneParam[5];
  113. if ($oneParam[15] == "bld") {
  114. $bold = 1;
  115. } else {
  116. $bold = 0;
  117. }
  118. if (isset($sAllWord[$word])) {
  119. $wordindex = $sAllWord[$word];
  120. $iAllWordIndex[$wordindex][1]++;
  121. if ($bold == 1) {
  122. $iAllWordIndex[$wordindex][3]++;
  123. } else {
  124. $iAllWordIndex[$wordindex][2]++;
  125. }
  126. } else {
  127. $wordindex = $g_wordIndexCounter;
  128. $sAllWord[$word] = $g_wordIndexCounter;
  129. $iAllWordIndex[$g_wordIndexCounter][0] = $word;
  130. $iAllWordIndex[$g_wordIndexCounter][1] = 1; //all word count
  131. if ($bold == 1) {
  132. $iAllWordIndex[$g_wordIndexCounter][2] = 0;
  133. $iAllWordIndex[$g_wordIndexCounter][3] = 1;
  134. } else {
  135. $iAllWordIndex[$g_wordIndexCounter][2] = 1;
  136. $iAllWordIndex[$g_wordIndexCounter][3] = 0;
  137. }
  138. $g_wordIndexCounter++;
  139. }
  140. $newWord = array($g_wordCounter, $book, $paragraph, $wordindex, $bold);
  141. $stmt->execute($newWord);
  142. $count++;
  143. }
  144. }
  145. // 提交更改
  146. $PDO->commit();
  147. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  148. $error = PDO_ErrorInfo();
  149. echo "error - $error[2] <br>";
  150. $log .= "$from, $FileName, error, $error[2] \r\n";
  151. } else {
  152. echo "updata $count recorders.<br />";
  153. $log .= "updata $count recorders.\r\n";
  154. }
  155. }
  156. // 开始一个事务,关闭自动提交
  157. $PDO->beginTransaction();
  158. $query = "INSERT INTO wordindex ('id','word','word_en','count','normal','bold','is_base','len') VALUES (?,?,?,?,?,?,?,?)";
  159. $stmt = $PDO->prepare($query);
  160. echo count($iAllWordIndex) . "words<br>";
  161. for ($iword = 0; $iword < count($iAllWordIndex); $iword++) {
  162. $wordindex = $iword;
  163. $newWord = array($wordindex, $iAllWordIndex[$iword][0], getWordEn($iAllWordIndex[$iword][0]), $iAllWordIndex[$iword][1], $iAllWordIndex[$iword][2], $iAllWordIndex[$iword][3], 0, mb_strlen($iAllWordIndex[$iword][0], "UTF-8"));
  164. //echo "<br>{$newWord[0]}-{$newWord[1]}-{$newWord[2]}-{$newWord[3]}-{$newWord[4]}-{$newWord[5]}-<br />";
  165. $stmt->execute($newWord);
  166. }
  167. // 提交更改
  168. $PDO->commit();
  169. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  170. $error = PDO_ErrorInfo();
  171. echo "error - $error[2] <br>";
  172. $log .= "$from, $FileName, error, $error[2] \r\n";
  173. } else {
  174. echo "updata iword recorders.<br />";
  175. $log .= "updata iword recorders.\r\n";
  176. }
  177. $myLogFile = fopen($dirLog . "insert_index.log", "a");
  178. fwrite($myLogFile, $log);
  179. fclose($myLogFile);
  180. echo "<h2>齐活!功德无量!all done!</h2>";
  181. ?>
  182. </body>
  183. </html>