db_insert_index_once.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. PDO_Connect(_FILE_DB_INDEX_);
  67. for ($iFile = $from; $iFile <= $to; $iFile++) {
  68. echo "<h3>{$iFile}</h3>";
  69. $FileName = $filelist[$iFile][1] . ".htm";
  70. $fileId = $filelist[$iFile][0];
  71. $inputFileName = $FileName;
  72. $outputFileNameHead = $filelist[$iFile][1];
  73. $bookId = $filelist[$iFile][2];
  74. $dirXml = $outputFileNameHead . "/";
  75. $xmlfile = $inputFileName;
  76. echo "doing:" . $xmlfile . "<br>";
  77. $log = $log . "$iFile,$FileName,open\r\n";
  78. $arrInserString = array();
  79. // 打开文件并读取数据
  80. $irow = 0;
  81. if (($fp = fopen($dirXmlBase . $dirXml . $outputFileNameHead . ".csv", "r")) !== false) {
  82. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  83. $irow++;
  84. if ($irow > 1) {
  85. $params = $data;
  86. $arrInserString[count($arrInserString)] = $params;
  87. }
  88. }
  89. fclose($fp);
  90. echo "单词表load:" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv<br>";
  91. } else {
  92. echo "can not open csv file. filename=" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv";
  93. }
  94. // 开始一个事务,关闭自动提交
  95. $PDO->beginTransaction();
  96. $query = "INSERT INTO "._TABLE_WORD_." ('id','book','paragraph','wordindex','bold') VALUES (?,?,?,?,?)";
  97. $stmt = $PDO->prepare($query);
  98. $count = 0;
  99. $count1 = 0;
  100. $sen = "";
  101. $sen1 = "";
  102. $sen_en = "";
  103. $sen_count = 0;
  104. $book = "";
  105. $paragraph = "";
  106. foreach ($arrInserString as $oneParam) {
  107. if ($oneParam[5] != "") {
  108. $g_wordCounter++;
  109. $book = substr($oneParam[2], 1);
  110. $paragraph = $oneParam[3];
  111. $word = $oneParam[5];
  112. if ($oneParam[15] == "bld") {
  113. $bold = 1;
  114. } else {
  115. $bold = 0;
  116. }
  117. if (isset($sAllWord[$word])) {
  118. $wordindex = $sAllWord[$word];
  119. $iAllWordIndex[$wordindex][1]++;
  120. if ($bold == 1) {
  121. $iAllWordIndex[$wordindex][3]++;
  122. } else {
  123. $iAllWordIndex[$wordindex][2]++;
  124. }
  125. } else {
  126. $wordindex = $g_wordIndexCounter;
  127. $sAllWord[$word] = $g_wordIndexCounter;
  128. $iAllWordIndex[$g_wordIndexCounter][0] = $word;
  129. $iAllWordIndex[$g_wordIndexCounter][1] = 1; //all word count
  130. if ($bold == 1) {
  131. $iAllWordIndex[$g_wordIndexCounter][2] = 0;
  132. $iAllWordIndex[$g_wordIndexCounter][3] = 1;
  133. } else {
  134. $iAllWordIndex[$g_wordIndexCounter][2] = 1;
  135. $iAllWordIndex[$g_wordIndexCounter][3] = 0;
  136. }
  137. $g_wordIndexCounter++;
  138. }
  139. $newWord = array($g_wordCounter, $book, $paragraph, $wordindex, $bold);
  140. $stmt->execute($newWord);
  141. $count++;
  142. }
  143. }
  144. // 提交更改
  145. $PDO->commit();
  146. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  147. $error = PDO_ErrorInfo();
  148. echo "error - $error[2] <br>";
  149. $log .= "$from, $FileName, error, $error[2] \r\n";
  150. } else {
  151. echo "updata $count recorders.<br />";
  152. $log .= "updata $count recorders.\r\n";
  153. }
  154. }
  155. // 开始一个事务,关闭自动提交
  156. $PDO->beginTransaction();
  157. $query = "INSERT INTO "._TABLE_WORD_INDEX_." ('id','word','word_en','count','normal','bold','is_base','len') VALUES (?,?,?,?,?,?,?,?)";
  158. $stmt = $PDO->prepare($query);
  159. echo count($iAllWordIndex) . "words<br>";
  160. for ($iword = 0; $iword < count($iAllWordIndex); $iword++) {
  161. $wordindex = $iword;
  162. $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"));
  163. //echo "<br>{$newWord[0]}-{$newWord[1]}-{$newWord[2]}-{$newWord[3]}-{$newWord[4]}-{$newWord[5]}-<br />";
  164. $stmt->execute($newWord);
  165. }
  166. // 提交更改
  167. $PDO->commit();
  168. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  169. $error = PDO_ErrorInfo();
  170. echo "error - $error[2] <br>";
  171. $log .= "$from, $FileName, error, $error[2] \r\n";
  172. } else {
  173. echo "updata iword recorders.<br />";
  174. $log .= "updata iword recorders.\r\n";
  175. }
  176. $myLogFile = fopen($dirLog . "insert_index.log", "a");
  177. fwrite($myLogFile, $log);
  178. fclose($myLogFile);
  179. echo "<h2>齐活!功德无量!all done!</h2>";
  180. ?>
  181. </body>
  182. </html>