db_insert_palitext.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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) {
  36. $to = $fileNums - 1;
  37. }
  38. $FileName = $filelist[$from][1] . ".htm";
  39. $fileId = $filelist[$from][0];
  40. $fileId = $filelist[$from][0];
  41. $dirLog = _DIR_LOG_ . "/";
  42. $inputFileName = $FileName;
  43. $outputFileNameHead = $filelist[$from][1];
  44. $bookId = $filelist[$from][2];
  45. $vriParNum = 0;
  46. $wordOrder = 1;
  47. $dirXmlBase = _DIR_PALI_CSV_ . "/";
  48. $dirPaliTextBase = _DIR_PALI_HTML_ . "/";
  49. $dirXml = $outputFileNameHead . "/";
  50. $xmlfile = $inputFileName;
  51. echo "doing:" . $xmlfile . "<br>";
  52. $log = $log . "$from,$FileName,open\r\n";
  53. $arrInserString = array();
  54. $db_file = _FILE_DB_PALITEXT_;
  55. PDO_Connect("$db_file");
  56. // 打开vri html文件并读取数据
  57. $pali_text_array = array();
  58. if (($fpPaliText = fopen($dirPaliTextBase . $xmlfile, "r")) !== false) {
  59. while (($data = fgets($fpPaliText)) !== false) {
  60. if (substr($data, 0, 2) === "<p") {
  61. array_push($pali_text_array, $data);
  62. }
  63. }
  64. fclose($fpPaliText);
  65. echo "pali text load:" . $dirPaliTextBase . $xmlfile . "<br>";
  66. } else {
  67. echo "can not pali text file. filename=" . $dirPaliTextBase . $xmlfile;
  68. }
  69. $inputRow = 0;
  70. if (($fp = fopen($dirXmlBase . $dirXml . $outputFileNameHead . "_pali.csv", "r")) !== false) {
  71. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  72. if ($inputRow > 0) {
  73. if (($inputRow - 1) < count($pali_text_array)) {
  74. $data[5] = $pali_text_array[$inputRow - 1];
  75. }
  76. $arrInserString[] = $data;
  77. }
  78. $inputRow++;
  79. }
  80. fclose($fp);
  81. echo "单词表load:" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv<br>";
  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. } else {
  96. $toc = "";
  97. }
  98. $newData = array($from + 1, $oneParam[2], $oneParam[3], $oneParam[4], $toc, $oneParam[6], $oneParam[5], mb_strlen($oneParam[6], "UTF-8"));
  99. $stmt->execute($newData);
  100. }
  101. // 提交更改
  102. $PDO->commit();
  103. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  104. $error = PDO_ErrorInfo();
  105. echo "error - $error[2] <br>";
  106. $log = $log . "$from, $FileName, error, $error[2] \r\n";
  107. } else {
  108. $count = count($arrInserString);
  109. echo "updata $count recorders.";
  110. }
  111. $myLogFile = fopen($dirLog . "db_insert_palitext.log", "a");
  112. fwrite($myLogFile, $log);
  113. fclose($myLogFile);
  114. ?>
  115. <?php
  116. if ($from == $to) {
  117. echo "<h2>齐活!功德无量!all done!</h2>";
  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>