db_update_toc_cli.php 4.7 KB

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