3 * @copyright Copyright (C) 2020, Friendica
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/>.
20 * Automatic post-databse structure change updates
22 * These functions are responsible for doing critical post update changes to the data (not the structure) in the database.
24 * Database structure changes are done in static/dbstructure.config.php
26 * For non-critical database migrations, please add a method in the Database\PostUpdate class
28 * If there is a need for a post update to a structure change, update this file
29 * by adding a new function at the end with the number of the new DB_UPDATE_VERSION.
31 * The numbered script in this file has to be exactly like the DB_UPDATE_VERSION
34 * You are currently on version 4711 and you are preparing changes that demand an update script.
36 * 1. Create a function "update_4712()" here in the update.php
37 * 2. Apply the needed structural changes in static/dbStructure.php
38 * 3. Set DB_UPDATE_VERSION in static/dbstructure.config.php to 4712.
40 * If you need to run a script before the database update, name the function "pre_update_4712()"
43 use Friendica\Core\Addon;
44 use Friendica\Core\Logger;
45 use Friendica\Core\Update;
46 use Friendica\Core\Worker;
47 use Friendica\Database\DBA;
48 use Friendica\Database\DBStructure;
50 use Friendica\Model\Contact;
51 use Friendica\Model\GContact;
52 use Friendica\Model\Item;
53 use Friendica\Model\User;
54 use Friendica\Model\Storage;
55 use Friendica\Util\DateTimeFormat;
56 use Friendica\Worker\Delivery;
58 function update_1179()
60 if (DI::config()->get('system', 'no_community_page')) {
61 DI::config()->set('system', 'community_page_style', CP_NO_COMMUNITY_PAGE);
64 // Update the central item storage with uid=0
65 Worker::add(PRIORITY_LOW, "threadupdate");
67 return Update::SUCCESS;
70 function update_1181()
73 // Fill the new fields in the term table.
74 // deactivated, the "term" table is deprecated
75 // Worker::add(PRIORITY_LOW, "TagUpdate");
77 return Update::SUCCESS;
80 function update_1189()
83 if (strlen(DI::config()->get('system', 'directory_submit_url')) &&
84 !strlen(DI::config()->get('system', 'directory'))) {
85 DI::config()->set('system', 'directory', dirname(DI::config()->get('system', 'directory_submit_url')));
86 DI::config()->delete('system', 'directory_submit_url');
89 return Update::SUCCESS;
92 function update_1191()
94 DI::config()->set('system', 'maintenance', 1);
96 if (Addon::isEnabled('forumlist')) {
97 Addon::uninstall('forumlist');
100 // select old formlist addon entries
101 $r = q("SELECT `uid`, `cat`, `k`, `v` FROM `pconfig` WHERE `cat` = '%s' ",
102 DBA::escape('forumlist')
105 // convert old forumlist addon entries in new config entries
106 if (DBA::isResult($r)) {
107 foreach ($r as $rr) {
109 $family = $rr['cat'];
113 if ($key === 'randomise') {
114 DI::pConfig()->delete($uid, $family, $key);
117 if ($key === 'show_on_profile') {
119 DI::pConfig()->set($uid, 'feature', 'forumlist_profile', $value);
122 DI::pConfig()->delete($uid, $family, $key);
125 if ($key === 'show_on_network') {
127 DI::pConfig()->set($uid, 'feature', 'forumlist_widget', $value);
130 DI::pConfig()->delete($uid, $family, $key);
135 DI::config()->set('system', 'maintenance', 0);
137 return Update::SUCCESS;
140 function update_1203()
142 $r = q("UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)",
143 DBA::escape(User::ACCOUNT_TYPE_COMMUNITY),
144 DBA::escape(User::PAGE_FLAGS_COMMUNITY),
145 DBA::escape(User::PAGE_FLAGS_PRVGROUP)
149 function update_1244()
151 // Sets legacy_password for all legacy hashes
152 DBA::update('user', ['legacy_password' => true], ['SUBSTR(password, 1, 4) != "$2y$"']);
154 // All legacy hashes are re-hashed using the new secure hashing function
155 $stmt = DBA::select('user', ['uid', 'password'], ['legacy_password' => true]);
156 while ($user = DBA::fetch($stmt)) {
157 DBA::update('user', ['password' => User::hashPassword($user['password'])], ['uid' => $user['uid']]);
160 // Logged in users are forcibly logged out
161 DBA::delete('session', ['1 = 1']);
163 return Update::SUCCESS;
166 function update_1245()
168 $rino = DI::config()->get('system', 'rino_encrypt');
171 return Update::SUCCESS;
174 DI::config()->set('system', 'rino_encrypt', 1);
176 return Update::SUCCESS;
179 function update_1247()
181 // Removing hooks with the old name
182 DBA::e("DELETE FROM `hook`
183 WHERE `hook` LIKE 'plugin_%'");
185 // Make sure we install the new renamed ones
189 function update_1260()
191 DI::config()->set('system', 'maintenance', 1);
194 'maintenance_reason',
196 '%s: Updating author-id and owner-id in item and thread table. ',
197 DateTimeFormat::utcNow().' '.date('e')
201 $items = DBA::p("SELECT `id`, `owner-link`, `owner-name`, `owner-avatar`, `network` FROM `item`
202 WHERE `owner-id` = 0 AND `owner-link` != ''");
203 while ($item = DBA::fetch($items)) {
204 $contact = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
205 'photo' => $item['owner-avatar'], 'network' => $item['network']];
206 $cid = Contact::getIdForURL($item['owner-link'], 0, false, $contact);
210 Item::update(['owner-id' => $cid], ['id' => $item['id']]);
214 DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
215 SET `thread`.`owner-id` = `item`.`owner-id` WHERE `thread`.`owner-id` = 0");
217 $items = DBA::p("SELECT `id`, `author-link`, `author-name`, `author-avatar`, `network` FROM `item`
218 WHERE `author-id` = 0 AND `author-link` != ''");
219 while ($item = DBA::fetch($items)) {
220 $contact = ['url' => $item['author-link'], 'name' => $item['author-name'],
221 'photo' => $item['author-avatar'], 'network' => $item['network']];
222 $cid = Contact::getIdForURL($item['author-link'], 0, false, $contact);
226 Item::update(['author-id' => $cid], ['id' => $item['id']]);
230 DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
231 SET `thread`.`author-id` = `item`.`author-id` WHERE `thread`.`author-id` = 0");
233 DI::config()->set('system', 'maintenance', 0);
234 return Update::SUCCESS;
237 function update_1261()
239 // This fixes the results of an issue in the develop branch of 2018-05.
240 DBA::update('contact', ['blocked' => false, 'pending' => false], ['uid' => 0, 'blocked' => true, 'pending' => true]);
241 return Update::SUCCESS;
244 function update_1278()
246 DI::config()->set('system', 'maintenance', 1);
249 'maintenance_reason',
251 '%s: Updating post-type.',
252 DateTimeFormat::utcNow().' '.date('e')
256 Item::update(['post-type' => Item::PT_PAGE], ['bookmark' => true]);
257 Item::update(['post-type' => Item::PT_PERSONAL_NOTE], ['type' => 'note']);
259 DI::config()->set('system', 'maintenance', 0);
261 return Update::SUCCESS;
264 function update_1288()
266 // Updates missing `uri-id` values
268 DBA::e("UPDATE `item-activity` INNER JOIN `item` ON `item`.`iaid` = `item-activity`.`id` SET `item-activity`.`uri-id` = `item`.`uri-id` WHERE `item-activity`.`uri-id` IS NULL OR `item-activity`.`uri-id` = 0");
269 DBA::e("UPDATE `item-content` INNER JOIN `item` ON `item`.`icid` = `item-content`.`id` SET `item-content`.`uri-id` = `item`.`uri-id` WHERE `item-content`.`uri-id` IS NULL OR `item-content`.`uri-id` = 0");
271 return Update::SUCCESS;
274 // Post-update script of PR 5751
275 function update_1298()
277 $keys = ['gender', 'marital', 'sexual'];
278 foreach ($keys as $translateKey) {
279 $allData = DBA::select('profile', ['id', $translateKey]);
280 $allLangs = DI::l10n()->getAvailableLanguages();
283 foreach ($allData as $key => $data) {
284 $toTranslate = $data[$translateKey];
285 if ($toTranslate != '') {
286 foreach ($allLangs as $key => $lang) {
287 $a = new \stdClass();
290 // First we get the the localizations
291 if (file_exists("view/lang/$lang/strings.php")) {
292 include "view/lang/$lang/strings.php";
294 if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
295 include "addon/morechoice/lang/$lang/strings.php";
298 $localizedStrings = $a->strings;
301 $key = array_search($toTranslate, $localizedStrings);
302 if ($key !== false) {
306 // defaulting to empty string
313 DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
314 Logger::notice('Updated contact', ['action' => 'update', 'contact' => $data['id'], "$translateKey" => $key,
315 'was' => $data[$translateKey]]);
316 Worker::add(PRIORITY_LOW, 'ProfileUpdate', $data['id']);
317 Contact::updateSelfFromUserID($data['id']);
318 GContact::updateForUser($data['id']);
324 Logger::notice($translateKey . " fix completed", ['action' => 'update', 'translateKey' => $translateKey, 'Success' => $success, 'Fail' => $fail ]);
326 return Update::SUCCESS;
329 function update_1309()
331 $queue = DBA::select('queue', ['id', 'cid', 'guid']);
332 while ($entry = DBA::fetch($queue)) {
333 $contact = DBA::selectFirst('contact', ['uid'], ['id' => $entry['cid']]);
334 if (!DBA::isResult($contact)) {
338 $item = Item::selectFirst(['id', 'gravity'], ['uid' => $contact['uid'], 'guid' => $entry['guid']]);
339 if (!DBA::isResult($item)) {
343 $deliver_options = ['priority' => PRIORITY_MEDIUM, 'dont_fork' => true];
344 Worker::add($deliver_options, 'Delivery', Delivery::POST, $item['id'], $entry['cid']);
345 Logger::info('Added delivery worker', ['item' => $item['id'], 'contact' => $entry['cid']]);
346 DBA::delete('queue', ['id' => $entry['id']]);
348 return Update::SUCCESS;
351 function update_1315()
353 DBA::delete('item-delivery-data', ['postopts' => '', 'inform' => '', 'queue_count' => 0, 'queue_done' => 0]);
354 return Update::SUCCESS;
357 function update_1318()
359 DBA::update('profile', ['marital' => "In a relation"], ['marital' => "Unavailable"]);
360 DBA::update('profile', ['marital' => "Single"], ['marital' => "Available"]);
362 Worker::add(PRIORITY_LOW, 'ProfileUpdate');
363 return Update::SUCCESS;
366 function update_1323()
368 $users = DBA::select('user', ['uid']);
369 while ($user = DBA::fetch($users)) {
370 Contact::updateSelfFromUserID($user['uid']);
374 return Update::SUCCESS;
377 function update_1327()
379 $contacts = DBA::select('contact', ['uid', 'id', 'blocked', 'readonly'], ["`uid` != ? AND (`blocked` OR `readonly`) AND NOT `pending`", 0]);
380 while ($contact = DBA::fetch($contacts)) {
381 Contact::setBlockedForUser($contact['id'], $contact['uid'], $contact['blocked']);
382 Contact::setIgnoredForUser($contact['id'], $contact['uid'], $contact['readonly']);
384 DBA::close($contacts);
386 return Update::SUCCESS;
389 function update_1330()
391 $currStorage = DI::config()->get('storage', 'class', '');
393 // set the name of the storage instead of the classpath as config
394 if (!empty($currStorage)) {
395 /** @var Storage\IStorage $currStorage */
396 if (!DI::config()->set('storage', 'name', $currStorage::getName())) {
397 return Update::FAILED;
400 // try to delete the class since it isn't needed. This won't work with config files
401 DI::config()->delete('storage', 'class');
404 // Update attachments and photos
405 if (!DBA::p("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 25) WHERE `photo`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'") ||
406 !DBA::p("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 25) WHERE `attach`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'")) {
407 return Update::FAILED;
410 return Update::SUCCESS;
413 function update_1332()
415 $condition = ["`is-default` IS NOT NULL"];
416 $profiles = DBA::select('profile', [], $condition);
418 while ($profile = DBA::fetch($profiles)) {
419 DI::profileField()->migrateFromLegacyProfile($profile);
421 DBA::close($profiles);
423 DBA::update('contact', ['profile-id' => null], ['`profile-id` IS NOT NULL']);
425 return Update::SUCCESS;
428 function update_1347()
430 foreach (Item::ACTIVITIES as $index => $activity) {
431 DBA::insert('verb', ['id' => $index + 1, 'name' => $activity], true);
434 return Update::SUCCESS;
437 function pre_update_1348()
439 if (!DBA::exists('contact', ['id' => 0])) {
440 DBA::insert('contact', ['nurl' => '']);
441 $lastid = DBA::lastInsertId();
443 DBA::update('contact', ['id' => 0], ['id' => $lastid]);
447 // The tables "permissionset" and "tag" could or could not exist during the update.
448 // This depends upon the previous version. Depending upon this situation we have to add
449 // the "0" values before adding the foreign keys - or after would be sufficient.
453 return Update::SUCCESS;
456 function update_1348()
458 // Insert a permissionset with id=0
459 // Inserting it without an ID and then changing the value to 0 tricks the auto increment
460 if (!DBA::exists('permissionset', ['id' => 0])) {
461 DBA::insert('permissionset', ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']);
462 $lastid = DBA::lastInsertId();
464 DBA::update('permissionset', ['id' => 0], ['id' => $lastid]);
468 if (!DBA::exists('tag', ['id' => 0])) {
469 DBA::insert('tag', ['name' => '']);
470 $lastid = DBA::lastInsertId();
472 DBA::update('tag', ['id' => 0], ['id' => $lastid]);
476 return Update::SUCCESS;
479 function update_1349()
482 foreach (Item::ACTIVITIES as $index => $activity) {
483 if (!DBA::exists('verb', ['id' => $index + 1, 'name' => $activity])) {
489 // The update failed - but it cannot be recovered, since the data doesn't match our expectation
490 // This means that we can't use this "shortcut" to fill the "vid" field and we have to rely upon
491 // the postupdate. This is not fatal, but means that it will take some longer time for the system
493 return Update::SUCCESS;
496 if (!DBA::e("UPDATE `item` INNER JOIN `item-activity` ON `item`.`uri-id` = `item-activity`.`uri-id`
497 SET `vid` = `item-activity`.`activity` + 1 WHERE `gravity` = ? AND (`vid` IS NULL OR `vid` = 0)", GRAVITY_ACTIVITY)) {
498 return Update::FAILED;
501 return Update::SUCCESS;
504 function update_1351()
506 if (!DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id` SET `thread`.`uri-id` = `item`.`uri-id`")) {
507 return Update::FAILED;
510 return Update::SUCCESS;
513 function pre_update_1354()
515 if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
516 && !DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])
517 && !DBA::e("ALTER TABLE `contact` CHANGE `ffi_keyword_blacklist` `ffi_keyword_denylist` text null")) {
518 return Update::FAILED;
521 return Update::SUCCESS;