2
0

WikiController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. $result = $this->searchService->get("term_{$term['guid']}");
  178. if (isset($result['_source']['tags'])) {
  179. $quality = $this->getQuality($result['_source']['tags']);
  180. } else {
  181. $quality = null;
  182. }
  183. $cats = $this->getCategories($result['_source']['tags']);
  184. $entry = [
  185. 'word' => $term['word'],
  186. 'lang' => $term['language'],
  187. 'slug' => $term['word'],
  188. 'meaning' => $term['meaning'],
  189. 'quality' => $quality, // featured | standard | draft | pending | null
  190. 'category' => '法义术语',
  191. 'tags' => $cats,
  192. 'langs' => [
  193. ['lang' => 'zh-Hant', 'label' => '繁体中文', 'word' => '无常'],
  194. ['lang' => 'en', 'label' => 'English', 'word' => 'Impermanence'],
  195. ],
  196. 'related' => $this->related($cats),
  197. 'content' => $term['html'] ?? ''
  198. ];
  199. $parsed = WikiContentParser::parse($entry['content']);
  200. return view('library.wiki.show', [
  201. 'entry' => array_merge($entry, [
  202. 'content' => $parsed['content'],
  203. 'toc' => $parsed['toc'],
  204. 'edit_url' => config('mint.server.dashboard_base_path') . "/workspace/term/{$term['guid']}/edit",
  205. 'zh' => '编辑',
  206. 'other_versions' => $this->otherVersions($term['word']),
  207. ]),
  208. 'categories' => $this->categories(),
  209. 'lang' => $lang,
  210. ]);
  211. }
  212. private function related(array $tags): array
  213. {
  214. $params = [
  215. 'pageSize' => 5,
  216. 'resourceType' => 'term',
  217. 'tags' => array_map(fn($n) => "category:{$n}", $tags),
  218. ];
  219. $result = $this->searchService->search($params);
  220. $relates = [];
  221. foreach ($result['hits']['hits'] as $item) {
  222. $relates[] = [
  223. 'word' => $item['_source']['title']['text']['pali'],
  224. 'zh' => $item['_source']['title']['text']['zh'],
  225. 'lang' => $item['_source']['language']
  226. ];
  227. }
  228. return $relates;
  229. }
  230. // ── Helpers ──────────────────────────────────────────────────
  231. private function categories(): array
  232. {
  233. $cats = collect(config('taxonomy'))
  234. ->map(fn($cat) => ['id' => $cat['id'], 'label' => $cat['label']])
  235. ->toArray();
  236. return $cats;
  237. }
  238. // 在 WikiController.php 中添加此方法
  239. // 在 WikiController.php 中添加
  240. public function home(string $lang = 'zh-Hans')
  241. {
  242. // Mock 语言列表
  243. $languages = [
  244. ['code' => 'zh-Hans', 'name' => '简体中文'],
  245. ['code' => 'zh-Hant', 'name' => '繁體中文'],
  246. ['code' => 'en', 'name' => 'English'],
  247. ['code' => 'ja', 'name' => '日本語'],
  248. ['code' => 'ko', 'name' => '한국어'],
  249. ['code' => 'vi', 'name' => 'Tiếng Việt'],
  250. ['code' => 'my', 'name' => 'မြန်မာစာ'],
  251. ];
  252. // Mock 统计数据
  253. $stats = [
  254. 'total_articles' => 2847,
  255. 'total_terms' => 1256,
  256. 'languages_count' => 10,
  257. 'contributors' => 328,
  258. 'today_updates' => 12,
  259. ];
  260. // Mock 热门搜索标签
  261. $hotTags = ['无常', 'Anicca', '四圣谛', '涅槃', '内观', '业力', '慈悲', '般若'];
  262. // Mock 每日一词(可选展示)
  263. $dailyTerm = [
  264. 'word' => 'Dhammapada',
  265. 'zh' => '法句经',
  266. 'lang' => 'pi',
  267. 'meaning' => '佛陀的偈颂集,佛教最重要的经典之一',
  268. ];
  269. return view('library.wiki.home', [
  270. 'languages' => $languages,
  271. 'currentLang' => $lang,
  272. 'stats' => $stats,
  273. 'hotTags' => $hotTags,
  274. 'dailyTerm' => $dailyTerm,
  275. ]);
  276. }
  277. private function qualities(): array
  278. {
  279. return [
  280. ['value' => 'featured', 'label' => '典范条目', 'subtitle' => '平台推荐'],
  281. ['value' => 'standard', 'label' => '规范条目', 'subtitle' => '邀请试读'],
  282. ['value' => 'draft', 'label' => '草稿', 'subtitle' => '仅供参考'],
  283. ['value' => 'pending', 'label' => '待定', 'subtitle' => '审稿专用'],
  284. ];
  285. }
  286. private function featured(array $all): array
  287. {
  288. return array_map(function ($item) {
  289. return ['word' => $item['word'], 'zh' => $item['meaning'], 'lang' => $item['language'], 'category' => '法义术语'];
  290. }, $all);
  291. }
  292. private function mockStats(): array
  293. {
  294. return [
  295. 'total' => 2847,
  296. 'this_month' => 43,
  297. 'contributors' => 128,
  298. ];
  299. }
  300. private function mockRecentUpdates(): array
  301. {
  302. return [
  303. ['word' => 'Nibbāna', 'lang' => 'pi'],
  304. ['word' => '四圣谛', 'lang' => 'zh'],
  305. ['word' => '阿含经', 'lang' => 'zh'],
  306. ['word' => 'Rājagaha', 'lang' => 'pi'],
  307. ];
  308. }
  309. private function otherVersions(string $word): array
  310. {
  311. $params = [
  312. 'query' => $word,
  313. 'pageSize' => 10,
  314. 'resourceType' => 'term',
  315. 'tags' => array_map(fn($n) => "quality:{$n}", array_keys($this->qualityRank)),
  316. ];
  317. $result = $this->searchService->search($params);
  318. $versions = [];
  319. foreach ($result['hits']['hits'] as $item) {
  320. $text = $item['_source']['title']['text'];
  321. $id = $item['_source']['resource_id'];
  322. $versions[] = [
  323. 'type' => 'term',
  324. 'id' => $id,
  325. 'lang' => $item['_source']['language'],
  326. 'title' => $text['zh'] ?? 'null',
  327. 'quality' => $this->getQuality($item['_source']['tags'] ?? 'quality:pending'),
  328. 'category' => '法義術語',
  329. 'snippet' => $item['_source']['summary']['text'],
  330. 'updated' => $item['_source']['updated_at'],
  331. ];
  332. }
  333. return $versions;
  334. }
  335. }