db_insert_bookword_from_csv_cli.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /*
  3. 生成 巴利原文段落表
  4. */
  5. require_once __DIR__."/../config.php";
  6. require_once __DIR__.'/../public/_pdo.php';
  7. echo "Insert Pali Text To DB".PHP_EOL;
  8. if ($argc != 3) {
  9. echo "help".PHP_EOL;
  10. echo $argv[0]." from to".PHP_EOL;
  11. echo "from = 1-217".PHP_EOL;
  12. echo "to = 1-217".PHP_EOL;
  13. exit;
  14. }
  15. $_from = (int) $argv[1];
  16. $_to = (int) $argv[2];
  17. if ($_to > 217) {
  18. $_to = 217;
  19. }
  20. $dirLog = _DIR_LOG_ . "/";
  21. $dirXmlBase = _DIR_PALI_CSV_ . "/";
  22. $filelist = array();
  23. $fileNums = 0;
  24. $log = "";
  25. global $dbh_word_index;
  26. $dns = _FILE_DB_BOOK_WORD_;
  27. $dbh_word_index = new PDO($dns, _DB_USERNAME_, _DB_PASSWORD_, array(PDO::ATTR_PERSISTENT => true));
  28. $dbh_word_index->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  29. if (($handle = fopen(__DIR__."/filelist.csv", 'r')) !== false) {
  30. while (($filelist[$fileNums] = fgetcsv($handle, 0, ',')) !== false) {
  31. $fileNums++;
  32. }
  33. }
  34. if ($_to == 0 || $_to >= $fileNums) {
  35. $_to = $fileNums ;
  36. }
  37. for ($from=$_from-1; $from < $_to; $from++) {
  38. echo "doing ".($from+1).PHP_EOL;
  39. $bookword = array();
  40. if (($fpoutput = fopen(_DIR_CSV_PALI_CANON_WORD_ . "/{$from}_words.csv", "r")) !== false) {
  41. $count = 0;
  42. while (($data = fgetcsv($fpoutput, 0, ',')) !== false) {
  43. $book = $data[1];
  44. if (isset($bookword[$data[3]])) {
  45. $bookword[$data[3]]++;
  46. } else {
  47. $bookword[$data[3]] = 1;
  48. }
  49. $count++;
  50. }
  51. }
  52. #删除原来的数据
  53. $query = "DELETE FROM "._TABLE_BOOK_WORD_." WHERE book = ?";
  54. $stmt = $dbh_word_index->prepare($query);
  55. $stmt->execute(array($book));
  56. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  57. $error = $dbh_word_index->errorInfo();
  58. echo "error - $error[2]".PHP_EOL;
  59. $log .= "$from, $FileName, error, $error[2] \r\n";
  60. }else{
  61. // 开始一个事务,关闭自动提交
  62. $dbh_word_index->beginTransaction();
  63. $query = "INSERT INTO "._TABLE_BOOK_WORD_." (book , wordindex , count) VALUES ( ? , ? , ? )";
  64. $stmt = $dbh_word_index->prepare($query);
  65. foreach ($bookword as $key => $value) {
  66. $stmt->execute(array($book, $key, $value));
  67. }
  68. // 提交更改
  69. $dbh_word_index->commit();
  70. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  71. $error = $dbh_word_index->errorInfo();
  72. echo "error - $error[2]".PHP_EOL;
  73. $log .= "$from, $FileName, error, $error[2] \r\n";
  74. } else {
  75. echo "updata $count recorders.".PHP_EOL;
  76. $log .= "updata $count recorders.\r\n";
  77. }
  78. }
  79. /*
  80. $myLogFile = fopen($dirLog . "insert_index.log", "a");
  81. fwrite($myLogFile, $log);
  82. fclose($myLogFile);
  83. */
  84. }
  85. echo "齐活!功德无量!all done!".PHP_EOL;
  86. ?>