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