PaliTextController.php 17 KB

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