2021-06-26 17:23:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Models;
|
2018-09-25 13:30:50 +02:00
|
|
|
|
2023-05-17 18:56:55 +02:00
|
|
|
use BookStack\Activity\Models\Loggable;
|
|
|
|
use BookStack\App\Model;
|
|
|
|
use BookStack\Users\Models\User;
|
2019-10-05 13:55:01 +02:00
|
|
|
use Carbon\Carbon;
|
2021-10-04 21:26:55 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2015-08-09 13:06:52 +02:00
|
|
|
|
2019-10-05 13:55:01 +02:00
|
|
|
/**
|
2021-06-26 17:23:15 +02:00
|
|
|
* Class PageRevision.
|
|
|
|
*
|
2022-03-26 21:38:03 +01:00
|
|
|
* @property mixed $id
|
2021-06-26 17:23:15 +02:00
|
|
|
* @property int $page_id
|
2022-04-24 00:20:46 +02:00
|
|
|
* @property string $name
|
2019-10-05 13:55:01 +02:00
|
|
|
* @property string $slug
|
|
|
|
* @property string $book_slug
|
2021-06-26 17:23:15 +02:00
|
|
|
* @property int $created_by
|
2019-10-05 13:55:01 +02:00
|
|
|
* @property Carbon $created_at
|
2021-10-04 21:26:55 +02:00
|
|
|
* @property Carbon $updated_at
|
2019-10-05 13:55:01 +02:00
|
|
|
* @property string $type
|
|
|
|
* @property string $summary
|
|
|
|
* @property string $markdown
|
|
|
|
* @property string $html
|
2022-04-24 00:20:46 +02:00
|
|
|
* @property string $text
|
2021-06-26 17:23:15 +02:00
|
|
|
* @property int $revision_number
|
2021-10-04 21:26:55 +02:00
|
|
|
* @property Page $page
|
2021-11-06 01:32:01 +01:00
|
|
|
* @property-read ?User $createdBy
|
2019-10-05 13:55:01 +02:00
|
|
|
*/
|
2022-08-09 14:25:18 +02:00
|
|
|
class PageRevision extends Model implements Loggable
|
2015-08-09 13:06:52 +02:00
|
|
|
{
|
2022-04-18 18:39:28 +02:00
|
|
|
protected $fillable = ['name', 'text', 'summary'];
|
2022-10-10 17:58:26 +02:00
|
|
|
protected $hidden = ['html', 'markdown', 'text'];
|
2015-08-09 13:06:52 +02:00
|
|
|
|
2016-03-09 23:32:07 +01:00
|
|
|
/**
|
2021-06-26 17:23:15 +02:00
|
|
|
* Get the user that created the page revision.
|
2016-03-09 23:32:07 +01:00
|
|
|
*/
|
2021-10-04 21:26:55 +02:00
|
|
|
public function createdBy(): BelongsTo
|
2015-08-09 13:06:52 +02:00
|
|
|
{
|
2016-05-01 22:20:50 +02:00
|
|
|
return $this->belongsTo(User::class, 'created_by');
|
2015-08-09 13:06:52 +02:00
|
|
|
}
|
|
|
|
|
2016-03-09 23:32:07 +01:00
|
|
|
/**
|
|
|
|
* Get the page this revision originates from.
|
|
|
|
*/
|
2021-10-04 21:26:55 +02:00
|
|
|
public function page(): BelongsTo
|
2015-08-09 13:06:52 +02:00
|
|
|
{
|
2016-05-01 22:20:50 +02:00
|
|
|
return $this->belongsTo(Page::class);
|
2015-08-09 13:06:52 +02:00
|
|
|
}
|
|
|
|
|
2016-03-09 23:32:07 +01:00
|
|
|
/**
|
|
|
|
* Get the url for this revision.
|
|
|
|
*/
|
2022-01-10 18:46:17 +01:00
|
|
|
public function getUrl(string $path = ''): string
|
2015-08-09 13:06:52 +02:00
|
|
|
{
|
2022-01-10 18:46:17 +01:00
|
|
|
return $this->page->getUrl('/revisions/' . $this->id . '/' . ltrim($path, '/'));
|
2015-08-09 13:06:52 +02:00
|
|
|
}
|
|
|
|
|
2016-07-07 19:42:21 +02:00
|
|
|
/**
|
2021-06-26 17:23:15 +02:00
|
|
|
* Get the previous revision for the same page if existing.
|
2016-07-07 19:42:21 +02:00
|
|
|
*/
|
2021-11-23 00:33:55 +01:00
|
|
|
public function getPrevious(): ?PageRevision
|
2016-07-07 19:42:21 +02:00
|
|
|
{
|
2019-10-05 13:55:01 +02:00
|
|
|
$id = static::newQuery()->where('page_id', '=', $this->page_id)
|
|
|
|
->where('id', '<', $this->id)
|
|
|
|
->max('id');
|
|
|
|
|
|
|
|
if ($id) {
|
|
|
|
return static::query()->find($id);
|
2016-07-07 19:42:21 +02:00
|
|
|
}
|
2019-10-05 13:55:01 +02:00
|
|
|
|
2016-09-29 11:10:46 +02:00
|
|
|
return null;
|
2016-07-07 19:42:21 +02:00
|
|
|
}
|
|
|
|
|
2017-08-26 16:41:33 +02:00
|
|
|
/**
|
|
|
|
* Allows checking of the exact class, Used to check entity type.
|
|
|
|
* Included here to align with entities in similar use cases.
|
2021-06-26 17:23:15 +02:00
|
|
|
* (Yup, Bit of an awkward hack).
|
|
|
|
*
|
2021-11-20 15:03:56 +01:00
|
|
|
* @deprecated Use instanceof instead.
|
2017-08-26 16:41:33 +02:00
|
|
|
*/
|
2021-11-20 15:03:56 +01:00
|
|
|
public static function isA(string $type): bool
|
2017-08-26 16:41:33 +02:00
|
|
|
{
|
|
|
|
return $type === 'revision';
|
|
|
|
}
|
2022-08-09 14:25:18 +02:00
|
|
|
|
|
|
|
public function logDescriptor(): string
|
|
|
|
{
|
|
|
|
return "Revision #{$this->revision_number} (ID: {$this->id}) for page ID {$this->page_id}";
|
|
|
|
}
|
2015-08-09 13:06:52 +02:00
|
|
|
}
|