Files
ahrommag/app/Http/Controllers/BlogController.php
2025-11-16 12:43:07 +03:30

79 lines
2.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Blog;
use Illuminate\Support\Facades\Log;
class BlogController extends Controller
{
public function fixContentIds()
{
$blogs = Blog::withoutGlobalScope('filtered')->get();
$updatedCount = 0;
foreach ($blogs as $blog) {
$oldContent = $blog->content;
$oldImage = $blog->image;
$contentChanged = false;
$imageChanged = false;
$patterns = [
'/https?:\/\/ahrom\.net\/mag\/images\//',
'/\/\/ahrom\.net\/mag\/images\//',
'/http:\/\/www\.ahrom\.net\/mag\/images\//',
'/https:\/\/www\.ahrom\.net\/mag\/images\//',
'/ahrom\.net\/mag\/images\//'
];
$newContent = preg_replace(
$patterns,
'https://ahromstorage.ir/ahrominvest/mag/images/',
$oldContent
);
$newContent = preg_replace_callback(
'/(https:\/\/ahromstorage\.ir\/ahrominvest\/mag\/images\/.*?)\.webp/i',
function($matches) {
return str_replace('.webp', '.jpg', $matches[0]);
},
$newContent
);
$newImage = $oldImage;
if (!empty($oldImage) && preg_match('/\.webp$/i', $oldImage)) {
$newImage = preg_replace('/\.webp$/i', '.jpg', $oldImage);
$imageChanged = true;
}
if ($newContent !== $oldContent) {
$contentChanged = true;
}
if ($contentChanged || $imageChanged) {
$blog->content = $newContent;
if ($imageChanged) {
$blog->image = $newImage;
}
$blog->save();
$updatedCount++;
$changes = [];
if ($contentChanged) $changes[] = 'content';
if ($imageChanged) $changes[] = 'image';
Log::info("Updated " . implode(' and ', $changes) . " in blog ID: {$blog->id}");
}
}
return response()->json([
'status' => 'completed',
'updated_count' => $updatedCount,
'total_blogs' => count($blogs),
'message' => $updatedCount > 0
? "Successfully updated {$updatedCount} blogs (content and/or image fields)"
: 'No matching URLs or webp images found to replace'
]);
}
}