db_insert_bold.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. </head>
  5. <body>
  6. <h2>Insert to bold</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_bold.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. $filelist=array();
  23. $fileNums=0;
  24. $log="";
  25. echo "<h2>$from</h2>";
  26. function getWordEn($strIn){
  27. $search = array('ā', 'ī', 'ū', 'ṅ', 'ñ' , 'ṭ', 'ḍ', 'ṇ', 'ḷ', 'ṃ');
  28. $replace = array('a', 'i', 'u', 'n', 'n' , 't', 'd', 'n', 'l', 'm');
  29. return(str_replace($search,$replace,$strIn));
  30. }
  31. if(($handle=fopen("filelist.csv",'r'))!==FALSE){
  32. while(($filelist[$fileNums]=fgetcsv($handle,0,','))!==FALSE){
  33. $fileNums++;
  34. }
  35. }
  36. if($to==0 || $to>=$fileNums) $to=$fileNums-1;
  37. $FileName=$filelist[$from][1].".htm";
  38. $fileId=$filelist[$from][0];
  39. $fileId=$filelist[$from][0];
  40. $dirLog="log/";
  41. $dirDb="db/";
  42. $inputFileName=$FileName;
  43. $outputFileNameHead=$filelist[$from][1];
  44. $bookId=$filelist[$from][2];
  45. $vriParNum=0;
  46. $wordOrder=1;
  47. $dirXmlBase="xml/";
  48. $dirXml=$outputFileNameHead."/";
  49. $currChapter="";
  50. $currParNum="";
  51. $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");
  52. $g_wordCounter=0;
  53. $arrUnPart[0]="word";
  54. $g_unPartCounter=-1;
  55. /*去掉标点符号的统计*/
  56. $arrAllPaliWordsCount=array();
  57. $g_paliWordCounter=0;
  58. $g_wordCounterInSutta=0;
  59. $g_paliWordCountCounter=0;
  60. $xmlfile = $inputFileName;
  61. echo "doing:".$xmlfile."<br>";
  62. $log=$log."$from,$FileName,open\r\n";
  63. $arrInserString=array();
  64. $db_file = $dirDb.'bold.db3';
  65. PDO_Connect("sqlite:$db_file");
  66. // 打开文件并读取数据
  67. if(($fp=fopen($dirXmlBase.$dirXml.$outputFileNameHead.".csv", "r"))!==FALSE){
  68. while(($data=fgetcsv($fp,0,','))!==FALSE){
  69. $params=$data;
  70. $arrInserString[count($arrInserString)]=$params;
  71. }
  72. fclose($fp);
  73. echo "单词表load:".$dirXmlBase.$dirXml.$outputFileNameHead.".csv<br>";
  74. }
  75. else{
  76. echo "can not open csv file. filename=".$dirXmlBase.$dirXml.$outputFileNameHead.".csv";
  77. }
  78. // 开始一个事务,关闭自动提交
  79. $PDO->beginTransaction();
  80. $query="INSERT INTO bold ('id','book','paragraph','word','word2','word_en') VALUES (NULL,?,?,?,?,?)";
  81. $stmt = $PDO->prepare($query);
  82. $allcount=1;
  83. $count=0;
  84. $count1=0;
  85. $sen="";
  86. $sen1="";
  87. $sen_en="";
  88. $sen_count=0;
  89. $book="";
  90. $paragraph="";
  91. foreach($arrInserString as $oneParam){
  92. if($oneParam[15]=="bld" ){
  93. if($oneParam[5]!=""){
  94. $sen_count++;
  95. }
  96. $sen.=$oneParam[4]." ";
  97. $sen1.=$oneParam[5]." ";
  98. $book=substr($oneParam[2],1);
  99. $paragraph=$oneParam[3];
  100. if($oneParam[5]!=""){
  101. $newWord=array($book,$paragraph,$oneParam[4],$oneParam[5],getWordEn($oneParam[5]));
  102. $stmt->execute($newWord);
  103. $count++;
  104. $allcount++;
  105. }
  106. }
  107. else{
  108. if($sen_count>1){
  109. $sen=rtrim($sen);
  110. $sen1=rtrim($sen1);
  111. $sen_en=getWordEn($sen1);
  112. $newWord=array($book,$paragraph,$sen,$sen1,$sen_en);
  113. $stmt->execute($newWord);
  114. $count1++;
  115. $allcount++;
  116. $sen="";
  117. $sen1="";
  118. $sen_en="";
  119. $sen_count=0;
  120. }
  121. else{
  122. $sen="";
  123. $sen1="";
  124. $sen_en="";
  125. $sen_count=0;
  126. }
  127. }
  128. }
  129. // 提交更改
  130. $PDO->commit();
  131. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  132. $error = PDO_ErrorInfo();
  133. echo "error - $error[2] <br>";
  134. $log=$log."$from, $FileName, error, $error[2] \r\n";
  135. }
  136. else{
  137. echo "updata $count-$count1 recorders.";
  138. }
  139. $myLogFile = fopen($dirLog."insert_bold.log", "a");
  140. fwrite($myLogFile, $log);
  141. fclose($myLogFile);
  142. ?>
  143. <?php
  144. if($from==$to){
  145. echo "<h2>all done!</h2>";
  146. }
  147. else{
  148. echo "<script>";
  149. echo "window.location.assign(\"db_insert_bold.php?from=".($from+1)."&to=".$to."\")";
  150. echo "</script>";
  151. echo "正在载入:".($from+1)."——".$filelist[$from+1][0];
  152. }
  153. ?>
  154. </body>
  155. </html>