db_update_palitext.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. </head>
  5. <body>
  6. <h2>Update Pali Text To single DB</h2>
  7. <p><a href="index.php">Home</a></p>
  8. <?php
  9. include "../public/_pdo.php";
  10. if(isset($_GET["from"])==false){
  11. ?>
  12. <form action="db_update_palitext.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. if(($handle=fopen("filelist.csv",'r'))!==FALSE){
  27. while(($filelist[$fileNums]=fgetcsv($handle,0,','))!==FALSE){
  28. $fileNums++;
  29. }
  30. }
  31. if($to==0 || $to>=$fileNums) $to=$fileNums-1;
  32. $FileName=$filelist[$from][1].".htm";
  33. $fileId=$filelist[$from][0];
  34. $fileId=$filelist[$from][0];
  35. $dirLog="log/";
  36. $dirDb="db/";
  37. $inputFileName=$FileName;
  38. $outputFileNameHead=$filelist[$from][1];
  39. $bookId=$filelist[$from][2];
  40. $vriParNum=0;
  41. $wordOrder=1;
  42. $dirXmlBase="xml/";
  43. $dirPaliTextBase="pali-text/";
  44. $dirXml=$outputFileNameHead."/";
  45. $xmlfile = $inputFileName;
  46. echo "doing:".$xmlfile."<br>";
  47. $log=$log."$from,$FileName,open\r\n";
  48. $arrInserString=array();
  49. // 打开vri html文件并读取数据
  50. $pali_text_array=array();//vri text
  51. if(($fpPaliText=fopen($dirPaliTextBase.$xmlfile, "r"))!==FALSE){
  52. while(($data=fgets($fpPaliText))!==FALSE){
  53. array_push($pali_text_array,$data);
  54. }
  55. fclose($fpPaliText);
  56. echo "pali text load:".$dirPaliTextBase.$xmlfile."<br>";
  57. }
  58. else{
  59. echo "can not pali text file. filename=".$dirPaliTextBase.$xmlfile;
  60. }
  61. // 打开text文件并读取数据
  62. $inputRow=0;
  63. if(($fp=fopen($dirXmlBase.$dirXml.$outputFileNameHead."_pali.csv", "r"))!==FALSE){
  64. while(($data=fgetcsv($fp,0,','))!==FALSE){
  65. if($inputRow>0){
  66. //if(($inputRow-1)<count($pali_text_array)){
  67. // $data[6]=$pali_text_array[$inputRow-1];
  68. //}
  69. $params=$data;
  70. array_push($arrInserString,$params);
  71. }
  72. $inputRow++;
  73. }
  74. fclose($fp);
  75. echo "单词表load:".$dirXmlBase.$dirXml.$outputFileNameHead.".csv<br>";
  76. }
  77. else{
  78. echo "can not open csv file. filename=".$dirXmlBase.$dirXml.$outputFileNameHead.".csv";
  79. }
  80. if(($inputRow-1)!=count($pali_text_array)){
  81. $log=$log."$from, $FileName,error,文件行数不匹配 inputRow=$inputRow pali_text_array=".count($pali_text_array)." \r\n";
  82. }
  83. $book=$from+1;
  84. $db_file = "pali_text_new/p{$book}_pali.db3";
  85. PDO_Connect("sqlite:$db_file");
  86. $query = "select paragraph,level,text from data where 1";
  87. echo $query;
  88. $Fetch_old = PDO_FetchAll($query);
  89. $db_file = $dirDb.'pali_text.db3';
  90. PDO_Connect("sqlite:$db_file");
  91. // 开始一个事务,关闭自动提交
  92. $PDO->beginTransaction();
  93. /*
  94. $query ="UPDATE pali_text SET text = ? WHERE book=? and paragraph=?";
  95. $stmt = $PDO->prepare($query);
  96. foreach($arrInserString as $oneParam){
  97. $bookid=substr($oneParam[1],1);
  98. $newData=array($oneParam[6],
  99. $bookid,
  100. $oneParam[2]);
  101. $stmt->execute($newData);
  102. }
  103. */
  104. $query ="UPDATE pali_text SET level = ? , toc = ? WHERE book=? and paragraph=?";
  105. $stmt = $PDO->prepare($query);
  106. foreach($Fetch_old as $onePara){
  107. $level=$onePara["level"];
  108. if($level==0){
  109. $level=100;
  110. }
  111. if($level==100){
  112. $maxLen=40;
  113. if(mb_strlen($onePara["text"],"UTF-8")>$maxLen){
  114. $toc=ltrim(mb_substr($onePara["text"],0,$maxLen,"UTF-8"))."…";
  115. }
  116. else{
  117. $toc=ltrim($onePara["text"]);
  118. }
  119. }
  120. else{
  121. $toc=ltrim($onePara["text"]);
  122. }
  123. $newData=array($level,
  124. $toc,
  125. $book,
  126. $onePara["paragraph"]);
  127. $stmt->execute($newData);
  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. $count=count($arrInserString);
  138. echo "updata $count recorders.";
  139. }
  140. $myLogFile = fopen($dirLog."db_insert_palitext.log", "a");
  141. fwrite($myLogFile, $log);
  142. fclose($myLogFile);
  143. ?>
  144. <?php
  145. if($from==$to){
  146. echo "<h2>all done!</h2>";
  147. }
  148. else{
  149. echo "<script>";
  150. echo "window.location.assign(\"db_update_palitext.php?from=".($from+1)."&to=".$to."\")";
  151. echo "</script>";
  152. echo "正在载入:".($from+1)."——".$filelist[$from+1][0];
  153. }
  154. ?>
  155. </body>
  156. </html>