M1: 数据模型 + sablog:import 脚本(MySQL 8 验证通过,不迁移 trackback 等过时功能)

This commit is contained in:
ak
2026-08-11 17:40:13 +08:00
parent 8d526a8cfd
commit f16bb75538
17 changed files with 1010 additions and 13 deletions
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Category extends Model
{
use HasFactory;
protected $fillable = [
'name',
'slug',
'display_order',
'post_count',
];
public function posts(): HasMany
{
return $this->hasMany(Post::class);
}
public function getUrlAttribute(): string
{
return route('category.show', $this->slug ?? $this->id);
}
}
+50
View File
@@ -0,0 +1,50 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Comment extends Model
{
use HasFactory;
public const STATUS_PENDING = 'pending';
public const STATUS_PUBLISHED = 'published';
public const STATUS_SPAM = 'spam';
public const STATUS_REJECTED = 'rejected';
protected $fillable = [
'post_id',
'user_id',
'author_name',
'author_email',
'author_url',
'content',
'ip',
'status',
'ai_review',
];
protected $casts = [
'ai_review' => 'array',
];
public $timestamps = false;
public function post(): BelongsTo
{
return $this->belongsTo(Post::class);
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function isVisible(): bool
{
return $this->status === self::STATUS_PUBLISHED;
}
}
+23
View File
@@ -0,0 +1,23 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Link extends Model
{
use HasFactory;
protected $fillable = [
'name',
'url',
'note',
'display_order',
'visible',
];
protected $casts = [
'visible' => 'boolean',
];
}
+24
View File
@@ -0,0 +1,24 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class PluginRecord extends Model
{
protected $fillable = [
'vendor',
'name',
'version',
'enabled',
];
protected $casts = [
'enabled' => 'boolean',
];
public function getKeyAttribute(): string
{
return $this->vendor.'/'.$this->name;
}
}
+111
View File
@@ -0,0 +1,111 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\Tags\HasTags;
class Post extends Model implements HasMedia
{
use HasFactory;
use InteractsWithMedia;
use HasTags;
protected $fillable = [
'category_id',
'user_id',
'title',
'slug',
'excerpt',
'content',
'keywords',
'status',
'is_sticky',
'close_comment',
'read_password',
'views',
'comment_count',
'meta',
'published_at',
];
protected $casts = [
'meta' => 'array',
'is_sticky' => 'boolean',
'close_comment' => 'boolean',
'published_at' => 'datetime',
];
public function category(): BelongsTo
{
return $this->belongsTo(Category::class);
}
public function author(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
public function comments(): HasMany
{
return $this->hasMany(Comment::class);
}
public function scopePublished(Builder $query): Builder
{
return $query->where('status', 'published')
->whereNotNull('published_at')
->where('published_at', '<=', now());
}
public function scopeSearch(Builder $query, ?string $keyword): Builder
{
if (! $keyword) {
return $query;
}
return $query->where(function (Builder $q) use ($keyword) {
$q->where('title', 'like', "%{$keyword}%")
->orWhere('content', 'like', "%{$keyword}%")
->orWhere('excerpt', 'like', "%{$keyword}%")
->orWhere('keywords', 'like', "%{$keyword}%");
});
}
public function registerMediaCollections(): void
{
$this->addMediaCollection('attachments')->useDisk(\App\Support\MediaDisk::name());
}
public function registerMediaConversions(?Media $media = null): void
{
$this->addMediaConversion('thumb')
->width(500)
->height(500)
->performOnCollections('attachments');
}
public function getUrlAttribute(): string
{
return route('posts.show', $this->slug ?? $this->id);
}
public function getExcerptOrFallbackAttribute(): string
{
if ($this->excerpt) {
return $this->excerpt;
}
$text = strip_tags($this->content);
$text = preg_replace('/\[[^\]]*\]/', '', $text);
return mb_substr($text, 0, 200);
}
}
+60
View File
@@ -0,0 +1,60 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
class Setting extends Model
{
protected $primaryKey = 'key';
public $incrementing = false;
protected $keyType = 'string';
protected $fillable = ['key', 'value'];
public const CACHE_KEY = 'blog.settings';
public static function get(string $key, mixed $default = null): mixed
{
$settings = self::allSettings();
return $settings[$key] ?? $default;
}
public static function set(string $key, mixed $value): void
{
self::updateOrCreate(['key' => $key], ['value' => is_scalar($value) || $value === null ? $value : json_encode($value)]);
self::flushCache();
}
public static function forget(string $key): void
{
self::where('key', $key)->delete();
self::flushCache();
}
public static function allSettings(): array
{
return Cache::remember(self::CACHE_KEY, now()->addDay(), function () {
return self::query()->pluck('value', 'key')->all();
});
}
public static function flushCache(): void
{
Cache::forget(self::CACHE_KEY);
}
public static function seedDefaults(array $defaults): void
{
foreach ($defaults as $key => $value) {
if (! self::query()->where('key', $key)->exists()) {
self::query()->create(['key' => $key, 'value' => $value]);
}
}
self::flushCache();
}
}
+50 -2
View File
@@ -2,16 +2,18 @@
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
/** @use HasFactory<UserFactory> */
use HasFactory, Notifiable;
use HasFactory, Notifiable, HasRoles;
/**
* The attributes that are mass assignable.
@@ -22,6 +24,13 @@ class User extends Authenticatable
'name',
'email',
'password',
'legacy_md5',
'url',
'logincount',
'loginip',
'regip',
'logintime',
'lastpost_at',
];
/**
@@ -32,6 +41,7 @@ class User extends Authenticatable
protected $hidden = [
'password',
'remember_token',
'legacy_md5',
];
/**
@@ -44,6 +54,44 @@ class User extends Authenticatable
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'logintime' => 'datetime',
'lastpost_at' => 'datetime',
];
}
public function posts(): HasMany
{
return $this->hasMany(Post::class);
}
public function comments(): HasMany
{
return $this->hasMany(Comment::class);
}
/**
* 验证登录:优先新密码哈希,兼容 sablog MD5 密码。
*/
public function verifyPassword(string $plain): bool
{
if ($this->password && Hash::check($plain, $this->password)) {
return true;
}
if ($this->legacy_md5 && md5($plain) === strtolower($this->legacy_md5)) {
// 登录成功后自动升级为现代哈希
$this->password = Hash::make($plain);
$this->legacy_md5 = null;
$this->save();
return true;
}
return false;
}
public function isAdmin(): bool
{
return $this->hasRole('admin');
}
}