]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/UpdateUrl.php
Implement the renderPages parameter
[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
19     /**
20      * The console command description.
21      *
22      * @var string
23      */
24     protected $description = 'Find and replace the given URLs in your BookStack database';
25
26     protected $db;
27
28     /**
29      * Create a new command instance.
30      *
31      * @return void
32      */
33     public function __construct(Connection $db)
34     {
35         $this->db = $db;
36         parent::__construct();
37     }
38
39     /**
40      * Execute the console command.
41      *
42      * @return mixed
43      */
44     public function handle()
45     {
46         $oldUrl = str_replace("'", '', $this->argument('oldUrl'));
47         $newUrl = str_replace("'", '', $this->argument('newUrl'));
48
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://");
52             return 1;
53         }
54
55         if (!$this->checkUserOkayToProceed($oldUrl, $newUrl)) {
56             return 1;
57         }
58
59         $columnsToUpdateByTable = [
60             "attachments" => ["path"],
61             "pages" => ["html", "text", "markdown"],
62             "images" => ["url"],
63             "comments" => ["html", "text"],
64         ];
65
66         foreach ($columnsToUpdateByTable as $table => $columns) {
67             foreach ($columns as $column) {
68                 $changeCount = $this->db->table($table)->update([
69                     $column => $this->db->raw("REPLACE({$column}, '{$oldUrl}', '{$newUrl}')")
70                 ]);
71                 $this->info("Updated {$changeCount} rows in {$table}->{$column}");
72             }
73         }
74
75         $this->info("URL update procedure complete.");
76         return 0;
77     }
78
79     /**
80      * Warn the user of the dangers of this operation.
81      * Returns a boolean indicating if they've accepted the warnings.
82      */
83     protected function checkUserOkayToProceed(string $oldUrl, string $newUrl): bool
84     {
85         $dangerWarning = "This will search for \"{$oldUrl}\" in your database and replace it with  \"{$newUrl}\".\n";
86         $dangerWarning .= "Are you sure you want to proceed?";
87         $backupConfirmation = "This operation could cause issues if used incorrectly. Have you made a backup of your existing database?";
88
89         return $this->confirm($dangerWarning) && $this->confirm($backupConfirmation);
90     }
91 }