]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/PageRevision.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / app / Entities / Models / PageRevision.php
1 <?php
2
3 namespace BookStack\Entities\Models;
4
5 use BookStack\Activity\Models\Loggable;
6 use BookStack\App\Model;
7 use BookStack\Users\Models\User;
8 use Carbon\Carbon;
9 use Illuminate\Database\Eloquent\Relations\BelongsTo;
10
11 /**
12  * Class PageRevision.
13  *
14  * @property mixed  $id
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
30  */
31 class PageRevision extends Model implements Loggable
32 {
33     protected $fillable = ['name', 'text', 'summary'];
34     protected $hidden = ['html', 'markdown', 'text'];
35
36     /**
37      * Get the user that created the page revision.
38      */
39     public function createdBy(): BelongsTo
40     {
41         return $this->belongsTo(User::class, 'created_by');
42     }
43
44     /**
45      * Get the page this revision originates from.
46      */
47     public function page(): BelongsTo
48     {
49         return $this->belongsTo(Page::class);
50     }
51
52     /**
53      * Get the url for this revision.
54      */
55     public function getUrl(string $path = ''): string
56     {
57         return $this->page->getUrl('/revisions/' . $this->id . '/' . ltrim($path, '/'));
58     }
59
60     /**
61      * Get the previous revision for the same page if existing.
62      */
63     public function getPrevious(): ?PageRevision
64     {
65         $id = static::newQuery()->where('page_id', '=', $this->page_id)
66             ->where('id', '<', $this->id)
67             ->max('id');
68
69         if ($id) {
70             return static::query()->find($id);
71         }
72
73         return null;
74     }
75
76     /**
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).
80      *
81      * @deprecated Use instanceof instead.
82      */
83     public static function isA(string $type): bool
84     {
85         return $type === 'revision';
86     }
87
88     public function logDescriptor(): string
89     {
90         return "Revision #{$this->revision_number} (ID: {$this->id}) for page ID {$this->page_id}";
91     }
92 }