content_download.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. require dirname(__FILE__) . '/vendor/autoload.php';
  3. require dirname(__FILE__) . '/config.php';
  4. require dirname(__FILE__) . '/console.php';
  5. require dirname(__FILE__) . '/pdo.php';
  6. console('debug', 'download full test search content start');
  7. $param = getopt('b:f:t:');
  8. if (isset($param['b'])) {
  9. $bookId = (int)$param['b'];
  10. console('debug', 'update book=' . $bookId);
  11. }
  12. $PDO = new PdoHelper;
  13. $PDO->connectDb();
  14. console('debug', 'connect database finished');
  15. $client = new GuzzleHttp\Client();
  16. $pageSize = 1000;
  17. $urlBase = Config['api_server'] . '/v2/pali-search-data';
  18. console('debug', 'url=' . $urlBase);
  19. if (isset($bookId)) {
  20. $from = $bookId;
  21. $to = $bookId;
  22. } else {
  23. $from = 1;
  24. $to = 217;
  25. }
  26. if(isset($param['f'])){
  27. $from = $param['f'];
  28. }
  29. if(isset($param['t'])){
  30. $to = $param['t'];
  31. }
  32. for ($book = $from; $book <= $to; $book++) {
  33. $currPage = 1;
  34. $urlBook = $urlBase . "?book={$book}";
  35. console('debug', 'fetch book=' . $book);
  36. do {
  37. $goNext = false;
  38. $url = $urlBook . "&start={$currPage}&page_size={$pageSize}";
  39. console('debug', 'url=' . $url);
  40. $res = $client->request('GET', $url);
  41. $status = $res->getStatusCode();
  42. if ($status === 200) {
  43. $json = json_decode($res->getBody());
  44. if ($json->ok) {
  45. $content = $json->data->rows;
  46. foreach ($json->data->rows as $row) {
  47. $book = $row->book;
  48. $paragraph = $row->paragraph;
  49. console('debug', "update start book={$book} para={$paragraph} ");
  50. $now = date("Y-m-d H:i:s");
  51. //查询是否存在
  52. $query = 'SELECT id from fts_texts where book=? and paragraph = ?';
  53. $result = $PDO->dbSelect($query, [$book, $paragraph]);
  54. if (count($result) > 0) {
  55. //存在 update
  56. $query = 'UPDATE fts_texts set
  57. "bold_single"=?,
  58. "bold_double"=?,
  59. "bold_multiple"=?,
  60. "content"=?,
  61. "pcd_book_id"=?,
  62. "updated_at"=? where id=? ';
  63. $update = $PDO->dbSelect($query, [
  64. $row->bold1,
  65. $row->bold2,
  66. $row->bold3,
  67. $row->content,
  68. $row->pcd_book_id,
  69. $now,
  70. $result[0]['id']
  71. ]);
  72. } else {
  73. // new
  74. $query = "INSERT INTO fts_texts (
  75. book,
  76. paragraph,
  77. bold_single,
  78. bold_double,
  79. bold_multiple,
  80. \"content\",
  81. created_at,
  82. updated_at,
  83. pcd_book_id) VALUES
  84. (?,?,?,?,?,?,?,?,? )";
  85. $insert = $PDO->dbSelect(
  86. $query,
  87. [
  88. $row->book,
  89. $row->paragraph,
  90. $row->bold1,
  91. $row->bold2,
  92. $row->bold3,
  93. $row->content,
  94. $now,
  95. $now,
  96. $row->pcd_book_id,
  97. ]
  98. );
  99. }
  100. }
  101. console('debug', "update done book={$book} para={$paragraph} ");
  102. $maxPage = $json->data->count;
  103. console('debug', 'max page =' . $maxPage);
  104. if ($currPage + $pageSize < $maxPage) {
  105. $goNext = true;
  106. } else {
  107. console('debug', "book {$book} is done");
  108. $goNext = false;
  109. }
  110. } else {
  111. console('error', '');
  112. }
  113. } else {
  114. console('error', 'status=' . $status);
  115. }
  116. $currPage += $pageSize;
  117. } while ($goNext);
  118. }
  119. console('debug', 'all done');