3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Console;
24 use Asika\SimpleConsole\Console;
25 use Friendica\Core\Config\Capability\IManageConfigValues;
26 use Friendica\Core\Worker;
27 use Friendica\Util\Strings;
28 use Friendica\Worker\Delivery;
30 class Relocate extends Console
32 protected $helpOptions = ['h', 'help', '?'];
35 * @var IManageConfigValues
39 * @var \Friendica\App\BaseURL
43 * @var \Friendica\Database\Database
47 protected function getHelp()
50 console relocate - Update the node base URL
52 bin/console relocate <new base URL> [-h|--help|-?] [-v]
55 Warning! Advanced function. Could make this server unreachable.
57 Change the base URL for this server. Sends relocation message to all the Friendica and Diaspora* contacts of all local users.
58 This process updates all the database fields that may contain a URL pointing at the current domain, as a result it takes
59 a while and the node will be in maintenance mode for the whole duration.
62 -h|--help|-? Show help information
63 -v Show more debug information.
68 public function __construct(\Friendica\App\BaseURL $baseUrl, \Friendica\Database\Database $database, IManageConfigValues $config, $argv = null)
70 parent::__construct($argv);
72 $this->baseUrl = $baseUrl;
73 $this->database = $database;
74 $this->config = $config;
77 protected function doExecute(): int
79 if (count($this->args) == 0) {
80 $this->out($this->getHelp());
84 if (count($this->args) > 1) {
85 throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
88 $new_url = rtrim($this->getArgument(0), '/');
90 $parsed = @parse_url($new_url);
91 if (!is_array($parsed) || empty($parsed['host']) || empty($parsed['scheme'])) {
92 throw new \InvalidArgumentException('Can not parse new base URL. Must have at least <scheme>://<domain>');
95 $this->out(sprintf('Relocation started from %s to %s. Could take a while to complete.', $this->baseUrl->get(true), $this->getArgument(0)));
97 $old_url = $this->baseUrl->get(true);
99 // Generate host names for relocation the addresses in the format user@address.tld
100 $new_host = str_replace('http://', '@', Strings::normaliseLink($new_url));
101 $old_host = str_replace('http://', '@', Strings::normaliseLink($old_url));
103 $this->out('Entering maintenance mode');
104 $this->config->set('system', 'maintenance', true);
105 $this->config->set('system', 'maintenance_reason', 'Relocating node to ' . $new_url);
108 if (!$this->database->transaction()) {
109 throw new \Exception('Unable to start a transaction, please retry later.');
113 $this->out('Updating apcontact table fields');
114 $this->database->replaceInTableFields('apcontact', ['url', 'inbox', 'outbox', 'sharedinbox', 'photo', 'header', 'alias', 'subscribe', 'baseurl'], $old_url, $new_url);
115 $this->database->replaceInTableFields('apcontact', ['addr'], $old_host, $new_host);
117 $this->out('Updating contact table fields');
118 $this->database->replaceInTableFields('contact', ['photo', 'thumb', 'micro', 'url', 'alias', 'request', 'batch', 'notify', 'poll', 'subscribe', 'baseurl', 'confirm', 'poco', 'avatar', 'header'], $old_url, $new_url);
119 $this->database->replaceInTableFields('contact', ['nurl'], Strings::normaliseLink($old_url), Strings::normaliseLink($new_url));
120 $this->database->replaceInTableFields('contact', ['addr'], $old_host, $new_host);
122 $this->out('Updating conv table fields');
123 $this->database->replaceInTableFields('conv', ['creator', 'recips'], $old_host, $new_host);
125 $this->out('Updating delayed-post table fields');
126 $this->database->replaceInTableFields('delayed-post', ['uri'], $old_url, $new_url);
128 $this->out('Updating endpoint table fields');
129 $this->database->replaceInTableFields('endpoint', ['url'], $old_url, $new_url);
131 $this->out('Updating event table fields');
132 $this->database->replaceInTableFields('event', ['uri'], $old_url, $new_url);
134 $this->out('Updating fcontact table fields');
135 $this->database->replaceInTableFields('fcontact', ['url', 'photo', 'request', 'batch', 'poll', 'confirm', 'alias'], $old_url, $new_url);
136 $this->database->replaceInTableFields('fcontact', ['addr'], $old_host, $new_host);
138 $this->out('Updating fsuggest table fields');
139 $this->database->replaceInTableFields('fsuggest', ['url', 'request', 'photo'], $old_url, $new_url);
141 $this->out('Updating gserver table fields');
142 $this->database->replaceInTableFields('gserver', ['url'], $old_url, $new_url);
143 $this->database->replaceInTableFields('gserver', ['nurl'], Strings::normaliseLink($old_url), Strings::normaliseLink($new_url));
145 $this->out('Updating inbox-status table fields');
146 $this->database->replaceInTableFields('inbox-status', ['url'], $old_url, $new_url);
148 $this->out('Updating item-uri table fields');
149 $this->database->replaceInTableFields('item-uri', ['uri'], $old_url, $new_url);
151 $this->out('Updating mail table fields');
152 $this->database->replaceInTableFields('mail', ['from-photo', 'from-url', 'uri', 'thr-parent'], $old_url, $new_url);
153 $this->database->replaceInTableFields('mail', ['parent-uri'], $old_host, $new_host);
155 $this->out('Updating notify table fields');
156 $this->database->replaceInTableFields('notify', ['url', 'photo', 'link', 'msg', 'name_cache', 'msg_cache'], $old_url, $new_url);
158 $this->out('Updating profile table fields');
159 $this->database->replaceInTableFields('profile', ['photo', 'thumb'], $old_url, $new_url);
161 $this->out('Updating post-content table fields');
162 $this->database->replaceInTableFields('post-content', ['body', 'raw-body', 'rendered-html', 'target', 'plink'], $old_url, $new_url);
163 $this->database->replaceInTableFields('post-content', ['body', 'raw-body', 'rendered-html', 'target'], $old_host, $new_host);
165 $this->out('Updating post-history table fields');
166 $this->database->replaceInTableFields('post-history', ['body', 'raw-body', 'rendered-html', 'target', 'plink'], $old_url, $new_url);
167 $this->database->replaceInTableFields('post-history', ['body', 'raw-body', 'rendered-html', 'target'], $old_host, $new_host);
169 $this->out('Updating post-link table fields');
170 $this->database->replaceInTableFields('post-link', ['url'], $old_url, $new_url);
172 $this->out('Updating post-media table fields');
173 $this->database->replaceInTableFields('post-media', ['url', 'preview', 'author-url', 'author-image', 'publisher-url', 'publisher-image'], $old_url, $new_url);
175 $this->out('Updating tag table fields');
176 $this->database->replaceInTableFields('tag', ['url'], $old_url, $new_url);
179 $this->out('Updating config values');
180 $this->config->set('system', 'url', $new_url);
181 $this->baseUrl->saveByURL($new_url);
183 $this->database->commit();
184 } catch (\Throwable $e) {
185 $this->database->rollback();
187 $this->out('Process aborted with message: ' . $e->getMessage() . ' thrown in ' . $e->getFile() . ':' . $e->getLine());
191 $this->out('Leaving maintenance mode');
192 $this->config->set('system', 'maintenance', false);
193 $this->config->set('system', 'maintenance_reason', '');
197 $this->out('Schedule relocation messages to remote Friendica and Diaspora hosts');
198 $users = $this->database->selectToArray('user', ['uid'], ['account_removed' => false, 'account_expired' => false]);
199 foreach ($users as $user) {
200 Worker::add(PRIORITY_HIGH, 'Notifier', Delivery::RELOCATION, $user['uid']);