3 namespace BookStack\Entities\Models;
5 use BookStack\Activity\Models\Loggable;
6 use BookStack\App\Model;
7 use BookStack\Users\Models\User;
9 use Illuminate\Database\Eloquent\Relations\BelongsTo;
15 * @property int $page_id
16 * @property string $name
17 * @property string $slug
18 * @property string $book_slug
19 * @property int $created_by
20 * @property Carbon $created_at
21 * @property Carbon $updated_at
22 * @property string $type
23 * @property string $summary
24 * @property string $markdown
25 * @property string $html
26 * @property string $text
27 * @property int $revision_number
28 * @property Page $page
29 * @property-read ?User $createdBy
31 class PageRevision extends Model implements Loggable
33 protected $fillable = ['name', 'text', 'summary'];
34 protected $hidden = ['html', 'markdown', 'text'];
37 * Get the user that created the page revision.
39 public function createdBy(): BelongsTo
41 return $this->belongsTo(User::class, 'created_by');
45 * Get the page this revision originates from.
47 public function page(): BelongsTo
49 return $this->belongsTo(Page::class);
53 * Get the url for this revision.
55 public function getUrl(string $path = ''): string
57 return $this->page->getUrl('/revisions/' . $this->id . '/' . ltrim($path, '/'));
61 * Get the previous revision for the same page if existing.
63 public function getPrevious(): ?PageRevision
65 $id = static::newQuery()->where('page_id', '=', $this->page_id)
66 ->where('id', '<', $this->id)
70 return static::query()->find($id);
77 * Allows checking of the exact class, Used to check entity type.
78 * Included here to align with entities in similar use cases.
79 * (Yup, Bit of an awkward hack).
81 * @deprecated Use instanceof instead.
83 public static function isA(string $type): bool
85 return $type === 'revision';
88 public function logDescriptor(): string
90 return "Revision #{$this->revision_number} (ID: {$this->id}) for page ID {$this->page_id}";