This commit is contained in:
ct
2025-07-17 03:42:15 +08:00
parent 62cefe271e
commit 65dc6cec81
8 changed files with 553 additions and 1 deletions

View File

@@ -0,0 +1,63 @@
<?php
namespace App\Console\Commands;
use App\Services\MemeMediaService;
use Illuminate\Console\Command;
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;
class GenerateMemesSitemap extends Command
{
protected $signature = 'sitemap:generate:memes';
protected $description = 'Generate sitemap for individual meme pages (memes.show)';
public function __construct(
private MemeMediaService $memeMediaService
) {
parent::__construct();
}
public function handle(): int
{
$this->info('Starting memes sitemap generation...');
$sitemap = Sitemap::create();
// Get the base URL from config
$baseUrl = config('app.url');
// Get all enabled memes
$memes = $this->memeMediaService->getAllEnabledMemes();
$this->info("Found {$memes->count()} enabled memes");
// Add a progress bar for large datasets
$progressBar = $this->output->createProgressBar($memes->count());
$progressBar->start();
// Add each meme to the sitemap
foreach ($memes as $meme) {
$url = Url::create($baseUrl . '/meme/' . $meme->slug)
->setPriority(0.8)
->setChangeFrequency(Url::CHANGE_FREQUENCY_MONTHLY)
->setLastModificationDate($meme->updated_at);
$sitemap->add($url);
$progressBar->advance();
}
$progressBar->finish();
$this->newLine();
// Save the sitemap
$sitemapPath = public_path('sitemap_memes.xml');
$sitemap->writeToFile($sitemapPath);
$this->info("Memes sitemap generated successfully!");
$this->info("Sitemap saved to: {$sitemapPath}");
$this->info("Total URLs: {$memes->count()}");
return Command::SUCCESS;
}
}

View File

@@ -0,0 +1,70 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Spatie\Sitemap\SitemapIndex;
class GenerateSitemap extends Command
{
protected $signature = 'sitemap:generate';
protected $description = 'Generate main sitemap index that links to all sub-sitemaps';
public function handle(): int
{
$this->info('Starting main sitemap generation...');
$sitemapIndex = SitemapIndex::create();
// Get the base URL from config
$baseUrl = config('app.url');
// List of sub-sitemaps to include in the main sitemap
$subSitemaps = [
[
'url' => $baseUrl . '/sitemap_static.xml',
'lastModified' => $this->getSitemapLastModified('sitemap_static.xml'),
],
[
'url' => $baseUrl . '/sitemap_memes.xml',
'lastModified' => $this->getSitemapLastModified('sitemap_memes.xml'),
],
// Future sitemaps can be added here:
// [
// 'url' => $baseUrl . '/sitemap_pages.xml',
// 'lastModified' => now(),
// ],
];
// Add each sub-sitemap to the index
foreach ($subSitemaps as $sitemap) {
$sitemapIndex->add($sitemap['url'], $sitemap['lastModified']);
$this->info("Added sub-sitemap: {$sitemap['url']}");
}
// Save the main sitemap index
$sitemapPath = public_path('sitemap.xml');
$sitemapIndex->writeToFile($sitemapPath);
$this->info("Main sitemap index generated successfully!");
$this->info("Sitemap saved to: {$sitemapPath}");
$this->info("Total sub-sitemaps: " . count($subSitemaps));
return Command::SUCCESS;
}
/**
* Get the last modification date of a sitemap file
*/
private function getSitemapLastModified(string $filename): \DateTime
{
$filepath = public_path($filename);
if (file_exists($filepath)) {
return new \DateTime('@' . filemtime($filepath));
}
// If file doesn't exist, return current time
return new \DateTime();
}
}

View File

@@ -0,0 +1,79 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;
class GenerateStaticSitemap extends Command
{
protected $signature = 'sitemap:generate:static';
protected $description = 'Generate sitemap for static pages (home, privacy, terms, dmca, meme library)';
public function handle(): int
{
$this->info('Starting static sitemap generation...');
$sitemap = Sitemap::create();
// Get the base URL from config
$baseUrl = config('app.url');
// Add static pages
$staticPages = [
[
'url' => $baseUrl,
'priority' => 1.0,
'changeFrequency' => Url::CHANGE_FREQUENCY_WEEKLY,
'lastModificationDate' => now(),
],
[
'url' => $baseUrl . '/meme-library',
'priority' => 0.9,
'changeFrequency' => Url::CHANGE_FREQUENCY_DAILY,
'lastModificationDate' => now(),
],
[
'url' => $baseUrl . '/privacy',
'priority' => 0.3,
'changeFrequency' => Url::CHANGE_FREQUENCY_YEARLY,
'lastModificationDate' => now()->subMonths(6), // Adjust based on when you last updated
],
[
'url' => $baseUrl . '/terms',
'priority' => 0.3,
'changeFrequency' => Url::CHANGE_FREQUENCY_YEARLY,
'lastModificationDate' => now()->subMonths(6), // Adjust based on when you last updated
],
[
'url' => $baseUrl . '/dmca-copyright',
'priority' => 0.2,
'changeFrequency' => Url::CHANGE_FREQUENCY_YEARLY,
'lastModificationDate' => now()->subMonths(6), // Adjust based on when you last updated
],
];
// Add each static page to the sitemap
foreach ($staticPages as $page) {
$url = Url::create($page['url'])
->setPriority($page['priority'])
->setChangeFrequency($page['changeFrequency'])
->setLastModificationDate($page['lastModificationDate']);
$sitemap->add($url);
$this->info("Added: {$page['url']}");
}
// Save the sitemap
$sitemapPath = public_path('sitemap_static.xml');
$sitemap->writeToFile($sitemapPath);
$this->info("Static sitemap generated successfully!");
$this->info("Sitemap saved to: {$sitemapPath}");
$this->info("Total URLs: " . count($staticPages));
return Command::SUCCESS;
}
}

View File

@@ -150,6 +150,16 @@ private function buildSearchQuery(?string $search = null): Builder
return $query;
}
/**
* Get all enabled memes for sitemap generation
*/
public function getAllEnabledMemes(): Collection
{
return MemeMedia::where('is_enabled', true)
->orderBy('updated_at', 'desc')
->get();
}
/**
* Get base query for enabled memes
*/