db_insert_index.php 4.9 KB

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