WikiController.php 13 KB

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