PaliTextController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Resources\PaliTextResource;
  4. use App\Models\BookTitle;
  5. use App\Models\PaliText;
  6. use App\Models\Tag;
  7. use App\Models\TagMap;
  8. use App\Services\PaliTextService;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Http\Response;
  11. use Illuminate\Support\Facades\Cache;
  12. use Illuminate\Support\Facades\DB;
  13. class PaliTextController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return Response
  19. */
  20. public function index(Request $request)
  21. {
  22. //
  23. $request->validate([
  24. 'view' => 'required|in:chapter-tag,chapter,chapter_children,children,paragraph,book-toc',
  25. ]);
  26. $all_count = 0;
  27. switch ($request->input('view')) {
  28. case 'chapter-tag':
  29. /**
  30. * 分面筛选用的 tag 清单:返回每个 tag 及其命中的 pali_texts 条数。
  31. *
  32. * 传 tags 时是**且**的关系:内层按 anchor 统计它命中了几个指定 tag,
  33. * 只留下 co = 指定 tag 个数的,也就是必须同时带上全部指定 tag;
  34. * 外层再对这批 anchor 重新按 tag 统计条数,作为下一级筛选的候选项。
  35. *
  36. * @param string $tags 逗号分隔;不传则统计全部带 tag 的 pali_texts
  37. */
  38. $tm = (new TagMap)->getTable();
  39. $tg = (new Tag)->getTable();
  40. $pt = (new PaliText)->getTable();
  41. if ($request->input('tags') && $request->input('tags') !== '') {
  42. $tags = explode(',', $request->input('tags'));
  43. foreach ($tags as $tag) {
  44. // code...
  45. if (! empty($tag)) {
  46. $tagNames[] = $tag;
  47. }
  48. }
  49. }
  50. if (isset($tagNames)) {
  51. $where1 = ' where co = '.count($tagNames);
  52. $a = implode(',', array_fill(0, count($tagNames), '?'));
  53. $in1 = "and t.name in ({$a})";
  54. $param = $tagNames;
  55. } else {
  56. $where1 = ' ';
  57. $in1 = ' ';
  58. }
  59. $query = "
  60. select tags.id,tags.name,co as count
  61. from (
  62. select tm.tag_id,count(*) as co from (
  63. select anchor_id as id from (
  64. select tm.anchor_id , count(*) as co
  65. from $tm as tm
  66. left join $tg as t on tm.tag_id = t.id
  67. left join $pt as pc on tm.anchor_id = pc.uid
  68. where tm.table_name = 'pali_texts'
  69. $in1
  70. group by tm.anchor_id
  71. ) T
  72. $where1
  73. ) CID
  74. left join $tm as tm on tm.anchor_id = CID.id
  75. group by tm.tag_id
  76. ) tid
  77. left join $tg on $tg.id = tid.tag_id
  78. order by count desc
  79. ";
  80. if (isset($param)) {
  81. $chapters = DB::select($query, $param);
  82. } else {
  83. $chapters = DB::select($query);
  84. }
  85. $all_count = count($chapters);
  86. break;
  87. case 'chapter':
  88. /**
  89. * 书目/章节清单:返回 uid、book、paragraph、level、toc、chapter_strlen 等。
  90. *
  91. * tags 同样是**且**的关系。传了 tags 就放宽到 level < 3(书及其下一级),
  92. * 不传则只取 level = 1,即 276 本书的顶层。
  93. *
  94. * 注意不传 tags 时这条 SQL 仍从 tag_maps 出发,所以严格说返回的是
  95. * 「至少带一个 tag 的 level 1 记录」。目前 276 本书都带了 tag,两者恰好
  96. * 等价;将来有书没打 tag,这里会静默漏掉它。
  97. *
  98. * @param string $tags 逗号分隔
  99. */
  100. if ($request->input('tags') && $request->input('tags') !== '') {
  101. $tags = explode(',', $request->input('tags'));
  102. foreach ($tags as $tag) {
  103. // code...
  104. if (! empty($tag)) {
  105. $tagNames[] = $tag;
  106. }
  107. }
  108. }
  109. $tm = (new TagMap)->getTable();
  110. $tg = (new Tag)->getTable();
  111. $pt = (new PaliText)->getTable();
  112. if (isset($tagNames)) {
  113. $where1 = ' where co = '.count($tagNames);
  114. $a = implode(',', array_fill(0, count($tagNames), '?'));
  115. $in1 = "and t.name in ({$a})";
  116. $param = $tagNames;
  117. $where2 = 'where level < 3';
  118. } else {
  119. $where1 = ' ';
  120. $in1 = ' ';
  121. $where2 = 'where level = 1';
  122. }
  123. $query = "
  124. select uid as id,book,paragraph,level,toc as title,chapter_strlen,parent,path from (
  125. select anchor_id as cid from (
  126. select tm.anchor_id , count(*) as co
  127. from $tm as tm
  128. left join $tg as t on tm.tag_id = t.id
  129. where tm.table_name = 'pali_texts'
  130. $in1
  131. group by tm.anchor_id
  132. ) T
  133. $where1
  134. ) CID
  135. left join $pt as pt on CID.cid = pt.uid
  136. $where2
  137. order by book,paragraph";
  138. if (isset($param)) {
  139. $chapters = DB::select($query, $param);
  140. } else {
  141. $chapters = DB::select($query);
  142. }
  143. $all_count = count($chapters);
  144. break;
  145. case 'chapter_children':
  146. /**
  147. * 直接下级目录:信任 pali_texts.parent 字段,只取 level < 8 的目录节点。
  148. *
  149. * 与下面的 children 是两种取法:这里按 parent 直连,children 按段落区间
  150. * 推算。父节点底下没有目录型子节点时,这里干脆返回空,**不会**退化成
  151. * 列出正文段落——需要那种行为的用 children。
  152. *
  153. * @param int $book
  154. * @param int $para 父节点的段落号
  155. */
  156. $table = PaliText::where('book', $request->input('book'))
  157. ->where('parent', $request->input('para'))
  158. ->where('level', '<', 8);
  159. $all_count = $table->count();
  160. $chapters = $table->orderBy('paragraph')->get();
  161. break;
  162. case 'children':
  163. /**
  164. * 下一层目录:不看 parent 字段,改用「段落区间 + 最浅的下一层」推算。
  165. *
  166. * 在 [para+1, para+chapter_len-1] 区间里找 level 落在 root.level+1 到 7
  167. * 之间、最浅的那一层,再取该层的全部节点。这样层级有跳空也取得到——
  168. * 实测确有 level 2 直接跳到 level 4 的书(如 63:15006、143:5678)。
  169. *
  170. * ⚠ **区间里一个目录层都没有时会退化**:改为返回区间内的全部记录,
  171. * 其中包含 level 100 的正文段落。大章节因此可能一次返回几百条,
  172. * 例如 115:2955(level 7、chapter_len 605)会返回 604 条。
  173. * 调用方要么限制取用量,要么改用 chapter_children。
  174. *
  175. * level >= 8 的节点(level 8 是叶子条目、level 100 是正文段落)没有
  176. * 下级,直接返回空。
  177. *
  178. * @param string $id pali_texts.uid;给了就优先按它定位
  179. * @param int $book
  180. * @param int $para
  181. */
  182. if ($request->has('id')) {
  183. $root = PaliText::where('uid', $request->input('id'))
  184. ->first();
  185. } else {
  186. $root = PaliText::where('book', $request->input('book'))
  187. ->where('paragraph', $request->input('para'))
  188. ->first();
  189. }
  190. if ($root->level >= 8) {
  191. $chapters = [];
  192. break;
  193. }
  194. $start = $root->paragraph + 1;
  195. $end = $root->paragraph + $root->chapter_len - 1;
  196. $nextLevelChapter = PaliText::where('book', $root->book)
  197. ->whereBetween('paragraph', [$start, $end])
  198. ->whereBetween('level', [$root->level + 1, 7])
  199. ->orderBy('level', 'asc')
  200. ->first();
  201. if ($nextLevelChapter) {
  202. // 存在子目录
  203. $chapters = PaliText::where('book', $root->book)
  204. ->whereBetween('paragraph', [$start, $end])
  205. ->where('level', $nextLevelChapter->level)
  206. ->orderBy('paragraph', 'asc')
  207. ->get();
  208. } else {
  209. $chapters = PaliText::where('book', $root->book)
  210. ->whereBetween('paragraph', [$start, $end])
  211. ->orderBy('paragraph', 'asc')
  212. ->get();
  213. }
  214. $all_count = count($chapters);
  215. break;
  216. case 'paragraph':
  217. /**
  218. * 取单条 pali_texts 记录,按 (book, para) 精确匹配。
  219. *
  220. * 这个分支**直接 return**,不走后面统一的 progress_line 补充,也不套
  221. * rows/count 的外壳——返回的就是那条记录本身。查不到返回 error。
  222. *
  223. * @param int $book
  224. * @param int $para
  225. */
  226. $result = PaliText::where('book', $request->input('book'))
  227. ->where('paragraph', $request->input('para'))
  228. ->first();
  229. if ($result) {
  230. return $this->ok($result);
  231. } else {
  232. return $this->error('no data');
  233. }
  234. break;
  235. case 'book-toc':
  236. /**
  237. * 获取全书目录
  238. * 2023-1-25 改进算法
  239. * 需求:目录显示丛书以及此丛书下面的所有书。比如,选择清净道论的一个章节。显示清净道论两本书的目录
  240. * 算法:
  241. * 1. 查询这个目录的顶级目录
  242. * 2. 查询book-title 获取丛书名
  243. * 3. 根据从书名找到全部的书
  244. * 4. 获取全部书的目录
  245. *
  246. * 输出形状与其它分支不同,有两处:开头插一条 book=0、paragraph=0 的
  247. * 合成记录放丛书名;其余每条的 level 都 +1,给那条丛书名让出第 1 层。
  248. * 也正因为结构不同,末尾统一补 progress_line 时把 book-toc 排除在外。
  249. *
  250. * @param string $series 丛书名;给了就直接按它取书目列表
  251. * @param int $book 未给 series 时,与 para 一起定位所属丛书
  252. * @param int $para
  253. */
  254. if ($request->has('series')) {
  255. $book_title = $request->input('series');
  256. // 获取丛书书目列表
  257. $books = BookTitle::where('title', $request->input('series'))->get();
  258. } else {
  259. // 查询这个目录的顶级目录
  260. // 查询书起始段落
  261. $rootPara = app(PaliTextService::class)->getBookPara(
  262. $request->input('book'),
  263. $request->input('para')
  264. );
  265. if (! $rootPara) {
  266. return $this->error('no book', 404, 404);
  267. }
  268. // 获取丛书书名
  269. $book_title = BookTitle::where('book', $rootPara->book)
  270. ->where('paragraph', $rootPara->paragraph)
  271. ->value('title');
  272. // 获取丛书书目列表
  273. $books = BookTitle::where('title', $book_title)->get();
  274. }
  275. $chapters = [];
  276. $chapters[] = ['book' => 0, 'paragraph' => 0, 'toc' => $book_title, 'level' => 1];
  277. foreach ($books as $book) {
  278. // code...
  279. $rootPara = PaliText::where('book', $book->book)
  280. ->where('paragraph', $book->paragraph)
  281. ->first();
  282. $table = PaliText::where('book', $rootPara->book)
  283. ->whereBetween('paragraph', [$rootPara->paragraph, ($rootPara->paragraph + $rootPara->chapter_len - 1)])
  284. ->where('level', '<', 8);
  285. $all_count = $table->count();
  286. $curr_chapters = $table->select(['book', 'paragraph', 'toc', 'level'])->orderBy('paragraph')->get();
  287. foreach ($curr_chapters as $chapter) {
  288. // code...
  289. $chapters[] = ['book' => $chapter->book, 'paragraph' => $chapter->paragraph, 'toc' => $chapter->toc, 'level' => ($chapter->level + 1)];
  290. }
  291. }
  292. break;
  293. default:
  294. return $this->error('unknown view', 400, 400);
  295. }
  296. if ($request->input('view') !== 'book-toc') {
  297. foreach ($chapters as $key => $value) {
  298. if (is_object($value)) {
  299. // TODO $value->book 可能不存在
  300. $progress_key = "/chapter_dynamic/{$value->book}/{$value->paragraph}/global";
  301. $chapters[$key]->progress_line = Cache::get($progress_key);
  302. }
  303. }
  304. }
  305. return $this->ok(['rows' => $chapters, 'count' => $all_count]);
  306. }
  307. /**
  308. * Store a newly created resource in storage.
  309. *
  310. * @return Response
  311. */
  312. public function store(Request $request)
  313. {
  314. //
  315. }
  316. /**
  317. * Display the specified resource.
  318. *
  319. * @return Response
  320. */
  321. public function show(string $id)
  322. {
  323. //
  324. $para = explode('-', $id);
  325. $paragraph = PaliText::where('book', $para[0])->where('paragraph', $para[1])->first();
  326. return $this->ok(new PaliTextResource($paragraph));
  327. }
  328. /**
  329. * Update the specified resource in storage.
  330. *
  331. * @return Response
  332. */
  333. public function update(Request $request, PaliText $paliText)
  334. {
  335. //
  336. }
  337. /**
  338. * Remove the specified resource from storage.
  339. *
  340. * @return Response
  341. */
  342. public function destroy(PaliText $paliText)
  343. {
  344. //
  345. }
  346. }