SearchPaliDataController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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->get('book');
  21. $maxParagraph = WbwTemplate::where('book',$book)->max('paragraph');
  22. $pageSize = $request->get('page_size',1000);
  23. $start = $request->get('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 = ['book'=>$book,
  67. 'paragraph' => $iPara,
  68. 'bold1' => implode(' ',$bold1),
  69. 'bold2' => implode(' ',$bold2),
  70. 'bold3' => implode(' ',$bold3),
  71. 'content' => $content,
  72. 'pcd_book_id' => $pcd_book_id
  73. ];
  74. $output[] = $update;
  75. }
  76. return $this->ok(['rows'=>$output,'count'=>$maxParagraph]);
  77. }
  78. private function getContent($book,$para){
  79. $words = WbwTemplate::where('book',$book)
  80. ->where('paragraph',$para)
  81. ->where('type',"<>",".ctl.")
  82. ->orderBy('wid')->get();
  83. $content = '';
  84. foreach ($words as $word) {
  85. if($word->style === 'bld'){
  86. if(strpos($word->word,"{")===FALSE){
  87. $content .= "**{$word->word}** ";
  88. }else{
  89. $content .= str_replace(['{','}'],['**','** '],$word->word);
  90. }
  91. }else if($word->style === 'note'){
  92. $content .= " _{$word->word}_ ";
  93. }else{
  94. $content .= $word->word . " ";
  95. }
  96. }
  97. return $content;
  98. }
  99. /**
  100. * Store a newly created resource in storage.
  101. *
  102. * @param \Illuminate\Http\Request $request
  103. * @return \Illuminate\Http\Response
  104. */
  105. public function store(Request $request)
  106. {
  107. //
  108. }
  109. /**
  110. * Display the specified resource.
  111. *
  112. * @param int $id
  113. * @return \Illuminate\Http\Response
  114. */
  115. public function show($id)
  116. {
  117. //
  118. }
  119. /**
  120. * Update the specified resource in storage.
  121. *
  122. * @param \Illuminate\Http\Request $request
  123. * @param int $id
  124. * @return \Illuminate\Http\Response
  125. */
  126. public function update(Request $request, $id)
  127. {
  128. //
  129. }
  130. /**
  131. * Remove the specified resource from storage.
  132. *
  133. * @param int $id
  134. * @return \Illuminate\Http\Response
  135. */
  136. public function destroy($id)
  137. {
  138. //
  139. }
  140. }