Started pages API
This commit is contained in:
parent
ebeca256f0
commit
1c8102bb89
|
@ -29,6 +29,11 @@ class Page extends BookChild
|
||||||
|
|
||||||
protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot'];
|
protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot'];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'draft' => 'boolean',
|
||||||
|
'template' => 'boolean',
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the entities that are visible to the current user.
|
* Get the entities that are visible to the current user.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -35,9 +35,9 @@ class PageRepo
|
||||||
* Get a page by ID.
|
* Get a page by ID.
|
||||||
* @throws NotFoundException
|
* @throws NotFoundException
|
||||||
*/
|
*/
|
||||||
public function getById(int $id): Page
|
public function getById(int $id, array $relations = ['book']): Page
|
||||||
{
|
{
|
||||||
$page = Page::visible()->with(['book'])->find($id);
|
$page = Page::visible()->with($relations)->find($id);
|
||||||
|
|
||||||
if (!$page) {
|
if (!$page) {
|
||||||
throw new NotFoundException(trans('errors.page_not_found'));
|
throw new NotFoundException(trans('errors.page_not_found'));
|
||||||
|
@ -152,12 +152,8 @@ class PageRepo
|
||||||
public function publishDraft(Page $draft, array $input): Page
|
public function publishDraft(Page $draft, array $input): Page
|
||||||
{
|
{
|
||||||
$this->baseRepo->update($draft, $input);
|
$this->baseRepo->update($draft, $input);
|
||||||
if (isset($input['template']) && userCan('templates-manage')) {
|
$this->updateTemplateStatusAndContentFromInput($draft, $input);
|
||||||
$draft->template = ($input['template'] === 'true');
|
|
||||||
}
|
|
||||||
|
|
||||||
$pageContent = new PageContent($draft);
|
|
||||||
$pageContent->setNewHTML($input['html']);
|
|
||||||
$draft->draft = false;
|
$draft->draft = false;
|
||||||
$draft->revision_count = 1;
|
$draft->revision_count = 1;
|
||||||
$draft->priority = $this->getNewPriority($draft);
|
$draft->priority = $this->getNewPriority($draft);
|
||||||
|
@ -181,12 +177,7 @@ class PageRepo
|
||||||
$oldHtml = $page->html;
|
$oldHtml = $page->html;
|
||||||
$oldName = $page->name;
|
$oldName = $page->name;
|
||||||
|
|
||||||
if (isset($input['template']) && userCan('templates-manage')) {
|
$this->updateTemplateStatusAndContentFromInput($page, $input);
|
||||||
$page->template = ($input['template'] === 'true');
|
|
||||||
}
|
|
||||||
|
|
||||||
$pageContent = new PageContent($page);
|
|
||||||
$pageContent->setNewHTML($input['html']);
|
|
||||||
$this->baseRepo->update($page, $input);
|
$this->baseRepo->update($page, $input);
|
||||||
|
|
||||||
// Update with new details
|
// Update with new details
|
||||||
|
@ -211,6 +202,20 @@ class PageRepo
|
||||||
return $page;
|
return $page;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function updateTemplateStatusAndContentFromInput(Page $page, array $input)
|
||||||
|
{
|
||||||
|
if (isset($input['template']) && userCan('templates-manage')) {
|
||||||
|
$page->template = ($input['template'] === 'true');
|
||||||
|
}
|
||||||
|
|
||||||
|
$pageContent = new PageContent($page);
|
||||||
|
if (isset($input['html'])) {
|
||||||
|
$pageContent->setNewHTML($input['html']);
|
||||||
|
} else {
|
||||||
|
$pageContent->setNewMarkdown($input['markdown']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves a page revision into the system.
|
* Saves a page revision into the system.
|
||||||
*/
|
*/
|
||||||
|
@ -243,11 +248,10 @@ class PageRepo
|
||||||
{
|
{
|
||||||
// If the page itself is a draft simply update that
|
// If the page itself is a draft simply update that
|
||||||
if ($page->draft) {
|
if ($page->draft) {
|
||||||
$page->fill($input);
|
|
||||||
if (isset($input['html'])) {
|
if (isset($input['html'])) {
|
||||||
$content = new PageContent($page);
|
(new PageContent($page))->setNewHTML($input['html']);
|
||||||
$content->setNewHTML($input['html']);
|
|
||||||
}
|
}
|
||||||
|
$page->fill($input);
|
||||||
$page->save();
|
$page->save();
|
||||||
return $page;
|
return $page;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ use BookStack\Entities\Models\Page;
|
||||||
use DOMDocument;
|
use DOMDocument;
|
||||||
use DOMNodeList;
|
use DOMNodeList;
|
||||||
use DOMXPath;
|
use DOMXPath;
|
||||||
|
use League\CommonMark\CommonMarkConverter;
|
||||||
|
|
||||||
class PageContent
|
class PageContent
|
||||||
{
|
{
|
||||||
|
@ -25,6 +26,27 @@ class PageContent
|
||||||
{
|
{
|
||||||
$this->page->html = $this->formatHtml($html);
|
$this->page->html = $this->formatHtml($html);
|
||||||
$this->page->text = $this->toPlainText();
|
$this->page->text = $this->toPlainText();
|
||||||
|
$this->page->markdown = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the content of the page with new provided Markdown content.
|
||||||
|
*/
|
||||||
|
public function setNewMarkdown(string $markdown)
|
||||||
|
{
|
||||||
|
$this->page->markdown = $markdown;
|
||||||
|
$html = $this->markdownToHtml($markdown);
|
||||||
|
$this->page->html = $this->formatHtml($html);
|
||||||
|
$this->page->text = $this->toPlainText();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert the given Markdown content to a HTML string.
|
||||||
|
*/
|
||||||
|
protected function markdownToHtml(string $markdown): string
|
||||||
|
{
|
||||||
|
$converter = new CommonMarkConverter();
|
||||||
|
return $converter->convertToHtml($markdown);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -5,7 +5,7 @@ use BookStack\Http\Controllers\Controller;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
|
|
||||||
class ApiController extends Controller
|
abstract class ApiController extends Controller
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $rules = [];
|
protected $rules = [];
|
||||||
|
|
|
@ -0,0 +1,126 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace BookStack\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use BookStack\Entities\Models\Book;
|
||||||
|
use BookStack\Entities\Models\Chapter;
|
||||||
|
use BookStack\Entities\Models\Page;
|
||||||
|
use BookStack\Entities\Repos\PageRepo;
|
||||||
|
use BookStack\Exceptions\PermissionsException;
|
||||||
|
use Exception;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class PageApiController extends ApiController
|
||||||
|
{
|
||||||
|
protected $pageRepo;
|
||||||
|
|
||||||
|
protected $rules = [
|
||||||
|
'create' => [
|
||||||
|
'book_id' => 'required_unless:chapter_id|integer',
|
||||||
|
'chapter_id' => 'required_unless:book_id|integer',
|
||||||
|
'name' => 'required|string|max:255',
|
||||||
|
'html' => 'required_without:markdown|string',
|
||||||
|
'markdown' => 'required_without:html|string',
|
||||||
|
'tags' => 'array',
|
||||||
|
],
|
||||||
|
'update' => [
|
||||||
|
'book_id' => 'required|integer',
|
||||||
|
'chapter_id' => 'required|integer',
|
||||||
|
'name' => 'string|min:1|max:255',
|
||||||
|
'html' => 'string',
|
||||||
|
'markdown' => 'string',
|
||||||
|
'tags' => 'array',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
public function __construct(PageRepo $pageRepo)
|
||||||
|
{
|
||||||
|
$this->pageRepo = $pageRepo;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a listing of pages visible to the user.
|
||||||
|
*/
|
||||||
|
public function list()
|
||||||
|
{
|
||||||
|
$pages = Page::visible();
|
||||||
|
return $this->apiListingResponse($pages, [
|
||||||
|
'id', 'book_id', 'chapter_id', 'name', 'slug', 'priority',
|
||||||
|
'draft', 'template',
|
||||||
|
'created_at', 'updated_at', 'created_by', 'updated_by',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new page in the system.
|
||||||
|
*/
|
||||||
|
public function create(Request $request)
|
||||||
|
{
|
||||||
|
$this->validate($request, $this->rules['create']);
|
||||||
|
|
||||||
|
if ($request->has('chapter_id')) {
|
||||||
|
$parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
|
||||||
|
} else {
|
||||||
|
$parent = Book::visible()->findOrFail($request->get('book_id'));
|
||||||
|
}
|
||||||
|
$this->checkOwnablePermission('page-create', $parent);
|
||||||
|
|
||||||
|
$draft = $this->pageRepo->getNewDraftPage($parent);
|
||||||
|
$this->pageRepo->publishDraft($draft, $request->only(array_keys($this->rules['create'])));
|
||||||
|
|
||||||
|
return response()->json($draft->load(['tags']));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* View the details of a single page.
|
||||||
|
*/
|
||||||
|
public function read(string $id)
|
||||||
|
{
|
||||||
|
$page = $this->pageRepo->getById($id, ['tags', 'createdBy', 'updatedBy']);
|
||||||
|
return response()->json($page);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the details of a single page.
|
||||||
|
*/
|
||||||
|
public function update(Request $request, string $id)
|
||||||
|
{
|
||||||
|
$page = $this->pageRepo->getById($id, []);
|
||||||
|
$this->checkOwnablePermission('page-update', $page);
|
||||||
|
|
||||||
|
$parent = null;
|
||||||
|
if ($request->has('chapter_id')) {
|
||||||
|
$parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
|
||||||
|
} else if ($request->has('book_id')) {
|
||||||
|
$parent = Book::visible()->findOrFail($request->get('book_id'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($parent && !$parent->matches($page->getParent())) {
|
||||||
|
$this->checkOwnablePermission('page-delete', $page);
|
||||||
|
try {
|
||||||
|
$this->pageRepo->move($page, $parent->getType() . ':' . $parent->id);
|
||||||
|
} catch (Exception $exception) {
|
||||||
|
if ($exception instanceof PermissionsException) {
|
||||||
|
$this->showPermissionError();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->jsonError(trans('errors.selected_book_chapter_not_found'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$updatedPage = $this->pageRepo->update($page, $request->all());
|
||||||
|
return response()->json($updatedPage->load(['tags']));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a page from the system.
|
||||||
|
*/
|
||||||
|
public function delete(string $id)
|
||||||
|
{
|
||||||
|
$page = $this->pageRepo->getById($id, []);
|
||||||
|
$this->checkOwnablePermission('page-delete', $page);
|
||||||
|
|
||||||
|
$this->pageRepo->destroy($page);
|
||||||
|
return response('', 204);
|
||||||
|
}
|
||||||
|
}
|
|
@ -34,3 +34,9 @@ Route::post('shelves', 'BookshelfApiController@create');
|
||||||
Route::get('shelves/{id}', 'BookshelfApiController@read');
|
Route::get('shelves/{id}', 'BookshelfApiController@read');
|
||||||
Route::put('shelves/{id}', 'BookshelfApiController@update');
|
Route::put('shelves/{id}', 'BookshelfApiController@update');
|
||||||
Route::delete('shelves/{id}', 'BookshelfApiController@delete');
|
Route::delete('shelves/{id}', 'BookshelfApiController@delete');
|
||||||
|
|
||||||
|
Route::get('pages', 'PageApiController@list');
|
||||||
|
Route::post('pages', 'PageApiController@create');
|
||||||
|
Route::get('pages/{id}', 'PageApiController@read');
|
||||||
|
Route::put('pages/{id}', 'PageApiController@update');
|
||||||
|
Route::delete('pages/{id}', 'PageApiController@delete');
|
||||||
|
|
Loading…
Reference in New Issue