]> BookStack Code Mirror - bookstack/commitdiff
Merge branch 'custom_role_system'
authorDan Brown <redacted>
Sat, 5 Mar 2016 18:21:44 +0000 (18:21 +0000)
committerDan Brown <redacted>
Sat, 5 Mar 2016 18:21:44 +0000 (18:21 +0000)
Conflicts:
app/Repos/BookRepo.php
app/Repos/ChapterRepo.php
app/Repos/PageRepo.php

.env.example
app/Entity.php
app/Repos/BookRepo.php
app/Repos/BookRepo.php.orig [new file with mode: 0644]
app/Repos/ChapterRepo.php
app/Repos/ChapterRepo.php.orig [new file with mode: 0644]
app/Repos/PageRepo.php
app/Repos/PageRepo.php.orig [new file with mode: 0644]
config/cache.php

index 6d706abddf5ff6c0d5632275136f697fd7593199..5661cda2209f753c3827e58a14d73a5f69445fd1 100644 (file)
@@ -12,8 +12,17 @@ DB_PASSWORD=database_user_password
 # Cache and session
 CACHE_DRIVER=file
 SESSION_DRIVER=file
+# If using Memcached, comment the above and uncomment these
+#CACHE_DRIVER=memcached
+#SESSION_DRIVER=memcached
 QUEUE_DRIVER=sync
 
+# Memcached settings
+# If using a UNIX socket path for the host, set the port to 0
+# This follows the following format: HOST:PORT:WEIGHT
+# For multiple servers separate with a comma
+MEMCACHED_SERVERS=127.0.0.1:11211:100
+
 # Storage
 STORAGE_TYPE=local
 # Amazon S3 Config
@@ -53,4 +62,4 @@ MAIL_HOST=localhost
 MAIL_PORT=1025
 MAIL_USERNAME=null
 MAIL_PASSWORD=null
-MAIL_ENCRYPTION=null
+MAIL_ENCRYPTION=null
\ No newline at end of file
index 97500917419cd82bd730b0f9859bfc309bb99be6..6bf29ca0fbb17b5650a653385edfd01211037441 100644 (file)
@@ -91,8 +91,8 @@ abstract class Entity extends Ownable
      */
     public function getShortName($length = 25)
     {
-        if(strlen($this->name) <= $length) return $this->name;
-        return substr($this->name, 0, $length-3) . '...';
+        if (strlen($this->name) <= $length) return $this->name;
+        return substr($this->name, 0, $length - 3) . '...';
     }
 
     /**
@@ -104,22 +104,40 @@ abstract class Entity extends Ownable
      */
     public static function fullTextSearchQuery($fieldsToSearch, $terms, $wheres = [])
     {
-        $termString = '';
-        foreach ($terms as $term) {
-            $termString .= htmlentities($term) . '* ';
+        $exactTerms = [];
+        foreach ($terms as $key => $term) {
+            $term = htmlentities($term, ENT_QUOTES);
+            $term =  preg_replace('/[+\-><\(\)~*\"@]+/', ' ', $term);
+            if (preg_match('/\s/', $term)) {
+                $exactTerms[] = '%' . $term . '%';
+                $term = '"' . $term . '"';
+            } else {
+                $term = '' . $term . '*';
+            }
+            if ($term !== '*') $terms[$key] = $term;
         }
+        $termString = implode(' ', $terms);
         $fields = implode(',', $fieldsToSearch);
-        $termStringEscaped = \DB::connection()->getPdo()->quote($termString);
-        $search = static::addSelect(\DB::raw('*, MATCH(name) AGAINST('.$termStringEscaped.' IN BOOLEAN MODE) AS title_relevance'));
+        $search = static::selectRaw('*, MATCH(name) AGAINST(? IN BOOLEAN MODE) AS title_relevance', [$termString]);
         $search = $search->whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
 
+        // Ensure at least one exact term matches if in search
+        if (count($exactTerms) > 0) {
+            $search = $search->where(function($query) use ($exactTerms, $fieldsToSearch) {
+                foreach ($exactTerms as $exactTerm) {
+                    foreach ($fieldsToSearch as $field) {
+                        $query->orWhere($field, 'like', $exactTerm);
+                    }
+                }
+            });
+        }
+
         // Add additional where terms
         foreach ($wheres as $whereTerm) {
             $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
         }
-
         // Load in relations
-        if (static::isA('page'))  {
+        if (static::isA('page')) {
             $search = $search->with('book', 'chapter', 'createdBy', 'updatedBy');
         } else if (static::isA('chapter')) {
             $search = $search->with('book');
index 4ae7cc0622a18ff5205f33eac8ab9300c4ef77c7..816db4cf072e22e73c553fb03b316f6d0eb2a8ae 100644 (file)
@@ -1,8 +1,6 @@
 <?php namespace BookStack\Repos;
 
 use Activity;
-use BookStack\Exceptions\NotFoundException;
-use BookStack\Services\RestrictionService;
 use Illuminate\Support\Str;
 use BookStack\Book;
 use Views;
@@ -243,7 +241,16 @@ class BookRepo
      */
     public function getBySearch($term, $count = 20, $paginationAppends = [])
     {
-        $terms = explode(' ', $term);
+        preg_match_all('/"(.*?)"/', $term, $matches);
+        if (count($matches[1]) > 0) {
+            $terms = $matches[1];
+            $term = trim(preg_replace('/"(.*?)"/', '', $term));
+        } else {
+            $terms = [];
+        }
+        if (!empty($term)) {
+            $terms = array_merge($terms, explode(' ', $term));
+        }
         $books = $this->restrictionService->enforceBookRestrictions($this->book->fullTextSearchQuery(['name', 'description'], $terms))
             ->paginate($count)->appends($paginationAppends);
         $words = join('|', explode(' ', preg_quote(trim($term), '/')));
diff --git a/app/Repos/BookRepo.php.orig b/app/Repos/BookRepo.php.orig
new file mode 100644 (file)
index 0000000..4b9709f
--- /dev/null
@@ -0,0 +1,295 @@
+<?php namespace BookStack\Repos;
+
+use Activity;
+use BookStack\Exceptions\NotFoundException;
+use BookStack\Services\RestrictionService;
+use Illuminate\Support\Str;
+use BookStack\Book;
+use Views;
+
+class BookRepo
+{
+
+    protected $book;
+    protected $pageRepo;
+    protected $chapterRepo;
+    protected $restrictionService;
+
+    /**
+     * BookRepo constructor.
+     * @param Book $book
+     * @param PageRepo $pageRepo
+     * @param ChapterRepo $chapterRepo
+     * @param RestrictionService $restrictionService
+     */
+    public function __construct(Book $book, PageRepo $pageRepo, ChapterRepo $chapterRepo, RestrictionService $restrictionService)
+    {
+        $this->book = $book;
+        $this->pageRepo = $pageRepo;
+        $this->chapterRepo = $chapterRepo;
+        $this->restrictionService = $restrictionService;
+    }
+
+    /**
+     * Base query for getting books.
+     * Takes into account any restrictions.
+     * @return mixed
+     */
+    private function bookQuery()
+    {
+        return $this->restrictionService->enforceBookRestrictions($this->book, 'view');
+    }
+
+    /**
+     * Get the book that has the given id.
+     * @param $id
+     * @return mixed
+     */
+    public function getById($id)
+    {
+        return $this->bookQuery()->findOrFail($id);
+    }
+
+    /**
+     * Get all books, Limited by count.
+     * @param int $count
+     * @return mixed
+     */
+    public function getAll($count = 10)
+    {
+        $bookQuery = $this->bookQuery()->orderBy('name', 'asc');
+        if (!$count) return $bookQuery->get();
+        return $bookQuery->take($count)->get();
+    }
+
+    /**
+     * Get all books paginated.
+     * @param int $count
+     * @return mixed
+     */
+    public function getAllPaginated($count = 10)
+    {
+        return $this->bookQuery()
+            ->orderBy('name', 'asc')->paginate($count);
+    }
+
+
+    /**
+     * Get the latest books.
+     * @param int $count
+     * @return mixed
+     */
+    public function getLatest($count = 10)
+    {
+        return $this->bookQuery()->orderBy('created_at', 'desc')->take($count)->get();
+    }
+
+    /**
+     * Gets the most recently viewed for a user.
+     * @param int $count
+     * @param int $page
+     * @return mixed
+     */
+    public function getRecentlyViewed($count = 10, $page = 0)
+    {
+        // TODO restrict
+        return Views::getUserRecentlyViewed($count, $page, $this->book);
+    }
+
+    /**
+     * Gets the most viewed books.
+     * @param int $count
+     * @param int $page
+     * @return mixed
+     */
+    public function getPopular($count = 10, $page = 0)
+    {
+        // TODO - Restrict
+        return Views::getPopular($count, $page, $this->book);
+    }
+
+    /**
+     * Get a book by slug
+     * @param $slug
+     * @return mixed
+     * @throws NotFoundException
+     */
+    public function getBySlug($slug)
+    {
+        $book = $this->bookQuery()->where('slug', '=', $slug)->first();
+        if ($book === null) throw new NotFoundException('Book not found');
+        return $book;
+    }
+
+    /**
+     * Checks if a book exists.
+     * @param $id
+     * @return bool
+     */
+    public function exists($id)
+    {
+        return $this->bookQuery()->where('id', '=', $id)->exists();
+    }
+
+    /**
+     * Get a new book instance from request input.
+     * @param $input
+     * @return Book
+     */
+    public function newFromInput($input)
+    {
+        return $this->book->newInstance($input);
+    }
+
+    /**
+     * Destroy a book identified by the given slug.
+     * @param $bookSlug
+     */
+    public function destroyBySlug($bookSlug)
+    {
+        $book = $this->getBySlug($bookSlug);
+        foreach ($book->pages as $page) {
+            $this->pageRepo->destroy($page);
+        }
+        foreach ($book->chapters as $chapter) {
+            $this->chapterRepo->destroy($chapter);
+        }
+        $book->views()->delete();
+        $book->restrictions()->delete();
+        $book->delete();
+    }
+
+    /**
+     * Get the next child element priority.
+     * @param Book $book
+     * @return int
+     */
+    public function getNewPriority($book)
+    {
+        $lastElem = $this->getChildren($book)->pop();
+        return $lastElem ? $lastElem->priority + 1 : 0;
+    }
+
+    /**
+     * @param string $slug
+     * @param bool|false $currentId
+     * @return bool
+     */
+    public function doesSlugExist($slug, $currentId = false)
+    {
+        $query = $this->book->where('slug', '=', $slug);
+        if ($currentId) {
+            $query = $query->where('id', '!=', $currentId);
+        }
+        return $query->count() > 0;
+    }
+
+    /**
+     * Provides a suitable slug for the given book name.
+     * Ensures the returned slug is unique in the system.
+     * @param string $name
+     * @param bool|false $currentId
+     * @return string
+     */
+    public function findSuitableSlug($name, $currentId = false)
+    {
+        $originalSlug = Str::slug($name);
+        $slug = $originalSlug;
+        $count = 2;
+        while ($this->doesSlugExist($slug, $currentId)) {
+            $slug = $originalSlug . '-' . $count;
+            $count++;
+        }
+        return $slug;
+    }
+
+    /**
+     * Get all child objects of a book.
+     * Returns a sorted collection of Pages and Chapters.
+     * Loads the bookslug onto child elements to prevent access database access for getting the slug.
+     * @param Book $book
+     * @return mixed
+     */
+    public function getChildren(Book $book)
+    {
+        $pageQuery = $book->pages()->where('chapter_id', '=', 0);
+        $pageQuery = $this->restrictionService->enforcePageRestrictions($pageQuery, 'view');
+        $pages = $pageQuery->get();
+
+        $chapterQuery = $book->chapters()->with(['pages' => function($query) {
+            $this->restrictionService->enforcePageRestrictions($query, 'view');
+        }]);
+        $chapterQuery = $this->restrictionService->enforceChapterRestrictions($chapterQuery, 'view');
+        $chapters = $chapterQuery->get();
+        $children = $pages->merge($chapters);
+        $bookSlug = $book->slug;
+        $children->each(function ($child) use ($bookSlug) {
+            $child->setAttribute('bookSlug', $bookSlug);
+            if ($child->isA('chapter')) {
+                $child->pages->each(function ($page) use ($bookSlug) {
+                    $page->setAttribute('bookSlug', $bookSlug);
+                });
+            }
+        });
+        return $children->sortBy('priority');
+    }
+
+    /**
+     * Get books by search term.
+     * @param $term
+     * @param int $count
+     * @param array $paginationAppends
+     * @return mixed
+     */
+    public function getBySearch($term, $count = 20, $paginationAppends = [])
+    {
+<<<<<<< HEAD
+        preg_match_all('/"(.*?)"/', $term, $matches);
+        if (count($matches[1]) > 0) {
+            $terms = $matches[1];
+            $term = trim(preg_replace('/"(.*?)"/', '', $term));
+        } else {
+            $terms = [];
+        }
+        if (!empty($term)) {
+            $terms = array_merge($terms, explode(' ', $term));
+        }
+        $books = $this->book->fullTextSearchQuery(['name', 'description'], $terms)
+=======
+        $terms = explode(' ', $term);
+        $books = $this->restrictionService->enforceBookRestrictions($this->book->fullTextSearchQuery(['name', 'description'], $terms))
+>>>>>>> custom_role_system
+            ->paginate($count)->appends($paginationAppends);
+        $words = join('|', explode(' ', preg_quote(trim($term), '/')));
+        foreach ($books as $book) {
+            //highlight
+            $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
+            $book->searchSnippet = $result;
+        }
+        return $books;
+    }
+
+    /**
+     * Updates books restrictions from a request
+     * @param $request
+     * @param $book
+     */
+    public function updateRestrictionsFromRequest($request, $book)
+    {
+        // TODO - extract into shared repo
+        $book->restricted = $request->has('restricted') && $request->get('restricted') === 'true';
+        $book->restrictions()->delete();
+        if ($request->has('restrictions')) {
+            foreach ($request->get('restrictions') as $roleId => $restrictions) {
+                foreach ($restrictions as $action => $value) {
+                    $book->restrictions()->create([
+                        'role_id' => $roleId,
+                        'action' => strtolower($action)
+                    ]);
+                }
+            }
+        }
+        $book->save();
+    }
+
+}
\ No newline at end of file
index 095596a608931f10ac1912e1e017fe403f72de96..6868bbf89eb4806caa9d8c2f8ab6cdf905076f3a 100644 (file)
@@ -156,7 +156,16 @@ class ChapterRepo
      */
     public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = [])
     {
-        $terms = explode(' ', $term);
+        preg_match_all('/"(.*?)"/', $term, $matches);
+        if (count($matches[1]) > 0) {
+            $terms = $matches[1];
+            $term = trim(preg_replace('/"(.*?)"/', '', $term));
+        } else {
+            $terms = [];
+        }
+        if (!empty($term)) {
+            $terms = array_merge($terms, explode(' ', $term));
+        }
         $chapters = $this->restrictionService->enforceChapterRestrictions($this->chapter->fullTextSearchQuery(['name', 'description'], $terms, $whereTerms))
             ->paginate($count)->appends($paginationAppends);
         $words = join('|', explode(' ', preg_quote(trim($term), '/')));
diff --git a/app/Repos/ChapterRepo.php.orig b/app/Repos/ChapterRepo.php.orig
new file mode 100644 (file)
index 0000000..be4a4e6
--- /dev/null
@@ -0,0 +1,226 @@
+<?php namespace BookStack\Repos;
+
+
+use Activity;
+use BookStack\Exceptions\NotFoundException;
+use BookStack\Services\RestrictionService;
+use Illuminate\Support\Str;
+use BookStack\Chapter;
+
+class ChapterRepo
+{
+
+    protected $chapter;
+    protected $restrictionService;
+
+    /**
+     * ChapterRepo constructor.
+     * @param Chapter $chapter
+     * @param RestrictionService $restrictionService
+     */
+    public function __construct(Chapter $chapter, RestrictionService $restrictionService)
+    {
+        $this->chapter = $chapter;
+        $this->restrictionService = $restrictionService;
+    }
+
+    /**
+     * Base query for getting chapters, Takes restrictions into account.
+     * @return mixed
+     */
+    private function chapterQuery()
+    {
+        return $this->restrictionService->enforceChapterRestrictions($this->chapter, 'view');
+    }
+
+    /**
+     * Check if an id exists.
+     * @param $id
+     * @return bool
+     */
+    public function idExists($id)
+    {
+        return $this->chapterQuery()->where('id', '=', $id)->count() > 0;
+    }
+
+    /**
+     * Get a chapter by a specific id.
+     * @param $id
+     * @return mixed
+     */
+    public function getById($id)
+    {
+        return $this->chapterQuery()->findOrFail($id);
+    }
+
+    /**
+     * Get all chapters.
+     * @return \Illuminate\Database\Eloquent\Collection|static[]
+     */
+    public function getAll()
+    {
+        return $this->chapterQuery()->all();
+    }
+
+    /**
+     * Get a chapter that has the given slug within the given book.
+     * @param $slug
+     * @param $bookId
+     * @return mixed
+     * @throws NotFoundException
+     */
+    public function getBySlug($slug, $bookId)
+    {
+        $chapter = $this->chapterQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
+        if ($chapter === null) throw new NotFoundException('Chapter not found');
+        return $chapter;
+    }
+
+    /**
+     * Get the child items for a chapter
+     * @param Chapter $chapter
+     */
+    public function getChildren(Chapter $chapter)
+    {
+        return $this->restrictionService->enforcePageRestrictions($chapter->pages())->get();
+    }
+
+    /**
+     * Create a new chapter from request input.
+     * @param $input
+     * @return $this
+     */
+    public function newFromInput($input)
+    {
+        return $this->chapter->fill($input);
+    }
+
+    /**
+     * Destroy a chapter and its relations by providing its slug.
+     * @param Chapter $chapter
+     */
+    public function destroy(Chapter $chapter)
+    {
+        if (count($chapter->pages) > 0) {
+            foreach ($chapter->pages as $page) {
+                $page->chapter_id = 0;
+                $page->save();
+            }
+        }
+        Activity::removeEntity($chapter);
+        $chapter->views()->delete();
+        $chapter->restrictions()->delete();
+        $chapter->delete();
+    }
+
+    /**
+     * Check if a chapter's slug exists.
+     * @param            $slug
+     * @param            $bookId
+     * @param bool|false $currentId
+     * @return bool
+     */
+    public function doesSlugExist($slug, $bookId, $currentId = false)
+    {
+        $query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId);
+        if ($currentId) {
+            $query = $query->where('id', '!=', $currentId);
+        }
+        return $query->count() > 0;
+    }
+
+    /**
+     * Finds a suitable slug for the provided name.
+     * Checks database to prevent duplicate slugs.
+     * @param            $name
+     * @param            $bookId
+     * @param bool|false $currentId
+     * @return string
+     */
+    public function findSuitableSlug($name, $bookId, $currentId = false)
+    {
+        $slug = Str::slug($name);
+        while ($this->doesSlugExist($slug, $bookId, $currentId)) {
+            $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
+        }
+        return $slug;
+    }
+
+    /**
+     * Get chapters by the given search term.
+     * @param       $term
+     * @param array $whereTerms
+     * @param int $count
+     * @param array $paginationAppends
+     * @return mixed
+     */
+    public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = [])
+    {
+<<<<<<< HEAD
+        preg_match_all('/"(.*?)"/', $term, $matches);
+        if (count($matches[1]) > 0) {
+            $terms = $matches[1];
+            $term = trim(preg_replace('/"(.*?)"/', '', $term));
+        } else {
+            $terms = [];
+        }
+        if (!empty($term)) {
+            $terms = array_merge($terms, explode(' ', $term));
+        }
+        $chapters = $this->chapter->fullTextSearchQuery(['name', 'description'], $terms, $whereTerms)
+=======
+        $terms = explode(' ', $term);
+        $chapters = $this->restrictionService->enforceChapterRestrictions($this->chapter->fullTextSearchQuery(['name', 'description'], $terms, $whereTerms))
+>>>>>>> custom_role_system
+            ->paginate($count)->appends($paginationAppends);
+        $words = join('|', explode(' ', preg_quote(trim($term), '/')));
+        foreach ($chapters as $chapter) {
+            //highlight
+            $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $chapter->getExcerpt(100));
+            $chapter->searchSnippet = $result;
+        }
+        return $chapters;
+    }
+
+    /**
+     * Changes the book relation of this chapter.
+     * @param         $bookId
+     * @param Chapter $chapter
+     * @return Chapter
+     */
+    public function changeBook($bookId, Chapter $chapter)
+    {
+        $chapter->book_id = $bookId;
+        foreach ($chapter->activity as $activity) {
+            $activity->book_id = $bookId;
+            $activity->save();
+        }
+        $chapter->slug = $this->findSuitableSlug($chapter->name, $bookId, $chapter->id);
+        $chapter->save();
+        return $chapter;
+    }
+
+    /**
+     * Updates pages restrictions from a request
+     * @param $request
+     * @param $chapter
+     */
+    public function updateRestrictionsFromRequest($request, $chapter)
+    {
+        // TODO - extract into shared repo
+        $chapter->restricted = $request->has('restricted') && $request->get('restricted') === 'true';
+        $chapter->restrictions()->delete();
+        if ($request->has('restrictions')) {
+            foreach($request->get('restrictions') as $roleId => $restrictions) {
+                foreach ($restrictions as $action => $value) {
+                    $chapter->restrictions()->create([
+                        'role_id' => $roleId,
+                        'action'  => strtolower($action)
+                    ]);
+                }
+            }
+        }
+        $chapter->save();
+    }
+
+}
\ No newline at end of file
index f3933af69da8cd3cce6ddcdceedf69b0fe64c869..3d675183e3f1c807d78916893cf4488de3b5155c 100644 (file)
@@ -96,6 +96,16 @@ class PageRepo
         return $page;
     }
 
+    /**
+     * Count the pages with a particular slug within a book.
+     * @param $slug
+     * @param $bookId
+     * @return mixed
+     */
+    public function countBySlug($slug, $bookId)
+    {
+        return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count();
+    }
 
     /**
      * Save a new page into the system.
@@ -190,7 +200,16 @@ class PageRepo
      */
     public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = [])
     {
-        $terms = explode(' ', $term);
+        preg_match_all('/"(.*?)"/', $term, $matches);
+        if (count($matches[1]) > 0) {
+            $terms = $matches[1];
+            $term = trim(preg_replace('/"(.*?)"/', '', $term));
+        } else {
+            $terms = [];
+        }
+        if (!empty($term)) {
+            $terms = array_merge($terms, explode(' ', $term));
+        }
         $pages = $this->restrictionService->enforcePageRestrictions($this->page->fullTextSearchQuery(['name', 'text'], $terms, $whereTerms))
             ->paginate($count)->appends($paginationAppends);
 
diff --git a/app/Repos/PageRepo.php.orig b/app/Repos/PageRepo.php.orig
new file mode 100644 (file)
index 0000000..c1e02b5
--- /dev/null
@@ -0,0 +1,437 @@
+<?php namespace BookStack\Repos;
+
+
+use Activity;
+use BookStack\Book;
+use BookStack\Chapter;
+use BookStack\Exceptions\NotFoundException;
+use BookStack\Services\RestrictionService;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Str;
+use BookStack\Page;
+use BookStack\PageRevision;
+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
+
+class PageRepo
+{
+    protected $page;
+    protected $pageRevision;
+    protected $restrictionService;
+
+    /**
+     * PageRepo constructor.
+     * @param Page $page
+     * @param PageRevision $pageRevision
+     * @param RestrictionService $restrictionService
+     */
+    public function __construct(Page $page, PageRevision $pageRevision, RestrictionService $restrictionService)
+    {
+        $this->page = $page;
+        $this->pageRevision = $pageRevision;
+        $this->restrictionService = $restrictionService;
+    }
+
+    /**
+     * Base query for getting pages, Takes restrictions into account.
+     * @return mixed
+     */
+    private function pageQuery()
+    {
+        return $this->restrictionService->enforcePageRestrictions($this->page, 'view');
+    }
+
+    /**
+     * Get a page via a specific ID.
+     * @param $id
+     * @return mixed
+     */
+    public function getById($id)
+    {
+        return $this->pageQuery()->findOrFail($id);
+    }
+
+    /**
+     * Get a page identified by the given slug.
+     * @param $slug
+     * @param $bookId
+     * @return mixed
+     * @throws NotFoundException
+     */
+    public function getBySlug($slug, $bookId)
+    {
+        $page = $this->pageQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
+        if ($page === null) throw new NotFoundException('Page not found');
+        return $page;
+    }
+
+    /**
+     * Search through page revisions and retrieve
+     * the last page in the current book that
+     * has a slug equal to the one given.
+     * @param $pageSlug
+     * @param $bookSlug
+     * @return null | Page
+     */
+    public function findPageUsingOldSlug($pageSlug, $bookSlug)
+    {
+        $revision = $this->pageRevision->where('slug', '=', $pageSlug)
+            ->whereHas('page', function($query) {
+                $this->restrictionService->enforcePageRestrictions($query);
+            })
+            ->where('book_slug', '=', $bookSlug)->orderBy('created_at', 'desc')
+            ->with('page')->first();
+        return $revision !== null ? $revision->page : null;
+    }
+
+    /**
+     * Get a new Page instance from the given input.
+     * @param $input
+     * @return Page
+     */
+    public function newFromInput($input)
+    {
+        $page = $this->page->fill($input);
+        return $page;
+    }
+
+
+    /**
+     * Save a new page into the system.
+     * Input validation must be done beforehand.
+     * @param array $input
+     * @param Book  $book
+     * @param int   $chapterId
+     * @return Page
+     */
+    public function saveNew(array $input, Book $book, $chapterId = null)
+    {
+        $page = $this->newFromInput($input);
+        $page->slug = $this->findSuitableSlug($page->name, $book->id);
+
+        if ($chapterId) $page->chapter_id = $chapterId;
+
+        $page->html = $this->formatHtml($input['html']);
+        $page->text = strip_tags($page->html);
+        $page->created_by = auth()->user()->id;
+        $page->updated_by = auth()->user()->id;
+
+        $book->pages()->save($page);
+        return $page;
+    }
+
+    /**
+     * Formats a page's html to be tagged correctly
+     * within the system.
+     * @param string $htmlText
+     * @return string
+     */
+    protected function formatHtml($htmlText)
+    {
+        if($htmlText == '') return $htmlText;
+        libxml_use_internal_errors(true);
+        $doc = new \DOMDocument();
+        $doc->loadHTML(mb_convert_encoding($htmlText, 'HTML-ENTITIES', 'UTF-8'));
+
+        $container = $doc->documentElement;
+        $body = $container->childNodes->item(0);
+        $childNodes = $body->childNodes;
+
+        // Ensure no duplicate ids are used
+        $idArray = [];
+
+        foreach ($childNodes as $index => $childNode) {
+            /** @var \DOMElement $childNode */
+            if (get_class($childNode) !== 'DOMElement') continue;
+
+            // Overwrite id if not a BookStack custom id
+            if ($childNode->hasAttribute('id')) {
+                $id = $childNode->getAttribute('id');
+                if (strpos($id, 'bkmrk') === 0 && array_search($id, $idArray) === false) {
+                    $idArray[] = $id;
+                    continue;
+                };
+            }
+
+            // Create an unique id for the element
+            // Uses the content as a basis to ensure output is the same every time
+            // the same content is passed through.
+            $contentId = 'bkmrk-' . substr(strtolower(preg_replace('/\s+/', '-', trim($childNode->nodeValue))), 0, 20);
+            $newId = urlencode($contentId);
+            $loopIndex = 0;
+            while (in_array($newId, $idArray)) {
+                $newId = urlencode($contentId . '-' . $loopIndex);
+                $loopIndex++;
+            }
+
+            $childNode->setAttribute('id', $newId);
+            $idArray[] = $newId;
+        }
+
+        // Generate inner html as a string
+        $html = '';
+        foreach ($childNodes as $childNode) {
+            $html .= $doc->saveHTML($childNode);
+        }
+
+        return $html;
+    }
+
+
+    /**
+     * Gets pages by a search term.
+     * Highlights page content for showing in results.
+     * @param string $term
+     * @param array $whereTerms
+     * @param int $count
+     * @param array $paginationAppends
+     * @return mixed
+     */
+    public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = [])
+    {
+<<<<<<< HEAD
+        preg_match_all('/"(.*?)"/', $term, $matches);
+        if (count($matches[1]) > 0) {
+            $terms = $matches[1];
+            $term = trim(preg_replace('/"(.*?)"/', '', $term));
+        } else {
+            $terms = [];
+        }
+        if (!empty($term)) {
+            $terms = array_merge($terms, explode(' ', $term));
+        }
+        $pages = $this->page->fullTextSearchQuery(['name', 'text'], $terms, $whereTerms)
+=======
+        $terms = explode(' ', $term);
+        $pages = $this->restrictionService->enforcePageRestrictions($this->page->fullTextSearchQuery(['name', 'text'], $terms, $whereTerms))
+>>>>>>> custom_role_system
+            ->paginate($count)->appends($paginationAppends);
+
+        // Add highlights to page text.
+        $words = join('|', explode(' ', preg_quote(trim($term), '/')));
+        //lookahead/behind assertions ensures cut between words
+        $s = '\s\x00-/:-@\[-`{-~'; //character set for start/end of words
+
+        foreach ($pages as $page) {
+            preg_match_all('#(?<=[' . $s . ']).{1,30}((' . $words . ').{1,30})+(?=[' . $s . '])#uis', $page->text, $matches, PREG_SET_ORDER);
+            //delimiter between occurrences
+            $results = [];
+            foreach ($matches as $line) {
+                $results[] = htmlspecialchars($line[0], 0, 'UTF-8');
+            }
+            $matchLimit = 6;
+            if (count($results) > $matchLimit) {
+                $results = array_slice($results, 0, $matchLimit);
+            }
+            $result = join('... ', $results);
+
+            //highlight
+            $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $result);
+            if (strlen($result) < 5) {
+                $result = $page->getExcerpt(80);
+            }
+            $page->searchSnippet = $result;
+        }
+        return $pages;
+    }
+
+    /**
+     * Search for image usage.
+     * @param $imageString
+     * @return mixed
+     */
+    public function searchForImage($imageString)
+    {
+        $pages = $this->pageQuery()->where('html', 'like', '%' . $imageString . '%')->get();
+        foreach ($pages as $page) {
+            $page->url = $page->getUrl();
+            $page->html = '';
+            $page->text = '';
+        }
+        return count($pages) > 0 ? $pages : false;
+    }
+
+    /**
+     * Updates a page with any fillable data and saves it into the database.
+     * @param Page   $page
+     * @param int    $book_id
+     * @param string $input
+     * @return Page
+     */
+    public function updatePage(Page $page, $book_id, $input)
+    {
+        // Save a revision before updating
+        if ($page->html !== $input['html'] || $page->name !== $input['name']) {
+            $this->saveRevision($page);
+        }
+
+        // Prevent slug being updated if no name change
+        if ($page->name !== $input['name']) {
+            $page->slug = $this->findSuitableSlug($input['name'], $book_id, $page->id);
+        }
+
+        // Update with new details
+        $page->fill($input);
+        $page->html = $this->formatHtml($input['html']);
+        $page->text = strip_tags($page->html);
+        $page->updated_by = auth()->user()->id;
+        $page->save();
+        return $page;
+    }
+
+    /**
+     * Restores a revision's content back into a page.
+     * @param Page $page
+     * @param Book $book
+     * @param  int $revisionId
+     * @return Page
+     */
+    public function restoreRevision(Page $page, Book $book, $revisionId)
+    {
+        $this->saveRevision($page);
+        $revision = $this->getRevisionById($revisionId);
+        $page->fill($revision->toArray());
+        $page->slug = $this->findSuitableSlug($page->name, $book->id, $page->id);
+        $page->text = strip_tags($page->html);
+        $page->updated_by = auth()->user()->id;
+        $page->save();
+        return $page;
+    }
+
+    /**
+     * Saves a page revision into the system.
+     * @param Page $page
+     * @return $this
+     */
+    public function saveRevision(Page $page)
+    {
+        $revision = $this->pageRevision->fill($page->toArray());
+        $revision->page_id = $page->id;
+        $revision->slug = $page->slug;
+        $revision->book_slug = $page->book->slug;
+        $revision->created_by = auth()->user()->id;
+        $revision->created_at = $page->updated_at;
+        $revision->save();
+        // Clear old revisions
+        if ($this->pageRevision->where('page_id', '=', $page->id)->count() > 50) {
+            $this->pageRevision->where('page_id', '=', $page->id)
+                ->orderBy('created_at', 'desc')->skip(50)->take(5)->delete();
+        }
+        return $revision;
+    }
+
+    /**
+     * Gets a single revision via it's id.
+     * @param $id
+     * @return mixed
+     */
+    public function getRevisionById($id)
+    {
+        return $this->pageRevision->findOrFail($id);
+    }
+
+    /**
+     * Checks if a slug exists within a book already.
+     * @param            $slug
+     * @param            $bookId
+     * @param bool|false $currentId
+     * @return bool
+     */
+    public function doesSlugExist($slug, $bookId, $currentId = false)
+    {
+        $query = $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId);
+        if ($currentId) $query = $query->where('id', '!=', $currentId);
+        return $query->count() > 0;
+    }
+
+    /**
+     * Changes the related book for the specified page.
+     * Changes the book id of any relations to the page that store the book id.
+     * @param int  $bookId
+     * @param Page $page
+     * @return Page
+     */
+    public function changeBook($bookId, Page $page)
+    {
+        $page->book_id = $bookId;
+        foreach ($page->activity as $activity) {
+            $activity->book_id = $bookId;
+            $activity->save();
+        }
+        $page->slug = $this->findSuitableSlug($page->name, $bookId, $page->id);
+        $page->save();
+        return $page;
+    }
+
+    /**
+     * Gets a suitable slug for the resource
+     * @param            $name
+     * @param            $bookId
+     * @param bool|false $currentId
+     * @return string
+     */
+    public function findSuitableSlug($name, $bookId, $currentId = false)
+    {
+        $slug = Str::slug($name);
+        while ($this->doesSlugExist($slug, $bookId, $currentId)) {
+            $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
+        }
+        return $slug;
+    }
+
+    /**
+     * Destroy a given page along with its dependencies.
+     * @param $page
+     */
+    public function destroy($page)
+    {
+        Activity::removeEntity($page);
+        $page->views()->delete();
+        $page->revisions()->delete();
+        $page->restrictions()->delete();
+        $page->delete();
+    }
+
+    /**
+     * Get the latest pages added to the system.
+     * @param $count
+     */
+    public function getRecentlyCreatedPaginated($count = 20)
+    {
+        return $this->pageQuery()->orderBy('created_at', 'desc')->paginate($count);
+    }
+
+    /**
+     * Get the latest pages added to the system.
+     * @param $count
+     */
+    public function getRecentlyUpdatedPaginated($count = 20)
+    {
+        return $this->pageQuery()->orderBy('updated_at', 'desc')->paginate($count);
+    }
+
+    /**
+     * Updates pages restrictions from a request
+     * @param $request
+     * @param $page
+     */
+    public function updateRestrictionsFromRequest($request, $page)
+    {
+        // TODO - extract into shared repo
+        $page->restricted = $request->has('restricted') && $request->get('restricted') === 'true';
+        $page->restrictions()->delete();
+        if ($request->has('restrictions')) {
+            foreach($request->get('restrictions') as $roleId => $restrictions) {
+                foreach ($restrictions as $action => $value) {
+                    $page->restrictions()->create([
+                        'role_id' => $roleId,
+                        'action'  => strtolower($action)
+                    ]);
+                }
+            }
+        }
+        $page->save();
+    }
+
+}
index 379135b0eb6904733cc3ac4f569b4df04ad15d9e..076a0299fc1196329d51875e8c64dbe678b7edc4 100644 (file)
@@ -1,5 +1,18 @@
 <?php
 
+// MEMCACHED - Split out configuration into an array
+if (env('CACHE_DRIVER') === 'memcached') {
+    $memcachedServerKeys = ['host', 'port', 'weight'];
+    $memcachedServers = explode(',', trim(env('MEMCACHED_SERVERS', '127.0.0.1:11211:100'), ','));
+    foreach ($memcachedServers as $index => $memcachedServer) {
+        $memcachedServerDetails = explode(':', $memcachedServer);
+        $components = count($memcachedServerDetails);
+        if ($components < 2) $memcachedServerDetails[] = '11211';
+        if ($components < 3) $memcachedServerDetails[] = '100';
+        $memcachedServers[$index] = array_combine($memcachedServerKeys, $memcachedServerDetails);
+    }
+}
+
 return [
 
     /*
@@ -49,11 +62,7 @@ return [
 
         'memcached' => [
             'driver'  => 'memcached',
-            'servers' => [
-                [
-                    'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100,
-                ],
-            ],
+            'servers' => env('CACHE_DRIVER') === 'memcached' ? $memcachedServers : [],
         ],
 
         'redis' => [