db_insert_page_index.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. //require_once "install_head.php";
  3. require_once "./_pdo.php";
  4. require_once "../path.php";
  5. if (PHP_SAPI == "cli") {
  6. echo $argc;
  7. if($argc>=3){
  8. $from=$argv[1];
  9. $to=$argv[2];
  10. echo "From: {$from} To:{$to}";
  11. }
  12. else if($argc>=1){
  13. $from=0;
  14. $to = 216;
  15. echo "生成全部217本书";
  16. }
  17. else{
  18. echo "参数错误";
  19. exit;
  20. }
  21. }
  22. else{
  23. echo "<!DOCTYPE html><html><head></head>";
  24. echo "<body><h2>Insert to Index</h2>";
  25. if(isset($_GET["from"])==false){
  26. echo '<form action="db_insert_index_once.php" method="get">';
  27. echo 'From: <input type="text" name="from" value="0"><br>';
  28. echo 'To: <input type="text" name="to" value="216"><br>';
  29. echo '<input type="submit">';
  30. echo '</form>';
  31. exit;
  32. }
  33. else{
  34. $from=$_GET["from"];
  35. $to=$_GET["to"];
  36. }
  37. }
  38. $g_wordCounter=0;
  39. $g_wordIndexCounter=0;
  40. $iAllWordIndex=array();
  41. $sAllWord=array();
  42. $dirLog=_DIR_LOG_."/";
  43. $dirXmlBase=_DIR_PALI_CSV_."/";
  44. $filelist=array();
  45. $fileNums=0;
  46. $log="";
  47. echo "\n <h2>$from</h2>";
  48. if(($handle=fopen("filelist.csv",'r'))!==FALSE){
  49. while(($filelist[$fileNums]=fgetcsv($handle,0,','))!==FALSE){
  50. $fileNums++;
  51. }
  52. }
  53. if($to==0 || $to>=$fileNums) $to=$fileNums-1;
  54. for($iFile=$from;$iFile<=$to;$iFile++){
  55. echo "<h3>{$iFile}</h3>";
  56. $FileName=$filelist[$iFile][1].".htm";
  57. $fileId=$filelist[$iFile][0];
  58. $inputFileName=$FileName;
  59. $outputFileNameHead=$filelist[$iFile][1];
  60. $bookId=$filelist[$iFile][2];
  61. $dirXml=$outputFileNameHead."/";
  62. $xmlfile = $inputFileName;
  63. echo "doing:".$xmlfile."<br>";
  64. $log=$log."$iFile,$FileName,open\r\n";
  65. $arrInserString=array();
  66. $db_file = _FILE_DB_PAGE_INDEX_;
  67. PDO_Connect("sqlite:$db_file");
  68. // 打开文件并读取数据
  69. $irow=0;
  70. if(($fp=fopen($dirXmlBase.$dirXml.$outputFileNameHead.".csv", "r"))!==FALSE){
  71. // 开始一个事务,关闭自动提交
  72. $PDO->beginTransaction();
  73. $query="INSERT INTO m ('book','para','page1','page2') VALUES (?,?,?,?)";
  74. $stmt = $PDO->prepare($query);
  75. $currPage=array(0,0);
  76. $currPara = 0;
  77. while(($data=fgetcsv($fp,0,','))!==FALSE){
  78. $irow++;
  79. if($irow>1){
  80. if($data[6]==".ctl." && mb_substr($data[5],0,1,"UTF-8")=="M"){
  81. $sPage= mb_substr($data[5],1,6,"UTF-8");
  82. $aPage = explode(".",$sPage);
  83. if(count($aPage)==2){
  84. $currPage = $aPage;
  85. }
  86. else{
  87. echo "错误的页码: {$data[5]} \n ";
  88. }
  89. }
  90. $para = $data[3];
  91. if($currPara!=$data[3]){
  92. $currPara=(int)$data[3];
  93. $book = mb_substr($data[2],1, null ,"UTF-8");
  94. $para = $currPara;
  95. $page1 = $currPage[0];
  96. $page2 = $currPage[1];
  97. $stmt->execute(array($book,$para,$page1,$page2));
  98. }
  99. }
  100. }
  101. // 提交更改
  102. $PDO->commit();
  103. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  104. $error = PDO_ErrorInfo();
  105. echo "error - $error[2] <br>";
  106. $log.="$from, $FileName, error, $error[2] \r\n";
  107. }
  108. else{
  109. echo "updata recorders.<br />";
  110. $log.="updata recorders.\r\n";
  111. }
  112. fclose($fp);
  113. echo "单词表load:".$dirXmlBase.$dirXml.$outputFileNameHead.".csv<br>\n";
  114. }
  115. else{
  116. echo "can not open csv file. filename=".$dirXmlBase.$dirXml.$outputFileNameHead.".csv";
  117. }
  118. }
  119. echo "齐活!功德无量!all done!";
  120. ?>
  121. </body>
  122. </html>