Queries: Moved out or removed some class-level items

Also ran auto-removal of unused imports across app folder.
This commit is contained in:
Dan Brown 2024-02-07 22:41:45 +00:00
parent 546cfb0dcc
commit b77ab6f3af
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
29 changed files with 85 additions and 107 deletions

View File

@ -3,7 +3,6 @@
namespace BookStack\Activity\Controllers;
use BookStack\Activity\CommentRepo;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Queries\PageQueries;
use BookStack\Http\Controller;
use Illuminate\Http\Request;

View File

@ -7,7 +7,6 @@ use Exception;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use Illuminate\Validation\Rules\Password;

View File

@ -3,7 +3,6 @@
namespace BookStack\App;
use BookStack\Activity\ActivityQueries;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Queries\EntityQueries;
use BookStack\Entities\Queries\RecentlyViewed;

View File

@ -4,7 +4,6 @@ namespace BookStack\App\Providers;
use BookStack\Theming\ThemeEvents;
use BookStack\Theming\ThemeService;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
class ThemeServiceProvider extends ServiceProvider

View File

@ -2,7 +2,6 @@
namespace BookStack\Console\Commands;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Queries\BookshelfQueries;
use BookStack\Entities\Tools\PermissionsUpdater;
use Illuminate\Console\Command;

View File

@ -6,7 +6,6 @@ use BookStack\Activity\ActivityQueries;
use BookStack\Activity\ActivityType;
use BookStack\Activity\Models\View;
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;

View File

@ -2,7 +2,6 @@
namespace BookStack\Entities\Controllers;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Queries\ChapterQueries;
use BookStack\Entities\Tools\ExportFormatter;
use BookStack\Http\ApiController;

View File

@ -7,7 +7,6 @@ use BookStack\Activity\Tools\CommentTree;
use BookStack\Activity\Tools\UserEntityWatchOptions;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Queries\EntityQueries;
use BookStack\Entities\Queries\PageQueries;
use BookStack\Entities\Repos\PageRepo;

View File

@ -2,7 +2,6 @@
namespace BookStack\Entities\Controllers;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Queries\PageQueries;
use BookStack\Entities\Tools\ExportFormatter;
use BookStack\Http\ApiController;

View File

@ -13,24 +13,9 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
* @property int $priority
* @property string $book_slug
* @property Book $book
*
* @method Builder whereSlugs(string $bookSlug, string $childSlug)
*/
abstract class BookChild extends Entity
{
/**
* Scope a query to find items where the child has the given childSlug
* where its parent has the bookSlug.
*/
public function scopeWhereSlugs(Builder $query, string $bookSlug, string $childSlug)
{
return $query->with('book')
->whereHas('book', function (Builder $query) use ($bookSlug) {
$query->where('slug', '=', $bookSlug);
})
->where('slug', '=', $childSlug);
}
/**
* Get the book this page sits in.
*/

View File

@ -69,13 +69,4 @@ class Chapter extends BookChild
->orderBy('priority', 'asc')
->get();
}
/**
* Get a visible chapter by its book and page slugs.
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public static function getBySlugs(string $bookSlug, string $chapterSlug): self
{
return static::visible()->whereSlugs($bookSlug, $chapterSlug)->firstOrFail();
}
}

View File

@ -32,9 +32,6 @@ class Page extends BookChild
{
use HasFactory;
public static $listAttributes = ['name', 'id', 'slug', 'book_id', 'chapter_id', 'draft', 'template', 'text', 'created_at', 'updated_at', 'priority'];
public static $contentAttributes = ['name', 'id', 'slug', 'book_id', 'chapter_id', 'draft', 'template', 'html', 'text', 'created_at', 'updated_at', 'priority'];
protected $fillable = ['name', 'priority'];
public string $textField = 'text';
@ -145,13 +142,4 @@ class Page extends BookChild
return $refreshed;
}
/**
* Get a visible page by its book and page slugs.
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public static function getBySlugs(string $bookSlug, string $pageSlug): self
{
return static::visible()->whereSlugs($bookSlug, $pageSlug)->firstOrFail();
}
}

View File

@ -8,6 +8,10 @@ use Illuminate\Database\Eloquent\Builder;
class BookQueries implements ProvidesEntityQueries
{
protected static array $listAttributes = [
'id', 'slug', 'name', 'description', 'created_at', 'updated_at', 'image_id'
];
public function start(): Builder
{
return Book::query();
@ -40,7 +44,8 @@ class BookQueries implements ProvidesEntityQueries
public function visibleForList(): Builder
{
return $this->start()->scopes('visible');
return $this->start()->scopes('visible')
->select(static::$listAttributes);
}
public function visibleForListWithCover(): Builder

View File

@ -8,6 +8,10 @@ use Illuminate\Database\Eloquent\Builder;
class BookshelfQueries implements ProvidesEntityQueries
{
protected static array $listAttributes = [
'id', 'slug', 'name', 'description', 'created_at', 'updated_at', 'image_id'
];
public function start(): Builder
{
return Bookshelf::query();
@ -46,7 +50,7 @@ class BookshelfQueries implements ProvidesEntityQueries
public function visibleForList(): Builder
{
return $this->start()->scopes('visible');
return $this->start()->scopes('visible')->select(static::$listAttributes);
}
public function visibleForListWithCover(): Builder

View File

@ -10,8 +10,7 @@ class ChapterQueries implements ProvidesEntityQueries
{
protected static array $listAttributes = [
'id', 'slug', 'name', 'description', 'priority',
'created_at', 'updated_at',
'created_by', 'updated_by', 'owned_by',
'created_at', 'updated_at'
];
public function start(): Builder

View File

@ -3,6 +3,8 @@
namespace BookStack\Entities\Queries;
use BookStack\Entities\Models\Entity;
use Illuminate\Database\Eloquent\Builder;
use InvalidArgumentException;
class EntityQueries
{
@ -25,9 +27,25 @@ class EntityQueries
$explodedId = explode(':', $identifier);
$entityType = $explodedId[0];
$entityId = intval($explodedId[1]);
$queries = $this->getQueriesForType($entityType);
return $queries->findVisibleById($entityId);
}
/**
* Start a query of visible entities of the given type,
* suitable for listing display.
*/
public function visibleForList(string $entityType): Builder
{
$queries = $this->getQueriesForType($entityType);
return $queries->visibleForList();
}
protected function getQueriesForType(string $type): ProvidesEntityQueries
{
/** @var ?ProvidesEntityQueries $queries */
$queries = match ($entityType) {
$queries = match ($type) {
'page' => $this->pages,
'chapter' => $this->chapters,
'book' => $this->books,
@ -36,9 +54,9 @@ class EntityQueries
};
if (is_null($queries)) {
return null;
throw new InvalidArgumentException("No entity query class configured for {$type}");
}
return $queries->findVisibleById($entityId);
return $queries;
}
}

View File

@ -8,6 +8,15 @@ use Illuminate\Database\Eloquent\Builder;
class PageQueries implements ProvidesEntityQueries
{
protected static array $contentAttributes = [
'name', 'id', 'slug', 'book_id', 'chapter_id', 'draft',
'template', 'html', 'text', 'created_at', 'updated_at', 'priority'
];
protected static array $listAttributes = [
'name', 'id', 'slug', 'book_id', 'chapter_id', 'draft',
'template', 'text', 'created_at', 'updated_at', 'priority'
];
public function start(): Builder
{
return Page::query();
@ -60,22 +69,14 @@ class PageQueries implements ProvidesEntityQueries
{
return $this->start()
->scopes('visible')
->select(array_merge(Page::$listAttributes, ['book_slug' => function ($builder) {
$builder->select('slug')
->from('books')
->whereColumn('books.id', '=', 'pages.book_id');
}]));
->select($this->mergeBookSlugForSelect(static::$listAttributes));
}
public function visibleWithContents(): Builder
{
return $this->start()
->scopes('visible')
->select(array_merge(Page::$contentAttributes, ['book_slug' => function ($builder) {
$builder->select('slug')
->from('books')
->whereColumn('books.id', '=', 'pages.book_id');
}]));
->select($this->mergeBookSlugForSelect(static::$contentAttributes));
}
public function currentUserDraftsForList(): Builder
@ -90,4 +91,13 @@ class PageQueries implements ProvidesEntityQueries
return $this->visibleForList()
->where('template', '=', true);
}
protected function mergeBookSlugForSelect(array $columns): array
{
return array_merge($columns, ['book_slug' => function ($builder) {
$builder->select('slug')
->from('books')
->whereColumn('books.id', '=', 'pages.book_id');
}]);
}
}

View File

@ -16,6 +16,19 @@ use Illuminate\Database\Eloquent\Builder;
*/
interface ProvidesEntityQueries
{
/**
* Start a new query for this entity type.
*/
public function start(): Builder;
/**
* Find the entity of the given ID, or return null if not found.
*/
public function findVisibleById(int $id): ?Entity;
/**
* Start a query for items that are visible, with selection
* configured for list display of this item.
*/
public function visibleForList(): Builder;
}

View File

@ -8,7 +8,6 @@ use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Entity;
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;

View File

@ -4,7 +4,6 @@ namespace BookStack\Permissions;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\BookChild;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\Page;

View File

@ -2,10 +2,6 @@
namespace BookStack\References;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Queries\EntityQueries;
use BookStack\Http\Controller;

View File

@ -2,7 +2,6 @@
namespace BookStack\Search;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Queries\PageQueries;
use BookStack\Entities\Queries\Popular;
use BookStack\Entities\Tools\SiblingFetcher;

View File

@ -3,9 +3,9 @@
namespace BookStack\Search;
use BookStack\Entities\EntityProvider;
use BookStack\Entities\Models\BookChild;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Queries\EntityQueries;
use BookStack\Permissions\PermissionApplicator;
use BookStack\Users\Models\User;
use Illuminate\Database\Connection;
@ -20,9 +20,6 @@ use SplObjectStorage;
class SearchRunner
{
protected EntityProvider $entityProvider;
protected PermissionApplicator $permissions;
/**
* Acceptable operators to be used in a query.
*
@ -38,10 +35,11 @@ class SearchRunner
*/
protected $termAdjustmentCache;
public function __construct(EntityProvider $entityProvider, PermissionApplicator $permissions)
{
$this->entityProvider = $entityProvider;
$this->permissions = $permissions;
public function __construct(
protected EntityProvider $entityProvider,
protected PermissionApplicator $permissions,
protected EntityQueries $entityQueries,
) {
$this->termAdjustmentCache = new SplObjectStorage();
}
@ -72,10 +70,9 @@ class SearchRunner
continue;
}
$entityModelInstance = $this->entityProvider->get($entityType);
$searchQuery = $this->buildQuery($searchOpts, $entityModelInstance);
$searchQuery = $this->buildQuery($searchOpts, $entityType);
$entityTotal = $searchQuery->count();
$searchResults = $this->getPageOfDataFromQuery($searchQuery, $entityModelInstance, $page, $count);
$searchResults = $this->getPageOfDataFromQuery($searchQuery, $entityType, $page, $count);
if ($entityTotal > ($page * $count)) {
$hasMore = true;
@ -108,8 +105,7 @@ class SearchRunner
continue;
}
$entityModelInstance = $this->entityProvider->get($entityType);
$search = $this->buildQuery($opts, $entityModelInstance)->where('book_id', '=', $bookId)->take(20)->get();
$search = $this->buildQuery($opts, $entityType)->where('book_id', '=', $bookId)->take(20)->get();
$results = $results->merge($search);
}
@ -122,8 +118,7 @@ class SearchRunner
public function searchChapter(int $chapterId, string $searchString): Collection
{
$opts = SearchOptions::fromString($searchString);
$entityModelInstance = $this->entityProvider->get('page');
$pages = $this->buildQuery($opts, $entityModelInstance)->where('chapter_id', '=', $chapterId)->take(20)->get();
$pages = $this->buildQuery($opts, 'page')->where('chapter_id', '=', $chapterId)->take(20)->get();
return $pages->sortByDesc('score');
}
@ -131,17 +126,17 @@ class SearchRunner
/**
* Get a page of result data from the given query based on the provided page parameters.
*/
protected function getPageOfDataFromQuery(EloquentBuilder $query, Entity $entityModelInstance, int $page = 1, int $count = 20): EloquentCollection
protected function getPageOfDataFromQuery(EloquentBuilder $query, string $entityType, int $page = 1, int $count = 20): EloquentCollection
{
$relations = ['tags'];
if ($entityModelInstance instanceof BookChild) {
if ($entityType === 'page' || $entityType === 'chapter') {
$relations['book'] = function (BelongsTo $query) {
$query->scopes('visible');
};
}
if ($entityModelInstance instanceof Page) {
if ($entityType === 'page') {
$relations['chapter'] = function (BelongsTo $query) {
$query->scopes('visible');
};
@ -157,18 +152,13 @@ class SearchRunner
/**
* Create a search query for an entity.
*/
protected function buildQuery(SearchOptions $searchOpts, Entity $entityModelInstance): EloquentBuilder
protected function buildQuery(SearchOptions $searchOpts, string $entityType): EloquentBuilder
{
$entityQuery = $entityModelInstance->newQuery()->scopes('visible');
if ($entityModelInstance instanceof Page) {
$entityQuery->select(array_merge($entityModelInstance::$listAttributes, ['owned_by']));
} else {
$entityQuery->select(['*']);
}
$entityModelInstance = $this->entityProvider->get($entityType);
$entityQuery = $this->entityQueries->visibleForList($entityType);
// Handle normal search terms
$this->applyTermSearch($entityQuery, $searchOpts, $entityModelInstance);
$this->applyTermSearch($entityQuery, $searchOpts, $entityType);
// Handle exact term matching
foreach ($searchOpts->exacts as $inputTerm) {
@ -198,7 +188,7 @@ class SearchRunner
/**
* For the given search query, apply the queries for handling the regular search terms.
*/
protected function applyTermSearch(EloquentBuilder $entityQuery, SearchOptions $options, Entity $entity): void
protected function applyTermSearch(EloquentBuilder $entityQuery, SearchOptions $options, string $entityType): void
{
$terms = $options->searches;
if (count($terms) === 0) {
@ -216,7 +206,7 @@ class SearchRunner
$subQuery->addBinding($scoreSelect['bindings'], 'select');
$subQuery->where('entity_type', '=', $entity->getMorphClass());
$subQuery->where('entity_type', '=', $entityType);
$subQuery->where(function (Builder $query) use ($terms) {
foreach ($terms as $inputTerm) {
$inputTerm = str_replace('\\', '\\\\', $inputTerm);

View File

@ -4,7 +4,6 @@ namespace BookStack\Uploads;
use BookStack\Exceptions\FileUploadException;
use Exception;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Contracts\Filesystem\Filesystem as Storage;
use Illuminate\Filesystem\FilesystemManager;
use Illuminate\Support\Facades\Log;

View File

@ -2,7 +2,6 @@
namespace BookStack\Uploads\Controllers;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Queries\PageQueries;
use BookStack\Exceptions\FileUploadException;
use BookStack\Http\ApiController;

View File

@ -8,8 +8,6 @@ use BookStack\Uploads\ImageRepo;
use BookStack\Uploads\ImageResizer;
use BookStack\Util\OutOfMemoryHandler;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
class GalleryImageController extends Controller

View File

@ -2,7 +2,6 @@
namespace BookStack\Uploads\Controllers;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Queries\PageQueries;
use BookStack\Http\ApiController;
use BookStack\Uploads\Image;

View File

@ -2,7 +2,6 @@
namespace BookStack\Uploads;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Queries\PageQueries;
use BookStack\Exceptions\ImageUploadException;
use BookStack\Permissions\PermissionApplicator;

View File

@ -2,9 +2,6 @@
namespace BookStack\Uploads;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Queries\EntityQueries;
use BookStack\Exceptions\ImageUploadException;
use Exception;