SearchPaliDataController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * 输出巴利语全文搜索数据
  4. * 提供给搜索引擎
  5. */
  6. namespace App\Http\Controllers;
  7. use App\Models\BookTitle;
  8. use App\Models\WbwTemplate;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Http\Response;
  11. class SearchPaliDataController extends Controller
  12. {
  13. /**
  14. * Display a listing of the resource.
  15. *
  16. * @return Response
  17. */
  18. public function index(Request $request)
  19. {
  20. //
  21. $book = $request->input('book');
  22. $maxParagraph = WbwTemplate::where('book', $book)->max('paragraph');
  23. $pageSize = $request->input('page_size', 1000);
  24. $start = $request->input('start', 1);
  25. $output = [];
  26. if ($start + $pageSize > $maxParagraph) {
  27. $endOfPara = $maxParagraph + 1;
  28. } else {
  29. $endOfPara = $start + $pageSize;
  30. }
  31. for ($iPara = $start; $iPara < $endOfPara; $iPara++) {
  32. $content = $this->getContent($book, $iPara);
  33. // 查找黑体字
  34. $words = WbwTemplate::where('book', $book)
  35. ->where('paragraph', $iPara)
  36. ->orderBy('wid')->get();
  37. $bold1 = [];
  38. $bold2 = [];
  39. $bold3 = [];
  40. $currBold = [];
  41. foreach ($words as $word) {
  42. if ($word->style === 'bld') {
  43. $currBold[] = $word->real;
  44. } else {
  45. $countBold = count($currBold);
  46. if ($countBold === 1) {
  47. $bold1[] = $currBold[0];
  48. } elseif ($countBold === 2) {
  49. $bold2 = array_merge($bold2, $currBold);
  50. } elseif ($countBold > 0) {
  51. $bold3 = array_merge($bold3, $currBold);
  52. }
  53. $currBold = [];
  54. }
  55. }
  56. $pcd_book = BookTitle::where('book', $book)
  57. ->where('paragraph', '<=', $iPara)
  58. ->orderBy('paragraph', 'desc')
  59. ->first();
  60. if ($pcd_book) {
  61. $pcd_book_id = $pcd_book->sn;
  62. } else {
  63. $pcd_book_id = BookTitle::where('book', $book)
  64. ->orderBy('paragraph')
  65. ->value('sn');
  66. }
  67. $update = [
  68. 'book' => $book,
  69. 'paragraph' => $iPara,
  70. 'bold1' => implode(' ', $bold1),
  71. 'bold2' => implode(' ', $bold2),
  72. 'bold3' => implode(' ', $bold3),
  73. 'content' => $content,
  74. 'pcd_book_id' => $pcd_book_id,
  75. ];
  76. $output[] = $update;
  77. }
  78. return $this->ok(['rows' => $output, 'count' => $maxParagraph]);
  79. }
  80. private function getContent($book, $para)
  81. {
  82. $words = WbwTemplate::where('book', $book)
  83. ->where('paragraph', $para)
  84. ->where('type', '<>', '.ctl.')
  85. ->orderBy('wid')->get();
  86. $content = '';
  87. foreach ($words as $word) {
  88. if ($word->style === 'bld') {
  89. if (strpos($word->word, '{') === false) {
  90. $content .= "**{$word->word}** ";
  91. } else {
  92. $content .= str_replace(['{', '}'], ['**', '** '], $word->word);
  93. }
  94. } elseif ($word->style === 'note') {
  95. $content .= " _{$word->word}_ ";
  96. } else {
  97. $content .= $word->word.' ';
  98. }
  99. }
  100. return $content;
  101. }
  102. /**
  103. * Store a newly created resource in storage.
  104. *
  105. * @return Response
  106. */
  107. public function store(Request $request)
  108. {
  109. //
  110. }
  111. /**
  112. * Display the specified resource.
  113. *
  114. * @param int $id
  115. * @return Response
  116. */
  117. public function show($id)
  118. {
  119. //
  120. }
  121. /**
  122. * Update the specified resource in storage.
  123. *
  124. * @param int $id
  125. * @return Response
  126. */
  127. public function update(Request $request, $id)
  128. {
  129. //
  130. }
  131. /**
  132. * Remove the specified resource from storage.
  133. *
  134. * @param int $id
  135. * @return Response
  136. */
  137. public function destroy($id)
  138. {
  139. //
  140. }
  141. }