mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-01-19 16:52:45 +08:00
commit
d56e7e7c79
|
@ -5,7 +5,7 @@ use BookStack\Model;
|
|||
class Tag extends Model
|
||||
{
|
||||
protected $fillable = ['name', 'value', 'order'];
|
||||
protected $hidden = ['id', 'entity_id', 'entity_type'];
|
||||
protected $hidden = ['id', 'entity_id', 'entity_type', 'created_at', 'updated_at'];
|
||||
|
||||
/**
|
||||
* Get the entity that this tag belongs to
|
||||
|
|
|
@ -18,7 +18,7 @@ class Book extends Entity implements HasCoverImage
|
|||
public $searchFactor = 2;
|
||||
|
||||
protected $fillable = ['name', 'description'];
|
||||
protected $hidden = ['restricted', 'pivot', 'image_id'];
|
||||
protected $hidden = ['restricted', 'pivot', 'image_id', 'deleted_at'];
|
||||
|
||||
/**
|
||||
* Get the url for this book.
|
||||
|
|
|
@ -12,7 +12,7 @@ class Bookshelf extends Entity implements HasCoverImage
|
|||
|
||||
protected $fillable = ['name', 'description', 'image_id'];
|
||||
|
||||
protected $hidden = ['restricted', 'image_id'];
|
||||
protected $hidden = ['restricted', 'image_id', 'deleted_at'];
|
||||
|
||||
/**
|
||||
* Get the books in this shelf.
|
||||
|
|
|
@ -11,7 +11,7 @@ class Chapter extends BookChild
|
|||
public $searchFactor = 1.3;
|
||||
|
||||
protected $fillable = ['name', 'description', 'priority', 'book_id'];
|
||||
protected $hidden = ['restricted', 'pivot'];
|
||||
protected $hidden = ['restricted', 'pivot', 'deleted_at'];
|
||||
|
||||
/**
|
||||
* Get the pages that this chapter contains.
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<?php namespace BookStack\Entities\Models;
|
||||
|
||||
use BookStack\Entities\Tools\PageContent;
|
||||
use BookStack\Uploads\Attachment;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
@ -27,7 +28,12 @@ class Page extends BookChild
|
|||
|
||||
public $textField = 'text';
|
||||
|
||||
protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot'];
|
||||
protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot', 'deleted_at'];
|
||||
|
||||
protected $casts = [
|
||||
'draft' => 'boolean',
|
||||
'template' => 'boolean',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the entities that are visible to the current user.
|
||||
|
@ -109,4 +115,15 @@ class Page extends BookChild
|
|||
{
|
||||
return $this->revisions()->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get this page for JSON display.
|
||||
*/
|
||||
public function forJsonDisplay(): Page
|
||||
{
|
||||
$refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy']);
|
||||
$refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
|
||||
$refreshed->html = (new PageContent($refreshed))->render();
|
||||
return $refreshed;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,9 +35,9 @@ class PageRepo
|
|||
* Get a page by ID.
|
||||
* @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) {
|
||||
throw new NotFoundException(trans('errors.page_not_found'));
|
||||
|
@ -152,12 +152,8 @@ class PageRepo
|
|||
public function publishDraft(Page $draft, array $input): Page
|
||||
{
|
||||
$this->baseRepo->update($draft, $input);
|
||||
if (isset($input['template']) && userCan('templates-manage')) {
|
||||
$draft->template = ($input['template'] === 'true');
|
||||
}
|
||||
$this->updateTemplateStatusAndContentFromInput($draft, $input);
|
||||
|
||||
$pageContent = new PageContent($draft);
|
||||
$pageContent->setNewHTML($input['html']);
|
||||
$draft->draft = false;
|
||||
$draft->revision_count = 1;
|
||||
$draft->priority = $this->getNewPriority($draft);
|
||||
|
@ -181,12 +177,7 @@ class PageRepo
|
|||
$oldHtml = $page->html;
|
||||
$oldName = $page->name;
|
||||
|
||||
if (isset($input['template']) && userCan('templates-manage')) {
|
||||
$page->template = ($input['template'] === 'true');
|
||||
}
|
||||
|
||||
$pageContent = new PageContent($page);
|
||||
$pageContent->setNewHTML($input['html']);
|
||||
$this->updateTemplateStatusAndContentFromInput($page, $input);
|
||||
$this->baseRepo->update($page, $input);
|
||||
|
||||
// Update with new details
|
||||
|
@ -211,6 +202,20 @@ class PageRepo
|
|||
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.
|
||||
*/
|
||||
|
@ -243,11 +248,10 @@ class PageRepo
|
|||
{
|
||||
// If the page itself is a draft simply update that
|
||||
if ($page->draft) {
|
||||
$page->fill($input);
|
||||
if (isset($input['html'])) {
|
||||
$content = new PageContent($page);
|
||||
$content->setNewHTML($input['html']);
|
||||
(new PageContent($page))->setNewHTML($input['html']);
|
||||
}
|
||||
$page->fill($input);
|
||||
$page->save();
|
||||
return $page;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ use BookStack\Entities\Models\Page;
|
|||
use DOMDocument;
|
||||
use DOMNodeList;
|
||||
use DOMXPath;
|
||||
use League\CommonMark\CommonMarkConverter;
|
||||
|
||||
class PageContent
|
||||
{
|
||||
|
@ -25,6 +26,27 @@ class PageContent
|
|||
{
|
||||
$this->page->html = $this->formatHtml($html);
|
||||
$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\Http\JsonResponse;
|
||||
|
||||
class ApiController extends Controller
|
||||
abstract class ApiController extends Controller
|
||||
{
|
||||
|
||||
protected $rules = [];
|
||||
|
|
|
@ -79,7 +79,8 @@ class BookApiController extends ApiController
|
|||
}
|
||||
|
||||
/**
|
||||
* Delete a single book from the system.
|
||||
* Delete a single book.
|
||||
* This will typically send the book to the recycle bin.
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function delete(string $id)
|
||||
|
|
|
@ -2,20 +2,14 @@
|
|||
|
||||
use BookStack\Entities\Models\Book;
|
||||
use BookStack\Entities\Tools\ExportFormatter;
|
||||
use BookStack\Entities\Repos\BookRepo;
|
||||
use Throwable;
|
||||
|
||||
class BookExportApiController extends ApiController
|
||||
{
|
||||
protected $bookRepo;
|
||||
protected $exportFormatter;
|
||||
|
||||
/**
|
||||
* BookExportController constructor.
|
||||
*/
|
||||
public function __construct(BookRepo $bookRepo, ExportFormatter $exportFormatter)
|
||||
public function __construct(ExportFormatter $exportFormatter)
|
||||
{
|
||||
$this->bookRepo = $bookRepo;
|
||||
$this->exportFormatter = $exportFormatter;
|
||||
}
|
||||
|
||||
|
|
|
@ -100,7 +100,8 @@ class BookshelfApiController extends ApiController
|
|||
|
||||
|
||||
/**
|
||||
* Delete a single shelf from the system.
|
||||
* Delete a single shelf.
|
||||
* This will typically send the shelf to the recycle bin.
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete(string $id)
|
||||
|
|
|
@ -86,7 +86,8 @@ class ChapterApiController extends ApiController
|
|||
}
|
||||
|
||||
/**
|
||||
* Delete a chapter from the system.
|
||||
* Delete a chapter.
|
||||
* This will typically send the chapter to the recycle bin.
|
||||
*/
|
||||
public function delete(string $id)
|
||||
{
|
||||
|
|
|
@ -7,15 +7,13 @@ use Throwable;
|
|||
|
||||
class ChapterExportApiController extends ApiController
|
||||
{
|
||||
protected $chapterRepo;
|
||||
protected $exportFormatter;
|
||||
|
||||
/**
|
||||
* ChapterExportController constructor.
|
||||
*/
|
||||
public function __construct(BookRepo $chapterRepo, ExportFormatter $exportFormatter)
|
||||
public function __construct(ExportFormatter $exportFormatter)
|
||||
{
|
||||
$this->chapterRepo = $chapterRepo;
|
||||
$this->exportFormatter = $exportFormatter;
|
||||
}
|
||||
|
||||
|
|
140
app/Http/Controllers/Api/PageApiController.php
Normal file
140
app/Http/Controllers/Api/PageApiController.php
Normal file
|
@ -0,0 +1,140 @@
|
|||
<?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_without:chapter_id|integer',
|
||||
'chapter_id' => 'required_without: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.
|
||||
*
|
||||
* The ID of a parent book or chapter is required to indicate
|
||||
* where this page should be located.
|
||||
*
|
||||
* Any HTML content provided should be kept to a single-block depth of plain HTML
|
||||
* elements to remain compatible with the BookStack front-end and editors.
|
||||
*/
|
||||
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->forJsonDisplay());
|
||||
}
|
||||
|
||||
/**
|
||||
* View the details of a single page.
|
||||
*
|
||||
* Pages will always have HTML content. They may have markdown content
|
||||
* if the markdown editor was used to last update the page.
|
||||
*/
|
||||
public function read(string $id)
|
||||
{
|
||||
$page = $this->pageRepo->getById($id, []);
|
||||
return response()->json($page->forJsonDisplay());
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the details of a single page.
|
||||
*
|
||||
* See the 'create' action for details on the provided HTML/Markdown.
|
||||
* Providing a 'book_id' or 'chapter_id' property will essentially move
|
||||
* the page into that parent element if you have permissions to do so.
|
||||
*/
|
||||
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->forJsonDisplay());
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a page.
|
||||
* This will typically send the page to the recycle bin.
|
||||
*/
|
||||
public function delete(string $id)
|
||||
{
|
||||
$page = $this->pageRepo->getById($id, []);
|
||||
$this->checkOwnablePermission('page-delete', $page);
|
||||
|
||||
$this->pageRepo->destroy($page);
|
||||
return response('', 204);
|
||||
}
|
||||
}
|
47
app/Http/Controllers/Api/PageExportApiController.php
Normal file
47
app/Http/Controllers/Api/PageExportApiController.php
Normal file
|
@ -0,0 +1,47 @@
|
|||
<?php namespace BookStack\Http\Controllers\Api;
|
||||
|
||||
use BookStack\Entities\Models\Page;
|
||||
use BookStack\Entities\Tools\ExportFormatter;
|
||||
use Throwable;
|
||||
|
||||
class PageExportApiController extends ApiController
|
||||
{
|
||||
protected $exportFormatter;
|
||||
|
||||
public function __construct(ExportFormatter $exportFormatter)
|
||||
{
|
||||
$this->exportFormatter = $exportFormatter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export a page as a PDF file.
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function exportPdf(int $id)
|
||||
{
|
||||
$page = Page::visible()->findOrFail($id);
|
||||
$pdfContent = $this->exportFormatter->pageToPdf($page);
|
||||
return $this->downloadResponse($pdfContent, $page->slug . '.pdf');
|
||||
}
|
||||
|
||||
/**
|
||||
* Export a page as a contained HTML file.
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function exportHtml(int $id)
|
||||
{
|
||||
$page = Page::visible()->findOrFail($id);
|
||||
$htmlContent = $this->exportFormatter->pageToContainedHtml($page);
|
||||
return $this->downloadResponse($htmlContent, $page->slug . '.html');
|
||||
}
|
||||
|
||||
/**
|
||||
* Export a page as a plain text file.
|
||||
*/
|
||||
public function exportPlainText(int $id)
|
||||
{
|
||||
$page = Page::visible()->findOrFail($id);
|
||||
$textContent = $this->exportFormatter->pageToPlainText($page);
|
||||
return $this->downloadResponse($textContent, $page->slug . '.txt');
|
||||
}
|
||||
}
|
9
dev/api/requests/pages-create.json
Normal file
9
dev/api/requests/pages-create.json
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"book_id": 1,
|
||||
"name": "My API Page",
|
||||
"html": "<p>my new API page</p>",
|
||||
"tags": [
|
||||
{"name": "Category", "value": "Not Bad Content"},
|
||||
{"name": "Rating", "value": "Average"}
|
||||
]
|
||||
}
|
9
dev/api/requests/pages-update.json
Normal file
9
dev/api/requests/pages-update.json
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"chapter_id": 1,
|
||||
"name": "My updated API Page",
|
||||
"html": "<p>my new API page - Updated</p>",
|
||||
"tags": [
|
||||
{"name": "Category", "value": "API Examples"},
|
||||
{"name": "Rating", "value": "Alright"}
|
||||
]
|
||||
}
|
|
@ -16,13 +16,9 @@
|
|||
"tags": [
|
||||
{
|
||||
"id": 13,
|
||||
"entity_id": 16,
|
||||
"entity_type": "BookStack\\Book",
|
||||
"name": "Category",
|
||||
"value": "Guide",
|
||||
"order": 0,
|
||||
"created_at": "2020-01-12 14:11:51",
|
||||
"updated_at": "2020-01-12 14:11:51"
|
||||
"order": 0
|
||||
}
|
||||
],
|
||||
"cover": {
|
||||
|
|
|
@ -19,9 +19,7 @@
|
|||
{
|
||||
"name": "Category",
|
||||
"value": "Guide",
|
||||
"order": 0,
|
||||
"created_at": "2020-05-22 22:51:51",
|
||||
"updated_at": "2020-05-22 22:51:51"
|
||||
"order": 0
|
||||
}
|
||||
],
|
||||
"pages": [
|
||||
|
@ -36,9 +34,9 @@
|
|||
"updated_at": "2019-08-26 14:32:59",
|
||||
"created_by": 1,
|
||||
"updated_by": 1,
|
||||
"draft": 0,
|
||||
"draft": false,
|
||||
"revision_count": 2,
|
||||
"template": 0
|
||||
"template": false
|
||||
},
|
||||
{
|
||||
"id": 7,
|
||||
|
@ -51,9 +49,9 @@
|
|||
"updated_at": "2019-06-06 12:03:04",
|
||||
"created_by": 3,
|
||||
"updated_by": 3,
|
||||
"draft": 0,
|
||||
"draft": false,
|
||||
"revision_count": 1,
|
||||
"template": 0
|
||||
"template": false
|
||||
}
|
||||
]
|
||||
}
|
35
dev/api/responses/pages-create.json
Normal file
35
dev/api/responses/pages-create.json
Normal file
|
@ -0,0 +1,35 @@
|
|||
{
|
||||
"id": 358,
|
||||
"book_id": 1,
|
||||
"chapter_id": 0,
|
||||
"name": "My API Page",
|
||||
"slug": "my-api-page",
|
||||
"html": "<p id=\"bkmrk-my-new-api-page\">my new API page</p>",
|
||||
"priority": 14,
|
||||
"created_at": "2020-11-28 15:01:39",
|
||||
"updated_at": "2020-11-28 15:01:39",
|
||||
"created_by": {
|
||||
"id": 1,
|
||||
"name": "Admin"
|
||||
},
|
||||
"updated_by": {
|
||||
"id": 1,
|
||||
"name": "Admin"
|
||||
},
|
||||
"draft": false,
|
||||
"markdown": "",
|
||||
"revision_count": 1,
|
||||
"template": false,
|
||||
"tags": [
|
||||
{
|
||||
"name": "Category",
|
||||
"value": "Not Bad Content",
|
||||
"order": 0
|
||||
},
|
||||
{
|
||||
"name": "Rating",
|
||||
"value": "Average",
|
||||
"order": 1
|
||||
}
|
||||
]
|
||||
}
|
47
dev/api/responses/pages-list.json
Normal file
47
dev/api/responses/pages-list.json
Normal file
|
@ -0,0 +1,47 @@
|
|||
{
|
||||
"data": [
|
||||
{
|
||||
"id": 1,
|
||||
"book_id": 1,
|
||||
"chapter_id": 1,
|
||||
"name": "How to create page content",
|
||||
"slug": "how-to-create-page-content",
|
||||
"priority": 0,
|
||||
"draft": false,
|
||||
"template": false,
|
||||
"created_at": "2019-05-05 21:49:58",
|
||||
"updated_at": "2020-07-04 15:50:58",
|
||||
"created_by": 1,
|
||||
"updated_by": 1
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"book_id": 1,
|
||||
"chapter_id": 1,
|
||||
"name": "How to use images",
|
||||
"slug": "how-to-use-images",
|
||||
"priority": 2,
|
||||
"draft": false,
|
||||
"template": false,
|
||||
"created_at": "2019-05-05 21:53:30",
|
||||
"updated_at": "2019-06-06 12:03:04",
|
||||
"created_by": 1,
|
||||
"updated_by": 1
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"book_id": 1,
|
||||
"chapter_id": 1,
|
||||
"name": "Drawings via draw.io",
|
||||
"slug": "drawings-via-drawio",
|
||||
"priority": 3,
|
||||
"draft": false,
|
||||
"template": false,
|
||||
"created_at": "2019-05-05 21:53:49",
|
||||
"updated_at": "2019-12-18 21:56:52",
|
||||
"created_by": 1,
|
||||
"updated_by": 1
|
||||
}
|
||||
],
|
||||
"total": 322
|
||||
}
|
35
dev/api/responses/pages-read.json
Normal file
35
dev/api/responses/pages-read.json
Normal file
|
@ -0,0 +1,35 @@
|
|||
{
|
||||
"id": 306,
|
||||
"book_id": 1,
|
||||
"chapter_id": 0,
|
||||
"name": "A page written in markdown",
|
||||
"slug": "a-page-written-in-markdown",
|
||||
"html": "<h1 id=\"bkmrk-how-this-is-built\">How this is built</h1>\r\n<p id=\"bkmrk-this-page-is-written\">This page is written in markdown. BookStack stores the page data in HTML.</p>\r\n<p id=\"bkmrk-here%27s-a-cute-pictur\">Here's a cute picture of my cat:</p>\r\n<p id=\"bkmrk-\"><a href=\"http://example.com/uploads/images/gallery/2020-04/yXSrubes.jpg\"><img src=\"http://example.com/uploads/images/gallery/2020-04/scaled-1680-/yXSrubes.jpg\" alt=\"yXSrubes.jpg\"></a></p>",
|
||||
"priority": 13,
|
||||
"created_at": "2020-02-02 21:40:38",
|
||||
"updated_at": "2020-11-28 14:43:20",
|
||||
"created_by": {
|
||||
"id": 1,
|
||||
"name": "Admin"
|
||||
},
|
||||
"updated_by": {
|
||||
"id": 1,
|
||||
"name": "Admin"
|
||||
},
|
||||
"draft": false,
|
||||
"markdown": "# How this is built\r\n\r\nThis page is written in markdown. BookStack stores the page data in HTML.\r\n\r\nHere's a cute picture of my cat:\r\n\r\n[![yXSrubes.jpg](http://example.com/uploads/images/gallery/2020-04/scaled-1680-/yXSrubes.jpg)](http://example.com/uploads/images/gallery/2020-04/yXSrubes.jpg)",
|
||||
"revision_count": 5,
|
||||
"template": false,
|
||||
"tags": [
|
||||
{
|
||||
"name": "Category",
|
||||
"value": "Top Content",
|
||||
"order": 0
|
||||
},
|
||||
{
|
||||
"name": "Animal",
|
||||
"value": "Cat",
|
||||
"order": 1
|
||||
}
|
||||
]
|
||||
}
|
35
dev/api/responses/pages-update.json
Normal file
35
dev/api/responses/pages-update.json
Normal file
|
@ -0,0 +1,35 @@
|
|||
{
|
||||
"id": 361,
|
||||
"book_id": 1,
|
||||
"chapter_id": 1,
|
||||
"name": "My updated API Page",
|
||||
"slug": "my-updated-api-page",
|
||||
"html": "<p id=\"bkmrk-my-new-api-page---up\">my new API page - Updated</p>",
|
||||
"priority": 16,
|
||||
"created_at": "2020-11-28 15:10:54",
|
||||
"updated_at": "2020-11-28 15:13:03",
|
||||
"created_by": {
|
||||
"id": 1,
|
||||
"name": "Admin"
|
||||
},
|
||||
"updated_by": {
|
||||
"id": 1,
|
||||
"name": "Admin"
|
||||
},
|
||||
"draft": false,
|
||||
"markdown": "",
|
||||
"revision_count": 5,
|
||||
"template": false,
|
||||
"tags": [
|
||||
{
|
||||
"name": "Category",
|
||||
"value": "API Examples",
|
||||
"order": 0
|
||||
},
|
||||
{
|
||||
"name": "Rating",
|
||||
"value": "Alright",
|
||||
"order": 0
|
||||
}
|
||||
]
|
||||
}
|
|
@ -16,13 +16,9 @@
|
|||
"tags": [
|
||||
{
|
||||
"id": 16,
|
||||
"entity_id": 14,
|
||||
"entity_type": "BookStack\\Bookshelf",
|
||||
"name": "Category",
|
||||
"value": "Guide",
|
||||
"order": 0,
|
||||
"created_at": "2020-04-10 13:31:04",
|
||||
"updated_at": "2020-04-10 13:31:04"
|
||||
"order": 0
|
||||
}
|
||||
],
|
||||
"cover": {
|
||||
|
|
|
@ -274,4 +274,6 @@
|
|||
.sticky-sidebar {
|
||||
position: sticky;
|
||||
top: $-m;
|
||||
max-height: calc(100vh - #{$-m});
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
|
|
@ -29,6 +29,16 @@ Route::get('chapters/{id}/export/html', 'ChapterExportApiController@exportHtml')
|
|||
Route::get('chapters/{id}/export/pdf', 'ChapterExportApiController@exportPdf');
|
||||
Route::get('chapters/{id}/export/plaintext', 'ChapterExportApiController@exportPlainText');
|
||||
|
||||
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');
|
||||
|
||||
Route::get('pages/{id}/export/html', 'PageExportApiController@exportHtml');
|
||||
Route::get('pages/{id}/export/pdf', 'PageExportApiController@exportPdf');
|
||||
Route::get('pages/{id}/export/plaintext', 'PageExportApiController@exportPlainText');
|
||||
|
||||
Route::get('shelves', 'BookshelfApiController@list');
|
||||
Route::post('shelves', 'BookshelfApiController@create');
|
||||
Route::get('shelves/{id}', 'BookshelfApiController@read');
|
||||
|
|
258
tests/Api/PagesApiTest.php
Normal file
258
tests/Api/PagesApiTest.php
Normal file
|
@ -0,0 +1,258 @@
|
|||
<?php namespace Tests\Api;
|
||||
|
||||
use BookStack\Entities\Models\Book;
|
||||
use BookStack\Entities\Models\Chapter;
|
||||
use BookStack\Entities\Models\Page;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PagesApiTest extends TestCase
|
||||
{
|
||||
use TestsApi;
|
||||
|
||||
protected $baseEndpoint = '/api/pages';
|
||||
|
||||
public function test_index_endpoint_returns_expected_page()
|
||||
{
|
||||
$this->actingAsApiEditor();
|
||||
$firstPage = Page::query()->orderBy('id', 'asc')->first();
|
||||
|
||||
$resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
|
||||
$resp->assertJson(['data' => [
|
||||
[
|
||||
'id' => $firstPage->id,
|
||||
'name' => $firstPage->name,
|
||||
'slug' => $firstPage->slug,
|
||||
'book_id' => $firstPage->book->id,
|
||||
'priority' => $firstPage->priority,
|
||||
]
|
||||
]]);
|
||||
}
|
||||
|
||||
public function test_create_endpoint()
|
||||
{
|
||||
$this->actingAsApiEditor();
|
||||
$book = Book::query()->first();
|
||||
$details = [
|
||||
'name' => 'My API page',
|
||||
'book_id' => $book->id,
|
||||
'html' => '<p>My new page content</p>',
|
||||
'tags' => [
|
||||
[
|
||||
'name' => 'tagname',
|
||||
'value' => 'tagvalue',
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
$resp = $this->postJson($this->baseEndpoint, $details);
|
||||
unset($details['html']);
|
||||
$resp->assertStatus(200);
|
||||
$newItem = Page::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
|
||||
$resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
|
||||
$this->assertDatabaseHas('tags', [
|
||||
'entity_id' => $newItem->id,
|
||||
'entity_type' => $newItem->getMorphClass(),
|
||||
'name' => 'tagname',
|
||||
'value' => 'tagvalue',
|
||||
]);
|
||||
$resp->assertSeeText('My new page content');
|
||||
$resp->assertJsonMissing(['book' => []]);
|
||||
$this->assertActivityExists('page_create', $newItem);
|
||||
}
|
||||
|
||||
public function test_page_name_needed_to_create()
|
||||
{
|
||||
$this->actingAsApiEditor();
|
||||
$book = Book::query()->first();
|
||||
$details = [
|
||||
'book_id' => $book->id,
|
||||
'html' => '<p>A page created via the API</p>',
|
||||
];
|
||||
|
||||
$resp = $this->postJson($this->baseEndpoint, $details);
|
||||
$resp->assertStatus(422);
|
||||
$resp->assertJson($this->validationResponse([
|
||||
"name" => ["The name field is required."]
|
||||
]));
|
||||
}
|
||||
|
||||
public function test_book_id_or_chapter_id_needed_to_create()
|
||||
{
|
||||
$this->actingAsApiEditor();
|
||||
$details = [
|
||||
'name' => 'My api page',
|
||||
'html' => '<p>A page created via the API</p>',
|
||||
];
|
||||
|
||||
$resp = $this->postJson($this->baseEndpoint, $details);
|
||||
$resp->assertStatus(422);
|
||||
$resp->assertJson($this->validationResponse([
|
||||
"book_id" => ["The book id field is required when chapter id is not present."],
|
||||
"chapter_id" => ["The chapter id field is required when book id is not present."]
|
||||
]));
|
||||
|
||||
$chapter = Chapter::visible()->first();
|
||||
$resp = $this->postJson($this->baseEndpoint, array_merge($details, ['chapter_id' => $chapter->id]));
|
||||
$resp->assertStatus(200);
|
||||
|
||||
$book = Book::visible()->first();
|
||||
$resp = $this->postJson($this->baseEndpoint, array_merge($details, ['book_id' => $book->id]));
|
||||
$resp->assertStatus(200);
|
||||
}
|
||||
|
||||
public function test_markdown_can_be_provided_for_create()
|
||||
{
|
||||
$this->actingAsApiEditor();
|
||||
$book = Book::visible()->first();
|
||||
$details = [
|
||||
'book_id' => $book->id,
|
||||
'name' => 'My api page',
|
||||
'markdown' => "# A new API page \n[link](https://example.com)",
|
||||
];
|
||||
|
||||
$resp = $this->postJson($this->baseEndpoint, $details);
|
||||
$resp->assertJson(['markdown' => $details['markdown']]);
|
||||
|
||||
$respHtml = $resp->json('html');
|
||||
$this->assertStringContainsString('new API page</h1>', $respHtml);
|
||||
$this->assertStringContainsString('link</a>', $respHtml);
|
||||
$this->assertStringContainsString('href="https://example.com"', $respHtml);
|
||||
}
|
||||
|
||||
public function test_read_endpoint()
|
||||
{
|
||||
$this->actingAsApiEditor();
|
||||
$page = Page::visible()->first();
|
||||
|
||||
$resp = $this->getJson($this->baseEndpoint . "/{$page->id}");
|
||||
$resp->assertStatus(200);
|
||||
$resp->assertJson([
|
||||
'id' => $page->id,
|
||||
'slug' => $page->slug,
|
||||
'created_by' => [
|
||||
'name' => $page->createdBy->name,
|
||||
],
|
||||
'book_id' => $page->book_id,
|
||||
'updated_by' => [
|
||||
'name' => $page->createdBy->name,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_read_endpoint_provides_rendered_html()
|
||||
{
|
||||
$this->actingAsApiEditor();
|
||||
$page = Page::visible()->first();
|
||||
$page->html = "<p>testing</p><script>alert('danger')</script><h1>Hello</h1>";
|
||||
$page->save();
|
||||
|
||||
$resp = $this->getJson($this->baseEndpoint . "/{$page->id}");
|
||||
$html = $resp->json('html');
|
||||
$this->assertStringNotContainsString('script', $html);
|
||||
$this->assertStringContainsString('Hello', $html);
|
||||
$this->assertStringContainsString('testing', $html);
|
||||
}
|
||||
|
||||
public function test_update_endpoint()
|
||||
{
|
||||
$this->actingAsApiEditor();
|
||||
$page = Page::visible()->first();
|
||||
$details = [
|
||||
'name' => 'My updated API page',
|
||||
'html' => '<p>A page created via the API</p>',
|
||||
'tags' => [
|
||||
[
|
||||
'name' => 'freshtag',
|
||||
'value' => 'freshtagval',
|
||||
]
|
||||
],
|
||||
];
|
||||
|
||||
$resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
|
||||
$page->refresh();
|
||||
|
||||
$resp->assertStatus(200);
|
||||
unset($details['html']);
|
||||
$resp->assertJson(array_merge($details, [
|
||||
'id' => $page->id, 'slug' => $page->slug, 'book_id' => $page->book_id
|
||||
]));
|
||||
$this->assertActivityExists('page_update', $page);
|
||||
}
|
||||
|
||||
public function test_providing_new_chapter_id_on_update_will_move_page()
|
||||
{
|
||||
$this->actingAsApiEditor();
|
||||
$page = Page::visible()->first();
|
||||
$chapter = Chapter::visible()->where('book_id', '!=', $page->book_id)->first();
|
||||
$details = [
|
||||
'name' => 'My updated API page',
|
||||
'chapter_id' => $chapter->id,
|
||||
'html' => '<p>A page created via the API</p>',
|
||||
];
|
||||
|
||||
$resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
|
||||
$resp->assertStatus(200);
|
||||
$resp->assertJson([
|
||||
'chapter_id' => $chapter->id,
|
||||
'book_id' => $chapter->book_id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_providing_move_via_update_requires_page_create_permission_on_new_parent()
|
||||
{
|
||||
$this->actingAsApiEditor();
|
||||
$page = Page::visible()->first();
|
||||
$chapter = Chapter::visible()->where('book_id', '!=', $page->book_id)->first();
|
||||
$this->setEntityRestrictions($chapter, ['view'], [$this->getEditor()->roles()->first()]);
|
||||
$details = [
|
||||
'name' => 'My updated API page',
|
||||
'chapter_id' => $chapter->id,
|
||||
'html' => '<p>A page created via the API</p>',
|
||||
];
|
||||
|
||||
$resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
|
||||
$resp->assertStatus(403);
|
||||
}
|
||||
|
||||
public function test_delete_endpoint()
|
||||
{
|
||||
$this->actingAsApiEditor();
|
||||
$page = Page::visible()->first();
|
||||
$resp = $this->deleteJson($this->baseEndpoint . "/{$page->id}");
|
||||
|
||||
$resp->assertStatus(204);
|
||||
$this->assertActivityExists('page_delete', $page);
|
||||
}
|
||||
|
||||
public function test_export_html_endpoint()
|
||||
{
|
||||
$this->actingAsApiEditor();
|
||||
$page = Page::visible()->first();
|
||||
|
||||
$resp = $this->get($this->baseEndpoint . "/{$page->id}/export/html");
|
||||
$resp->assertStatus(200);
|
||||
$resp->assertSee($page->name);
|
||||
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.html"');
|
||||
}
|
||||
|
||||
public function test_export_plain_text_endpoint()
|
||||
{
|
||||
$this->actingAsApiEditor();
|
||||
$page = Page::visible()->first();
|
||||
|
||||
$resp = $this->get($this->baseEndpoint . "/{$page->id}/export/plaintext");
|
||||
$resp->assertStatus(200);
|
||||
$resp->assertSee($page->name);
|
||||
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.txt"');
|
||||
}
|
||||
|
||||
public function test_export_pdf_endpoint()
|
||||
{
|
||||
$this->actingAsApiEditor();
|
||||
$page = Page::visible()->first();
|
||||
|
||||
$resp = $this->get($this->baseEndpoint . "/{$page->id}/export/pdf");
|
||||
$resp->assertStatus(200);
|
||||
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.pdf"');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user