Skip to content

Commit

Permalink
Merge pull request BookStackApp#3656 from BookStackApp/x_linking
Browse files Browse the repository at this point in the history
Link reference tracking & updating
  • Loading branch information
ssddanbrown authored Aug 29, 2022
2 parents 401c156 + 961e418 commit e537d0c
Show file tree
Hide file tree
Showing 48 changed files with 1,421 additions and 140 deletions.
2 changes: 1 addition & 1 deletion .env.example.complete
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ APP_DEFAULT_DARK_MODE=false
# Page revision limit
# Number of page revisions to keep in the system before deleting old revisions.
# If set to 'false' a limit will not be enforced.
REVISION_LIMIT=50
REVISION_LIMIT=100

# Recycle Bin Lifetime
# The number of days that content will remain in the recycle bin before
Expand Down
2 changes: 1 addition & 1 deletion app/Config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// The number of revisions to keep in the database.
// Once this limit is reached older revisions will be deleted.
// If set to false then a limit will not be enforced.
'revision_limit' => env('REVISION_LIMIT', 50),
'revision_limit' => env('REVISION_LIMIT', 100),

// The number of days that content will remain in the recycle bin before
// being considered for auto-removal. It is not a guarantee that content will
Expand Down
8 changes: 5 additions & 3 deletions app/Console/Commands/RegenerateCommentContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use BookStack\Actions\Comment;
use BookStack\Actions\CommentRepo;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class RegenerateCommentContent extends Command
{
Expand Down Expand Up @@ -43,9 +44,9 @@ public function __construct(CommentRepo $commentRepo)
*/
public function handle()
{
$connection = \DB::getDefaultConnection();
$connection = DB::getDefaultConnection();
if ($this->option('database') !== null) {
\DB::setDefaultConnection($this->option('database'));
DB::setDefaultConnection($this->option('database'));
}

Comment::query()->chunk(100, function ($comments) {
Expand All @@ -55,7 +56,8 @@ public function handle()
}
});

\DB::setDefaultConnection($connection);
DB::setDefaultConnection($connection);
$this->comment('Comment HTML content has been regenerated');
return 0;
}
}
1 change: 1 addition & 0 deletions app/Console/Commands/RegeneratePermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ public function handle()

DB::setDefaultConnection($connection);
$this->comment('Permissions regenerated');
return 0;
}
}
58 changes: 58 additions & 0 deletions app/Console/Commands/RegenerateReferences.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace BookStack\Console\Commands;

use BookStack\References\ReferenceStore;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class RegenerateReferences extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bookstack:regenerate-references {--database= : The database connection to use.}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Regenerate all the cross-item model reference index';

protected ReferenceStore $references;

/**
* Create a new command instance.
*
* @return void
*/
public function __construct(ReferenceStore $references)
{
$this->references = $references;
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$connection = DB::getDefaultConnection();

if ($this->option('database')) {
DB::setDefaultConnection($this->option('database'));
}

$this->references->updateForAllPages();

DB::setDefaultConnection($connection);

$this->comment('References have been regenerated');
return 0;
}
}
7 changes: 7 additions & 0 deletions app/Entities/Models/BookChild.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BookStack\Entities\Models;

use BookStack\References\ReferenceUpdater;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

Expand Down Expand Up @@ -57,9 +58,15 @@ public function book(): BelongsTo
*/
public function changeBook(int $newBookId): Entity
{
$oldUrl = $this->getUrl();
$this->book_id = $newBookId;
$this->refreshSlug();
$this->save();

if ($oldUrl !== $this->getUrl()) {
app()->make(ReferenceUpdater::class)->updateEntityPageReferences($this, $oldUrl);
}

$this->refresh();

// Update all child pages if a chapter
Expand Down
17 changes: 17 additions & 0 deletions app/Entities/Models/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use BookStack\Interfaces\Sluggable;
use BookStack\Interfaces\Viewable;
use BookStack\Model;
use BookStack\References\Reference;
use BookStack\Search\SearchIndex;
use BookStack\Search\SearchTerm;
use BookStack\Traits\HasCreatorAndUpdater;
Expand Down Expand Up @@ -203,6 +204,22 @@ public function deletions(): MorphMany
return $this->morphMany(Deletion::class, 'deletable');
}

/**
* Get the references pointing from this entity to other items.
*/
public function referencesFrom(): MorphMany
{
return $this->morphMany(Reference::class, 'from');
}

/**
* Get the references pointing to this entity from other items.
*/
public function referencesTo(): MorphMany
{
return $this->morphMany(Reference::class, 'to');
}

/**
* Check if this instance or class is a certain type of entity.
* Examples of $type are 'page', 'book', 'chapter'.
Expand Down
11 changes: 10 additions & 1 deletion app/Entities/Repos/BaseRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\HasCoverImage;
use BookStack\Exceptions\ImageUploadException;
use BookStack\References\ReferenceUpdater;
use BookStack\Uploads\ImageRepo;
use Illuminate\Http\UploadedFile;

class BaseRepo
{
protected TagRepo $tagRepo;
protected ImageRepo $imageRepo;
protected ReferenceUpdater $referenceUpdater;

public function __construct(TagRepo $tagRepo, ImageRepo $imageRepo)
public function __construct(TagRepo $tagRepo, ImageRepo $imageRepo, ReferenceUpdater $referenceUpdater)
{
$this->tagRepo = $tagRepo;
$this->imageRepo = $imageRepo;
$this->referenceUpdater = $referenceUpdater;
}

/**
Expand Down Expand Up @@ -48,6 +51,8 @@ public function create(Entity $entity, array $input)
*/
public function update(Entity $entity, array $input)
{
$oldUrl = $entity->getUrl();

$entity->fill($input);
$entity->updated_by = user()->id;

Expand All @@ -64,6 +69,10 @@ public function update(Entity $entity, array $input)

$entity->rebuildPermissions();
$entity->indexForSearch();

if ($oldUrl !== $entity->getUrl()) {
$this->referenceUpdater->updateEntityPageReferences($entity, $oldUrl);
}
}

/**
Expand Down
Loading

0 comments on commit e537d0c

Please sign in to comment.