2021-06-26 23:23:15 +08:00
|
|
|
<?php
|
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
namespace BookStack\Entities\Controllers;
|
2019-10-05 19:55:01 +08:00
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
use BookStack\Activity\ActivityType;
|
2022-10-24 19:12:48 +08:00
|
|
|
use BookStack\Entities\Models\PageRevision;
|
2019-10-05 19:55:01 +08:00
|
|
|
use BookStack\Entities\Repos\PageRepo;
|
2023-06-13 22:13:07 +08:00
|
|
|
use BookStack\Entities\Repos\RevisionRepo;
|
2021-06-26 23:23:15 +08:00
|
|
|
use BookStack\Entities\Tools\PageContent;
|
2019-10-05 19:55:01 +08:00
|
|
|
use BookStack\Exceptions\NotFoundException;
|
2022-08-09 20:25:18 +08:00
|
|
|
use BookStack\Facades\Activity;
|
2023-05-19 03:53:39 +08:00
|
|
|
use BookStack\Http\Controller;
|
2022-11-01 05:26:31 +08:00
|
|
|
use BookStack\Util\SimpleListOptions;
|
|
|
|
use Illuminate\Http\Request;
|
2020-11-30 03:08:13 +08:00
|
|
|
use Ssddanbrown\HtmlDiff\Diff;
|
2019-10-05 19:55:01 +08:00
|
|
|
|
|
|
|
class PageRevisionController extends Controller
|
|
|
|
{
|
2023-06-12 23:45:30 +08:00
|
|
|
public function __construct(
|
2023-06-13 22:13:07 +08:00
|
|
|
protected PageRepo $pageRepo,
|
|
|
|
protected RevisionRepo $revisionRepo,
|
2023-06-12 23:45:30 +08:00
|
|
|
) {
|
2019-10-05 19:55:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows the last revisions for this page.
|
2021-06-26 23:23:15 +08:00
|
|
|
*
|
2019-10-05 19:55:01 +08:00
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
2022-11-01 05:26:31 +08:00
|
|
|
public function index(Request $request, string $bookSlug, string $pageSlug)
|
2019-10-05 19:55:01 +08:00
|
|
|
{
|
|
|
|
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
|
2022-11-01 05:26:31 +08:00
|
|
|
$listOptions = SimpleListOptions::fromRequest($request, 'page_revisions', true)->withSortOptions([
|
|
|
|
'id' => trans('entities.pages_revisions_sort_number')
|
|
|
|
]);
|
|
|
|
|
2022-08-11 00:50:35 +08:00
|
|
|
$revisions = $page->revisions()->select([
|
2022-11-01 05:26:31 +08:00
|
|
|
'id', 'page_id', 'name', 'created_at', 'created_by', 'updated_at',
|
|
|
|
'type', 'revision_number', 'summary',
|
|
|
|
])
|
2022-08-11 00:50:35 +08:00
|
|
|
->selectRaw("IF(markdown = '', false, true) as is_markdown")
|
|
|
|
->with(['page.book', 'createdBy'])
|
2022-11-01 05:26:31 +08:00
|
|
|
->reorder('id', $listOptions->getOrder())
|
|
|
|
->reorder('created_at', $listOptions->getOrder())
|
|
|
|
->paginate(50);
|
2022-08-11 00:50:35 +08:00
|
|
|
|
|
|
|
$this->setPageTitle(trans('entities.pages_revisions_named', ['pageName' => $page->getShortName()]));
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2019-10-05 19:55:01 +08:00
|
|
|
return view('pages.revisions', [
|
2022-11-01 05:26:31 +08:00
|
|
|
'revisions' => $revisions,
|
|
|
|
'page' => $page,
|
|
|
|
'listOptions' => $listOptions,
|
2019-10-05 19:55:01 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows a preview of a single revision.
|
2021-06-26 23:23:15 +08:00
|
|
|
*
|
2019-10-05 19:55:01 +08:00
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function show(string $bookSlug, string $pageSlug, int $revisionId)
|
|
|
|
{
|
|
|
|
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
|
2022-10-24 19:12:48 +08:00
|
|
|
/** @var ?PageRevision $revision */
|
2019-10-05 19:55:01 +08:00
|
|
|
$revision = $page->revisions()->where('id', '=', $revisionId)->first();
|
|
|
|
if ($revision === null) {
|
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$page->fill($revision->toArray());
|
2020-05-23 19:28:14 +08:00
|
|
|
// TODO - Refactor PageContent so we don't need to juggle this
|
|
|
|
$page->html = $revision->html;
|
|
|
|
$page->html = (new PageContent($page))->render();
|
2019-10-05 19:55:01 +08:00
|
|
|
|
|
|
|
$this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2019-10-05 19:55:01 +08:00
|
|
|
return view('pages.revision', [
|
2021-06-26 23:23:15 +08:00
|
|
|
'page' => $page,
|
|
|
|
'book' => $page->book,
|
|
|
|
'diff' => null,
|
|
|
|
'revision' => $revision,
|
2019-10-05 19:55:01 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows the changes of a single revision.
|
2021-06-26 23:23:15 +08:00
|
|
|
*
|
2019-10-05 19:55:01 +08:00
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function changes(string $bookSlug, string $pageSlug, int $revisionId)
|
|
|
|
{
|
|
|
|
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
|
2022-10-24 19:12:48 +08:00
|
|
|
/** @var ?PageRevision $revision */
|
2019-10-05 19:55:01 +08:00
|
|
|
$revision = $page->revisions()->where('id', '=', $revisionId)->first();
|
|
|
|
if ($revision === null) {
|
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$prev = $revision->getPrevious();
|
|
|
|
$prevContent = $prev->html ?? '';
|
2020-11-30 03:08:13 +08:00
|
|
|
$diff = Diff::excecute($prevContent, $revision->html);
|
2019-10-05 19:55:01 +08:00
|
|
|
|
|
|
|
$page->fill($revision->toArray());
|
2020-05-23 19:28:14 +08:00
|
|
|
// TODO - Refactor PageContent so we don't need to juggle this
|
|
|
|
$page->html = $revision->html;
|
|
|
|
$page->html = (new PageContent($page))->render();
|
2022-09-18 08:25:20 +08:00
|
|
|
$this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
|
2019-10-05 19:55:01 +08:00
|
|
|
|
|
|
|
return view('pages.revision', [
|
2021-06-26 23:23:15 +08:00
|
|
|
'page' => $page,
|
|
|
|
'book' => $page->book,
|
|
|
|
'diff' => $diff,
|
|
|
|
'revision' => $revision,
|
2019-10-05 19:55:01 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restores a page using the content of the specified revision.
|
2021-06-26 23:23:15 +08:00
|
|
|
*
|
2019-10-05 19:55:01 +08:00
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function restore(string $bookSlug, string $pageSlug, int $revisionId)
|
|
|
|
{
|
|
|
|
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
|
|
|
|
$this->checkOwnablePermission('page-update', $page);
|
|
|
|
|
|
|
|
$page = $this->pageRepo->restoreRevision($page, $revisionId);
|
|
|
|
|
|
|
|
return redirect($page->getUrl());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a revision using the id of the specified revision.
|
2021-06-26 23:23:15 +08:00
|
|
|
*
|
2019-10-05 19:55:01 +08:00
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function destroy(string $bookSlug, string $pageSlug, int $revId)
|
|
|
|
{
|
|
|
|
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
|
|
|
|
$this->checkOwnablePermission('page-delete', $page);
|
|
|
|
|
|
|
|
$revision = $page->revisions()->where('id', '=', $revId)->first();
|
|
|
|
if ($revision === null) {
|
|
|
|
throw new NotFoundException("Revision #{$revId} not found");
|
|
|
|
}
|
|
|
|
|
2022-03-27 00:44:34 +08:00
|
|
|
// Check if it's the latest revision, cannot delete the latest revision.
|
|
|
|
if (intval($page->currentRevision->id ?? null) === intval($revId)) {
|
2019-10-05 19:55:01 +08:00
|
|
|
$this->showErrorNotification(trans('entities.revision_cannot_delete_latest'));
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2019-10-05 19:55:01 +08:00
|
|
|
return redirect($page->getUrl('/revisions'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$revision->delete();
|
2022-08-09 20:25:18 +08:00
|
|
|
Activity::add(ActivityType::REVISION_DELETE, $revision);
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2019-10-05 19:55:01 +08:00
|
|
|
return redirect($page->getUrl('/revisions'));
|
|
|
|
}
|
2023-06-13 22:13:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroys existing drafts, belonging to the current user, for the given page.
|
|
|
|
*/
|
|
|
|
public function destroyUserDraft(string $pageId)
|
|
|
|
{
|
|
|
|
$page = $this->pageRepo->getById($pageId);
|
|
|
|
$this->revisionRepo->deleteDraftsForCurrentUser($page);
|
|
|
|
|
|
|
|
return response('', 200);
|
|
|
|
}
|
2019-10-05 19:55:01 +08:00
|
|
|
}
|