db_update_toc_cli.php 4.3 KB

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