3 namespace BookStack\Console\Commands;
5 use Illuminate\Console\Command;
6 use Illuminate\Database\Connection;
8 class UpdateUrl 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}';
20 * The console command description.
24 protected $description = 'Find and replace the given URLs in your BookStack database';
29 * Create a new command instance.
33 public function __construct(Connection $db)
36 parent::__construct();
40 * Execute the console command.
44 public function handle()
46 $oldUrl = str_replace("'", '', $this->argument('oldUrl'));
47 $newUrl = str_replace("'", '', $this->argument('newUrl'));
49 $urlPattern = '/https?:\/\/(.+)/';
50 if (!preg_match($urlPattern, $oldUrl) || !preg_match($urlPattern, $newUrl)) {
51 $this->error("The given urls are expected to be full urls starting with http:// or https://");
55 if (!$this->checkUserOkayToProceed($oldUrl, $newUrl)) {
59 $columnsToUpdateByTable = [
60 "attachments" => ["path"],
61 "pages" => ["html", "text", "markdown"],
63 "settings" => ["value"],
64 "comments" => ["html", "text"],
67 foreach ($columnsToUpdateByTable as $table => $columns) {
68 foreach ($columns as $column) {
69 $changeCount = $this->db->table($table)->update([
70 $column => $this->db->raw("REPLACE({$column}, '{$oldUrl}', '{$newUrl}')")
72 $this->info("Updated {$changeCount} rows in {$table}->{$column}");
76 $this->info("URL update procedure complete.");
81 * Warn the user of the dangers of this operation.
82 * Returns a boolean indicating if they've accepted the warnings.
84 protected function checkUserOkayToProceed(string $oldUrl, string $newUrl): bool
86 $dangerWarning = "This will search for \"{$oldUrl}\" in your database and replace it with \"{$newUrl}\".\n";
87 $dangerWarning .= "Are you sure you want to proceed?";
88 $backupConfirmation = "This operation could cause issues if used incorrectly. Have you made a backup of your existing database?";
90 return $this->confirm($dangerWarning) && $this->confirm($backupConfirmation);