SearchPaliDataController.php 4.3 KB

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