db_insert_page_index.php 3.9 KB

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