]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/UpdateUrl.php
Updated System CLI
[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
8 class UpdateUrl 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      * @return mixed
31      */
32     public function handle(Connection $db)
33     {
34         $oldUrl = str_replace("'", '', $this->argument('oldUrl'));
35         $newUrl = str_replace("'", '', $this->argument('newUrl'));
36
37         $urlPattern = '/https?:\/\/(.+)/';
38         if (!preg_match($urlPattern, $oldUrl) || !preg_match($urlPattern, $newUrl)) {
39             $this->error('The given urls are expected to be full urls starting with http:// or https://');
40
41             return 1;
42         }
43
44         if (!$this->checkUserOkayToProceed($oldUrl, $newUrl)) {
45             return 1;
46         }
47
48         $columnsToUpdateByTable = [
49             'attachments' => ['path'],
50             'pages'       => ['html', 'text', 'markdown'],
51             'images'      => ['url'],
52             'settings'    => ['value'],
53             'comments'    => ['html', 'text'],
54         ];
55
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}");
60             }
61         }
62
63         $jsonColumnsToUpdateByTable = [
64             'settings' => ['value'],
65         ];
66
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}");
73             }
74         }
75
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.');
79         $this->info('============================================================================');
80
81         return 0;
82     }
83
84     /**
85      * Perform a find+replace operations in the provided table and column.
86      * Returns the count of rows changed.
87      */
88     protected function replaceValueInTable(
89         Connection $db,
90         string $table,
91         string $column,
92         string $oldUrl,
93         string $newUrl
94     ): int {
95         $oldQuoted = $db->getPdo()->quote($oldUrl);
96         $newQuoted = $db->getPdo()->quote($newUrl);
97
98         return $db->table($table)->update([
99             $column => $db->raw("REPLACE({$column}, {$oldQuoted}, {$newQuoted})"),
100         ]);
101     }
102
103     /**
104      * Warn the user of the dangers of this operation.
105      * Returns a boolean indicating if they've accepted the warnings.
106      */
107     protected function checkUserOkayToProceed(string $oldUrl, string $newUrl): bool
108     {
109         if ($this->option('force')) {
110             return true;
111         }
112
113         $dangerWarning = "This will search for \"{$oldUrl}\" in your database and replace it with  \"{$newUrl}\".\n";
114         $dangerWarning .= 'Are you sure you want to proceed?';
115         $backupConfirmation = 'This operation could cause issues if used incorrectly. Have you made a backup of your existing database?';
116
117         return $this->confirm($dangerWarning) && $this->confirm($backupConfirmation);
118     }
119 }