1818use BookStack \Facades \Activity ;
1919use BookStack \References \ReferenceStore ;
2020use BookStack \References \ReferenceUpdater ;
21+ use BookStack \Util \DatabaseTransaction ;
2122use Exception ;
2223
2324class PageRepo
@@ -61,8 +62,10 @@ public function getNewDraftPage(Entity $parent)
6162 ]);
6263 }
6364
64- $ page ->save ();
65- $ page ->refresh ()->rebuildPermissions ();
65+ (new DatabaseTransaction (function () use ($ page ) {
66+ $ page ->save ();
67+ $ page ->refresh ()->rebuildPermissions ();
68+ }))->run ();
6669
6770 return $ page ;
6871 }
@@ -72,26 +75,29 @@ public function getNewDraftPage(Entity $parent)
7275 */
7376 public function publishDraft (Page $ draft , array $ input ): Page
7477 {
75- $ draft ->draft = false ;
76- $ draft ->revision_count = 1 ;
77- $ draft ->priority = $ this ->getNewPriority ($ draft );
78- $ this ->updateTemplateStatusAndContentFromInput ($ draft , $ input );
79- $ this ->baseRepo ->update ($ draft , $ input );
80-
81- $ summary = trim ($ input ['summary ' ] ?? '' ) ?: trans ('entities.pages_initial_revision ' );
82- $ this ->revisionRepo ->storeNewForPage ($ draft , $ summary );
83- $ draft ->refresh ();
84-
85- Activity::add (ActivityType::PAGE_CREATE , $ draft );
86- $ this ->baseRepo ->sortParent ($ draft );
87-
88- return $ draft ;
78+ return (new DatabaseTransaction (function () use ($ draft , $ input ) {
79+ $ draft ->draft = false ;
80+ $ draft ->revision_count = 1 ;
81+ $ draft ->priority = $ this ->getNewPriority ($ draft );
82+ $ this ->updateTemplateStatusAndContentFromInput ($ draft , $ input );
83+ $ this ->baseRepo ->update ($ draft , $ input );
84+ $ draft ->rebuildPermissions ();
85+
86+ $ summary = trim ($ input ['summary ' ] ?? '' ) ?: trans ('entities.pages_initial_revision ' );
87+ $ this ->revisionRepo ->storeNewForPage ($ draft , $ summary );
88+ $ draft ->refresh ();
89+
90+ Activity::add (ActivityType::PAGE_CREATE , $ draft );
91+ $ this ->baseRepo ->sortParent ($ draft );
92+
93+ return $ draft ;
94+ }))->run ();
8995 }
9096
9197 /**
9298 * Directly update the content for the given page from the provided input.
9399 * Used for direct content access in a way that performs required changes
94- * (Search index & reference regen) without performing an official update.
100+ * (Search index and reference regen) without performing an official update.
95101 */
96102 public function setContentFromInput (Page $ page , array $ input ): void
97103 {
@@ -116,7 +122,7 @@ public function update(Page $page, array $input): Page
116122 $ page ->revision_count ++;
117123 $ page ->save ();
118124
119- // Remove all update drafts for this user & page.
125+ // Remove all update drafts for this user and page.
120126 $ this ->revisionRepo ->deleteDraftsForCurrentUser ($ page );
121127
122128 // Save a revision after updating
@@ -269,16 +275,18 @@ public function move(Page $page, string $parentIdentifier): Entity
269275 throw new PermissionsException ('User does not have permission to create a page within the new parent ' );
270276 }
271277
272- $ page ->chapter_id = ($ parent instanceof Chapter) ? $ parent ->id : null ;
273- $ newBookId = ($ parent instanceof Chapter) ? $ parent ->book ->id : $ parent ->id ;
274- $ page ->changeBook ($ newBookId );
275- $ page ->rebuildPermissions ();
278+ return (new DatabaseTransaction (function () use ($ page , $ parent ) {
279+ $ page ->chapter_id = ($ parent instanceof Chapter) ? $ parent ->id : null ;
280+ $ newBookId = ($ parent instanceof Chapter) ? $ parent ->book ->id : $ parent ->id ;
281+ $ page ->changeBook ($ newBookId );
282+ $ page ->rebuildPermissions ();
276283
277- Activity::add (ActivityType::PAGE_MOVE , $ page );
284+ Activity::add (ActivityType::PAGE_MOVE , $ page );
278285
279- $ this ->baseRepo ->sortParent ($ page );
286+ $ this ->baseRepo ->sortParent ($ page );
280287
281- return $ parent ;
288+ return $ parent ;
289+ }))->run ();
282290 }
283291
284292 /**
0 commit comments