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;
49 use Friendica\Model\Contact;
50 use Friendica\Model\GContact;
51 use Friendica\Model\Item;
52 use Friendica\Model\User;
53 use Friendica\Model\Storage;
54 use Friendica\Util\DateTimeFormat;
55 use Friendica\Worker\Delivery;
57 function update_1179()
59 if (DI::config()->get('system', 'no_community_page')) {
60 DI::config()->set('system', 'community_page_style', CP_NO_COMMUNITY_PAGE);
63 // Update the central item storage with uid=0
64 Worker::add(PRIORITY_LOW, "threadupdate");
66 return Update::SUCCESS;
69 function update_1181()
72 // Fill the new fields in the term table.
73 Worker::add(PRIORITY_LOW, "TagUpdate");
75 return Update::SUCCESS;
78 function update_1189()
81 if (strlen(DI::config()->get('system', 'directory_submit_url')) &&
82 !strlen(DI::config()->get('system', 'directory'))) {
83 DI::config()->set('system', 'directory', dirname(DI::config()->get('system', 'directory_submit_url')));
84 DI::config()->delete('system', 'directory_submit_url');
87 return Update::SUCCESS;
90 function update_1191()
92 DI::config()->set('system', 'maintenance', 1);
94 if (Addon::isEnabled('forumlist')) {
95 //delete forumlist manually from addon and hook table
96 // since Addon::uninstall() don't work here
97 DBA::delete('addon', ['name' => 'forumlist']);
98 DBA::delete('hook', ['file' => 'addon/forumlist/forumlist.php']);
101 // select old formlist addon entries
102 $r = q("SELECT `uid`, `cat`, `k`, `v` FROM `pconfig` WHERE `cat` = '%s' ",
103 DBA::escape('forumlist')
106 // convert old forumlist addon entries in new config entries
107 if (DBA::isResult($r)) {
108 foreach ($r as $rr) {
110 $family = $rr['cat'];
114 if ($key === 'randomise') {
115 DI::pConfig()->delete($uid, $family, $key);
118 if ($key === 'show_on_profile') {
120 DI::pConfig()->set($uid, 'feature', 'forumlist_profile', $value);
123 DI::pConfig()->delete($uid, $family, $key);
126 if ($key === 'show_on_network') {
128 DI::pConfig()->set($uid, 'feature', 'forumlist_widget', $value);
131 DI::pConfig()->delete($uid, $family, $key);
136 DI::config()->set('system', 'maintenance', 0);
138 return Update::SUCCESS;
141 function update_1203()
143 $r = q("UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)",
144 DBA::escape(User::ACCOUNT_TYPE_COMMUNITY),
145 DBA::escape(User::PAGE_FLAGS_COMMUNITY),
146 DBA::escape(User::PAGE_FLAGS_PRVGROUP)
150 function update_1244()
152 // Sets legacy_password for all legacy hashes
153 DBA::update('user', ['legacy_password' => true], ['SUBSTR(password, 1, 4) != "$2y$"']);
155 // All legacy hashes are re-hashed using the new secure hashing function
156 $stmt = DBA::select('user', ['uid', 'password'], ['legacy_password' => true]);
157 while ($user = DBA::fetch($stmt)) {
158 DBA::update('user', ['password' => User::hashPassword($user['password'])], ['uid' => $user['uid']]);
161 // Logged in users are forcibly logged out
162 DBA::delete('session', ['1 = 1']);
164 return Update::SUCCESS;
167 function update_1245()
169 $rino = DI::config()->get('system', 'rino_encrypt');
172 return Update::SUCCESS;
175 DI::config()->set('system', 'rino_encrypt', 1);
177 return Update::SUCCESS;
180 function update_1247()
182 // Removing hooks with the old name
183 DBA::e("DELETE FROM `hook`
184 WHERE `hook` LIKE 'plugin_%'");
186 // Make sure we install the new renamed ones
190 function update_1260()
192 DI::config()->set('system', 'maintenance', 1);
195 'maintenance_reason',
197 '%s: Updating author-id and owner-id in item and thread table. ',
198 DateTimeFormat::utcNow().' '.date('e')
202 $items = DBA::p("SELECT `id`, `owner-link`, `owner-name`, `owner-avatar`, `network` FROM `item`
203 WHERE `owner-id` = 0 AND `owner-link` != ''");
204 while ($item = DBA::fetch($items)) {
205 $contact = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
206 'photo' => $item['owner-avatar'], 'network' => $item['network']];
207 $cid = Contact::getIdForURL($item['owner-link'], 0, false, $contact);
211 Item::update(['owner-id' => $cid], ['id' => $item['id']]);
215 DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
216 SET `thread`.`owner-id` = `item`.`owner-id` WHERE `thread`.`owner-id` = 0");
218 $items = DBA::p("SELECT `id`, `author-link`, `author-name`, `author-avatar`, `network` FROM `item`
219 WHERE `author-id` = 0 AND `author-link` != ''");
220 while ($item = DBA::fetch($items)) {
221 $contact = ['url' => $item['author-link'], 'name' => $item['author-name'],
222 'photo' => $item['author-avatar'], 'network' => $item['network']];
223 $cid = Contact::getIdForURL($item['author-link'], 0, false, $contact);
227 Item::update(['author-id' => $cid], ['id' => $item['id']]);
231 DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
232 SET `thread`.`author-id` = `item`.`author-id` WHERE `thread`.`author-id` = 0");
234 DI::config()->set('system', 'maintenance', 0);
235 return Update::SUCCESS;
238 function update_1261()
240 // This fixes the results of an issue in the develop branch of 2018-05.
241 DBA::update('contact', ['blocked' => false, 'pending' => false], ['uid' => 0, 'blocked' => true, 'pending' => true]);
242 return Update::SUCCESS;
245 function update_1278()
247 DI::config()->set('system', 'maintenance', 1);
250 'maintenance_reason',
252 '%s: Updating post-type.',
253 DateTimeFormat::utcNow().' '.date('e')
257 Item::update(['post-type' => Item::PT_PAGE], ['bookmark' => true]);
258 Item::update(['post-type' => Item::PT_PERSONAL_NOTE], ['type' => 'note']);
260 DI::config()->set('system', 'maintenance', 0);
262 return Update::SUCCESS;
265 function update_1288()
267 // Updates missing `uri-id` values
269 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");
270 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");
272 return Update::SUCCESS;
275 // Post-update script of PR 5751
276 function update_1298()
278 $keys = ['gender', 'marital', 'sexual'];
279 foreach ($keys as $translateKey) {
280 $allData = DBA::select('profile', ['id', $translateKey]);
281 $allLangs = DI::l10n()->getAvailableLanguages();
284 foreach ($allData as $key => $data) {
285 $toTranslate = $data[$translateKey];
286 if ($toTranslate != '') {
287 foreach ($allLangs as $key => $lang) {
288 $a = new \stdClass();
291 // First we get the the localizations
292 if (file_exists("view/lang/$lang/strings.php")) {
293 include "view/lang/$lang/strings.php";
295 if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
296 include "addon/morechoice/lang/$lang/strings.php";
299 $localizedStrings = $a->strings;
302 $key = array_search($toTranslate, $localizedStrings);
303 if ($key !== false) {
307 // defaulting to empty string
314 DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
315 Logger::notice('Updated contact', ['action' => 'update', 'contact' => $data['id'], "$translateKey" => $key,
316 'was' => $data[$translateKey]]);
317 Worker::add(PRIORITY_LOW, 'ProfileUpdate', $data['id']);
318 Contact::updateSelfFromUserID($data['id']);
319 GContact::updateForUser($data['id']);
325 Logger::notice($translateKey . " fix completed", ['action' => 'update', 'translateKey' => $translateKey, 'Success' => $success, 'Fail' => $fail ]);
327 return Update::SUCCESS;
330 function update_1309()
332 $queue = DBA::select('queue', ['id', 'cid', 'guid']);
333 while ($entry = DBA::fetch($queue)) {
334 $contact = DBA::selectFirst('contact', ['uid'], ['id' => $entry['cid']]);
335 if (!DBA::isResult($contact)) {
339 $item = Item::selectFirst(['id', 'gravity'], ['uid' => $contact['uid'], 'guid' => $entry['guid']]);
340 if (!DBA::isResult($item)) {
344 $deliver_options = ['priority' => PRIORITY_MEDIUM, 'dont_fork' => true];
345 Worker::add($deliver_options, 'Delivery', Delivery::POST, $item['id'], $entry['cid']);
346 Logger::info('Added delivery worker', ['item' => $item['id'], 'contact' => $entry['cid']]);
347 DBA::delete('queue', ['id' => $entry['id']]);
349 return Update::SUCCESS;
352 function update_1315()
354 DBA::delete('item-delivery-data', ['postopts' => '', 'inform' => '', 'queue_count' => 0, 'queue_done' => 0]);
355 return Update::SUCCESS;
358 function update_1318()
360 DBA::update('profile', ['marital' => "In a relation"], ['marital' => "Unavailable"]);
361 DBA::update('profile', ['marital' => "Single"], ['marital' => "Available"]);
363 Worker::add(PRIORITY_LOW, 'ProfileUpdate');
364 return Update::SUCCESS;
367 function update_1323()
369 $users = DBA::select('user', ['uid']);
370 while ($user = DBA::fetch($users)) {
371 Contact::updateSelfFromUserID($user['uid']);
375 return Update::SUCCESS;
378 function update_1327()
380 $contacts = DBA::select('contact', ['uid', 'id', 'blocked', 'readonly'], ["`uid` != ? AND (`blocked` OR `readonly`) AND NOT `pending`", 0]);
381 while ($contact = DBA::fetch($contacts)) {
382 Contact::setBlockedForUser($contact['id'], $contact['uid'], $contact['blocked']);
383 Contact::setIgnoredForUser($contact['id'], $contact['uid'], $contact['readonly']);
385 DBA::close($contacts);
387 return Update::SUCCESS;
390 function update_1330()
392 $currStorage = DI::config()->get('storage', 'class', '');
394 // set the name of the storage instead of the classpath as config
395 if (!empty($currStorage)) {
396 /** @var Storage\IStorage $currStorage */
397 if (!DI::config()->set('storage', 'name', $currStorage::getName())) {
398 return Update::FAILED;
401 // try to delete the class since it isn't needed. This won't work with config files
402 DI::config()->delete('storage', 'class');
405 // Update attachments and photos
406 if (!DBA::p("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 25) WHERE `photo`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'") ||
407 !DBA::p("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 25) WHERE `attach`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'")) {
408 return Update::FAILED;
411 return Update::SUCCESS;
414 function update_1332()
416 $condition = ["`is-default` IS NOT NULL"];
417 $profiles = DBA::select('profile', [], $condition);
419 while ($profile = DBA::fetch($profiles)) {
420 DI::profileField()->migrateFromLegacyProfile($profile);
422 DBA::close($profiles);
424 DBA::update('contact', ['profile-id' => null], ['`profile-id` IS NOT NULL']);
426 return Update::SUCCESS;