db_insert_palitext.php 3.5 KB

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