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://');
56 if (!$this->checkUserOkayToProceed($oldUrl, $newUrl)) {
60 $columnsToUpdateByTable = [
61 'attachments' => ['path'],
62 'pages' => ['html', 'text', 'markdown'],
64 'settings' => ['value'],
65 'comments' => ['html', 'text'],
68 foreach ($columnsToUpdateByTable as $table => $columns) {
69 foreach ($columns as $column) {
70 $changeCount = $this->replaceValueInTable($table, $column, $oldUrl, $newUrl);
71 $this->info("Updated {$changeCount} rows in {$table}->{$column}");
75 $jsonColumnsToUpdateByTable = [
76 'settings' => ['value'],
79 foreach ($jsonColumnsToUpdateByTable as $table => $columns) {
80 foreach ($columns as $column) {
81 $oldJson = trim(json_encode($oldUrl), '"');
82 $newJson = trim(json_encode($newUrl), '"');
83 $changeCount = $this->replaceValueInTable($table, $column, $oldJson, $newJson);
84 $this->info("Updated {$changeCount} JSON encoded rows in {$table}->{$column}");
88 $this->info('URL update procedure complete.');
89 $this->info('============================================================================');
90 $this->info('Be sure to run "php artisan cache:clear" to clear any old URLs in the cache.');
91 $this->info('============================================================================');
97 * Perform a find+replace operations in the provided table and column.
98 * Returns the count of rows changed.
100 protected function replaceValueInTable(string $table, string $column, string $oldUrl, string $newUrl): int
102 $oldQuoted = $this->db->getPdo()->quote($oldUrl);
103 $newQuoted = $this->db->getPdo()->quote($newUrl);
105 return $this->db->table($table)->update([
106 $column => $this->db->raw("REPLACE({$column}, {$oldQuoted}, {$newQuoted})"),
111 * Warn the user of the dangers of this operation.
112 * Returns a boolean indicating if they've accepted the warnings.
114 protected function checkUserOkayToProceed(string $oldUrl, string $newUrl): bool
116 $dangerWarning = "This will search for \"{$oldUrl}\" in your database and replace it with \"{$newUrl}\".\n";
117 $dangerWarning .= 'Are you sure you want to proceed?';
118 $backupConfirmation = 'This operation could cause issues if used incorrectly. Have you made a backup of your existing database?';
120 return $this->confirm($dangerWarning) && $this->confirm($backupConfirmation);