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