db_insert_palitext_cli.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. $to = $_to;
  21. $filelist = array();
  22. $fileNums = 0;
  23. $log = "";
  24. echo "doing $_from";
  25. if (($handle = fopen(__DIR__."/filelist.csv", 'r')) !== false) {
  26. while (($filelist[$fileNums] = fgetcsv($handle, 0, ',')) !== false) {
  27. $fileNums++;
  28. }
  29. }
  30. if ($to == 0 || $to >= $fileNums) {
  31. $to = $fileNums - 1;
  32. }
  33. PDO_Connect(_FILE_DB_PALITEXT_,_DB_USERNAME_,_DB_PASSWORD_);
  34. for ($from=$_from-1; $from < $to; $from++) {
  35. # code...
  36. $FileName = $filelist[$from][1] . ".htm";
  37. $fileId = $filelist[$from][0];
  38. $fileId = $filelist[$from][0];
  39. $dirLog = _DIR_LOG_ . "/";
  40. $inputFileName = $FileName;
  41. $outputFileNameHead = $filelist[$from][1];
  42. $bookId = $filelist[$from][2];
  43. $vriParNum = 0;
  44. $wordOrder = 1;
  45. $dirXmlBase = _DIR_PALI_CSV_ . "/";
  46. $dirPaliTextBase = _DIR_PALI_HTML_ . "/";
  47. $dirXml = $outputFileNameHead . "/";
  48. $xmlfile = $inputFileName;
  49. echo "doing:" . $xmlfile . PHP_EOL;
  50. $log = $log . "$from,$FileName,open\r\n";
  51. $arrInserString = array();
  52. // 打开vri html文件并读取数据
  53. $pali_text_array = array();
  54. if (($fpPaliText = fopen($dirPaliTextBase . $xmlfile, "r")) !== false) {
  55. while (($data = fgets($fpPaliText)) !== false) {
  56. if (substr($data, 0, 2) === "<p") {
  57. array_push($pali_text_array, $data);
  58. }
  59. }
  60. fclose($fpPaliText);
  61. echo "pali text load:" . $dirPaliTextBase . $xmlfile . PHP_EOL;
  62. } else {
  63. echo "can not pali text file. filename=" . $dirPaliTextBase . $xmlfile;
  64. }
  65. $inputRow = 0;
  66. if (($fp = fopen($dirXmlBase . $dirXml . $outputFileNameHead . "_pali.csv", "r")) !== false) {
  67. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  68. if ($inputRow > 0) {
  69. if (($inputRow - 1) < count($pali_text_array)) {
  70. $data[5] = $pali_text_array[$inputRow - 1];
  71. }
  72. $arrInserString[] = $data;
  73. }
  74. $inputRow++;
  75. }
  76. fclose($fp);
  77. echo "单词表load:" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv".PHP_EOL;
  78. } else {
  79. echo "can not open csv file. filename=" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv";
  80. continue;
  81. }
  82. if (($inputRow - 1) != count($pali_text_array)) {
  83. $log = $log . "$from, $FileName,error,文件行数不匹配 inputRow=$inputRow pali_text_array=" . count($pali_text_array) . " \r\n";
  84. echo "line count error".PHP_EOL;
  85. }
  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]".PHP_EOL;
  107. $log = $log . "$from, $FileName, error, $error[2] \r\n";
  108. } else {
  109. $count = count($arrInserString);
  110. echo "updata $count recorders.".PHP_EOL;
  111. }
  112. /*
  113. $myLogFile = fopen($dirLog . "db_insert_palitext.log", "a");
  114. fwrite($myLogFile, $log);
  115. fclose($myLogFile);
  116. */
  117. }
  118. echo "all done!".PHP_EOL;
  119. ?>