diff --git a/code/app/home/config/router.php b/code/app/home/config/router.php index 3900d5d..6d660cd 100644 --- a/code/app/home/config/router.php +++ b/code/app/home/config/router.php @@ -24,28 +24,32 @@ Route::get('/robots', function (\think\Request $Request, SiteContext $SiteContex * Sitemap 索引文件 */ Route::get('/sitemap_index', function (\think\Request $Request, SiteContext $SiteContext) { - return view('sitemap/sitemap_index.xml'); + return $SiteContext->getSiteMapByCode('INDEX'); + // return view('sitemap/sitemap_index.xml'); })->ext('xml'); /** * 首页、分类列表、排行榜、男生/女生频道。 */ Route::get('/sitemap-main', function (\think\Request $Request, SiteContext $SiteContext) { - return view('sitemap/sitemap-main.xml'); + return $SiteContext->getSiteMapByCode('MAIN'); + // return view('sitemap/sitemap-main.xml'); })->ext('xml'); /** * 小说目录页面。 */ Route::get('/sitemap-books-catalog-:page', function (\think\Request $Request, SiteContext $SiteContext) { - return view('sitemap/sitemap-books-catalog.xml'); + return $SiteContext->getSiteMapByCode('CATALOG'); + // return view('sitemap/sitemap-books-catalog.xml'); })->ext('xml'); /** * 小说详情页面。 */ Route::get('/sitemap-books-:page', function (\think\Request $Request, SiteContext $SiteContext) { - return view('sitemap/sitemap-books.xml'); + return $SiteContext->getSiteMapByCode('BOOK'); + // return view('sitemap/sitemap-books.xml'); })->ext('xml'); /** @@ -59,7 +63,8 @@ Route::get('/sitemap-forget-books-:page', function (\think\Request $Request, Sit * 章节页面(如果包含) */ Route::get('/sitemap-chapters-:page', function (\think\Request $Request, SiteContext $SiteContext) { - return view('sitemap/sitemap-chapters.xml'); + return $SiteContext->getSiteMapByCode('CHAPTER'); + // return view('sitemap/sitemap-chapters.xml'); })->ext('xml'); diff --git a/code/app/model/DomainModel.php b/code/app/model/DomainModel.php index 0e6f32b..42bd649 100644 --- a/code/app/model/DomainModel.php +++ b/code/app/model/DomainModel.php @@ -89,12 +89,17 @@ class DomainModel extends BaseModel /** - * Undocumented function + * GetFomartSubjectEx * * @param string $strKey + * @param string $strDomain + * @param string $strController + * @param string $strAction + * @param string $strDiff + * @param boolean $boolJoin * @return string */ - public function getFomartSubject($strKey, $strDiff = '', bool $boolJoin = true) + public function getFomartSubjectEx(string $strKey, string $strDomain, string $strController, string $strAction, $strDiff = '', bool $boolJoin = true): string { $intSfgId = $this->t_cfg->{$strKey}['sfg_id']; $SubjectFomartModel = NULL; @@ -104,10 +109,6 @@ class DomainModel extends BaseModel return ''; } - $strDomain = request()->DomainModel->d_domain; - $strController = request()->controller(); - $strAction = request()->action(); - if ($boolJoin) { $strUUId = sprintf('%s-%s-%s-%s-%s', $strDomain, $strController, $strAction, $strKey, $strDiff); } else { @@ -126,8 +127,6 @@ class DomainModel extends BaseModel 'csf_key' => $strUUId, 'sf_id' => $SubjectFomartModel->sf_id, ]); - - } else { $intSfId = $arrConfigSubjectFomart['sf_id']; @@ -137,12 +136,65 @@ class DomainModel extends BaseModel break; } } - } return $SubjectFomartModel->sf_val ?? ''; - } + /** + * Undocumented function + * + * @param string $strKey + * @return string + */ + public function getFomartSubject($strKey, $strDiff = '', bool $boolJoin = true) + { + $strDomain = request()->DomainModel->d_domain; + $strController = request()->controller(); + $strAction = request()->action(); + return $this->getFomartSubjectEx($strKey, $strDomain, $strController, $strAction, $strDiff, $boolJoin); + } + + /** + * GetFomartUrl + * + * @param string $strKey + * @param array $arrArgs + * @return string + */ + public function getFomartUrl(string $strKey, array $arrArgs): string + { + $strSfVal = $this->getFomartSubject($strKey, '', false); + $arrKeys = []; + $arrVals = []; + foreach ($arrArgs as $strKey => $strVal) { + $arrKeys[] = '{' . $strKey . '}'; + $arrVals[] = $strVal; + } + + return str_replace($arrKeys, $arrVals, $strSfVal); + } + + /** + * GetFomartUrl + * + * @param string $strKey + * @param array $arrArgs + * @return string + */ + public function getFomartUrlEx(string $strKey, string $strDomain, string $strController, string $strAction, array $arrArgs): string + { + $strSfVal = $this->getFomartSubjectEx($strKey, $strDomain, $strController, $strAction, '', false); + $arrKeys = []; + $arrVals = []; + foreach ($arrArgs as $strKey => $strVal) { + $arrKeys[] = '{' . $strKey . '}'; + $arrVals[] = $strVal; + } + + $strUri = str_replace($arrKeys, $arrVals, $strSfVal); + $strUri = str_replace('//', '/', $strUri); + return $strUri; + } } diff --git a/code/app/services/ChapterService.php b/code/app/services/ChapterService.php index 82f3e51..b104d07 100644 --- a/code/app/services/ChapterService.php +++ b/code/app/services/ChapterService.php @@ -217,14 +217,14 @@ class ChapterService * @param integer $intCPage * @return void */ - public function getChapterInfo(int $intNId, int $intCSortNum = 0, int $intCPage = 0) + public function getChapterInfo(int $intNId, int|NULL $intCSortNum = 0, int|NULL $intCPage = 0) { // 小说详情 $arrNovel = $this->NovelModel->getNovelByNId($intNId); $ChapterModel = ChapterModel::getInstance(); - if ($intCSortNum == 0 && $intCPage == 0) { + if ($intCSortNum === NULL && $intCPage === NULL) { // 最新章节 $arrChapter = $ChapterModel->getLatestChapterByNId($intNId); } else { diff --git a/code/app/services/NovelService.php b/code/app/services/NovelService.php index 0fde9c0..d121546 100644 --- a/code/app/services/NovelService.php +++ b/code/app/services/NovelService.php @@ -48,29 +48,6 @@ class NovelService $this->NovelClicksModel = NovelClicksModel::getInstance(); } - /** - * Undocumented function - * - * @param array $arrArgs - * @return string - */ - public function getPageUrl($strKey, array $arrArgs,): string - { - $strSfVal = $this->SiteContext->DomainModel->getFomartSubject($strKey, '', false); - $strSfVal = htmlspecialchars_decode($strSfVal); - $arrKeys = []; - $arrVals = []; - foreach ($arrArgs as $strKey => $strVal) { - $arrKeys[] = '{' . $strKey . '}'; - $arrVals[] = $strVal; - } - // var_dump($strSfVal); - // var_dump($arrKeys); - // var_dump($arrVals); - - return str_replace($arrKeys, $arrVals, $strSfVal); - } - /** * Undocumented function @@ -82,7 +59,7 @@ class NovelService public function getNovelInfoUrl($intNId, $intPinYin): string { $strKey = 'NOVEL_INFO_URL'; - $strUrl = $this->getPageUrl($strKey, [ + $strUrl = $this->SiteContext->DomainModel->getFomartUrl($strKey, [ 'intNId' => $intNId, 'strPinYin' => $intPinYin ]); @@ -99,7 +76,7 @@ class NovelService public function getForgeNovelInfoUrl($intNId, $intPinYin, $intNForgeId): string { $strKey = 'KAN_NOVEL_INFO_URL'; - $strUrl = $this->getPageUrl($strKey, [ + $strUrl = $this->SiteContext->DomainModel->getFomartUrl($strKey, [ 'intNId' => $intNId, 'strPinYin' => $intPinYin, 'intNForgeId' => $intNForgeId, @@ -112,15 +89,15 @@ class NovelService * Undocumented function * * @param int $intNId - * @param int $intPinYin + * @param string $intPinYin * @param int $intChapterSort * @param integer $intChapterPage * @return string */ - public function getNovelChapterUrl(int $intNId, $intPinYin, $intChapterSort, $intChapterPage = 0): string + public function getNovelChapterUrl(int $intNId, string $intPinYin, int $intChapterSort, int $intChapterPage = 0): string { $strKey = 'CONTENT_INFO_URL'; - $strUrl = $this->getPageUrl($strKey, [ + $strUrl = $this->SiteContext->DomainModel->getFomartUrl($strKey, [ 'intNId' => $intNId, 'strPinYin' => $intPinYin, 'intChapterSort' => $intChapterSort, @@ -140,28 +117,28 @@ class NovelService public function getNovelLasterChapterUrl($intNId, $intPinYin): string { $strKey = 'NEWS_CONTENT_INFO_URL'; - $strUrl = $this->getPageUrl($strKey, [ + $strUrl = $this->SiteContext->DomainModel->getFomartUrl($strKey, [ 'intNId' => $intNId, 'strPinYin' => $intPinYin, ]); return (string)$strUrl; } + /** - * 小说分类/书库 - * Undocumented function + * 获取小说分类URl * - * @param [type] $strGender - * @param [type] $strCategory - * @param [type] $strStatus - * @param [type] $strOrder - * @param [type] $intPage + * @param string $strGender + * @param string $strCategory + * @param string $strStatus + * @param string $strOrder + * @param integer $intPage * @return string */ - public function getNovelCategoryUrl($strGender, $strCategory, $strStatus, $strOrder, $intPage): string + public function getNovelCategoryUrl(string $strGender, string $strCategory, string $strStatus, string $strOrder, int|string $intPage): string { $strKey = 'CATEGORY_INFO_URL'; - $strUrl = $this->getPageUrl($strKey, [ + $strUrl = $this->SiteContext->DomainModel->getFomartUrl($strKey, [ 'strGender' => !empty($strGender) ? $strGender : '', 'strCategory' => !empty($strCategory) ? $strCategory : 'all', 'strStatus' => !empty($strStatus) ? $strStatus : 'all', @@ -176,20 +153,19 @@ class NovelService /** * 排行榜url - * Undocumented function * - * @param [type] $strGender - * @param [type] $strCategory - * @param [type] $strStatus - * @param [type] $strOrder - * @param [type] $intPage + * @param string $strGender + * @param string $strCategory + * @param string $strStatus + * @param string $strOrder + * @param integer $intPage * @return string */ - public function getNovelRankUrl($strGender, $strCategory, $strStatus, $strOrder, $intPage): string + public function getNovelRankUrl(string $strGender, string $strCategory, string $strStatus, string $strOrder, int $intPage): string { $strKey = 'RANK_LIST_URL'; - $strUrl = $this->getPageUrl($strKey, [ + $strUrl = $this->SiteContext->DomainModel->getFomartUrl($strKey, [ 'strGender' => !empty($strGender) ? $strGender : '', 'strCategory' => !empty($strCategory) ? $strCategory : 'all', 'strStatus' => !empty($strStatus) ? $strStatus : 'all', @@ -213,7 +189,7 @@ class NovelService public function getNovelCatalogUrl($intNId, $intPinYin, $strOrder, $intPage = 1): string { $strKey = 'NOVEL_CATALOG_URL'; - $strUrl = $this->getPageUrl($strKey, [ + $strUrl = $this->SiteContext->DomainModel->getFomartUrl($strKey, [ 'intNId' => $intNId, 'strPinYin' => $intPinYin, 'strOrder' => $strOrder, @@ -233,31 +209,13 @@ class NovelService public function getNovelSearchUrl($strKeyWords, $intPage = 1): string { $strKey = 'SEARCH_INFO_URL'; - $strUrl = $this->getPageUrl($strKey, [ + $strUrl = $this->SiteContext->DomainModel->getFomartUrl($strKey, [ 'intPage' => $intPage, 'strSearchKeywords' => $strKeyWords ]); return (string)$strUrl; } - - - /** - * GetCategoryFilter - * - * @return array - */ - public function getCategoryFilter(string $strNSex = ''): array - { - if ($strNSex == 'man') { - return CategoryModel::$arrCategoryAll['man']; - } else if ($strNSex == 'women') { - return CategoryModel::$arrCategoryAll['women']; - } else { - return array_combine(CategoryModel::$arrCategoryPinYin, CategoryModel::$arrCategory); - } - } - /** * 获取排行榜小说 * @@ -659,7 +617,7 @@ class NovelService $intLimit = $arrNovel['limit'] ?? 10; $intTotal = $arrNovel['total'] ?? 0; $intButtomNum = $arrParams['button_num']; - $strBaseUrl = app(NovelService::class)->getNovelCategoryUrl($strGender, $strCategory, $strStatus, $strOrder, '{page}'); + $strBaseUrl = $this->getNovelCategoryUrl($strGender, $strCategory, $strStatus, $strOrder, '{page}'); $arrPaginator = CusteomPage02::generate($intPage, $intTotal, $intLimit, $strBaseUrl, $intButtomNum); diff --git a/code/app/services/SiteContext.php b/code/app/services/SiteContext.php index 9162e48..533cfd5 100644 --- a/code/app/services/SiteContext.php +++ b/code/app/services/SiteContext.php @@ -14,6 +14,7 @@ use think\facade\Request; use think\facade\Config; use think\exception\HttpException; use think\facade\View; +use think\Response; class SiteContext { @@ -58,6 +59,7 @@ class SiteContext * @var NovelClicksModel */ public $NovelClicksModel; + public function __construct() { $this->Request = request(); @@ -313,7 +315,7 @@ class SiteContext $strOrder = $this->Request->param('strOrder'); $strStatus = $this->Request->param('strStatus'); $strNovelOrder = ''; - if(!empty($strOrder) && $strOrder != 'all'){ + if (!empty($strOrder) && $strOrder != 'all') { $strNovelOrder = StaticConfig::$arrNovelRankSortType[$strOrder]; } ConverterMovel::setVal([ @@ -322,4 +324,54 @@ class SiteContext 'strNovelStatus' => empty($strStatus) ? "" : StaticConfig::$arrNovelStatus[$strStatus], ]); } + + public function getSiteMapByCode(string $strCode): Response + { + $strFile = root_path('storage/SiteMap') . $this->DomainModel->d_domain . '/'; + + $intPage = $this->Request->route('page', 1); + + switch ($strCode) { + case 'INDEX': + $strFile .= 'sitemap_index.xml'; + break; + case 'MAIN': + $strFile .= 'sitemap-main.xml'; + break; + case 'CATALOG': + $strFile .= "sitemap-books-catalog-{$intPage}.xml"; + break; + case 'BOOK': + $strFile .= "sitemap-books-{$intPage}.xml"; + break; + case 'CHAPTER': + $strFile .= "sitemap-chapters-{$intPage}.xml"; + break; + default: + throw new \think\exception\HttpException(404, 'Sitemap file not found'); + break; + } + + if (!file_exists($strFile)) { + throw new \think\exception\HttpException(404, 'Sitemap file not found'); + } + + $Stream = fopen($strFile, 'rb'); + if (!$Stream) { + http_response_code(500); + echo 'Failed to open file stream'; + exit; + } + + header('Content-Type: application/xml'); + header('Content-Length: ' . filesize($strFile)); + header('Content-Disposition: inline'); + + while (!feof($Stream)) { + echo fread($Stream, 8192); + flush(); + } + fclose($Stream); + exit; + } } diff --git a/code/app/services/StaticConfig.php b/code/app/services/StaticConfig.php index 39ad6b8..4b8befe 100644 --- a/code/app/services/StaticConfig.php +++ b/code/app/services/StaticConfig.php @@ -2,6 +2,7 @@ namespace app\services; +use app\model\CategoryModel; class StaticConfig { @@ -35,4 +36,20 @@ class StaticConfig 'zheng' => '正序', 'dao' => '倒叙', ]; + + /** + * GetCategoryFilter + * + * @return array + */ + static function getCategoryFilter(string $strNSex = ''): array + { + if ($strNSex == 'man') { + return CategoryModel::$arrCategoryAll['man']; + } else if ($strNSex == 'women') { + return CategoryModel::$arrCategoryAll['women']; + } else { + return array_combine(CategoryModel::$arrCategoryPinYin, CategoryModel::$arrCategory); + } + } } diff --git a/code/app/task/logic/SiteMapLogic.php b/code/app/task/logic/SiteMapLogic.php new file mode 100644 index 0000000..2f5e264 --- /dev/null +++ b/code/app/task/logic/SiteMapLogic.php @@ -0,0 +1,369 @@ +arrNovelCategory = StaticConfig::getCategoryFilter(); + + $this->arrNovelSort = StaticConfig::$arrNovelSortType; + + $this->arrNovelStatus = StaticConfig::$arrNovelStatus; + + $this->strDate = date('Y-m-d'); + $this->strSiteMapPath = root_path('storage/SiteMap'); + + $this->intPageSize = 3; + + $this->NovelModel = NovelModel::getInstance(); + + NovelModel::$arrOptions['projection'] = [ + '_id' => 0, + 'n_id' => 1, + 'n_name' => 1, + 'n_name_pinyin' => 1, + 'n_category' => 1, + 'n_category_pinyin' => 1, + 'n_category_sex_en' => 1, + ]; + } + + /** + * Undocumented function + * + * @return DomainModel[] + */ + public function getDomain(): array + { + if (empty($this->arrDomain)) { + $this->arrDomainModel = DomainModel::getAllDomainOnCache(); + } + return $this->arrDomainModel; + } + + /** + * Generate Site Map + * + * @return void + */ + public function generateSiteMap() + { + $arrResult = $this->NovelModel->findPaginatedWithCache([], 1, 1); + $this->intTotal = $arrResult['total']; + + $this->generateMapIndex(); + + $this->generateMapMain(); + + $intTotalPage = ceil($this->intTotal / $this->intPageSize); + for ($intPage = 1; $intPage <= $intTotalPage; $intPage++) { + $this->generateMapInfo($intPage); + } + + echo date('Y-m-d H:i:s') . PHP_EOL; + } + + + protected function generateMapInfo(int $intPage) + { + + foreach ($this->getDomain() as $DomainModel) { + + $strDomainDir = $this->strSiteMapPath . $DomainModel->d_domain; + if (is_dir($strDomainDir) == false) { + mkdir($strDomainDir); + } + + $strHead = << + +EOF; + $strTemplate = << +%s +{$this->strDate} +weekly +%s + +EOF; + $strEnd = << +EOF; + + $strMapBooksFile = sprintf('%s/sitemap-books-catalog-%s.xml', $strDomainDir, $intPage); + $strMapCatalogFile = sprintf('%s/sitemap-books-%s.xml', $strDomainDir, $intPage); + $strMapChaptersFile = sprintf('%s/sitemap-chapters-%s.xml', $strDomainDir, $intPage); + + file_put_contents($strMapBooksFile, $strHead); + file_put_contents($strMapCatalogFile, $strHead); + file_put_contents($strMapChaptersFile, $strHead); + + # 取 intpage * limit ~ intpage * (limit+1) 写入以上文件 + foreach ($this->fetchNovelsByBatch($intPage * $this->intPageSize, $this->intPageSize) as $Novel) { + + # 1 + $strKey = "NOVEL_INFO_URL"; + $strDomain = $DomainModel->d_domain; + $strController = ""; + $strAction = ""; + $arrArgs = [ + 'intNId' => $Novel->n_id, + 'strPinYin' => $Novel->n_name_pinyin, + ]; + + $strUri = $DomainModel->getFomartUrlEx($strKey, $strDomain, $strController, $strAction, $arrArgs); + $strUrl = sprintf("https://www.%s%s", $DomainModel->d_domain, $strUri); + $strPriority = '0.8'; + $strContent = sprintf($strTemplate, $strUrl, $strPriority); + file_put_contents($strMapBooksFile, $strContent, FILE_APPEND); + + # 2 + $strKey = "NOVEL_CATALOG_URL"; + $strDomain = $DomainModel->d_domain; + $strController = ""; + $strAction = ""; + $arrArgs = [ + 'intNId' => $Novel->n_id, + 'strPinYin' => $Novel->n_name_pinyin, + 'strOrder' => 'zheng', + 'intPage' => 1, + ]; + + $strUri = $DomainModel->getFomartUrlEx($strKey, $strDomain, $strController, $strAction, $arrArgs); + $strUrl = sprintf("https://www.%s%s", $DomainModel->d_domain, $strUri); + $strPriority = '0.8'; + $strContent = sprintf($strTemplate, $strUrl, $strPriority); + file_put_contents($strMapCatalogFile, $strContent, FILE_APPEND); + + # 3 + $strKey = "NEWS_CONTENT_INFO_URL"; + $strDomain = $DomainModel->d_domain; + $strController = ""; + $strAction = ""; + $arrArgs = [ + 'intNId' => $Novel->n_id, + 'strPinYin' => $Novel->n_name_pinyin, + ]; + + $strUri = $DomainModel->getFomartUrlEx($strKey, $strDomain, $strController, $strAction, $arrArgs); + $strUrl = sprintf("https://www.%s%s", $DomainModel->d_domain, $strUri); + $strPriority = '0.6'; + $strContent = sprintf($strTemplate, $strUrl, $strPriority); + file_put_contents($strMapChaptersFile, $strContent, FILE_APPEND); + } + + file_put_contents($strMapBooksFile, $strEnd, FILE_APPEND); + file_put_contents($strMapCatalogFile, $strEnd, FILE_APPEND); + file_put_contents($strMapChaptersFile, $strEnd, FILE_APPEND); + } + } + + + /** + * 分批查询小说数据,并以生成器方式返回 + * + * @param int $intStart 跳过的记录数(skip) + * @param int $intCount 需要查询的总记录数 + * @return \Generator 逐条返回小说数据 + * @throws \MongoDB\Exception\Exception + */ + public function fetchNovelsByBatch(int $intStart, int $intCount): \Generator + { + $intRecordsFetched = 0; + $intRemainingRecords = min($intCount, $this->NovelModel->getCol()->countDocuments() - $intStart); + + while ($intRecordsFetched < $intRemainingRecords) { + $intLimit = min($this->intBatchSize, $intRemainingRecords - $intRecordsFetched); + $Cursor = $this->NovelModel->getCol()->find( + [ + 'n_have_chapter' => 1, + ], + [ + 'skip' => $intStart + $intRecordsFetched, + 'limit' => $intLimit, + 'sort' => ['_id' => 1], + 'projection' => [ + '_id' => 1, + 'n_id' => 1, + 'n_name' => 1, + 'n_name_pinyin' => 1, + 'n_category' => 1, + 'n_category_pinyin' => 1, + 'n_category_sex_en' => 1, + ], + ] + ); + + foreach ($Cursor as $novel) { + yield $novel; + } + + $intRecordsFetched += $intLimit; + unset($Cursor); + gc_collect_cycles(); + } + } + + + /** + * Generate Map Index + * + * @return void + */ + protected function generateMapMain() + { + + foreach ($this->getDomain() as $DomainModel) { + + $strDomainDir = $this->strSiteMapPath . $DomainModel->d_domain; + if (is_dir($strDomainDir) == false) { + mkdir($strDomainDir); + } + + $strMapIndexFile = $strDomainDir . '/sitemap-main.xml'; + + $strContent = << + + + https://www.{$DomainModel->d_domain}/ + {$this->strDate} + daily + 1.0 + +EOF; + file_put_contents($strMapIndexFile, $strContent); + + foreach ($this->arrNovelCategory as $strCategoryKey => $strCategoryVal) { + foreach ($this->arrNovelSort as $strSortKey => $strSortVal) { + foreach ($this->arrNovelStatus as $strStatusKey => $strStatusVal) { + + $strKey = "CATEGORY_INFO_URL"; + $strDomain = $DomainModel->d_domain; + $strController = ""; + $strAction = ""; + $arrArgs = [ + 'strCategory' => $strCategoryKey, + 'strOrder' => $strSortKey, + 'strStatus' => $strStatusKey, + 'strGender' => '', + 'intPage' => 1, + ]; + + $strUri = $DomainModel->getFomartUrlEx($strKey, $strDomain, $strController, $strAction, $arrArgs); + + $strUrl = sprintf("https://www.%s%s", $DomainModel->d_domain, $strUri); + + $strContent = << +{$strUrl} +{$this->strDate} +weekly +0.8 + +EOF; + file_put_contents($strMapIndexFile, $strContent, FILE_APPEND); + } + } + } + + $strContent = << +EOF; + file_put_contents($strMapIndexFile, $strContent, FILE_APPEND); + } + } + + /** + * Generate Map Index + * + * @return void + */ + protected function generateMapIndex() + { + $intTotalPage = ceil($this->intTotal / $this->intPageSize); + + foreach ($this->getDomain() as $DomainModel) { + + $strDomainDir = $this->strSiteMapPath . $DomainModel->d_domain; + if (is_dir($strDomainDir) == false) { + mkdir($strDomainDir); + } + + $strMapIndexFile = $strDomainDir . '/sitemap_index.xml'; + + $strContent = << + + +https://www.{$DomainModel->d_domain}/sitemap-main.xml +{$this->strDate} + +EOF; + file_put_contents($strMapIndexFile, $strContent); + + for ($intPage = 1; $intPage <= $intTotalPage; $intPage++) { + $strContent = << +https://www.{$DomainModel->d_domain}/sitemap-books-{$intPage}.xml +{$this->strDate} + + +https://www.{$DomainModel->d_domain}/sitemap-books-catalog-{$intPage}.xml +{$this->strDate} + + +https://www.{$DomainModel->d_domain}/sitemap-chapters-{$intPage}.xml +{$this->strDate} + +EOF; + file_put_contents($strMapIndexFile, $strContent, FILE_APPEND); + } + $strContent = << +EOF; + file_put_contents($strMapIndexFile, $strContent, FILE_APPEND); + } + } +} diff --git a/code/app/task/module/PlanTask.php b/code/app/task/module/PlanTask.php index 264f37c..bad83f7 100644 --- a/code/app/task/module/PlanTask.php +++ b/code/app/task/module/PlanTask.php @@ -8,7 +8,7 @@ use app\model\PlanTaskModel; use app\task\crawler\zw630\Scheduler as Zw630Scheduler; use app\task\crawler\zw74\Scheduler as Zw74Scheduler; use app\task\crawler\youzhi\Scheduler as YouZhiScheduler; - +use app\task\logic\SiteMapLogic; use microserver\QueueManage; use microserver\ProcessSanitizer; @@ -48,10 +48,31 @@ class PlanTask case 'PULL_YOU_ZHI_SHI_PIN': self::pullYouZhiShiPin(); break; - + + case 'GENERATE_SITE_MAP': + self::generateSiteMap(); + break; } } } + + /** + * Undocumented function + * + * @return void + */ + public static function generateSiteMap() + { + $arrTask = [ + 'callback' => [SiteMapLogic::class, 'generateSiteMap'], + 'data' => [] + ]; + + $QueueManage = new QueueManage(2); + $QueueManage->set($arrTask); + } + + /** * Undocumented function * diff --git a/code/database/seeders/PlanTaskSeeder.php b/code/database/seeders/PlanTaskSeeder.php index 666f9d8..47895cc 100644 --- a/code/database/seeders/PlanTaskSeeder.php +++ b/code/database/seeders/PlanTaskSeeder.php @@ -52,6 +52,13 @@ class PlanTaskSeeder 'pt_enable' => 0, 'pt_limit' => 1 * 24 * 3600, ], + + [ + 'pt_name' => '生成网站地图', + 'pt_code' => 'GENERATE_SITE_MAP', + 'pt_enable' => 0, + 'pt_limit' => 1 * 24 * 3600, + ], ]; foreach ($arrDataNovel as $arrPlanTask) { diff --git a/code/extend/template/taglib/Novel.php b/code/extend/template/taglib/Novel.php index 3db2203..359d610 100644 --- a/code/extend/template/taglib/Novel.php +++ b/code/extend/template/taglib/Novel.php @@ -414,7 +414,7 @@ EOT; $strParse = <<getCategoryFilter( {$n_sex}); +\$arrCategory = \\app\\services\\StaticConfig::getCategoryFilter( {$n_sex}); if (!empty(\$arrCategory) && is_array(\$arrCategory)): diff --git a/code/storage/SiteMap/.gitignore b/code/storage/SiteMap/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/code/storage/SiteMap/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file