]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/UpdateUrlCommand.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / app / Console / Commands / UpdateUrlCommand.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use Illuminate\Console\Command;
6 use Illuminate\Database\Connection;
7
8 class UpdateUrlCommand extends Command
9 {
10     /**
11      * The name and signature of the console command.
12      *
13      * @var string
14      */
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}';
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     /**
28      * Execute the console command.
29      */
30     public function handle(Connection $db): int
31     {
32         $oldUrl = str_replace("'", '', $this->argument('oldUrl'));
33         $newUrl = str_replace("'", '', $this->argument('newUrl'));
34
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://');
38
39             return 1;
40         }
41
42         if (!$this->checkUserOkayToProceed($oldUrl, $newUrl)) {
43             return 1;
44         }
45
46         $columnsToUpdateByTable = [
47             'attachments' => ['path'],
48             'pages'       => ['html', 'text', 'markdown'],
49             'chapters'    => ['description_html'],
50             'books'       => ['description_html'],
51             'bookshelves' => ['description_html'],
52             'page_revisions' => ['html', 'text', 'markdown'],
53             'images'      => ['url'],
54             'settings'    => ['value'],
55             'comments'    => ['html', 'text'],
56         ];
57
58         foreach ($columnsToUpdateByTable as $table => $columns) {
59             foreach ($columns as $column) {
60                 $changeCount = $this->replaceValueInTable($db, $table, $column, $oldUrl, $newUrl);
61                 $this->info("Updated {$changeCount} rows in {$table}->{$column}");
62             }
63         }
64
65         $jsonColumnsToUpdateByTable = [
66             'settings' => ['value'],
67         ];
68
69         foreach ($jsonColumnsToUpdateByTable as $table => $columns) {
70             foreach ($columns as $column) {
71                 $oldJson = trim(json_encode($oldUrl), '"');
72                 $newJson = trim(json_encode($newUrl), '"');
73                 $changeCount = $this->replaceValueInTable($db, $table, $column, $oldJson, $newJson);
74                 $this->info("Updated {$changeCount} JSON encoded rows in {$table}->{$column}");
75             }
76         }
77
78         $this->info('URL update procedure complete.');
79         $this->info('============================================================================');
80         $this->info('Be sure to run "php artisan cache:clear" to clear any old URLs in the cache.');
81
82         if (!str_starts_with($newUrl, url('/'))) {
83             $this->warn('You still need to update your APP_URL env value. This is currently set to:');
84             $this->warn(url('/'));
85         }
86
87         $this->info('============================================================================');
88
89         return 0;
90     }
91
92     /**
93      * Perform a find+replace operations in the provided table and column.
94      * Returns the count of rows changed.
95      */
96     protected function replaceValueInTable(
97         Connection $db,
98         string $table,
99         string $column,
100         string $oldUrl,
101         string $newUrl
102     ): int {
103         $oldQuoted = $db->getPdo()->quote($oldUrl);
104         $newQuoted = $db->getPdo()->quote($newUrl);
105
106         return $db->table($table)->update([
107             $column => $db->raw("REPLACE({$column}, {$oldQuoted}, {$newQuoted})"),
108         ]);
109     }
110
111     /**
112      * Warn the user of the dangers of this operation.
113      * Returns a boolean indicating if they've accepted the warnings.
114      */
115     protected function checkUserOkayToProceed(string $oldUrl, string $newUrl): bool
116     {
117         if ($this->option('force')) {
118             return true;
119         }
120
121         $dangerWarning = "This will search for \"{$oldUrl}\" in your database and replace it with  \"{$newUrl}\".\n";
122         $dangerWarning .= 'Are you sure you want to proceed?';
123         $backupConfirmation = 'This operation could cause issues if used incorrectly. Have you made a backup of your existing database?';
124
125         return $this->confirm($dangerWarning) && $this->confirm($backupConfirmation);
126     }
127 }