This commit is contained in:
ct
2025-07-17 03:42:55 +08:00
parent 06675de71b
commit 85917e0cdf
46 changed files with 2426 additions and 121 deletions

View File

@@ -0,0 +1,64 @@
<?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,71 @@
<?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,80 @@
<?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

@@ -0,0 +1,51 @@
<?php
namespace App\Console\Commands;
use App\Models\MemeMedia;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
class PopulateMemeMediaSlugs extends Command
{
protected $signature = 'memes:populate-slugs';
protected $description = 'Populate slug field for existing MemeMedia records';
public function handle()
{
$this->info('Starting to populate MemeMedia slugs...');
$memes = MemeMedia::whereNull('slug')->get();
if ($memes->isEmpty()) {
$this->info('No memes found without slugs.');
return;
}
$this->info("Found {$memes->count()} memes without slugs.");
$bar = $this->output->createProgressBar($memes->count());
$bar->start();
foreach ($memes as $meme) {
$baseSlug = Str::slug($meme->name);
$slug = $baseSlug;
$counter = 1;
// Ensure slug is unique
while (MemeMedia::where('slug', $slug)->exists()) {
$slug = $baseSlug.'-'.$counter;
$counter++;
}
$meme->update(['slug' => $slug]);
$bar->advance();
}
$bar->finish();
$this->newLine();
$this->info('Successfully populated all MemeMedia slugs!');
}
}