]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/UpdateUrl.php
2a16884680729e3dc502909cdc71d76addb70177
[bookstack] / app / Console / Commands / UpdateUrl.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use Illuminate\Console\Command;
6 use Illuminate\Database\Connection;
7 use Illuminate\Support\Facades\DB;
8
9 class UpdateUrl extends Command
10 {
11     /**
12      * The name and signature of the console command.
13      *
14      * @var string
15      */
16     protected $signature = 'bookstack:update-url
17                             {oldUrl : URL to replace}
18                             {newUrl : URL to use as the replacement}';
19
20     /**
21      * The console command description.
22      *
23      * @var string
24      */
25     protected $description = 'Find and replace the given URLs in your BookStack database';
26
27     protected $db;
28
29     /**
30      * Create a new command instance.
31      *
32      * @return void
33      */
34     public function __construct(Connection $db)
35     {
36         $this->db = $db;
37         parent::__construct();
38     }
39
40     /**
41      * Execute the console command.
42      *
43      * @return mixed
44      */
45     public function handle()
46     {
47         $oldUrl = str_replace("'", '', $this->argument('oldUrl'));
48         $newUrl = str_replace("'", '', $this->argument('newUrl'));
49
50         $urlPattern = '/https?:\/\/(.+)/';
51         if (!preg_match($urlPattern, $oldUrl) || !preg_match($urlPattern, $newUrl)) {
52             $this->error("The given urls are expected to be full urls starting with http:// or https://");
53             return 1;
54         }
55
56         if (!$this->checkUserOkayToProceed($oldUrl, $newUrl)) {
57             return 1;
58         }
59
60         $columnsToUpdateByTable = [
61             "attachments" => ["path"],
62             "pages" => ["html", "text", "markdown"],
63             "images" => ["url"],
64             "settings" => ["value"],
65             "comments" => ["html", "text"],
66         ];
67
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}");
72             }
73         }
74
75         $jsonColumnsToUpdateByTable = [
76             "settings" => ["value"],
77         ];
78
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}");
85             }
86         }
87
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('============================================================================');
92         return 0;
93     }
94
95     /**
96      * Perform a find+replace operations in the provided table and column.
97      * Returns the count of rows changed.
98      */
99     protected function replaceValueInTable(string $table, string $column, string $oldUrl, string $newUrl): int
100     {
101         $oldQuoted = $this->db->getPdo()->quote($oldUrl);
102         $newQuoted = $this->db->getPdo()->quote($newUrl);
103         return $this->db->table($table)->update([
104             $column => $this->db->raw("REPLACE({$column}, {$oldQuoted}, {$newQuoted})")
105         ]);
106     }
107
108     /**
109      * Warn the user of the dangers of this operation.
110      * Returns a boolean indicating if they've accepted the warnings.
111      */
112     protected function checkUserOkayToProceed(string $oldUrl, string $newUrl): bool
113     {
114         $dangerWarning = "This will search for \"{$oldUrl}\" in your database and replace it with  \"{$newUrl}\".\n";
115         $dangerWarning .= "Are you sure you want to proceed?";
116         $backupConfirmation = "This operation could cause issues if used incorrectly. Have you made a backup of your existing database?";
117
118         return $this->confirm($dangerWarning) && $this->confirm($backupConfirmation);
119     }
120 }