db_insert_bookword_from_csv_cli.php 3.3 KB

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