mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-11-22 14:33:20 +08:00
Queries: Moved out or removed some class-level items
Also ran auto-removal of unused imports across app folder.
This commit is contained in:
parent
546cfb0dcc
commit
b77ab6f3af
|
@ -3,7 +3,6 @@
|
||||||
namespace BookStack\Activity\Controllers;
|
namespace BookStack\Activity\Controllers;
|
||||||
|
|
||||||
use BookStack\Activity\CommentRepo;
|
use BookStack\Activity\CommentRepo;
|
||||||
use BookStack\Entities\Models\Page;
|
|
||||||
use BookStack\Entities\Queries\PageQueries;
|
use BookStack\Entities\Queries\PageQueries;
|
||||||
use BookStack\Http\Controller;
|
use BookStack\Http\Controller;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
|
@ -7,7 +7,6 @@ use Exception;
|
||||||
use Illuminate\Contracts\Container\BindingResolutionException;
|
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Illuminate\Validation\Rules\Password;
|
use Illuminate\Validation\Rules\Password;
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
namespace BookStack\App;
|
namespace BookStack\App;
|
||||||
|
|
||||||
use BookStack\Activity\ActivityQueries;
|
use BookStack\Activity\ActivityQueries;
|
||||||
use BookStack\Entities\Models\Book;
|
|
||||||
use BookStack\Entities\Models\Page;
|
use BookStack\Entities\Models\Page;
|
||||||
use BookStack\Entities\Queries\EntityQueries;
|
use BookStack\Entities\Queries\EntityQueries;
|
||||||
use BookStack\Entities\Queries\RecentlyViewed;
|
use BookStack\Entities\Queries\RecentlyViewed;
|
||||||
|
|
|
@ -4,7 +4,6 @@ namespace BookStack\App\Providers;
|
||||||
|
|
||||||
use BookStack\Theming\ThemeEvents;
|
use BookStack\Theming\ThemeEvents;
|
||||||
use BookStack\Theming\ThemeService;
|
use BookStack\Theming\ThemeService;
|
||||||
use Illuminate\Support\Facades\Route;
|
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
class ThemeServiceProvider extends ServiceProvider
|
class ThemeServiceProvider extends ServiceProvider
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace BookStack\Console\Commands;
|
namespace BookStack\Console\Commands;
|
||||||
|
|
||||||
use BookStack\Entities\Models\Bookshelf;
|
|
||||||
use BookStack\Entities\Queries\BookshelfQueries;
|
use BookStack\Entities\Queries\BookshelfQueries;
|
||||||
use BookStack\Entities\Tools\PermissionsUpdater;
|
use BookStack\Entities\Tools\PermissionsUpdater;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
|
|
|
@ -6,7 +6,6 @@ use BookStack\Activity\ActivityQueries;
|
||||||
use BookStack\Activity\ActivityType;
|
use BookStack\Activity\ActivityType;
|
||||||
use BookStack\Activity\Models\View;
|
use BookStack\Activity\Models\View;
|
||||||
use BookStack\Activity\Tools\UserEntityWatchOptions;
|
use BookStack\Activity\Tools\UserEntityWatchOptions;
|
||||||
use BookStack\Entities\Models\Bookshelf;
|
|
||||||
use BookStack\Entities\Queries\BookQueries;
|
use BookStack\Entities\Queries\BookQueries;
|
||||||
use BookStack\Entities\Queries\BookshelfQueries;
|
use BookStack\Entities\Queries\BookshelfQueries;
|
||||||
use BookStack\Entities\Repos\BookRepo;
|
use BookStack\Entities\Repos\BookRepo;
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace BookStack\Entities\Controllers;
|
namespace BookStack\Entities\Controllers;
|
||||||
|
|
||||||
use BookStack\Entities\Models\Chapter;
|
|
||||||
use BookStack\Entities\Queries\ChapterQueries;
|
use BookStack\Entities\Queries\ChapterQueries;
|
||||||
use BookStack\Entities\Tools\ExportFormatter;
|
use BookStack\Entities\Tools\ExportFormatter;
|
||||||
use BookStack\Http\ApiController;
|
use BookStack\Http\ApiController;
|
||||||
|
|
|
@ -7,7 +7,6 @@ use BookStack\Activity\Tools\CommentTree;
|
||||||
use BookStack\Activity\Tools\UserEntityWatchOptions;
|
use BookStack\Activity\Tools\UserEntityWatchOptions;
|
||||||
use BookStack\Entities\Models\Book;
|
use BookStack\Entities\Models\Book;
|
||||||
use BookStack\Entities\Models\Chapter;
|
use BookStack\Entities\Models\Chapter;
|
||||||
use BookStack\Entities\Models\Page;
|
|
||||||
use BookStack\Entities\Queries\EntityQueries;
|
use BookStack\Entities\Queries\EntityQueries;
|
||||||
use BookStack\Entities\Queries\PageQueries;
|
use BookStack\Entities\Queries\PageQueries;
|
||||||
use BookStack\Entities\Repos\PageRepo;
|
use BookStack\Entities\Repos\PageRepo;
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace BookStack\Entities\Controllers;
|
namespace BookStack\Entities\Controllers;
|
||||||
|
|
||||||
use BookStack\Entities\Models\Page;
|
|
||||||
use BookStack\Entities\Queries\PageQueries;
|
use BookStack\Entities\Queries\PageQueries;
|
||||||
use BookStack\Entities\Tools\ExportFormatter;
|
use BookStack\Entities\Tools\ExportFormatter;
|
||||||
use BookStack\Http\ApiController;
|
use BookStack\Http\ApiController;
|
||||||
|
|
|
@ -13,24 +13,9 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
* @property int $priority
|
* @property int $priority
|
||||||
* @property string $book_slug
|
* @property string $book_slug
|
||||||
* @property Book $book
|
* @property Book $book
|
||||||
*
|
|
||||||
* @method Builder whereSlugs(string $bookSlug, string $childSlug)
|
|
||||||
*/
|
*/
|
||||||
abstract class BookChild extends Entity
|
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.
|
* Get the book this page sits in.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -69,13 +69,4 @@ class Chapter extends BookChild
|
||||||
->orderBy('priority', 'asc')
|
->orderBy('priority', 'asc')
|
||||||
->get();
|
->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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,9 +32,6 @@ class Page extends BookChild
|
||||||
{
|
{
|
||||||
use HasFactory;
|
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'];
|
protected $fillable = ['name', 'priority'];
|
||||||
|
|
||||||
public string $textField = 'text';
|
public string $textField = 'text';
|
||||||
|
@ -145,13 +142,4 @@ class Page extends BookChild
|
||||||
|
|
||||||
return $refreshed;
|
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,10 @@ use Illuminate\Database\Eloquent\Builder;
|
||||||
|
|
||||||
class BookQueries implements ProvidesEntityQueries
|
class BookQueries implements ProvidesEntityQueries
|
||||||
{
|
{
|
||||||
|
protected static array $listAttributes = [
|
||||||
|
'id', 'slug', 'name', 'description', 'created_at', 'updated_at', 'image_id'
|
||||||
|
];
|
||||||
|
|
||||||
public function start(): Builder
|
public function start(): Builder
|
||||||
{
|
{
|
||||||
return Book::query();
|
return Book::query();
|
||||||
|
@ -40,7 +44,8 @@ class BookQueries implements ProvidesEntityQueries
|
||||||
|
|
||||||
public function visibleForList(): Builder
|
public function visibleForList(): Builder
|
||||||
{
|
{
|
||||||
return $this->start()->scopes('visible');
|
return $this->start()->scopes('visible')
|
||||||
|
->select(static::$listAttributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function visibleForListWithCover(): Builder
|
public function visibleForListWithCover(): Builder
|
||||||
|
|
|
@ -8,6 +8,10 @@ use Illuminate\Database\Eloquent\Builder;
|
||||||
|
|
||||||
class BookshelfQueries implements ProvidesEntityQueries
|
class BookshelfQueries implements ProvidesEntityQueries
|
||||||
{
|
{
|
||||||
|
protected static array $listAttributes = [
|
||||||
|
'id', 'slug', 'name', 'description', 'created_at', 'updated_at', 'image_id'
|
||||||
|
];
|
||||||
|
|
||||||
public function start(): Builder
|
public function start(): Builder
|
||||||
{
|
{
|
||||||
return Bookshelf::query();
|
return Bookshelf::query();
|
||||||
|
@ -46,7 +50,7 @@ class BookshelfQueries implements ProvidesEntityQueries
|
||||||
|
|
||||||
public function visibleForList(): Builder
|
public function visibleForList(): Builder
|
||||||
{
|
{
|
||||||
return $this->start()->scopes('visible');
|
return $this->start()->scopes('visible')->select(static::$listAttributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function visibleForListWithCover(): Builder
|
public function visibleForListWithCover(): Builder
|
||||||
|
|
|
@ -10,8 +10,7 @@ class ChapterQueries implements ProvidesEntityQueries
|
||||||
{
|
{
|
||||||
protected static array $listAttributes = [
|
protected static array $listAttributes = [
|
||||||
'id', 'slug', 'name', 'description', 'priority',
|
'id', 'slug', 'name', 'description', 'priority',
|
||||||
'created_at', 'updated_at',
|
'created_at', 'updated_at'
|
||||||
'created_by', 'updated_by', 'owned_by',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
public function start(): Builder
|
public function start(): Builder
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
namespace BookStack\Entities\Queries;
|
namespace BookStack\Entities\Queries;
|
||||||
|
|
||||||
use BookStack\Entities\Models\Entity;
|
use BookStack\Entities\Models\Entity;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
class EntityQueries
|
class EntityQueries
|
||||||
{
|
{
|
||||||
|
@ -25,9 +27,25 @@ class EntityQueries
|
||||||
$explodedId = explode(':', $identifier);
|
$explodedId = explode(':', $identifier);
|
||||||
$entityType = $explodedId[0];
|
$entityType = $explodedId[0];
|
||||||
$entityId = intval($explodedId[1]);
|
$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 */
|
/** @var ?ProvidesEntityQueries $queries */
|
||||||
$queries = match ($entityType) {
|
$queries = match ($type) {
|
||||||
'page' => $this->pages,
|
'page' => $this->pages,
|
||||||
'chapter' => $this->chapters,
|
'chapter' => $this->chapters,
|
||||||
'book' => $this->books,
|
'book' => $this->books,
|
||||||
|
@ -36,9 +54,9 @@ class EntityQueries
|
||||||
};
|
};
|
||||||
|
|
||||||
if (is_null($queries)) {
|
if (is_null($queries)) {
|
||||||
return null;
|
throw new InvalidArgumentException("No entity query class configured for {$type}");
|
||||||
}
|
}
|
||||||
|
|
||||||
return $queries->findVisibleById($entityId);
|
return $queries;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,15 @@ use Illuminate\Database\Eloquent\Builder;
|
||||||
|
|
||||||
class PageQueries implements ProvidesEntityQueries
|
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
|
public function start(): Builder
|
||||||
{
|
{
|
||||||
return Page::query();
|
return Page::query();
|
||||||
|
@ -60,22 +69,14 @@ class PageQueries implements ProvidesEntityQueries
|
||||||
{
|
{
|
||||||
return $this->start()
|
return $this->start()
|
||||||
->scopes('visible')
|
->scopes('visible')
|
||||||
->select(array_merge(Page::$listAttributes, ['book_slug' => function ($builder) {
|
->select($this->mergeBookSlugForSelect(static::$listAttributes));
|
||||||
$builder->select('slug')
|
|
||||||
->from('books')
|
|
||||||
->whereColumn('books.id', '=', 'pages.book_id');
|
|
||||||
}]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function visibleWithContents(): Builder
|
public function visibleWithContents(): Builder
|
||||||
{
|
{
|
||||||
return $this->start()
|
return $this->start()
|
||||||
->scopes('visible')
|
->scopes('visible')
|
||||||
->select(array_merge(Page::$contentAttributes, ['book_slug' => function ($builder) {
|
->select($this->mergeBookSlugForSelect(static::$contentAttributes));
|
||||||
$builder->select('slug')
|
|
||||||
->from('books')
|
|
||||||
->whereColumn('books.id', '=', 'pages.book_id');
|
|
||||||
}]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function currentUserDraftsForList(): Builder
|
public function currentUserDraftsForList(): Builder
|
||||||
|
@ -90,4 +91,13 @@ class PageQueries implements ProvidesEntityQueries
|
||||||
return $this->visibleForList()
|
return $this->visibleForList()
|
||||||
->where('template', '=', true);
|
->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');
|
||||||
|
}]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,19 @@ use Illuminate\Database\Eloquent\Builder;
|
||||||
*/
|
*/
|
||||||
interface ProvidesEntityQueries
|
interface ProvidesEntityQueries
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Start a new query for this entity type.
|
||||||
|
*/
|
||||||
public function start(): Builder;
|
public function start(): Builder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find the entity of the given ID, or return null if not found.
|
||||||
|
*/
|
||||||
public function findVisibleById(int $id): ?Entity;
|
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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,6 @@ use BookStack\Entities\Models\Chapter;
|
||||||
use BookStack\Entities\Models\Entity;
|
use BookStack\Entities\Models\Entity;
|
||||||
use BookStack\Entities\Models\HasCoverImage;
|
use BookStack\Entities\Models\HasCoverImage;
|
||||||
use BookStack\Entities\Models\HasHtmlDescription;
|
use BookStack\Entities\Models\HasHtmlDescription;
|
||||||
use BookStack\Entities\Models\Page;
|
|
||||||
use BookStack\Entities\Queries\PageQueries;
|
use BookStack\Entities\Queries\PageQueries;
|
||||||
use BookStack\Exceptions\ImageUploadException;
|
use BookStack\Exceptions\ImageUploadException;
|
||||||
use BookStack\References\ReferenceStore;
|
use BookStack\References\ReferenceStore;
|
||||||
|
|
|
@ -4,7 +4,6 @@ namespace BookStack\Permissions;
|
||||||
|
|
||||||
use BookStack\Entities\Models\Book;
|
use BookStack\Entities\Models\Book;
|
||||||
use BookStack\Entities\Models\BookChild;
|
use BookStack\Entities\Models\BookChild;
|
||||||
use BookStack\Entities\Models\Bookshelf;
|
|
||||||
use BookStack\Entities\Models\Chapter;
|
use BookStack\Entities\Models\Chapter;
|
||||||
use BookStack\Entities\Models\Entity;
|
use BookStack\Entities\Models\Entity;
|
||||||
use BookStack\Entities\Models\Page;
|
use BookStack\Entities\Models\Page;
|
||||||
|
|
|
@ -2,10 +2,6 @@
|
||||||
|
|
||||||
namespace BookStack\References;
|
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\Entities\Queries\EntityQueries;
|
||||||
use BookStack\Http\Controller;
|
use BookStack\Http\Controller;
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace BookStack\Search;
|
namespace BookStack\Search;
|
||||||
|
|
||||||
use BookStack\Entities\Models\Page;
|
|
||||||
use BookStack\Entities\Queries\PageQueries;
|
use BookStack\Entities\Queries\PageQueries;
|
||||||
use BookStack\Entities\Queries\Popular;
|
use BookStack\Entities\Queries\Popular;
|
||||||
use BookStack\Entities\Tools\SiblingFetcher;
|
use BookStack\Entities\Tools\SiblingFetcher;
|
||||||
|
|
|
@ -3,9 +3,9 @@
|
||||||
namespace BookStack\Search;
|
namespace BookStack\Search;
|
||||||
|
|
||||||
use BookStack\Entities\EntityProvider;
|
use BookStack\Entities\EntityProvider;
|
||||||
use BookStack\Entities\Models\BookChild;
|
|
||||||
use BookStack\Entities\Models\Entity;
|
use BookStack\Entities\Models\Entity;
|
||||||
use BookStack\Entities\Models\Page;
|
use BookStack\Entities\Models\Page;
|
||||||
|
use BookStack\Entities\Queries\EntityQueries;
|
||||||
use BookStack\Permissions\PermissionApplicator;
|
use BookStack\Permissions\PermissionApplicator;
|
||||||
use BookStack\Users\Models\User;
|
use BookStack\Users\Models\User;
|
||||||
use Illuminate\Database\Connection;
|
use Illuminate\Database\Connection;
|
||||||
|
@ -20,9 +20,6 @@ use SplObjectStorage;
|
||||||
|
|
||||||
class SearchRunner
|
class SearchRunner
|
||||||
{
|
{
|
||||||
protected EntityProvider $entityProvider;
|
|
||||||
protected PermissionApplicator $permissions;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Acceptable operators to be used in a query.
|
* Acceptable operators to be used in a query.
|
||||||
*
|
*
|
||||||
|
@ -38,10 +35,11 @@ class SearchRunner
|
||||||
*/
|
*/
|
||||||
protected $termAdjustmentCache;
|
protected $termAdjustmentCache;
|
||||||
|
|
||||||
public function __construct(EntityProvider $entityProvider, PermissionApplicator $permissions)
|
public function __construct(
|
||||||
{
|
protected EntityProvider $entityProvider,
|
||||||
$this->entityProvider = $entityProvider;
|
protected PermissionApplicator $permissions,
|
||||||
$this->permissions = $permissions;
|
protected EntityQueries $entityQueries,
|
||||||
|
) {
|
||||||
$this->termAdjustmentCache = new SplObjectStorage();
|
$this->termAdjustmentCache = new SplObjectStorage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,10 +70,9 @@ class SearchRunner
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$entityModelInstance = $this->entityProvider->get($entityType);
|
$searchQuery = $this->buildQuery($searchOpts, $entityType);
|
||||||
$searchQuery = $this->buildQuery($searchOpts, $entityModelInstance);
|
|
||||||
$entityTotal = $searchQuery->count();
|
$entityTotal = $searchQuery->count();
|
||||||
$searchResults = $this->getPageOfDataFromQuery($searchQuery, $entityModelInstance, $page, $count);
|
$searchResults = $this->getPageOfDataFromQuery($searchQuery, $entityType, $page, $count);
|
||||||
|
|
||||||
if ($entityTotal > ($page * $count)) {
|
if ($entityTotal > ($page * $count)) {
|
||||||
$hasMore = true;
|
$hasMore = true;
|
||||||
|
@ -108,8 +105,7 @@ class SearchRunner
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$entityModelInstance = $this->entityProvider->get($entityType);
|
$search = $this->buildQuery($opts, $entityType)->where('book_id', '=', $bookId)->take(20)->get();
|
||||||
$search = $this->buildQuery($opts, $entityModelInstance)->where('book_id', '=', $bookId)->take(20)->get();
|
|
||||||
$results = $results->merge($search);
|
$results = $results->merge($search);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,8 +118,7 @@ class SearchRunner
|
||||||
public function searchChapter(int $chapterId, string $searchString): Collection
|
public function searchChapter(int $chapterId, string $searchString): Collection
|
||||||
{
|
{
|
||||||
$opts = SearchOptions::fromString($searchString);
|
$opts = SearchOptions::fromString($searchString);
|
||||||
$entityModelInstance = $this->entityProvider->get('page');
|
$pages = $this->buildQuery($opts, 'page')->where('chapter_id', '=', $chapterId)->take(20)->get();
|
||||||
$pages = $this->buildQuery($opts, $entityModelInstance)->where('chapter_id', '=', $chapterId)->take(20)->get();
|
|
||||||
|
|
||||||
return $pages->sortByDesc('score');
|
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.
|
* 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'];
|
$relations = ['tags'];
|
||||||
|
|
||||||
if ($entityModelInstance instanceof BookChild) {
|
if ($entityType === 'page' || $entityType === 'chapter') {
|
||||||
$relations['book'] = function (BelongsTo $query) {
|
$relations['book'] = function (BelongsTo $query) {
|
||||||
$query->scopes('visible');
|
$query->scopes('visible');
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($entityModelInstance instanceof Page) {
|
if ($entityType === 'page') {
|
||||||
$relations['chapter'] = function (BelongsTo $query) {
|
$relations['chapter'] = function (BelongsTo $query) {
|
||||||
$query->scopes('visible');
|
$query->scopes('visible');
|
||||||
};
|
};
|
||||||
|
@ -157,18 +152,13 @@ class SearchRunner
|
||||||
/**
|
/**
|
||||||
* Create a search query for an entity.
|
* 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');
|
$entityModelInstance = $this->entityProvider->get($entityType);
|
||||||
|
$entityQuery = $this->entityQueries->visibleForList($entityType);
|
||||||
if ($entityModelInstance instanceof Page) {
|
|
||||||
$entityQuery->select(array_merge($entityModelInstance::$listAttributes, ['owned_by']));
|
|
||||||
} else {
|
|
||||||
$entityQuery->select(['*']);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle normal search terms
|
// Handle normal search terms
|
||||||
$this->applyTermSearch($entityQuery, $searchOpts, $entityModelInstance);
|
$this->applyTermSearch($entityQuery, $searchOpts, $entityType);
|
||||||
|
|
||||||
// Handle exact term matching
|
// Handle exact term matching
|
||||||
foreach ($searchOpts->exacts as $inputTerm) {
|
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.
|
* 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;
|
$terms = $options->searches;
|
||||||
if (count($terms) === 0) {
|
if (count($terms) === 0) {
|
||||||
|
@ -216,7 +206,7 @@ class SearchRunner
|
||||||
|
|
||||||
$subQuery->addBinding($scoreSelect['bindings'], 'select');
|
$subQuery->addBinding($scoreSelect['bindings'], 'select');
|
||||||
|
|
||||||
$subQuery->where('entity_type', '=', $entity->getMorphClass());
|
$subQuery->where('entity_type', '=', $entityType);
|
||||||
$subQuery->where(function (Builder $query) use ($terms) {
|
$subQuery->where(function (Builder $query) use ($terms) {
|
||||||
foreach ($terms as $inputTerm) {
|
foreach ($terms as $inputTerm) {
|
||||||
$inputTerm = str_replace('\\', '\\\\', $inputTerm);
|
$inputTerm = str_replace('\\', '\\\\', $inputTerm);
|
||||||
|
|
|
@ -4,7 +4,6 @@ namespace BookStack\Uploads;
|
||||||
|
|
||||||
use BookStack\Exceptions\FileUploadException;
|
use BookStack\Exceptions\FileUploadException;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
|
||||||
use Illuminate\Contracts\Filesystem\Filesystem as Storage;
|
use Illuminate\Contracts\Filesystem\Filesystem as Storage;
|
||||||
use Illuminate\Filesystem\FilesystemManager;
|
use Illuminate\Filesystem\FilesystemManager;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace BookStack\Uploads\Controllers;
|
namespace BookStack\Uploads\Controllers;
|
||||||
|
|
||||||
use BookStack\Entities\Models\Page;
|
|
||||||
use BookStack\Entities\Queries\PageQueries;
|
use BookStack\Entities\Queries\PageQueries;
|
||||||
use BookStack\Exceptions\FileUploadException;
|
use BookStack\Exceptions\FileUploadException;
|
||||||
use BookStack\Http\ApiController;
|
use BookStack\Http\ApiController;
|
||||||
|
|
|
@ -8,8 +8,6 @@ use BookStack\Uploads\ImageRepo;
|
||||||
use BookStack\Uploads\ImageResizer;
|
use BookStack\Uploads\ImageResizer;
|
||||||
use BookStack\Util\OutOfMemoryHandler;
|
use BookStack\Util\OutOfMemoryHandler;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\App;
|
|
||||||
use Illuminate\Support\Facades\Log;
|
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
|
|
||||||
class GalleryImageController extends Controller
|
class GalleryImageController extends Controller
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace BookStack\Uploads\Controllers;
|
namespace BookStack\Uploads\Controllers;
|
||||||
|
|
||||||
use BookStack\Entities\Models\Page;
|
|
||||||
use BookStack\Entities\Queries\PageQueries;
|
use BookStack\Entities\Queries\PageQueries;
|
||||||
use BookStack\Http\ApiController;
|
use BookStack\Http\ApiController;
|
||||||
use BookStack\Uploads\Image;
|
use BookStack\Uploads\Image;
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace BookStack\Uploads;
|
namespace BookStack\Uploads;
|
||||||
|
|
||||||
use BookStack\Entities\Models\Page;
|
|
||||||
use BookStack\Entities\Queries\PageQueries;
|
use BookStack\Entities\Queries\PageQueries;
|
||||||
use BookStack\Exceptions\ImageUploadException;
|
use BookStack\Exceptions\ImageUploadException;
|
||||||
use BookStack\Permissions\PermissionApplicator;
|
use BookStack\Permissions\PermissionApplicator;
|
||||||
|
|
|
@ -2,9 +2,6 @@
|
||||||
|
|
||||||
namespace BookStack\Uploads;
|
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\Entities\Queries\EntityQueries;
|
||||||
use BookStack\Exceptions\ImageUploadException;
|
use BookStack\Exceptions\ImageUploadException;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user