db_update_toc_cli.php 4.1 KB

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