db_insert_index.php 4.8 KB

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