SearchPaliDataController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. for($iPara=$start; $iPara < $start+$pageSize; $iPara++){
  26. $content = $this->getContent($book,$iPara);
  27. //查找黑体字
  28. $words = WbwTemplate::where('book',$book)
  29. ->where('paragraph',$iPara)
  30. ->orderBy('wid')->get();
  31. $bold1 = array();
  32. $bold2 = array();
  33. $bold3 = array();
  34. $currBold = array();
  35. foreach ($words as $word) {
  36. if($word->style==='bld'){
  37. $currBold[] = $word->real;
  38. }else{
  39. $countBold = count($currBold);
  40. if($countBold === 1){
  41. $bold1[] = $currBold[0];
  42. }else if($countBold === 2){
  43. $bold2 = array_merge($bold2,$currBold);
  44. }else if($countBold > 0){
  45. $bold3 = array_merge($bold3,$currBold);
  46. }
  47. $currBold = [];
  48. }
  49. }
  50. $pcd_book = BookTitle::where('book',$book)
  51. ->where('paragraph','<=',$iPara)
  52. ->orderBy('paragraph','desc')
  53. ->first();
  54. if($pcd_book){
  55. $pcd_book_id = $pcd_book->sn;
  56. }else{
  57. $pcd_book_id = BookTitle::where('book',$book)
  58. ->orderBy('paragraph')
  59. ->value('sn');
  60. }
  61. $update = ['book'=>$book,
  62. 'paragraph' => $iPara,
  63. 'bold1' => implode(' ',$bold1),
  64. 'bold2' => implode(' ',$bold2),
  65. 'bold3' => implode(' ',$bold3),
  66. 'content' => $content,
  67. 'pcd_book_id' => $pcd_book_id
  68. ];
  69. $output[] = $update;
  70. }
  71. return $this->ok(['rows'=>$output,'count'=>$maxParagraph]);
  72. }
  73. private function getContent($book,$para){
  74. $words = WbwTemplate::where('book',$book)
  75. ->where('paragraph',$para)
  76. ->where('type',"<>",".ctl.")
  77. ->orderBy('wid')->get();
  78. $content = '';
  79. foreach ($words as $word) {
  80. if($word->style === 'bld'){
  81. if(strpos($word->word,"{")===FALSE){
  82. $content .= "**{$word->word}** ";
  83. }else{
  84. $content .= str_replace(['{','}'],['**','** '],$word->word);
  85. }
  86. }else if($word->style === 'note'){
  87. $content .= " _{$word->word}_ ";
  88. }else{
  89. $content .= $word->word . " ";
  90. }
  91. }
  92. return $content;
  93. }
  94. /**
  95. * Store a newly created resource in storage.
  96. *
  97. * @param \Illuminate\Http\Request $request
  98. * @return \Illuminate\Http\Response
  99. */
  100. public function store(Request $request)
  101. {
  102. //
  103. }
  104. /**
  105. * Display the specified resource.
  106. *
  107. * @param int $id
  108. * @return \Illuminate\Http\Response
  109. */
  110. public function show($id)
  111. {
  112. //
  113. }
  114. /**
  115. * Update the specified resource in storage.
  116. *
  117. * @param \Illuminate\Http\Request $request
  118. * @param int $id
  119. * @return \Illuminate\Http\Response
  120. */
  121. public function update(Request $request, $id)
  122. {
  123. //
  124. }
  125. /**
  126. * Remove the specified resource from storage.
  127. *
  128. * @param int $id
  129. * @return \Illuminate\Http\Response
  130. */
  131. public function destroy($id)
  132. {
  133. //
  134. }
  135. }