db_insert_palitext_cli.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. define("_DB_", _PG_DB_PALITEXT_);
  13. define("_TABLE_",_PG_TABLE_PALI_TEXT_);
  14. fwrite(STDOUT, "Insert Pali Text To DB".PHP_EOL);
  15. if ($argc != 3) {
  16. echo "help".PHP_EOL;
  17. echo $argv[0]." from to".PHP_EOL;
  18. echo "from = 1-217".PHP_EOL;
  19. echo "to = 1-217".PHP_EOL;
  20. exit;
  21. }
  22. $_from = (int) $argv[1];
  23. $_to = (int) $argv[2];
  24. if ($_to > 217) {
  25. $_to = 217;
  26. }
  27. $to = $_to;
  28. $filelist = array();
  29. $fileNums = 0;
  30. $log = "";
  31. fwrite(STDOUT, "doing $_from".PHP_EOL);
  32. if (($handle = fopen(__DIR__."/filelist.csv", 'r')) !== false) {
  33. while (($filelist[$fileNums] = fgetcsv($handle, 0, ',')) !== false) {
  34. $fileNums++;
  35. }
  36. }
  37. if ($to == 0 || $to > $fileNums) {
  38. $to = $fileNums;
  39. }
  40. $dns = _DB_;
  41. $dbh = new PDO($dns, _DB_USERNAME_, _DB_PASSWORD_, array(PDO::ATTR_PERSISTENT => true));
  42. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  43. for ($from=$_from-1; $from < $to; $from++) {
  44. # code...
  45. $FileName = $filelist[$from][1] . ".htm";
  46. $fileId = $filelist[$from][0];
  47. $fileId = $filelist[$from][0];
  48. $dirLog = _DIR_LOG_ . "/";
  49. $inputFileName = $FileName;
  50. $outputFileNameHead = $filelist[$from][1];
  51. $bookId = $filelist[$from][2];
  52. $vriParNum = 0;
  53. $wordOrder = 1;
  54. $dirXmlBase = _DIR_PALI_CSV_ . "/";
  55. $dirPaliTextBase = _DIR_PALI_HTML_ . "/";
  56. $dirXml = $outputFileNameHead . "/";
  57. $xmlfile = $inputFileName;
  58. fwrite(STDOUT, "doing:" . $xmlfile . PHP_EOL);
  59. $log = $log . "$from,$FileName,open\r\n";
  60. $arrInserString = array();
  61. // 打开vri html文件并读取数据
  62. $pali_text_array = array();
  63. if (($fpPaliText = fopen($dirPaliTextBase . $xmlfile, "r")) !== false) {
  64. while (($data = fgets($fpPaliText)) !== false) {
  65. if (substr($data, 0, 2) === "<p") {
  66. array_push($pali_text_array, $data);
  67. }
  68. }
  69. fclose($fpPaliText);
  70. fwrite(STDOUT, "pali text load:" . $dirPaliTextBase . $xmlfile . PHP_EOL);
  71. } else {
  72. fwrite(STDERR, "can not pali text file. filename=" . $dirPaliTextBase . $xmlfile.PHP_EOL);
  73. continue;
  74. }
  75. $inputRow = 0;
  76. if (($fp = fopen($dirXmlBase . $dirXml . $outputFileNameHead . "_pali.csv", "r")) !== false) {
  77. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  78. if ($inputRow > 0) {
  79. if (($inputRow - 1) < count($pali_text_array)) {
  80. $data[5] = $pali_text_array[$inputRow - 1];
  81. }
  82. $arrInserString[] = $data;
  83. }
  84. $inputRow++;
  85. }
  86. fclose($fp);
  87. fwrite(STDOUT, "单词表load:" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv".PHP_EOL);
  88. } else {
  89. fwrite(STDERR, "can not open csv file. filename=" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv".PHP_EOL);
  90. continue;
  91. }
  92. if (($inputRow - 1) != count($pali_text_array)) {
  93. $log = "$from, $FileName,error,文件行数不匹配 inputRow=$inputRow pali_text_array=" . count($pali_text_array) . PHP_EOL;
  94. fwrite(STDERR, $log);
  95. continue;
  96. }
  97. #删除 旧数据
  98. $query = "DELETE FROM "._TABLE_." WHERE book=?";
  99. $stmt = $dbh->prepare($query);
  100. try{
  101. $stmt->execute(array($from+1));
  102. }catch(PDOException $e){
  103. fwrite(STDERR,$e->getMessage());
  104. continue;
  105. }
  106. // 开始一个事务,关闭自动提交
  107. $dbh->beginTransaction();
  108. $query = "INSERT INTO "._TABLE_." ( book , paragraph , level , class , toc , text , html , lenght , created_at,updated_at ) VALUES ( ? , ? , ? , ? , ? , ? , ? , ? ,now(),now() )";
  109. $stmt = $dbh->prepare($query);
  110. foreach ($arrInserString as $oneParam) {
  111. if ($oneParam[3] < 100) {
  112. $toc = $oneParam[6];
  113. } else {
  114. $toc = "";
  115. }
  116. $newData = array($from + 1, $oneParam[2], $oneParam[3], $oneParam[4], $toc, $oneParam[6], $oneParam[5], mb_strlen($oneParam[6], "UTF-8"));
  117. try{
  118. $stmt->execute($newData);
  119. }catch(PDOException $e){
  120. fwrite(STDERR,$e->getMessage());
  121. fwrite(STDERR,implode(",",$newData));
  122. break;
  123. }
  124. }
  125. // 提交更改
  126. $dbh->commit();
  127. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  128. $error = $dbh->errorInfo();
  129. fwrite(STDERR, "error - $error[2]".PHP_EOL);
  130. $log = $log . "$from, $FileName, error, $error[2] \r\n";
  131. } else {
  132. $count = count($arrInserString);
  133. fwrite(STDOUT, "updata $count recorders.".PHP_EOL);
  134. }
  135. /*
  136. $myLogFile = fopen($dirLog . "db_insert_palitext.log", "a");
  137. fwrite($myLogFile, $log);
  138. fclose($myLogFile);
  139. */
  140. }
  141. fwrite(STDOUT, "all done!".PHP_EOL);
  142. ?>