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;
12 use Friendica\Model\Contact;
13 use Friendica\Model\GContact;
14 use Friendica\Model\Item;
15 use Friendica\Model\User;
16 use Friendica\Model\Storage;
17 use Friendica\Util\DateTimeFormat;
18 use Friendica\Worker\Delivery;
22 * update.php - automatic system update
24 * This function is responsible for doing post update changes to the data
25 * (not the structure) in the database.
27 * Database structure changes are done in static/dbstructure.config.php
29 * If there is a need for a post process to a structure change, update this file
30 * by adding a new function at the end with the number of the new DB_UPDATE_VERSION.
32 * The numbered script in this file has to be exactly like the DB_UPDATE_VERSION
35 * You are currently on version 4711 and you are preparing changes that demand an update script.
37 * 1. Create a function "update_4712()" here in the update.php
38 * 2. Apply the needed structural changes in static/dbStructure.php
39 * 3. Set DB_UPDATE_VERSION in static/dbstructure.config.php to 4712.
41 * If you need to run a script before the database update, name the function "pre_update_4712()"
44 function update_1178()
46 require_once 'mod/profiles.php';
48 $profiles = q("SELECT `uid`, `about`, `locality`, `pub_keywords`, `gender` FROM `profile` WHERE `is-default`");
50 foreach ($profiles as $profile) {
51 if ($profile["about"].$profile["locality"].$profile["pub_keywords"].$profile["gender"] == "") {
55 $profile["pub_keywords"] = profile_clean_keywords($profile["pub_keywords"]);
57 $r = q("UPDATE `contact` SET `about` = '%s', `location` = '%s', `keywords` = '%s', `gender` = '%s' WHERE `self` AND `uid` = %d",
58 DBA::escape($profile["about"]),
59 DBA::escape($profile["locality"]),
60 DBA::escape($profile["pub_keywords"]),
61 DBA::escape($profile["gender"]),
62 intval($profile["uid"])
67 function update_1179()
69 if (Config::get('system', 'no_community_page')) {
70 Config::set('system', 'community_page_style', CP_NO_COMMUNITY_PAGE);
73 // Update the central item storage with uid=0
74 Worker::add(PRIORITY_LOW, "threadupdate");
76 return Update::SUCCESS;
79 function update_1181()
82 // Fill the new fields in the term table.
83 Worker::add(PRIORITY_LOW, "TagUpdate");
85 return Update::SUCCESS;
88 function update_1189()
91 if (strlen(Config::get('system', 'directory_submit_url')) &&
92 !strlen(Config::get('system', 'directory'))) {
93 Config::set('system', 'directory', dirname(Config::get('system', 'directory_submit_url')));
94 Config::delete('system', 'directory_submit_url');
97 return Update::SUCCESS;
100 function update_1191()
102 Config::set('system', 'maintenance', 1);
104 if (Addon::isEnabled('forumlist')) {
105 $addon = 'forumlist';
106 $addons = Config::get('system', 'addon');
110 $addons_arr = explode(",", str_replace(" ", "", $addons));
112 $idx = array_search($addon, $addons_arr);
113 if ($idx !== false) {
114 unset($addons_arr[$idx]);
115 //delete forumlist manually from addon and hook table
116 // since Addon::uninstall() don't work here
117 q("DELETE FROM `addon` WHERE `name` = 'forumlist' ");
118 q("DELETE FROM `hook` WHERE `file` = 'addon/forumlist/forumlist.php' ");
119 Config::set('system', 'addon', implode(", ", $addons_arr));
124 // select old formlist addon entries
125 $r = q("SELECT `uid`, `cat`, `k`, `v` FROM `pconfig` WHERE `cat` = '%s' ",
126 DBA::escape('forumlist')
129 // convert old forumlist addon entries in new config entries
130 if (DBA::isResult($r)) {
131 foreach ($r as $rr) {
133 $family = $rr['cat'];
137 if ($key === 'randomise') {
138 PConfig::delete($uid, $family, $key);
141 if ($key === 'show_on_profile') {
143 DI::pConfig()->set($uid, feature, forumlist_profile, $value);
146 PConfig::delete($uid, $family, $key);
149 if ($key === 'show_on_network') {
151 DI::pConfig()->set($uid, feature, forumlist_widget, $value);
154 PConfig::delete($uid, $family, $key);
159 Config::set('system', 'maintenance', 0);
161 return Update::SUCCESS;
164 function update_1203()
166 $r = q("UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)",
167 DBA::escape(User::ACCOUNT_TYPE_COMMUNITY),
168 DBA::escape(User::PAGE_FLAGS_COMMUNITY),
169 DBA::escape(User::PAGE_FLAGS_PRVGROUP)
173 function update_1244()
175 // Sets legacy_password for all legacy hashes
176 DBA::update('user', ['legacy_password' => true], ['SUBSTR(password, 1, 4) != "$2y$"']);
178 // All legacy hashes are re-hashed using the new secure hashing function
179 $stmt = DBA::select('user', ['uid', 'password'], ['legacy_password' => true]);
180 while ($user = DBA::fetch($stmt)) {
181 DBA::update('user', ['password' => User::hashPassword($user['password'])], ['uid' => $user['uid']]);
184 // Logged in users are forcibly logged out
185 DBA::delete('session', ['1 = 1']);
187 return Update::SUCCESS;
190 function update_1245()
192 $rino = Config::get('system', 'rino_encrypt');
195 return Update::SUCCESS;
198 Config::set('system', 'rino_encrypt', 1);
200 return Update::SUCCESS;
203 function update_1247()
205 // Removing hooks with the old name
206 DBA::e("DELETE FROM `hook`
207 WHERE `hook` LIKE 'plugin_%'");
209 // Make sure we install the new renamed ones
213 function update_1260()
215 Config::set('system', 'maintenance', 1);
218 'maintenance_reason',
220 '%s: Updating author-id and owner-id in item and thread table. ',
221 DateTimeFormat::utcNow().' '.date('e')
225 $items = DBA::p("SELECT `id`, `owner-link`, `owner-name`, `owner-avatar`, `network` FROM `item`
226 WHERE `owner-id` = 0 AND `owner-link` != ''");
227 while ($item = DBA::fetch($items)) {
228 $contact = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
229 'photo' => $item['owner-avatar'], 'network' => $item['network']];
230 $cid = Contact::getIdForURL($item['owner-link'], 0, false, $contact);
234 Item::update(['owner-id' => $cid], ['id' => $item['id']]);
238 DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
239 SET `thread`.`owner-id` = `item`.`owner-id` WHERE `thread`.`owner-id` = 0");
241 $items = DBA::p("SELECT `id`, `author-link`, `author-name`, `author-avatar`, `network` FROM `item`
242 WHERE `author-id` = 0 AND `author-link` != ''");
243 while ($item = DBA::fetch($items)) {
244 $contact = ['url' => $item['author-link'], 'name' => $item['author-name'],
245 'photo' => $item['author-avatar'], 'network' => $item['network']];
246 $cid = Contact::getIdForURL($item['author-link'], 0, false, $contact);
250 Item::update(['author-id' => $cid], ['id' => $item['id']]);
254 DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
255 SET `thread`.`author-id` = `item`.`author-id` WHERE `thread`.`author-id` = 0");
257 Config::set('system', 'maintenance', 0);
258 return Update::SUCCESS;
261 function update_1261()
263 // This fixes the results of an issue in the develop branch of 2018-05.
264 DBA::update('contact', ['blocked' => false, 'pending' => false], ['uid' => 0, 'blocked' => true, 'pending' => true]);
265 return Update::SUCCESS;
268 function update_1278()
270 Config::set('system', 'maintenance', 1);
273 'maintenance_reason',
275 '%s: Updating post-type.',
276 DateTimeFormat::utcNow().' '.date('e')
280 Item::update(['post-type' => Item::PT_PAGE], ['bookmark' => true]);
281 Item::update(['post-type' => Item::PT_PERSONAL_NOTE], ['type' => 'note']);
283 Config::set('system', 'maintenance', 0);
285 return Update::SUCCESS;
288 function update_1288()
290 // Updates missing `uri-id` values
292 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");
293 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");
295 return Update::SUCCESS;
298 // Post-update script of PR 5751
299 function update_1298()
301 $keys = ['gender', 'marital', 'sexual'];
302 foreach ($keys as $translateKey) {
303 $allData = DBA::select('profile', ['id', $translateKey]);
304 $allLangs = L10n::getAvailableLanguages();
307 foreach ($allData as $key => $data) {
308 $toTranslate = $data[$translateKey];
309 if ($toTranslate != '') {
310 foreach ($allLangs as $key => $lang) {
311 $a = new \stdClass();
314 // First we get the the localizations
315 if (file_exists("view/lang/$lang/strings.php")) {
316 include "view/lang/$lang/strings.php";
318 if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
319 include "addon/morechoice/lang/$lang/strings.php";
322 $localizedStrings = $a->strings;
325 $key = array_search($toTranslate, $localizedStrings);
326 if ($key !== false) {
330 // defaulting to empty string
337 DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
338 Logger::notice('Updated contact', ['action' => 'update', 'contact' => $data['id'], "$translateKey" => $key,
339 'was' => $data[$translateKey]]);
340 Worker::add(PRIORITY_LOW, 'ProfileUpdate', $data['id']);
341 Contact::updateSelfFromUserID($data['id']);
342 GContact::updateForUser($data['id']);
348 Logger::notice($translateKey . " fix completed", ['action' => 'update', 'translateKey' => $translateKey, 'Success' => $success, 'Fail' => $fail ]);
350 return Update::SUCCESS;
353 function update_1309()
355 $queue = DBA::select('queue', ['id', 'cid', 'guid']);
356 while ($entry = DBA::fetch($queue)) {
357 $contact = DBA::selectFirst('contact', ['uid'], ['id' => $entry['cid']]);
358 if (!DBA::isResult($contact)) {
362 $item = Item::selectFirst(['id', 'gravity'], ['uid' => $contact['uid'], 'guid' => $entry['guid']]);
363 if (!DBA::isResult($item)) {
367 $deliver_options = ['priority' => PRIORITY_MEDIUM, 'dont_fork' => true];
368 Worker::add($deliver_options, 'Delivery', Delivery::POST, $item['id'], $entry['cid']);
369 Logger::info('Added delivery worker', ['command' => $cmd, 'item' => $item['id'], 'contact' => $entry['cid']]);
370 DBA::delete('queue', ['id' => $entry['id']]);
372 return Update::SUCCESS;
375 function update_1315()
377 DBA::delete('item-delivery-data', ['postopts' => '', 'inform' => '', 'queue_count' => 0, 'queue_done' => 0]);
378 return Update::SUCCESS;
381 function update_1318()
383 DBA::update('profile', ['marital' => "In a relation"], ['marital' => "Unavailable"]);
384 DBA::update('profile', ['marital' => "Single"], ['marital' => "Available"]);
386 Worker::add(PRIORITY_LOW, 'ProfileUpdate');
387 return Update::SUCCESS;
390 function update_1323()
392 $users = DBA::select('user', ['uid']);
393 while ($user = DBA::fetch($users)) {
394 Contact::updateSelfFromUserID($user['uid']);
398 return Update::SUCCESS;
401 function update_1327()
403 $contacts = DBA::select('contact', ['uid', 'id', 'blocked', 'readonly'], ["`uid` != ? AND (`blocked` OR `readonly`) AND NOT `pending`", 0]);
404 while ($contact = DBA::fetch($contacts)) {
405 Contact::setBlockedForUser($contact['id'], $contact['uid'], $contact['blocked']);
406 Contact::setIgnoredForUser($contact['id'], $contact['uid'], $contact['readonly']);
408 DBA::close($contacts);
410 return Update::SUCCESS;
413 function update_1330()
415 $currStorage = Config::get('storage', 'class', '');
417 // set the name of the storage instead of the classpath as config
418 if (!empty($currStorage)) {
419 /** @var Storage\IStorage $currStorage */
420 if (!Config::set('storage', 'name', $currStorage::getName())) {
421 return Update::FAILED;
424 // try to delete the class since it isn't needed. This won't work with config files
425 Config::delete('storage', 'class');
428 // Update attachments and photos
429 if (!DBA::p("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 25) WHERE `photo`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'") ||
430 !DBA::p("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 25) WHERE `attach`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'")) {
431 return Update::FAILED;
434 return Update::SUCCESS;