3 namespace BookStack\Console\Commands;
5 use Illuminate\Console\Command;
6 use Illuminate\Database\Connection;
8 class UpdateUrlCommand extends Command
11 * The name and signature of the console command.
15 protected $signature = 'bookstack:update-url
16 {oldUrl : URL to replace}
17 {newUrl : URL to use as the replacement}
18 {--force : Force the operation to run, ignoring confirmations}';
21 * The console command description.
25 protected $description = 'Find and replace the given URLs in your BookStack database';
28 * Execute the console command.
30 public function handle(Connection $db): int
32 $oldUrl = str_replace("'", '', $this->argument('oldUrl'));
33 $newUrl = str_replace("'", '', $this->argument('newUrl'));
35 $urlPattern = '/https?:\/\/(.+)/';
36 if (!preg_match($urlPattern, $oldUrl) || !preg_match($urlPattern, $newUrl)) {
37 $this->error('The given urls are expected to be full urls starting with http:// or https://');
42 if (!$this->checkUserOkayToProceed($oldUrl, $newUrl)) {
46 $columnsToUpdateByTable = [
47 'attachments' => ['path'],
48 'entity_page_data' => ['html', 'text', 'markdown'],
49 'entity_container_data' => ['description_html'],
50 'page_revisions' => ['html', 'text', 'markdown'],
52 'settings' => ['value'],
53 'comments' => ['html'],
56 foreach ($columnsToUpdateByTable as $table => $columns) {
57 foreach ($columns as $column) {
58 $changeCount = $this->replaceValueInTable($db, $table, $column, $oldUrl, $newUrl);
59 $this->info("Updated {$changeCount} rows in {$table}->{$column}");
63 $jsonColumnsToUpdateByTable = [
64 'settings' => ['value'],
67 foreach ($jsonColumnsToUpdateByTable as $table => $columns) {
68 foreach ($columns as $column) {
69 $oldJson = trim(json_encode($oldUrl), '"');
70 $newJson = trim(json_encode($newUrl), '"');
71 $changeCount = $this->replaceValueInTable($db, $table, $column, $oldJson, $newJson);
72 $this->info("Updated {$changeCount} JSON encoded rows in {$table}->{$column}");
76 $this->info('URL update procedure complete.');
77 $this->info('============================================================================');
78 $this->info('Be sure to run "php artisan cache:clear" to clear any old URLs in the cache.');
80 if (!str_starts_with($newUrl, url('/'))) {
81 $this->warn('You still need to update your APP_URL env value. This is currently set to:');
82 $this->warn(url('/'));
85 $this->info('============================================================================');
91 * Perform a find+replace operations in the provided table and column.
92 * Returns the count of rows changed.
94 protected function replaceValueInTable(
101 $oldQuoted = $db->getPdo()->quote($oldUrl);
102 $newQuoted = $db->getPdo()->quote($newUrl);
104 return $db->table($table)->update([
105 $column => $db->raw("REPLACE({$column}, {$oldQuoted}, {$newQuoted})"),
110 * Warn the user of the dangers of this operation.
111 * Returns a boolean indicating if they've accepted the warnings.
113 protected function checkUserOkayToProceed(string $oldUrl, string $newUrl): bool
115 if ($this->option('force')) {
119 $dangerWarning = "This will search for \"{$oldUrl}\" in your database and replace it with \"{$newUrl}\".\n";
120 $dangerWarning .= 'Are you sure you want to proceed?';
121 $backupConfirmation = 'This operation could cause issues if used incorrectly. Have you made a backup of your existing database?';
123 return $this->confirm($dangerWarning) && $this->confirm($backupConfirmation);