WikiController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. namespace App\Http\Controllers\Library;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Helpers\WikiContentParser;
  6. use App\Services\TermService;
  7. use Illuminate\Support\Str;
  8. use App\Services\OpenSearchService;
  9. class WikiController extends Controller
  10. {
  11. // 质量等级(数值越小等级越高)
  12. private $qualityRank = [
  13. 'featured' => 1,
  14. 'standard' => 2,
  15. 'draft' => 3,
  16. 'pending' => 4,
  17. ];
  18. public function __construct(
  19. private TermService $termService,
  20. private OpenSearchService $searchService
  21. ) {}
  22. // ── Mock 数据 ────────────────────────────────────────────────
  23. private function mockEntries(): array
  24. {
  25. return [
  26. [
  27. 'word' => 'Anicca',
  28. 'lang' => 'zh-Hans',
  29. 'slug' => 'anicca',
  30. 'meaning' => '无常',
  31. 'quality' => 'featured', // featured | stub | review | null
  32. 'category' => '法义术语',
  33. 'tags' => ['三相', '法义术语', '相应部', '内观', '五蕴'],
  34. 'langs' => [
  35. ['lang' => 'zh-Hant', 'label' => '繁体中文', 'word' => '无常'],
  36. ['lang' => 'en', 'label' => 'English', 'word' => 'Impermanence'],
  37. ],
  38. 'related' => [
  39. ['word' => 'Dukkha', 'zh' => '苦', 'lang' => 'pi'],
  40. ['word' => 'Anattā', 'zh' => '无我', 'lang' => 'pi'],
  41. ['word' => 'Vipassanā', 'zh' => '内观', 'lang' => 'pi'],
  42. ['word' => 'Ti-lakkhaṇa', 'zh' => '三相', 'lang' => 'pi'],
  43. ],
  44. 'content' => <<<HTML
  45. <h2>词源与释义</h2>
  46. <blockquote>
  47. 「诸比丘,色是无常的。无常的即是苦的。苦的即是无我的。无我的即应如实以正慧观察……」
  48. <cite>SN 22.15 · Khandha-saṃyutta · 相应部·蕴相应</cite>
  49. </blockquote>
  50. HTML,
  51. ],
  52. ];
  53. }
  54. // ── Actions ──────────────────────────────────────────────────
  55. public function index(Request $request, string $lang)
  56. {
  57. $quality = $request->input('quality')
  58. ?? $request->cookie('wiki_quality_filter')
  59. ?? 'draft';
  60. $cookie = cookie()->forever('wiki_quality_filter', $quality);
  61. $category = $request->input('category');
  62. $subs = null;
  63. if ($category) {
  64. $taxNode = collect(config('taxonomy'))->firstWhere('id', $category);
  65. $subs = $taxNode ? $this->subEntries($taxNode['subs'], $lang, $quality) : null;
  66. }
  67. $result = $this->termService->communityTerms($lang);
  68. $fakeRequest = Request::create('', 'GET', []);
  69. $terms = $result['data']->toArray($fakeRequest);
  70. $first = $terms[0];
  71. $today = [
  72. 'word' => $first['word'],
  73. 'lang' => $first['language'],
  74. 'slug' => $first['word'],
  75. 'meaning' => $first['meaning'],
  76. 'quality' => 'featured',
  77. 'category' => '法义术语',
  78. 'content' => $first['summary'],
  79. ];
  80. return response()
  81. ->view('library.wiki.index', [
  82. 'today' => $request->has('category') ? null : $today,
  83. 'featured' => $category ? null : $this->featured($terms),
  84. 'stats' => $this->mockStats(),
  85. 'recentUpdates' => $this->mockRecentUpdates(),
  86. 'categories' => $this->categories(),
  87. 'lang' => $lang,
  88. 'category' => $category,
  89. 'subs' => $subs, // null | array of subs with entries
  90. 'quality' => $quality,
  91. 'qualities' => $this->qualities(),
  92. ])->withCookie($cookie);
  93. }
  94. /**
  95. * 给每个二级分类注入 词条列表
  96. * 真实数据替换时:按 sub['tags'] 查询术语表,返回同结构数组即可
  97. */
  98. private function subEntries(array $subs, string $lang, string $quality): array
  99. {
  100. return array_map(function ($sub) use ($lang, $quality) {
  101. $entries = $this->querySubCat($sub['tags'], $lang, $quality);
  102. return array_merge($sub, ['entries' => $entries]);
  103. }, $subs);
  104. }
  105. private function querySubCat(array $cats, string $lang, string $quality): array
  106. {
  107. $params = [
  108. 'pageSize' => 1000,
  109. 'resourceType' => 'term',
  110. 'language' => $lang,
  111. 'tags' => array_map(fn($n) => "category:{$n}", $cats),
  112. ];
  113. $result = $this->searchService->search($params);
  114. // 当前允许的最大等级
  115. $maxRank = $this->qualityRank[$quality] ?? 4;
  116. $unique = [];
  117. foreach ($result['hits']['hits'] as $item) {
  118. $text = $item['_source']['title']['text'];
  119. $id = $item['_source']['resource_id'];
  120. if (isset($item['_source']['tags'])) {
  121. $itemQuality = $this->getQuality($item['_source']['tags']) ?? 'pending';
  122. } else {
  123. $itemQuality = 'pending';
  124. }
  125. $itemRank = $this->qualityRank[$itemQuality] ?? 4;
  126. // 按输入质量过滤
  127. if ($itemRank > $maxRank) {
  128. continue;
  129. }
  130. $record = [
  131. 'id' => $id,
  132. 'word' => $text['pali'],
  133. 'zh' => $text['zh'],
  134. 'quality' => $itemQuality,
  135. ];
  136. // 用 pali + zh 去重
  137. $key = mb_strtolower(trim($text['pali']) . '|' . trim($text['zh']));
  138. // 如果不存在,直接保存
  139. if (!isset($unique[$key])) {
  140. $unique[$key] = $record;
  141. continue;
  142. }
  143. // 已存在时,保留质量更高的
  144. $existingQuality = $unique[$key]['quality'];
  145. $existingRank = $this->qualityRank[$existingQuality] ?? 4;
  146. if ($itemRank < $existingRank) {
  147. $unique[$key] = $record;
  148. }
  149. }
  150. return array_values($unique);
  151. }
  152. private function getQuality(array $tags)
  153. {
  154. $qualityTag = array_find($tags, function ($tag) {
  155. return str_contains($tag, 'quality:');
  156. });
  157. if ($qualityTag) {
  158. return mb_substr($qualityTag, 8, null, "UTF-8");
  159. } else {
  160. return null;
  161. }
  162. }
  163. private function getCategories(array $tags)
  164. {
  165. $catTag = array_filter($tags, function ($tag) {
  166. return str_contains($tag, 'category:');
  167. });
  168. return array_map(fn($item) => mb_substr($item, mb_strlen('category:', 'UTF-8')), $catTag);
  169. }
  170. public function show(string $lang, string $word)
  171. {
  172. if (Str::isUuid($word)) {
  173. $term = $this->termService->find($word, 'html');
  174. } else {
  175. $term = $this->termService->communityWiki($word, $lang, 'html');
  176. }
  177. try {
  178. $result = $this->searchService->get("term_{$term['guid']}");
  179. } catch (\Throwable $th) {
  180. abort(404);
  181. }
  182. if (isset($result['_source']['tags'])) {
  183. $quality = $this->getQuality($result['_source']['tags']);
  184. } else {
  185. $quality = null;
  186. }
  187. $cats = $this->getCategories($result['_source']['tags']);
  188. $entry = [
  189. 'word' => $term['word'],
  190. 'lang' => $term['language'],
  191. 'slug' => $term['word'],
  192. 'meaning' => $term['meaning'],
  193. 'quality' => $quality, // featured | standard | draft | pending | null
  194. 'category' => '法义术语',
  195. 'tags' => $cats,
  196. 'langs' => [
  197. ['lang' => 'zh-Hant', 'label' => '繁体中文', 'word' => '无常'],
  198. ['lang' => 'en', 'label' => 'English', 'word' => 'Impermanence'],
  199. ],
  200. 'related' => $this->related($cats),
  201. 'content' => $term['html'] ?? ''
  202. ];
  203. $parsed = WikiContentParser::parse($entry['content']);
  204. return view('library.wiki.show', [
  205. 'entry' => array_merge($entry, [
  206. 'content' => $parsed['content'],
  207. 'toc' => $parsed['toc'],
  208. 'edit_url' => config('mint.server.dashboard_base_path') . "/workspace/term/{$term['guid']}/edit",
  209. 'zh' => '编辑',
  210. 'other_versions' => $this->otherVersions($term['word']),
  211. ]),
  212. 'categories' => $this->categories(),
  213. 'lang' => $lang,
  214. ]);
  215. }
  216. private function related(array $tags): array
  217. {
  218. $params = [
  219. 'pageSize' => 5,
  220. 'resourceType' => 'term',
  221. 'tags' => array_map(fn($n) => "category:{$n}", $tags),
  222. ];
  223. $result = $this->searchService->search($params);
  224. $relates = [];
  225. foreach ($result['hits']['hits'] as $item) {
  226. $relates[] = [
  227. 'word' => $item['_source']['title']['text']['pali'],
  228. 'zh' => $item['_source']['title']['text']['zh'],
  229. 'lang' => $item['_source']['language']
  230. ];
  231. }
  232. return $relates;
  233. }
  234. // ── Helpers ──────────────────────────────────────────────────
  235. private function categories(): array
  236. {
  237. $cats = collect(config('taxonomy'))
  238. ->map(fn($cat) => ['id' => $cat['id'], 'label' => $cat['label']])
  239. ->toArray();
  240. return $cats;
  241. }
  242. // 在 WikiController.php 中添加此方法
  243. // 在 WikiController.php 中添加
  244. public function home(string $lang = 'zh-Hans')
  245. {
  246. // Mock 语言列表
  247. $languages = [
  248. ['code' => 'zh-Hans', 'name' => '简体中文'],
  249. ['code' => 'zh-Hant', 'name' => '繁體中文'],
  250. ['code' => 'en', 'name' => 'English'],
  251. ['code' => 'ja', 'name' => '日本語'],
  252. ['code' => 'ko', 'name' => '한국어'],
  253. ['code' => 'vi', 'name' => 'Tiếng Việt'],
  254. ['code' => 'my', 'name' => 'မြန်မာစာ'],
  255. ];
  256. // Mock 统计数据
  257. $stats = [
  258. 'total_articles' => 2847,
  259. 'total_terms' => 1256,
  260. 'languages_count' => 10,
  261. 'contributors' => 328,
  262. 'today_updates' => 12,
  263. ];
  264. // Mock 热门搜索标签
  265. $hotTags = ['无常', 'Anicca', '四圣谛', '涅槃', '内观', '业力', '慈悲', '般若'];
  266. // Mock 每日一词(可选展示)
  267. $dailyTerm = [
  268. 'word' => 'Dhammapada',
  269. 'zh' => '法句经',
  270. 'lang' => 'pi',
  271. 'meaning' => '佛陀的偈颂集,佛教最重要的经典之一',
  272. ];
  273. return view('library.wiki.home', [
  274. 'languages' => $languages,
  275. 'currentLang' => $lang,
  276. 'stats' => $stats,
  277. 'hotTags' => $hotTags,
  278. 'dailyTerm' => $dailyTerm,
  279. ]);
  280. }
  281. private function qualities(): array
  282. {
  283. return [
  284. ['value' => 'featured', 'label' => '典范条目', 'subtitle' => '平台推荐'],
  285. ['value' => 'standard', 'label' => '规范条目', 'subtitle' => '邀请试读'],
  286. ['value' => 'draft', 'label' => '草稿', 'subtitle' => '仅供参考'],
  287. ['value' => 'pending', 'label' => '待定', 'subtitle' => '审稿专用'],
  288. ];
  289. }
  290. private function featured(array $all): array
  291. {
  292. return array_map(function ($item) {
  293. return ['word' => $item['word'], 'zh' => $item['meaning'], 'lang' => $item['language'], 'category' => '法义术语'];
  294. }, $all);
  295. }
  296. private function mockStats(): array
  297. {
  298. return [
  299. 'total' => 2847,
  300. 'this_month' => 43,
  301. 'contributors' => 128,
  302. ];
  303. }
  304. private function mockRecentUpdates(): array
  305. {
  306. return [
  307. ['word' => 'Nibbāna', 'lang' => 'pi'],
  308. ['word' => '四圣谛', 'lang' => 'zh'],
  309. ['word' => '阿含经', 'lang' => 'zh'],
  310. ['word' => 'Rājagaha', 'lang' => 'pi'],
  311. ];
  312. }
  313. private function otherVersions(string $word): array
  314. {
  315. $params = [
  316. 'query' => $word,
  317. 'pageSize' => 10,
  318. 'resourceType' => 'term',
  319. 'tags' => array_map(fn($n) => "quality:{$n}", array_keys($this->qualityRank)),
  320. ];
  321. $result = $this->searchService->search($params);
  322. $versions = [];
  323. foreach ($result['hits']['hits'] as $item) {
  324. $text = $item['_source']['title']['text'];
  325. $id = $item['_source']['resource_id'];
  326. $versions[] = [
  327. 'type' => 'term',
  328. 'id' => $id,
  329. 'lang' => $item['_source']['language'],
  330. 'title' => $text['zh'] ?? 'null',
  331. 'quality' => $this->getQuality($item['_source']['tags'] ?? 'quality:pending'),
  332. 'category' => '法義術語',
  333. 'snippet' => $item['_source']['summary']['text'],
  334. 'updated' => $item['_source']['updated_at'],
  335. ];
  336. }
  337. return $versions;
  338. }
  339. }