Spaces:
Running
Running
Download app/Controllers/SutraController.php from buddhaword/web: direct link, hf CLI and curl.
- Browser
- Download file 13.5 kB
-
https://huggingface.co/spaces/buddhaword/web/resolve/main/app/Controllers/SutraController.php
- Command line
-
hf download hf://spaces/buddhaword/web/app/Controllers/SutraController.php
-
curl -L -o SutraController.php https://huggingface.co/spaces/buddhaword/web/resolve/main/app/Controllers/SutraController.php
13.5 kB
| namespace App\Controllers; | |
| use App\Models\Sutra; | |
| require_once __DIR__ . '/../Helpers/cache.php'; | |
| class SutraController { | |
| /** | |
| * Read a derived sutra cache. The cache is stale as soon as the source | |
| * sutra_api.json is refreshed (sync), even if the 24h TTL has not elapsed. | |
| */ | |
| private function readSutraCache($file, $maxAge = 86400) { | |
| $cached = readCache($file, $maxAge); | |
| if ($cached === null) return null; | |
| $path = getCachePath($file); | |
| $source = getCachePath('sutra_api.json'); | |
| if (file_exists($source) && file_exists($path) && filemtime($source) > filemtime($path)) { | |
| return null; | |
| } | |
| return $cached; | |
| } | |
| public function index() { | |
| /* The list page only needs category names. Caching them avoids decoding | |
| the ~6 MB sutra store on every visit (big TTFB win on mobile). */ | |
| $cacheFile = 'sutra_categories_v1.json'; | |
| $categories = null; | |
| $cached = $this->readSutraCache($cacheFile); | |
| if ($cached !== null) { | |
| $decoded = json_decode($cached, true); | |
| if (is_array($decoded) && !empty($decoded)) { | |
| $categories = $decoded; | |
| } | |
| } | |
| if ($categories === null) { | |
| $sutras = Sutra::getAll(); | |
| $categoriesFromData = []; | |
| foreach ($sutras as $item) { | |
| $category = $item['ໝວດທັມ'] ?? ''; | |
| if ($category !== '' && !in_array($category, $categoriesFromData)) { | |
| $categoriesFromData[] = $category; | |
| } | |
| } | |
| $imagesDir = __DIR__ . '/../../public/images/sutra/'; | |
| $availableCategories = []; | |
| if (is_dir($imagesDir)) { | |
| $files = scandir($imagesDir); | |
| foreach ($files as $file) { | |
| if (pathinfo($file, PATHINFO_EXTENSION) === 'jpg') { | |
| $availableCategories[] = pathinfo($file, PATHINFO_FILENAME); | |
| } | |
| } | |
| } | |
| $categories = array_values(array_intersect($categoriesFromData, $availableCategories)); | |
| if (empty($categories)) { | |
| $categories = $availableCategories; | |
| } | |
| if (!empty($categories)) { | |
| writeCache($cacheFile, json_encode($categories, JSON_UNESCAPED_UNICODE)); | |
| } | |
| } | |
| $canonicalUrl = canonicalUrl(); | |
| $siteUrl = getSiteUrl(); | |
| $jsonLd = [ | |
| [ | |
| '@context' => 'https://schema.org', | |
| '@type' => 'WebSite', | |
| 'name' => 'ຄຳສອນພຸດທະ', | |
| 'url' => $siteUrl, | |
| 'description' => 'ເວັບໄຊທ໌ສຳລັບຮຽນຮູ້ພຣະສູດ ແລະ ຄຳສອນຂອງພຣະພຸດທະເຈົ້າ', | |
| 'inLanguage' => 'lo', | |
| 'potentialAction' => [ | |
| '@type' => 'SearchAction', | |
| 'target' => [ | |
| '@type' => 'EntryPoint', | |
| 'urlTemplate' => absoluteUrl('/api/search') . '?keyword={search_term}', | |
| ], | |
| 'query-input' => 'required name=search_term', | |
| ], | |
| ], | |
| [ | |
| '@context' => 'https://schema.org', | |
| '@type' => 'CollectionPage', | |
| 'name' => 'ພຣະສູດ - ຄຳສອນພຸດທະ', | |
| 'description' => 'ລວມພຣະສູດ ແລະ ຄຳສອນຂອງພຣະພຸດທະເຈົ້າ', | |
| 'url' => $canonicalUrl, | |
| 'inLanguage' => 'lo', | |
| 'isPartOf' => [ | |
| '@type' => 'WebSite', | |
| 'name' => 'ຄຳສອນພຸດທະ', | |
| 'url' => $siteUrl, | |
| ], | |
| ], | |
| ]; | |
| return view('pages.sutra.index', [ | |
| 'categories' => $categories, | |
| 'seo' => [ | |
| 'title' => 'ຄຳສອນພຸດທະ - ຮຽນຮູ້ພຣະສູດ ແລະ ທັມມະ', | |
| 'description' => 'ເວັບໄຊທ໌ສຳລັບຮຽນຮູ້ພຣະສູດ ພຣະທັມ ຄຳສອນຂອງພຣະພຸດທະເຈົ້າ ພ້ອມທັງປຶ້ມ, ວິດີໂອ ແລະ ປະຕິທິນກິດຈະກຳ', | |
| 'json_ld' => $jsonLd, | |
| ] | |
| ]); | |
| } | |
| public function category($category) { | |
| /* Cache a slim list per category: the list only renders 4 fields, so we | |
| never need to ship the full sutra text to the browser. */ | |
| $cacheFile = 'sutra_cat_' . md5($category) . '.json'; | |
| $filteredSutras = null; | |
| $cached = $this->readSutraCache($cacheFile); | |
| if ($cached !== null) { | |
| $decoded = json_decode($cached, true); | |
| if (is_array($decoded)) { | |
| $filteredSutras = $decoded; | |
| } | |
| } | |
| if ($filteredSutras === null) { | |
| $sutras = Sutra::getAll(); | |
| $filteredSutras = array_values(array_map(function($item) { | |
| return [ | |
| 'ID' => $item['ID'] ?? '', | |
| 'ຊື່ພຣະສູດ' => $item['ຊື່ພຣະສູດ'] ?? '', | |
| 'ສຽງ' => $item['ສຽງ'] ?? '', | |
| 'ໝວດທັມ' => $item['ໝວດທັມ'] ?? '', | |
| ]; | |
| }, array_filter($sutras, function($item) use ($category) { | |
| return ($item['ໝວດທັມ'] ?? '') === $category; | |
| }))); | |
| writeCache($cacheFile, json_encode($filteredSutras, JSON_UNESCAPED_UNICODE)); | |
| } | |
| $canonicalUrl = canonicalUrl(); | |
| $count = count($filteredSutras); | |
| $jsonLd = [ | |
| '@context' => 'https://schema.org', | |
| '@type' => 'CollectionPage', | |
| 'name' => $category . ' - ຄຳສອນພຸດທະ', | |
| 'description' => 'ພຣະສູດໃນໝວດ ' . $category . ' ຈຳນວນ ' . $count . ' ພຣະສູດ', | |
| 'url' => $canonicalUrl, | |
| 'inLanguage' => 'lo', | |
| 'about' => $category, | |
| ]; | |
| return view('pages.sutra.category', [ | |
| 'category' => $category, | |
| 'sutras' => $filteredSutras, | |
| 'seo' => [ | |
| 'title' => $category . ' - ຄຳສອນພຸດທະ', | |
| 'description' => 'ພຣະສູດໃນໝວດ ' . $category . ' ຈຳນວນ ' . $count . ' ພຣະສູດ', | |
| 'keywords' => $category . ', ພຣະສູດ, ຄຳສອນພຸດທະ, ທັມ', | |
| 'json_ld' => $jsonLd, | |
| ] | |
| ]); | |
| } | |
| public function show($id) { | |
| /* Cache one sutra by id so opening an article does not decode the whole store. */ | |
| $cacheFile = 'sutra_item_' . md5((string) $id) . '.json'; | |
| $sutra = null; | |
| $cached = $this->readSutraCache($cacheFile); | |
| if ($cached !== null) { | |
| $decoded = json_decode($cached, true); | |
| if (is_array($decoded) && !empty($decoded)) { | |
| $sutra = $decoded; | |
| } | |
| } | |
| if (!$sutra) { | |
| $sutras = Sutra::getAll(); | |
| foreach ($sutras as $item) { | |
| if ($item['ID'] == $id) { | |
| $sutra = $item; | |
| break; | |
| } | |
| } | |
| if (!$sutra) { | |
| $sutras = Sutra::getAll(true); | |
| foreach ($sutras as $item) { | |
| if ($item['ID'] == $id) { | |
| $sutra = $item; | |
| break; | |
| } | |
| } | |
| } | |
| if ($sutra) { | |
| writeCache($cacheFile, json_encode($sutra, JSON_UNESCAPED_UNICODE)); | |
| } | |
| } | |
| if (!$sutra) { | |
| http_response_code(404); | |
| return view('pages.sutra.show', [ | |
| 'sutra' => [ | |
| 'ID' => $id, | |
| 'ຊື່ພຣະສູດ' => 'ບໍ່ພົບພຣະສູດ', | |
| 'ພຣະສູດ' => 'ຂໍອະໄພ, ບໍ່ພົບພຣະສູດທີ່ທ່ານກຳລັງຊອກຫາ.', | |
| 'ໝວດທັມ' => '', | |
| 'ສຽງ' => '', | |
| 'ຮູບ' => '' | |
| ], | |
| 'prevID' => null, | |
| 'nextID' => null, | |
| 'seo' => [ | |
| 'title' => 'ບໍ່ພົບໜ້ານີ້ - ຄຳສອນພຸດທະ', | |
| 'description' => 'ຂໍອະໄພ, ບໍ່ພົບໜ້າທີ່ທ່ານກຳລັງຊອກຫາ.', | |
| 'robots' => 'noindex, follow', | |
| ] | |
| ]); | |
| } | |
| $prevID = null; | |
| $nextID = null; | |
| if (!empty($sutra['ໝວດທັມ'])) { | |
| $category = $sutra['ໝວດທັມ']; | |
| $categorySutras = null; | |
| $catCached = $this->readSutraCache('sutra_cat_' . md5($category) . '.json'); | |
| if ($catCached !== null) { | |
| $decoded = json_decode($catCached, true); | |
| if (is_array($decoded)) { | |
| $categorySutras = $decoded; | |
| } | |
| } | |
| if ($categorySutras === null) { | |
| $allSutras = Sutra::getAll(); | |
| $categorySutras = array_values(array_map(function($item) { | |
| return [ | |
| 'ID' => $item['ID'] ?? '', | |
| 'ຊື່ພຣະສູດ' => $item['ຊື່ພຣະສູດ'] ?? '', | |
| 'ສຽງ' => $item['ສຽງ'] ?? '', | |
| 'ໝວດທັມ' => $item['ໝວດທັມ'] ?? '', | |
| ]; | |
| }, array_filter($allSutras, function($item) use ($category) { | |
| return ($item['ໝວດທັມ'] ?? '') === $category; | |
| }))); | |
| if (!empty($categorySutras)) { | |
| writeCache('sutra_cat_' . md5($category) . '.json', json_encode($categorySutras, JSON_UNESCAPED_UNICODE)); | |
| } | |
| } | |
| foreach ($categorySutras as $index => $item) { | |
| if ($item['ID'] == $id) { | |
| $prevID = $categorySutras[$index - 1]['ID'] ?? null; | |
| $nextID = $categorySutras[$index + 1]['ID'] ?? null; | |
| break; | |
| } | |
| } | |
| } | |
| $description = strip_tags($sutra['ພຣະສູດ'] ?? ''); | |
| $description = mb_strlen($description) > 160 ? mb_substr($description, 0, strrpos(mb_substr($description, 0, 160), ' ') ?: 157) . '...' : $description; | |
| $canonicalUrl = canonicalUrl(); | |
| $sutraTitle = $sutra['ຊື່ພຣະສູດ'] ?? 'ພຣະສູດ'; | |
| $category = $sutra['ໝວດທັມ'] ?? ''; | |
| $jsonLd = [ | |
| '@context' => 'https://schema.org', | |
| '@type' => 'Article', | |
| 'headline' => $sutraTitle, | |
| 'description' => $description, | |
| 'url' => $canonicalUrl, | |
| 'inLanguage' => 'lo', | |
| 'isPartOf' => [ | |
| '@type' => 'Collection', | |
| 'name' => 'ຄຳສອນພຸດທະ', | |
| ], | |
| 'breadcrumb' => [ | |
| '@type' => 'BreadcrumbList', | |
| 'itemListElement' => [ | |
| ['@type' => 'ListItem', 'position' => 1, 'name' => 'ໜ້າຫຼັກ', 'item' => absoluteUrl('')], | |
| ['@type' => 'ListItem', 'position' => 2, 'name' => 'ພຣະສູດ', 'item' => absoluteUrl('/sutra')], | |
| ['@type' => 'ListItem', 'position' => 3, 'name' => $sutraTitle], | |
| ], | |
| ], | |
| ]; | |
| if ($category) { | |
| $jsonLd['about'] = $category; | |
| $jsonLd['keywords'] = $category . ', ພຣະສູດ, ຄຳສອນພຸດທະ, ທັມ'; | |
| $jsonLd['articleSection'] = $category; | |
| } else { | |
| $jsonLd['keywords'] = 'ພຣະສູດ, ຄຳສອນພຸດທະ, ທັມ'; | |
| } | |
| if (!empty($sutra['ຮູບ'])) { | |
| $jsonLd['image'] = $sutra['ຮູບ']; | |
| } | |
| $jsonLd['datePublished'] = $sutra['datePublished'] ?? date('Y-m-d'); | |
| $jsonLd['author'] = [ | |
| '@type' => 'Organization', | |
| 'name' => 'ຄຳສອນພຸດທະ', | |
| ]; | |
| $publishedTime = $sutra['datePublished'] ?? date('Y-m-d'); | |
| return view('pages.sutra.show', [ | |
| 'sutra' => $sutra, | |
| 'prevID' => $prevID, | |
| 'nextID' => $nextID, | |
| 'seo' => [ | |
| 'title' => $sutraTitle . ' - ຄຳສອນພຸດທະ', | |
| 'description' => $description, | |
| 'keywords' => ($category ? $category . ', ' : '') . 'ພຣະສູດ, ຄຳສອນພຸດທະ, ທັມ', | |
| 'image' => !empty($sutra['ຮູບ']) ? $sutra['ຮູບ'] : absoluteUrl('assets/images/logo_shared.png'), | |
| 'json_ld' => $jsonLd, | |
| 'og_type' => 'article', | |
| 'article_published_time' => $publishedTime, | |
| ] | |
| ]); | |
| } | |
| } | |