use BookStack\Activity\CommentRepo;
use BookStack\Entities\Models\Page;
+use BookStack\Entities\Queries\PageQueries;
use BookStack\Http\Controller;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class CommentController extends Controller
{
public function __construct(
- protected CommentRepo $commentRepo
+ protected CommentRepo $commentRepo,
+ protected PageQueries $pageQueries,
) {
}
'parent_id' => ['nullable', 'integer'],
]);
- $page = Page::visible()->find($pageId);
+ $page = $this->pageQueries->findVisibleById($pageId);
if ($page === null) {
return response('Not found', 404);
}
namespace BookStack\Console\Commands;
use BookStack\Entities\Models\Bookshelf;
+use BookStack\Entities\Queries\BookshelfQueries;
use BookStack\Entities\Tools\PermissionsUpdater;
use Illuminate\Console\Command;
/**
* Execute the console command.
*/
- public function handle(PermissionsUpdater $permissionsUpdater): int
+ public function handle(PermissionsUpdater $permissionsUpdater, BookshelfQueries $queries): int
{
$shelfSlug = $this->option('slug');
$cascadeAll = $this->option('all');
return 0;
}
- $shelves = Bookshelf::query()->get(['id']);
+ $shelves = $queries->start()->get(['id']);
}
if ($shelfSlug) {
- $shelves = Bookshelf::query()->where('slug', '=', $shelfSlug)->get(['id']);
+ $shelves = $queries->start()->where('slug', '=', $shelfSlug)->get(['id']);
if ($shelves->count() === 0) {
$this->info('No shelves found with the given slug.');
}
use BookStack\Activity\Tools\UserEntityWatchOptions;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Queries\BookQueries;
+use BookStack\Entities\Queries\BookshelfQueries;
use BookStack\Entities\Repos\BookRepo;
use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Tools\Cloner;
protected ShelfContext $shelfContext,
protected BookRepo $bookRepo,
protected BookQueries $queries,
+ protected BookshelfQueries $shelfQueries,
protected ReferenceFetcher $referenceFetcher,
) {
}
$bookshelf = null;
if ($shelfSlug !== null) {
- $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
+ $bookshelf = $this->shelfQueries->findVisibleBySlugOrFail($shelfSlug);
$this->checkOwnablePermission('bookshelf-update', $bookshelf);
}
$bookshelf = null;
if ($shelfSlug !== null) {
- $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
+ $bookshelf = $this->shelfQueries->findVisibleBySlugOrFail($shelfSlug);
$this->checkOwnablePermission('bookshelf-update', $bookshelf);
}
namespace BookStack\Entities\Controllers;
use BookStack\Entities\Models\Bookshelf;
+use BookStack\Entities\Queries\BookshelfQueries;
use BookStack\Entities\Repos\BookshelfRepo;
use BookStack\Http\ApiController;
use Exception;
class BookshelfApiController extends ApiController
{
public function __construct(
- protected BookshelfRepo $bookshelfRepo
+ protected BookshelfRepo $bookshelfRepo,
+ protected BookshelfQueries $queries,
) {
}
*/
public function list()
{
- $shelves = Bookshelf::visible();
+ $shelves = $this->queries->visibleForList();
return $this->apiListingResponse($shelves, [
'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
*/
public function read(string $id)
{
- $shelf = Bookshelf::visible()->findOrFail($id);
+ $shelf = $this->queries->findVisibleByIdOrFail(intval($id));
$shelf = $this->forJsonDisplay($shelf);
$shelf->load([
'createdBy', 'updatedBy', 'ownedBy',
*/
public function update(Request $request, string $id)
{
- $shelf = Bookshelf::visible()->findOrFail($id);
+ $shelf = $this->queries->findVisibleByIdOrFail(intval($id));
$this->checkOwnablePermission('bookshelf-update', $shelf);
$requestData = $this->validate($request, $this->rules()['update']);
*/
public function delete(string $id)
{
- $shelf = Bookshelf::visible()->findOrFail($id);
+ $shelf = $this->queries->findVisibleByIdOrFail(intval($id));
$this->checkOwnablePermission('bookshelf-delete', $shelf);
$this->bookshelfRepo->destroy($shelf);
namespace BookStack\Entities\Controllers;
use BookStack\Entities\Models\Chapter;
+use BookStack\Entities\Queries\ChapterQueries;
use BookStack\Entities\Tools\ExportFormatter;
use BookStack\Http\ApiController;
use Throwable;
class ChapterExportApiController extends ApiController
{
- protected $exportFormatter;
-
- /**
- * ChapterExportController constructor.
- */
- public function __construct(ExportFormatter $exportFormatter)
- {
- $this->exportFormatter = $exportFormatter;
+ public function __construct(
+ protected ExportFormatter $exportFormatter,
+ protected ChapterQueries $queries,
+ ) {
$this->middleware('can:content-export');
}
*/
public function exportPdf(int $id)
{
- $chapter = Chapter::visible()->findOrFail($id);
+ $chapter = $this->queries->findVisibleByIdOrFail($id);
$pdfContent = $this->exportFormatter->chapterToPdf($chapter);
return $this->download()->directly($pdfContent, $chapter->slug . '.pdf');
*/
public function exportHtml(int $id)
{
- $chapter = Chapter::visible()->findOrFail($id);
+ $chapter = $this->queries->findVisibleByIdOrFail($id);
$htmlContent = $this->exportFormatter->chapterToContainedHtml($chapter);
return $this->download()->directly($htmlContent, $chapter->slug . '.html');
*/
public function exportPlainText(int $id)
{
- $chapter = Chapter::visible()->findOrFail($id);
+ $chapter = $this->queries->findVisibleByIdOrFail($id);
$textContent = $this->exportFormatter->chapterToPlainText($chapter);
return $this->download()->directly($textContent, $chapter->slug . '.txt');
*/
public function exportMarkdown(int $id)
{
- $chapter = Chapter::visible()->findOrFail($id);
+ $chapter = $this->queries->findVisibleByIdOrFail($id);
$markdown = $this->exportFormatter->chapterToMarkdown($chapter);
return $this->download()->directly($markdown, $chapter->slug . '.md');
$query->scopes('visible');
};
- $pages = Page::visible()->with(['updatedBy', 'book' => $visibleBelongsScope, 'chapter' => $visibleBelongsScope])
+ $pages = $this->queries->visibleForList()
+ ->addSelect('updated_by')
+ ->with(['updatedBy', 'book' => $visibleBelongsScope, 'chapter' => $visibleBelongsScope])
->orderBy('updated_at', 'desc')
->paginate(20)
->setPath(url('/pages/recently-updated'));
namespace BookStack\Entities\Controllers;
use BookStack\Entities\Models\Page;
+use BookStack\Entities\Queries\PageQueries;
use BookStack\Entities\Tools\ExportFormatter;
use BookStack\Http\ApiController;
use Throwable;
class PageExportApiController extends ApiController
{
- protected $exportFormatter;
-
- public function __construct(ExportFormatter $exportFormatter)
- {
- $this->exportFormatter = $exportFormatter;
+ public function __construct(
+ protected ExportFormatter $exportFormatter,
+ protected PageQueries $queries,
+ ) {
$this->middleware('can:content-export');
}
*/
public function exportPdf(int $id)
{
- $page = Page::visible()->findOrFail($id);
+ $page = $this->queries->findVisibleByIdOrFail($id);
$pdfContent = $this->exportFormatter->pageToPdf($page);
return $this->download()->directly($pdfContent, $page->slug . '.pdf');
*/
public function exportHtml(int $id)
{
- $page = Page::visible()->findOrFail($id);
+ $page = $this->queries->findVisibleByIdOrFail($id);
$htmlContent = $this->exportFormatter->pageToContainedHtml($page);
return $this->download()->directly($htmlContent, $page->slug . '.html');
*/
public function exportPlainText(int $id)
{
- $page = Page::visible()->findOrFail($id);
+ $page = $this->queries->findVisibleByIdOrFail($id);
$textContent = $this->exportFormatter->pageToPlainText($page);
return $this->download()->directly($textContent, $page->slug . '.txt');
*/
public function exportMarkdown(int $id)
{
- $page = Page::visible()->findOrFail($id);
+ $page = $this->queries->findVisibleByIdOrFail($id);
$markdown = $this->exportFormatter->pageToMarkdown($page);
return $this->download()->directly($markdown, $page->slug . '.md');
return $this->start()->scopes('visible')->find($id);
}
+ public function findVisibleByIdOrFail(int $id): Bookshelf
+ {
+ $shelf = $this->findVisibleById($id);
+
+ if (is_null($shelf)) {
+ throw new NotFoundException(trans('errors.bookshelf_not_found'));
+ }
+
+ return $shelf;
+ }
+
public function findVisibleBySlugOrFail(string $slug): Bookshelf
{
/** @var ?Bookshelf $shelf */
use BookStack\Entities\Models\HasCoverImage;
use BookStack\Entities\Models\HasHtmlDescription;
use BookStack\Entities\Models\Page;
+use BookStack\Entities\Queries\PageQueries;
use BookStack\Exceptions\ImageUploadException;
use BookStack\References\ReferenceStore;
use BookStack\References\ReferenceUpdater;
protected ImageRepo $imageRepo,
protected ReferenceUpdater $referenceUpdater,
protected ReferenceStore $referenceStore,
+ protected PageQueries $pageQueries,
) {
}
return;
}
- $templateExists = Page::query()->visible()
- ->where('template', '=', true)
+ $templateExists = $this->pageQueries->visibleTemplates()
->where('id', '=', $templateId)
->exists();
namespace BookStack\Entities\Tools;
use BookStack\Entities\Models\Page;
+use BookStack\Entities\Queries\PageQueries;
use BookStack\Entities\Tools\Markdown\MarkdownToHtml;
use BookStack\Exceptions\ImageUploadException;
use BookStack\Facades\Theme;
class PageContent
{
+ protected PageQueries $pageQueries;
+
public function __construct(
protected Page $page
) {
+ $this->pageQueries = app()->make(PageQueries::class);
}
/**
return PageIncludeContent::fromHtmlAndTag('', $tag);
}
- $matchedPage = Page::visible()->find($tag->getPageId());
+ $matchedPage = $this->pageQueries->findVisibleById($tag->getPageId());
$content = PageIncludeContent::fromHtmlAndTag($matchedPage->html ?? '', $tag);
if (Theme::hasListeners(ThemeEvents::PAGE_INCLUDE_PARSE)) {
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
+use BookStack\Entities\Queries\BookshelfQueries;
class ShelfContext
{
- protected $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id';
+ protected string $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id';
+
+ public function __construct(
+ protected BookshelfQueries $shelfQueries,
+ ) {
+ }
/**
* Get the current bookshelf context for the given book.
return null;
}
- /** @var Bookshelf $shelf */
- $shelf = Bookshelf::visible()->find($contextBookshelfId);
+ $shelf = $this->shelfQueries->findVisibleById($contextBookshelfId);
$shelfContainsBook = $shelf && $shelf->contains($book);
return $shelfContainsBook ? $shelf : null;
/**
* Store the current contextual shelf ID.
*/
- public function setShelfContext(int $shelfId)
+ public function setShelfContext(int $shelfId): void
{
session()->put($this->KEY_SHELF_CONTEXT_ID, $shelfId);
}
/**
* Clear the session stored shelf context id.
*/
- public function clearShelfContext()
+ public function clearShelfContext(): void
{
session()->forget($this->KEY_SHELF_CONTEXT_ID);
}
namespace BookStack\Search;
use BookStack\Entities\Models\Page;
+use BookStack\Entities\Queries\PageQueries;
use BookStack\Entities\Queries\Popular;
use BookStack\Entities\Tools\SiblingFetcher;
use BookStack\Http\Controller;
class SearchController extends Controller
{
public function __construct(
- protected SearchRunner $searchRunner
+ protected SearchRunner $searchRunner,
+ protected PageQueries $pageQueries,
) {
}
$searchOptions->setFilter('is_template');
$entities = $this->searchRunner->searchEntities($searchOptions, 'page', 1, 20)['results'];
} else {
- $entities = Page::visible()
- ->where('template', '=', true)
+ $entities = $this->pageQueries->visibleTemplates()
->where('draft', '=', false)
->orderBy('updated_at', 'desc')
->take(20)
- ->get(Page::$listAttributes);
+ ->get();
}
return view('search.parts.entity-selector-list', [
namespace BookStack\Uploads\Controllers;
use BookStack\Entities\Models\Page;
+use BookStack\Entities\Queries\PageQueries;
use BookStack\Exceptions\FileUploadException;
use BookStack\Http\ApiController;
use BookStack\Uploads\Attachment;
class AttachmentApiController extends ApiController
{
public function __construct(
- protected AttachmentService $attachmentService
+ protected AttachmentService $attachmentService,
+ protected PageQueries $pageQueries,
) {
}
$requestData = $this->validate($request, $this->rules()['create']);
$pageId = $request->get('uploaded_to');
- $page = Page::visible()->findOrFail($pageId);
+ $page = $this->pageQueries->findVisibleByIdOrFail($pageId);
$this->checkOwnablePermission('page-update', $page);
if ($request->hasFile('file')) {
$page = $attachment->page;
if ($requestData['uploaded_to'] ?? false) {
$pageId = $request->get('uploaded_to');
- $page = Page::visible()->findOrFail($pageId);
+ $page = $this->pageQueries->findVisibleByIdOrFail($pageId);
$attachment->uploaded_to = $requestData['uploaded_to'];
}
namespace BookStack\Uploads\Controllers;
use BookStack\Entities\Models\Page;
+use BookStack\Entities\Queries\PageQueries;
use BookStack\Http\ApiController;
use BookStack\Uploads\Image;
use BookStack\Uploads\ImageRepo;
public function __construct(
protected ImageRepo $imageRepo,
protected ImageResizer $imageResizer,
+ protected PageQueries $pageQueries,
) {
}
{
$this->checkPermission('image-create-all');
$data = $this->validate($request, $this->rules()['create']);
- Page::visible()->findOrFail($data['uploaded_to']);
+ $page = $this->pageQueries->findVisibleByIdOrFail($data['uploaded_to']);
- $image = $this->imageRepo->saveNew($data['image'], $data['type'], $data['uploaded_to']);
+ $image = $this->imageRepo->saveNew($data['image'], $data['type'], $page->id);
if (isset($data['name'])) {
$image->refresh();
namespace BookStack\Uploads;
use BookStack\Entities\Models\Page;
+use BookStack\Entities\Queries\PageQueries;
use BookStack\Exceptions\ImageUploadException;
use BookStack\Permissions\PermissionApplicator;
use Exception;
protected ImageService $imageService,
protected PermissionApplicator $permissions,
protected ImageResizer $imageResizer,
+ protected PageQueries $pageQueries,
) {
}
*/
public function getEntityFiltered(
string $type,
- string $filterType = null,
- int $page = 0,
- int $pageSize = 24,
- int $uploadedTo = null,
- string $search = null
+ ?string $filterType,
+ int $page,
+ int $pageSize,
+ int $uploadedTo,
+ ?string $search
): array {
- /** @var Page $contextPage */
- $contextPage = Page::visible()->findOrFail($uploadedTo);
+ $contextPage = $this->pageQueries->findVisibleByIdOrFail($uploadedTo);
$parentFilter = null;
if ($filterType === 'book' || $filterType === 'page') {
*/
public function getPagesUsingImage(Image $image): array
{
- $pages = Page::visible()
+ $pages = $this->pageQueries->visibleForList()
->where('html', 'like', '%' . $image->url . '%')
- ->get(['id', 'name', 'slug', 'book_id']);
+ ->get();
foreach ($pages as $page) {
$page->setAttribute('url', $page->getUrl());