3 use Friendica\Core\Addon;
4 use Friendica\Core\Config;
5 use Friendica\Core\L10n;
6 use Friendica\Core\Logger;
7 use Friendica\Core\PConfig;
8 use Friendica\Core\Update;
9 use Friendica\Core\Worker;
10 use Friendica\Database\DBA;
11 use Friendica\Model\Contact;
12 use Friendica\Model\GContact;
13 use Friendica\Model\Item;
14 use Friendica\Model\User;
15 use Friendica\Util\DateTimeFormat;
16 use Friendica\Worker\Delivery;
20 * update.php - automatic system update
22 * This function is responsible for doing post update changes to the data
23 * (not the structure) in the database.
25 * Database structure changes are done in static/dbstructure.config.php
27 * If there is a need for a post process to a structure change, update this file
28 * by adding a new function at the end with the number of the new DB_UPDATE_VERSION.
30 * The numbered script in this file has to be exactly like the DB_UPDATE_VERSION
33 * You are currently on version 4711 and you are preparing changes that demand an update script.
35 * 1. Create a function "update_4712()" here in the update.php
36 * 2. Apply the needed structural changes in static/dbStructure.php
37 * 3. Set DB_UPDATE_VERSION in static/dbstructure.config.php to 4712.
39 * If you need to run a script before the database update, name the function "pre_update_4712()"
42 function update_1178()
44 require_once 'mod/profiles.php';
46 $profiles = q("SELECT `uid`, `about`, `locality`, `pub_keywords`, `gender` FROM `profile` WHERE `is-default`");
48 foreach ($profiles as $profile) {
49 if ($profile["about"].$profile["locality"].$profile["pub_keywords"].$profile["gender"] == "") {
53 $profile["pub_keywords"] = profile_clean_keywords($profile["pub_keywords"]);
55 $r = q("UPDATE `contact` SET `about` = '%s', `location` = '%s', `keywords` = '%s', `gender` = '%s' WHERE `self` AND `uid` = %d",
56 DBA::escape($profile["about"]),
57 DBA::escape($profile["locality"]),
58 DBA::escape($profile["pub_keywords"]),
59 DBA::escape($profile["gender"]),
60 intval($profile["uid"])
65 function update_1179()
67 if (Config::get('system', 'no_community_page')) {
68 Config::set('system', 'community_page_style', CP_NO_COMMUNITY_PAGE);
71 // Update the central item storage with uid=0
72 Worker::add(PRIORITY_LOW, "threadupdate");
74 return Update::SUCCESS;
77 function update_1181()
80 // Fill the new fields in the term table.
81 Worker::add(PRIORITY_LOW, "TagUpdate");
83 return Update::SUCCESS;
86 function update_1189()
89 if (strlen(Config::get('system', 'directory_submit_url')) &&
90 !strlen(Config::get('system', 'directory'))) {
91 Config::set('system', 'directory', dirname(Config::get('system', 'directory_submit_url')));
92 Config::delete('system', 'directory_submit_url');
95 return Update::SUCCESS;
98 function update_1191()
100 Config::set('system', 'maintenance', 1);
102 if (Addon::isEnabled('forumlist')) {
103 $addon = 'forumlist';
104 $addons = Config::get('system', 'addon');
108 $addons_arr = explode(",", str_replace(" ", "", $addons));
110 $idx = array_search($addon, $addons_arr);
111 if ($idx !== false) {
112 unset($addons_arr[$idx]);
113 //delete forumlist manually from addon and hook table
114 // since Addon::uninstall() don't work here
115 q("DELETE FROM `addon` WHERE `name` = 'forumlist' ");
116 q("DELETE FROM `hook` WHERE `file` = 'addon/forumlist/forumlist.php' ");
117 Config::set('system', 'addon', implode(", ", $addons_arr));
122 // select old formlist addon entries
123 $r = q("SELECT `uid`, `cat`, `k`, `v` FROM `pconfig` WHERE `cat` = '%s' ",
124 DBA::escape('forumlist')
127 // convert old forumlist addon entries in new config entries
128 if (DBA::isResult($r)) {
129 foreach ($r as $rr) {
131 $family = $rr['cat'];
135 if ($key === 'randomise') {
136 PConfig::delete($uid, $family, $key);
139 if ($key === 'show_on_profile') {
141 PConfig::set($uid, feature, forumlist_profile, $value);
144 PConfig::delete($uid, $family, $key);
147 if ($key === 'show_on_network') {
149 PConfig::set($uid, feature, forumlist_widget, $value);
152 PConfig::delete($uid, $family, $key);
157 Config::set('system', 'maintenance', 0);
159 return Update::SUCCESS;
162 function update_1203()
164 $r = q("UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)",
165 DBA::escape(User::ACCOUNT_TYPE_COMMUNITY),
166 DBA::escape(User::PAGE_FLAGS_COMMUNITY),
167 DBA::escape(User::PAGE_FLAGS_PRVGROUP)
171 function update_1244()
173 // Sets legacy_password for all legacy hashes
174 DBA::update('user', ['legacy_password' => true], ['SUBSTR(password, 1, 4) != "$2y$"']);
176 // All legacy hashes are re-hashed using the new secure hashing function
177 $stmt = DBA::select('user', ['uid', 'password'], ['legacy_password' => true]);
178 while ($user = DBA::fetch($stmt)) {
179 DBA::update('user', ['password' => User::hashPassword($user['password'])], ['uid' => $user['uid']]);
182 // Logged in users are forcibly logged out
183 DBA::delete('session', ['1 = 1']);
185 return Update::SUCCESS;
188 function update_1245()
190 $rino = Config::get('system', 'rino_encrypt');
193 return Update::SUCCESS;
196 Config::set('system', 'rino_encrypt', 1);
198 return Update::SUCCESS;
201 function update_1247()
203 // Removing hooks with the old name
204 DBA::e("DELETE FROM `hook`
205 WHERE `hook` LIKE 'plugin_%'");
207 // Make sure we install the new renamed ones
211 function update_1260()
213 Config::set('system', 'maintenance', 1);
216 'maintenance_reason',
218 '%s: Updating author-id and owner-id in item and thread table. ',
219 DateTimeFormat::utcNow().' '.date('e')
223 $items = DBA::p("SELECT `id`, `owner-link`, `owner-name`, `owner-avatar`, `network` FROM `item`
224 WHERE `owner-id` = 0 AND `owner-link` != ''");
225 while ($item = DBA::fetch($items)) {
226 $contact = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
227 'photo' => $item['owner-avatar'], 'network' => $item['network']];
228 $cid = Contact::getIdForURL($item['owner-link'], 0, false, $contact);
232 Item::update(['owner-id' => $cid], ['id' => $item['id']]);
236 DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
237 SET `thread`.`owner-id` = `item`.`owner-id` WHERE `thread`.`owner-id` = 0");
239 $items = DBA::p("SELECT `id`, `author-link`, `author-name`, `author-avatar`, `network` FROM `item`
240 WHERE `author-id` = 0 AND `author-link` != ''");
241 while ($item = DBA::fetch($items)) {
242 $contact = ['url' => $item['author-link'], 'name' => $item['author-name'],
243 'photo' => $item['author-avatar'], 'network' => $item['network']];
244 $cid = Contact::getIdForURL($item['author-link'], 0, false, $contact);
248 Item::update(['author-id' => $cid], ['id' => $item['id']]);
252 DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
253 SET `thread`.`author-id` = `item`.`author-id` WHERE `thread`.`author-id` = 0");
255 Config::set('system', 'maintenance', 0);
256 return Update::SUCCESS;
259 function update_1261()
261 // This fixes the results of an issue in the develop branch of 2018-05.
262 DBA::update('contact', ['blocked' => false, 'pending' => false], ['uid' => 0, 'blocked' => true, 'pending' => true]);
263 return Update::SUCCESS;
266 function update_1278()
268 Config::set('system', 'maintenance', 1);
271 'maintenance_reason',
273 '%s: Updating post-type.',
274 DateTimeFormat::utcNow().' '.date('e')
278 Item::update(['post-type' => Item::PT_PAGE], ['bookmark' => true]);
279 Item::update(['post-type' => Item::PT_PERSONAL_NOTE], ['type' => 'note']);
281 Config::set('system', 'maintenance', 0);
283 return Update::SUCCESS;
286 function update_1288()
288 // Updates missing `uri-id` values
290 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");
291 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");
293 return Update::SUCCESS;
296 // Post-update script of PR 5751
297 function update_1298()
299 $keys = ['gender', 'marital', 'sexual'];
300 foreach ($keys as $translateKey) {
301 $allData = DBA::select('profile', ['id', $translateKey]);
302 $allLangs = L10n::getAvailableLanguages();
305 foreach ($allData as $key => $data) {
306 $toTranslate = $data[$translateKey];
307 if ($toTranslate != '') {
308 foreach ($allLangs as $key => $lang) {
309 $a = new \stdClass();
312 // First we get the the localizations
313 if (file_exists("view/lang/$lang/strings.php")) {
314 include "view/lang/$lang/strings.php";
316 if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
317 include "addon/morechoice/lang/$lang/strings.php";
320 $localizedStrings = $a->strings;
323 $key = array_search($toTranslate, $localizedStrings);
324 if ($key !== false) {
328 // defaulting to empty string
335 DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
336 Logger::notice('Updated contact', ['action' => 'update', 'contact' => $data['id'], "$translateKey" => $key,
337 'was' => $data[$translateKey]]);
338 Worker::add(PRIORITY_LOW, 'ProfileUpdate', $data['id']);
339 Contact::updateSelfFromUserID($data['id']);
340 GContact::updateForUser($data['id']);
346 Logger::notice($translateKey . " fix completed", ['action' => 'update', 'translateKey' => $translateKey, 'Success' => $success, 'Fail' => $fail ]);
348 return Update::SUCCESS;
351 function update_1309()
353 $queue = DBA::select('queue', ['id', 'cid', 'guid']);
354 while ($entry = DBA::fetch($queue)) {
355 $contact = DBA::selectFirst('contact', ['uid'], ['id' => $entry['cid']]);
356 if (!DBA::isResult($contact)) {
360 $item = Item::selectFirst(['id', 'gravity'], ['uid' => $contact['uid'], 'guid' => $entry['guid']]);
361 if (!DBA::isResult($item)) {
365 $deliver_options = ['priority' => PRIORITY_MEDIUM, 'dont_fork' => true];
366 Worker::add($deliver_options, 'Delivery', Delivery::POST, $item['id'], $entry['cid']);
367 Logger::info('Added delivery worker', ['command' => $cmd, 'item' => $item['id'], 'contact' => $entry['cid']]);
368 DBA::delete('queue', ['id' => $entry['id']]);
370 return Update::SUCCESS;
373 function update_1315()
375 DBA::delete('item-delivery-data', ['postopts' => '', 'inform' => '', 'queue_count' => 0, 'queue_done' => 0]);
376 return Update::SUCCESS;
379 function update_1318()
381 DBA::update('profile', ['marital' => "In a relation"], ['marital' => "Unavailable"]);
382 DBA::update('profile', ['marital' => "Single"], ['marital' => "Available"]);
384 Worker::add(PRIORITY_LOW, 'ProfileUpdate');
385 return Update::SUCCESS;
388 function update_1323()
390 $users = DBA::select('user', ['uid']);
391 while ($user = DBA::fetch($users)) {
392 Contact::updateSelfFromUserID($user['uid']);
396 return Update::SUCCESS;
399 function update_1327()
401 $contacts = DBA::select('contact', ['uid', 'id', 'blocked', 'readonly'], ["`uid` != ? AND (`blocked` OR `readonly`) AND NOT `pending`", 0]);
402 while ($contact = DBA::fetch($contacts)) {
403 Contact::setBlockedForUser($contact['id'], $contact['uid'], $contact['blocked']);
404 Contact::setIgnoredForUser($contact['id'], $contact['uid'], $contact['readonly']);
406 DBA::close($contacts);
408 return Update::SUCCESS;