]> BookStack Code Mirror - bookstack/commitdiff
Prevent revision encoding issues
authorDan Brown <redacted>
Sun, 18 Oct 2015 18:40:33 +0000 (19:40 +0100)
committerDan Brown <redacted>
Sun, 18 Oct 2015 18:40:33 +0000 (19:40 +0100)
app/Http/Controllers/PageController.php
app/Repos/PageRepo.php
resources/views/base.blade.php

index 56ea6992c88edde8bd007223ee78681f99fccaad..16a44e4c5434344c321b449480307dfc2493c0f5 100644 (file)
@@ -196,13 +196,19 @@ class PageController extends Controller
         return view('pages/revision', ['page' => $page, 'book' => $book]);
     }
 
+    /**
+     * Restores a page using the content of the specified revision.
+     * @param $bookSlug
+     * @param $pageSlug
+     * @param $revisionId
+     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
+     */
     public function restoreRevision($bookSlug, $pageSlug, $revisionId)
     {
         $this->checkPermission('page-update');
         $book = $this->bookRepo->getBySlug($bookSlug);
         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
-        $revision = $this->pageRepo->getRevisionById($revisionId);
-        $page = $this->pageRepo->updatePage($page, $book->id, $revision->toArray());
+        $page = $this->pageRepo->restoreRevision($page, $book, $revisionId);
         Activity::add($page, 'page_restore', $book->id);
         return redirect($page->getUrl());
     }
index 448ba794370c6845ae7ff62d8f9a1f84c44daeef..2f7bbe709909e337cf90c26eb609173c0c08506a 100644 (file)
@@ -82,7 +82,6 @@ class PageRepo
         $page->updated_by = auth()->user()->id;
 
         $book->pages()->save($page);
-        $this->saveRevision($page);
         return $page;
     }
 
@@ -202,13 +201,37 @@ class PageRepo
      */
     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);
+        }
+
+        // Update with new details
         $page->fill($input);
         $page->slug = $this->findSuitableSlug($page->name, $book_id, $page->id);
         $page->html = $this->formatHtml($input['html']);
         $page->text = strip_tags($page->html);
-        $page->updated_by = Auth::user()->id;
+        $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;
     }
 
@@ -217,15 +240,12 @@ class PageRepo
      * @param Page $page
      * @return $this
      */
-    public function saveRevision(Page $page)
+    private function saveRevision(Page $page)
     {
-        $lastRevision = $this->getLastRevision($page);
-        if ($lastRevision && ($lastRevision->html === $page->html && $lastRevision->name === $page->name)) {
-            return $page;
-        }
         $revision = $this->pageRevision->fill($page->toArray());
         $revision->page_id = $page->id;
-        $revision->created_by = Auth::user()->id;
+        $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) {
@@ -235,17 +255,6 @@ class PageRepo
         return $revision;
     }
 
-    /**
-     * Gets the most recent revision for a page.
-     * @param Page $page
-     * @return mixed
-     */
-    public function getLastRevision(Page $page)
-    {
-        return $this->pageRevision->where('page_id', '=', $page->id)
-            ->orderBy('created_at', 'desc')->first();
-    }
-
     /**
      * Gets a single revision via it's id.
      * @param $id
@@ -266,12 +275,17 @@ class PageRepo
     public function doesSlugExist($slug, $bookId, $currentId = false)
     {
         $query = $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId);
-        if ($currentId) {
-            $query = $query->where('id', '!=', $currentId);
-        }
+        if ($currentId) $query = $query->where('id', '!=', $currentId);
         return $query->count() > 0;
     }
 
+    /**
+     * Sets the book id 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 setBookId($bookId, Page $page)
     {
         $page->book_id = $bookId;
index 80086a4134441747a92eae7f411b8ef4bff284d7..3f76b04226eba9c8771b2f0692d9d5d4a08db7e1 100644 (file)
@@ -6,6 +6,7 @@
     <!-- Meta-->
     <meta name="viewport" content="width=device-width">
     <meta name="token" content="{{ csrf_token() }}">
+    <meta charset="utf-8">
 
     <!-- Styles and Fonts -->
     <link rel="stylesheet" href="/css/styles.css">