db_insert_bookword_from_csv.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. require_once "install_head.php";
  3. ?>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. </head>
  8. <body>
  9. <h2>Insert to Index</h2>
  10. <?php
  11. include "./_pdo.php";
  12. include "../path.php";
  13. if (isset($_GET["from"]) == false) {
  14. ?>
  15. <form action="db_insert_bookword_from_csv.php" method="get">
  16. From: <input type="text" name="from" value="0"><br>
  17. To: <input type="text" name="to" value="216"><br>
  18. <input type="submit">
  19. </form>
  20. <?php
  21. return;
  22. }
  23. $from = $_GET["from"];
  24. $to = $_GET["to"];
  25. $dirLog = _DIR_LOG_ . "/";
  26. $dirXmlBase = _DIR_PALI_CSV_ . "/";
  27. $filelist = array();
  28. $fileNums = 0;
  29. $log = "";
  30. echo "<h2>doing : No.{$from} book </h2>";
  31. global $dbh_word_index;
  32. $dns = "" . _FILE_DB_BOOK_WORD_;
  33. $dbh_word_index = new PDO($dns, "", "", array(PDO::ATTR_PERSISTENT => true));
  34. $dbh_word_index->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  35. if (($handle = fopen("filelist.csv", 'r')) !== false) {
  36. while (($filelist[$fileNums] = fgetcsv($handle, 0, ',')) !== false) {
  37. $fileNums++;
  38. }
  39. }
  40. if ($to == 0 || $to >= $fileNums) {
  41. $to = $fileNums - 1;
  42. }
  43. $bookword = array();
  44. if (($fpoutput = fopen(_DIR_CSV_PALI_CANON_WORD_ . "/{$from}_words.csv", "r")) !== false) {
  45. $count = 0;
  46. while (($data = fgetcsv($fpoutput, 0, ',')) !== false) {
  47. $book = $data[1];
  48. if (isset($bookword[$data[3]])) {
  49. $bookword[$data[3]]++;
  50. } else {
  51. $bookword[$data[3]] = 1;
  52. }
  53. $count++;
  54. }
  55. }
  56. // 开始一个事务,关闭自动提交
  57. $dbh_word_index->beginTransaction();
  58. $query = "INSERT INTO "._TABLE_BOOK_WORD_." ('book','wordindex','count') VALUES ( ? , ? , ? )";
  59. $stmt = $dbh_word_index->prepare($query);
  60. foreach ($bookword as $key => $value) {
  61. $stmt->execute(array($book, $key, $value));
  62. }
  63. // 提交更改
  64. $dbh_word_index->commit();
  65. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  66. $error = $dbh_word_index->errorInfo();
  67. echo "error - $error[2] <br>";
  68. $log .= "$from, $FileName, error, $error[2] \r\n";
  69. } else {
  70. echo "updata $count recorders.<br />";
  71. $log .= "updata $count recorders.\r\n";
  72. }
  73. $myLogFile = fopen($dirLog . "insert_index.log", "a");
  74. fwrite($myLogFile, $log);
  75. fclose($myLogFile);
  76. if ($from >= $to) {
  77. echo "<h2>齐活!功德无量!all done!</h2>";
  78. } else {
  79. echo "<script>";
  80. echo "window.location.assign(\"db_insert_bookword_from_csv.php?from=" . ($from + 1) . "&to=" . $to . "\")";
  81. echo "</script>";
  82. echo "正在载入:" . ($from + 1) . "——" . $filelist[$from + 1][0];
  83. }
  84. ?>
  85. </body>
  86. </html>