]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/PageRevision.php
Tiny header
[bookstack] / app / Entities / Models / PageRevision.php
1 <?php
2
3 namespace BookStack\Entities\Models;
4
5 use BookStack\Auth\User;
6 use BookStack\Model;
7 use Carbon\Carbon;
8 use Illuminate\Database\Eloquent\Relations\BelongsTo;
9
10 /**
11  * Class PageRevision.
12  *
13  * @property mixed  $id
14  * @property int    $page_id
15  * @property string $slug
16  * @property string $book_slug
17  * @property int    $created_by
18  * @property Carbon $created_at
19  * @property Carbon $updated_at
20  * @property string $type
21  * @property string $summary
22  * @property string $markdown
23  * @property string $html
24  * @property int    $revision_number
25  * @property Page   $page
26  * @property-read ?User $createdBy
27  */
28 class PageRevision extends Model
29 {
30     protected $fillable = ['name', 'html', 'text', 'markdown', 'summary'];
31     protected $hidden = ['html', 'markdown', 'restricted', 'text'];
32
33     /**
34      * Get the user that created the page revision.
35      */
36     public function createdBy(): BelongsTo
37     {
38         return $this->belongsTo(User::class, 'created_by');
39     }
40
41     /**
42      * Get the page this revision originates from.
43      */
44     public function page(): BelongsTo
45     {
46         return $this->belongsTo(Page::class);
47     }
48
49     /**
50      * Get the url for this revision.
51      */
52     public function getUrl(string $path = ''): string
53     {
54         return $this->page->getUrl('/revisions/' . $this->id . '/' . ltrim($path, '/'));
55     }
56
57     /**
58      * Get the previous revision for the same page if existing.
59      */
60     public function getPrevious(): ?PageRevision
61     {
62         $id = static::newQuery()->where('page_id', '=', $this->page_id)
63             ->where('id', '<', $this->id)
64             ->max('id');
65
66         if ($id) {
67             return static::query()->find($id);
68         }
69
70         return null;
71     }
72
73     /**
74      * Allows checking of the exact class, Used to check entity type.
75      * Included here to align with entities in similar use cases.
76      * (Yup, Bit of an awkward hack).
77      *
78      * @deprecated Use instanceof instead.
79      */
80     public static function isA(string $type): bool
81     {
82         return $type === 'revision';
83     }
84 }