2
0

db_insert_palitext.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "../config.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 = (int)$_GET["from"];
  25. $to = (int)$_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. PDO_Connect(_FILE_DB_PALITEXT_,_DB_USERNAME_,_DB_PASSWORD_);
  55. // 打开vri html文件并读取数据
  56. $pali_text_array = array();
  57. if (($fpPaliText = fopen($dirPaliTextBase . $xmlfile, "r")) !== false) {
  58. while (($data = fgets($fpPaliText)) !== false) {
  59. if (substr($data, 0, 2) === "<p") {
  60. array_push($pali_text_array, $data);
  61. }
  62. }
  63. fclose($fpPaliText);
  64. echo "pali text load:" . $dirPaliTextBase . $xmlfile . "<br>";
  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. } else {
  82. echo "can not open csv file. filename=" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv";
  83. }
  84. if (($inputRow - 1) != count($pali_text_array)) {
  85. $log = $log . "$from, $FileName,error,文件行数不匹配 inputRow=$inputRow pali_text_array=" . count($pali_text_array) . " \r\n";
  86. }
  87. $query = "DELETE FROM "._TABLE_PALI_TEXT_." WHERE book=?";
  88. PDO_Execute($query,array($from+1));
  89. // 开始一个事务,关闭自动提交
  90. $PDO->beginTransaction();
  91. $query = "INSERT INTO "._TABLE_PALI_TEXT_." ( book , paragraph , level , class , toc , text , html , lenght ) VALUES ( ? , ? , ? , ? , ? , ? , ? , ? )";
  92. $stmt = $PDO->prepare($query);
  93. foreach ($arrInserString as $oneParam) {
  94. if ($oneParam[3] < 100) {
  95. $toc = $oneParam[6];
  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. } else {
  109. $count = count($arrInserString);
  110. echo "updata $count recorders.";
  111. }
  112. $myLogFile = fopen($dirLog . "db_insert_palitext.log", "a");
  113. fwrite($myLogFile, $log);
  114. fclose($myLogFile);
  115. ?>
  116. <?php
  117. if ($from == $to) {
  118. echo "<h2>齐活!功德无量!all done!</h2>";
  119. } else {
  120. echo "<script>";
  121. echo "window.location.assign(\"db_insert_palitext.php?from=" . ($from + 1) . "&to=" . $to . "\")";
  122. echo "</script>";
  123. echo "正在载入:" . ($from + 1) . "——" . $filelist[$from + 1][0];
  124. }
  125. ?>
  126. </body>
  127. </html>