db_update_toc_cli.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. require_once __DIR__."/../config.php";
  3. require_once __DIR__."/../public/function.php";
  4. set_exception_handler(function($e){
  5. fwrite(STDERR,"error-msg:".$e->getMessage().PHP_EOL);
  6. fwrite(STDERR,"error-file:".$e->getFile().PHP_EOL);
  7. fwrite(STDERR,"error-line:".$e->getLine().PHP_EOL);
  8. exit;
  9. });
  10. define("_DB_RES_INDEX_", _PG_DB_RESRES_INDEX_);
  11. define("_TABLE_",_PG_TABLE_RES_INDEX_);
  12. fwrite(STDOUT, "Update toc to res_index".PHP_EOL);
  13. if ($argc != 4) {
  14. echo $argv[0]." help".PHP_EOL;
  15. echo " from to language".PHP_EOL;
  16. echo "from = 1-217".PHP_EOL;
  17. echo "to = 1-217".PHP_EOL;
  18. echo "language = pali/zh-hans/zh-hant".PHP_EOL;
  19. exit;
  20. }
  21. $_from = (int) $argv[1];
  22. $_to = (int) $argv[2];
  23. if ($_to > 217) {
  24. $_to = 217;
  25. }
  26. $to = $_to;
  27. $_file = $argv[3];
  28. $_lang = $argv[3];
  29. $filelist = array();
  30. $fileNums = 0;
  31. $log = "";
  32. fwrite(STDOUT, "doing $_from".PHP_EOL);
  33. if (($handle = fopen(__DIR__."/filelist.csv", 'r')) !== false) {
  34. while (($filelist[$fileNums] = fgetcsv($handle, 0, ',')) !== false) {
  35. $fileNums++;
  36. }
  37. }
  38. if ($to == 0 || $to >= $fileNums) {
  39. $to = $fileNums - 1;
  40. }
  41. $dns = _DB_RES_INDEX_;
  42. $dbh = new PDO($dns, _DB_USERNAME_, _DB_PASSWORD_, array(PDO::ATTR_PERSISTENT => true));
  43. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  44. for ($from=$_from-1; $from < $_to; $from++) {
  45. # code...
  46. $FileName = $filelist[$from][1] . ".htm";
  47. $fileId = $filelist[$from][0];
  48. $fileId = $filelist[$from][0];
  49. $dirLog = _DIR_LOG_ . "/";
  50. $dirDb = "/";
  51. $inputFileName = $FileName;
  52. $outputFileNameHead = $filelist[$from][1];
  53. $bookId = $filelist[$from][2];
  54. $vriParNum = 0;
  55. $wordOrder = 1;
  56. $dirXmlBase = _DIR_PALI_CSV_ . "/";
  57. $dirPaliTextBase = _DIR_PALI_HTML_ . "/";
  58. $dirXml = $outputFileNameHead . "/";
  59. $xmlfile = $inputFileName;
  60. $log = $log . date("Y-m-d h:i:sa") . ",$from,$FileName,open\r\n";
  61. $arrInserString = array();
  62. // 打开csv文件并读取数据
  63. $strFileName = _DIR_PALI_TITLE_ . "/" . ($from + 1) . "_{$_file}.csv";
  64. if(!file_exists($strFileName)){
  65. continue;
  66. }
  67. $inputRow = 0;
  68. if (($fp = fopen($strFileName, "r")) !== false) {
  69. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  70. if ($inputRow > 0 && $data[3] != 100 && !empty($data[6])) {
  71. array_push($arrInserString, $data);
  72. }
  73. $inputRow++;
  74. }
  75. fclose($fp);
  76. fwrite(STDOUT, "res load:" . _DIR_PALI_TITLE_ . "/" . ($from + 1) . "_title.csv".PHP_EOL);
  77. } else {
  78. fwrite(STDOUT, "can not open csv ".PHP_EOL);
  79. continue;
  80. }
  81. $book = $from + 1;
  82. //删除已有标题
  83. $query = "DELETE FROM \""._TABLE_."\" WHERE book = ? AND language = ? ";
  84. try{
  85. $stmt = $dbh->prepare($query);
  86. $stmt->execute(array($book,$_lang));
  87. }catch(PDOException $e){
  88. fwrite(STDERR,"error:".$e->getMessage());
  89. continue;
  90. }
  91. // 开始一个事务,关闭自动提交
  92. $dbh->beginTransaction();
  93. $query = "INSERT INTO \""._TABLE_."\" (book , paragraph, title, title_en , level, type , language , author , share , create_time , update_time,created_at,updated_at ) VALUES ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ,now(),now())";
  94. try{
  95. $stmt = $dbh->prepare($query);
  96. }catch(PDOException $e){
  97. fwrite(STDERR,"error:".$e->getMessage());
  98. break;
  99. }
  100. if ($_lang == "pali") {
  101. $type = 1;
  102. } else {
  103. $type = 2;
  104. }
  105. foreach ($arrInserString as $title) {
  106. if (isset($title[7])) {
  107. $author = $title[7];
  108. }else {
  109. $author = "cscd4";
  110. }
  111. $title[6] = mb_substr($title[6],0,1024);
  112. $newData = array(
  113. $book,
  114. $title[2],
  115. $title[6],
  116. getWordEn($title[6]),
  117. $title[3],
  118. $type,
  119. $_lang,
  120. $author,
  121. 1,
  122. mTime(),
  123. mTime(),
  124. );
  125. try{
  126. $stmt->execute($newData);
  127. }catch(PDOException $e){
  128. fwrite(STDERR,"error:".$e->getMessage());
  129. fwrite(STDERR,implode(",",$newData));
  130. break;
  131. }
  132. }
  133. // 提交更改
  134. $dbh->commit();
  135. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  136. $error = $dbh->errorInfo();
  137. fwrite(STDERR, "error - $error[2] ".PHP_EOL);
  138. $log = $log . "$from, error, $error[2] \r\n";
  139. } else {
  140. $count = count($arrInserString);
  141. fwrite(STDOUT, "updata $count title recorders.".PHP_EOL);
  142. }
  143. //段落信息结束
  144. /*
  145. $myLogFile = fopen(_DIR_LOG_ . "/db_update_toc.log", "a");
  146. fwrite($myLogFile, $log);
  147. fclose($myLogFile);
  148. */
  149. }
  150. fwrite(STDOUT, "齐活!功德无量!all done!".PHP_EOL);
  151. function getWordEn($strIn)
  152. {
  153. $strIn = strtolower($strIn);
  154. $search = array('ā', 'ī', 'ū', 'ṅ', 'ñ', 'ṭ', 'ḍ', 'ṇ', 'ḷ', 'ṃ');
  155. $replace = array('a', 'i', 'u', 'n', 'n', 't', 'd', 'n', 'l', 'm');
  156. return (str_replace($search, $replace, $strIn));
  157. }
  158. ?>