db_insert_palitext.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. </head>
  5. <body>
  6. <h2>Insert Pali Text To DB</h2>
  7. <p><a href="index.php">Home</a></p>
  8. <?php
  9. require_once "./_pdo.php";
  10. require_once "../path.php";
  11. if(isset($_GET["from"])==false){
  12. ?>
  13. <form action="db_insert_palitext.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. $filelist=array();
  24. $fileNums=0;
  25. $log="";
  26. echo "<h2>$from</h2>";
  27. if(($handle=fopen("filelist.csv",'r'))!==FALSE){
  28. while(($filelist[$fileNums]=fgetcsv($handle,0,','))!==FALSE){
  29. $fileNums++;
  30. }
  31. }
  32. if($to==0 || $to>=$fileNums) $to=$fileNums-1;
  33. $FileName=$filelist[$from][1].".htm";
  34. $fileId=$filelist[$from][0];
  35. $fileId=$filelist[$from][0];
  36. $dirLog=_DIR_LOG_."/";
  37. $inputFileName=$FileName;
  38. $outputFileNameHead=$filelist[$from][1];
  39. $bookId=$filelist[$from][2];
  40. $vriParNum=0;
  41. $wordOrder=1;
  42. $dirXmlBase=_DIR_PALI_CSV_."/";
  43. $dirPaliTextBase=_DIR_PALI_HTML_."/";
  44. $dirXml=$outputFileNameHead."/";
  45. $xmlfile = $inputFileName;
  46. echo "doing:".$xmlfile."<br>";
  47. $log=$log."$from,$FileName,open\r\n";
  48. $arrInserString=array();
  49. $db_file =_FILE_DB_PALITEXT_;
  50. PDO_Connect("sqlite:$db_file");
  51. // 打开vri html文件并读取数据
  52. $pali_text_array=array();
  53. if(($fpPaliText=fopen($dirPaliTextBase.$xmlfile, "r"))!==FALSE){
  54. while(($data=fgets($fpPaliText))!==FALSE){
  55. array_push($pali_text_array,$data);
  56. }
  57. fclose($fpPaliText);
  58. echo "pali text load:".$dirPaliTextBase.$xmlfile."<br>";
  59. }
  60. else{
  61. echo "can not pali text file. filename=".$dirPaliTextBase.$xmlfile;
  62. }
  63. $inputRow=0;
  64. if(($fp=fopen($dirXmlBase.$dirXml.$outputFileNameHead."_pali.csv", "r"))!==FALSE){
  65. while(($data=fgetcsv($fp , 0 , ',' )) !== FALSE ){
  66. if($inputRow>0){
  67. if(($inputRow-1)<count($pali_text_array)){
  68. $data[5]=$pali_text_array[$inputRow-1];
  69. }
  70. $arrInserString[]=$data;
  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. // 开始一个事务,关闭自动提交
  84. $PDO->beginTransaction();
  85. $query="INSERT INTO pali_text ('id', 'book','paragraph','level','class','toc','text','html','lenght') VALUES (NULL, ? , ? , ? , ? , ? , ? , ?,? )";
  86. $stmt = $PDO->prepare($query);
  87. foreach($arrInserString as $oneParam){
  88. if($oneParam[3]<100){
  89. $toc = $oneParam[6];
  90. }
  91. else{
  92. $toc = "";
  93. }
  94. $newData=array($from+1, $oneParam[2], $oneParam[3], $oneParam[4], $toc , $oneParam[6], $oneParam[5], mb_strlen($oneParam[6],"UTF-8"));
  95. $stmt->execute($newData);
  96. }
  97. // 提交更改
  98. $PDO->commit();
  99. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  100. $error = PDO_ErrorInfo();
  101. echo "error - $error[2] <br>";
  102. $log=$log."$from, $FileName, error, $error[2] \r\n";
  103. }
  104. else{
  105. $count=count($arrInserString);
  106. echo "updata $count recorders.";
  107. }
  108. $myLogFile = fopen($dirLog."db_insert_palitext.log", "a");
  109. fwrite($myLogFile, $log);
  110. fclose($myLogFile);
  111. ?>
  112. <?php
  113. if($from==$to){
  114. echo "<h2>齐活!功德无量!all done!</h2>";
  115. }
  116. else{
  117. echo "<script>";
  118. echo "window.location.assign(\"db_insert_palitext.php?from=".($from+1)."&to=".$to."\")";
  119. echo "</script>";
  120. echo "正在载入:".($from+1)."——".$filelist[$from+1][0];
  121. }
  122. ?>
  123. </body>
  124. </html>