PaliTextController.php 17 KB

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