db_insert_palitext.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. if( substr($data,0,2) === "<p" ){
  56. array_push($pali_text_array,$data);
  57. }
  58. }
  59. fclose($fpPaliText);
  60. echo "pali text load:".$dirPaliTextBase.$xmlfile."<br>";
  61. }
  62. else{
  63. echo "can not pali text file. filename=".$dirPaliTextBase.$xmlfile;
  64. }
  65. $inputRow=0;
  66. if(($fp=fopen($dirXmlBase.$dirXml.$outputFileNameHead."_pali.csv", "r"))!==FALSE){
  67. while(($data=fgetcsv($fp , 0 , ',' )) !== FALSE ){
  68. if($inputRow>0){
  69. if(($inputRow-1)<count($pali_text_array)){
  70. $data[5]=$pali_text_array[$inputRow-1];
  71. }
  72. $arrInserString[]=$data;
  73. }
  74. $inputRow++;
  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. if(($inputRow-1)!=count($pali_text_array)){
  83. $log=$log."$from, $FileName,error,文件行数不匹配 inputRow=$inputRow pali_text_array=".count($pali_text_array)." \r\n";
  84. }
  85. // 开始一个事务,关闭自动提交
  86. $PDO->beginTransaction();
  87. $query="INSERT INTO pali_text ('id', 'book','paragraph','level','class','toc','text','html','lenght') VALUES (NULL, ? , ? , ? , ? , ? , ? , ?,? )";
  88. $stmt = $PDO->prepare($query);
  89. foreach($arrInserString as $oneParam){
  90. if($oneParam[3]<100){
  91. $toc = $oneParam[6];
  92. }
  93. else{
  94. $toc = "";
  95. }
  96. $newData=array($from+1, $oneParam[2], $oneParam[3], $oneParam[4], $toc , $oneParam[6], $oneParam[5], mb_strlen($oneParam[6],"UTF-8"));
  97. $stmt->execute($newData);
  98. }
  99. // 提交更改
  100. $PDO->commit();
  101. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  102. $error = PDO_ErrorInfo();
  103. echo "error - $error[2] <br>";
  104. $log=$log."$from, $FileName, error, $error[2] \r\n";
  105. }
  106. else{
  107. $count=count($arrInserString);
  108. echo "updata $count recorders.";
  109. }
  110. $myLogFile = fopen($dirLog."db_insert_palitext.log", "a");
  111. fwrite($myLogFile, $log);
  112. fclose($myLogFile);
  113. ?>
  114. <?php
  115. if($from==$to){
  116. echo "<h2>齐活!功德无量!all done!</h2>";
  117. }
  118. else{
  119. echo "<script>";
  120. echo "window.location.assign(\"db_insert_palitext.php?from=".($from+1)."&to=".$to."\")";
  121. echo "</script>";
  122. echo "正在载入:".($from+1)."——".$filelist[$from+1][0];
  123. }
  124. ?>
  125. </body>
  126. </html>