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\Item;
13 use Friendica\Model\User;
14 use Friendica\Util\DateTimeFormat;
18 * update.php - automatic system update
20 * This function is responsible for doing post update changes to the data
21 * (not the structure) in the database.
23 * Database structure changes are done in config/dbstructure.config.php
25 * If there is a need for a post process to a structure change, update this file
26 * by adding a new function at the end with the number of the new DB_UPDATE_VERSION.
28 * The numbered script in this file has to be exactly like the DB_UPDATE_VERSION
31 * You are currently on version 4711 and you are preparing changes that demand an update script.
33 * 1. Create a function "update_4712()" here in the update.php
34 * 2. Apply the needed structural changes in config/dbStructure.php
35 * 3. Set DB_UPDATE_VERSION in config/dbstructure.config.php to 4712.
37 * If you need to run a script before the database update, name the function "pre_update_4712()"
40 function update_1178()
42 require_once 'mod/profiles.php';
44 $profiles = q("SELECT `uid`, `about`, `locality`, `pub_keywords`, `gender` FROM `profile` WHERE `is-default`");
46 foreach ($profiles as $profile) {
47 if ($profile["about"].$profile["locality"].$profile["pub_keywords"].$profile["gender"] == "") {
51 $profile["pub_keywords"] = profile_clean_keywords($profile["pub_keywords"]);
54 "UPDATE `contact` SET `about` = '%s', `location` = '%s', `keywords` = '%s', `gender` = '%s' WHERE `self` AND `uid` = %d",
55 DBA::escape($profile["about"]),
56 DBA::escape($profile["locality"]),
57 DBA::escape($profile["pub_keywords"]),
58 DBA::escape($profile["gender"]),
59 intval($profile["uid"])
64 function update_1179()
66 if (Config::get('system', 'no_community_page')) {
67 Config::set('system', 'community_page_style', CP_NO_COMMUNITY_PAGE);
70 // Update the central item storage with uid=0
71 Worker::add(PRIORITY_LOW, "threadupdate");
73 return Update::SUCCESS;
76 function update_1181()
79 // Fill the new fields in the term table.
80 Worker::add(PRIORITY_LOW, "TagUpdate");
82 return Update::SUCCESS;
85 function update_1189()
88 if (strlen(Config::get('system', 'directory_submit_url')) &&
89 !strlen(Config::get('system', 'directory'))) {
90 Config::set('system', 'directory', dirname(Config::get('system', 'directory_submit_url')));
91 Config::delete('system', 'directory_submit_url');
94 return Update::SUCCESS;
97 function update_1191()
99 Config::set('system', 'maintenance', 1);
101 if (Addon::isEnabled('forumlist')) {
102 $addon = 'forumlist';
103 $addons = Config::get('system', 'addon');
107 $addons_arr = explode(",", str_replace(" ", "", $addons));
109 $idx = array_search($addon, $addons_arr);
110 if ($idx !== false) {
111 unset($addons_arr[$idx]);
112 //delete forumlist manually from addon and hook table
113 // since Addon::uninstall() don't work here
114 q("DELETE FROM `addon` WHERE `name` = 'forumlist' ");
115 q("DELETE FROM `hook` WHERE `file` = 'addon/forumlist/forumlist.php' ");
116 Config::set('system', 'addon', implode(", ", $addons_arr));
121 // select old formlist addon entries
123 "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()
165 "UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)",
166 DBA::escape(Contact::ACCOUNT_TYPE_COMMUNITY),
167 DBA::escape(Contact::PAGE_COMMUNITY),
168 DBA::escape(Contact::PAGE_PRVGROUP)
172 function update_1244()
174 // Sets legacy_password for all legacy hashes
175 DBA::update('user', ['legacy_password' => true], ['SUBSTR(password, 1, 4) != "$2y$"']);
177 // All legacy hashes are re-hashed using the new secure hashing function
178 $stmt = DBA::select('user', ['uid', 'password'], ['legacy_password' => true]);
179 while ($user = DBA::fetch($stmt)) {
180 DBA::update('user', ['password' => User::hashPassword($user['password'])], ['uid' => $user['uid']]);
183 // Logged in users are forcibly logged out
184 DBA::delete('session', ['1 = 1']);
186 return Update::SUCCESS;
189 function update_1245()
191 $rino = Config::get('system', 'rino_encrypt');
194 return Update::SUCCESS;
197 Config::set('system', 'rino_encrypt', 1);
199 return Update::SUCCESS;
202 function update_1247()
204 // Removing hooks with the old name
205 DBA::e("DELETE FROM `hook`
206 WHERE `hook` LIKE 'plugin_%'");
208 // Make sure we install the new renamed ones
212 function update_1260()
214 Config::set('system', 'maintenance', 1);
217 'maintenance_reason',
219 '%s: Updating author-id and owner-id in item and thread table. ',
220 DateTimeFormat::utcNow().' '.date('e')
224 $items = DBA::p("SELECT `id`, `owner-link`, `owner-name`, `owner-avatar`, `network` FROM `item`
225 WHERE `owner-id` = 0 AND `owner-link` != ''");
226 while ($item = DBA::fetch($items)) {
227 $contact = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
228 'photo' => $item['owner-avatar'], 'network' => $item['network']];
229 $cid = Contact::getIdForURL($item['owner-link'], 0, false, $contact);
233 Item::update(['owner-id' => $cid], ['id' => $item['id']]);
237 DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
238 SET `thread`.`owner-id` = `item`.`owner-id` WHERE `thread`.`owner-id` = 0");
240 $items = DBA::p("SELECT `id`, `author-link`, `author-name`, `author-avatar`, `network` FROM `item`
241 WHERE `author-id` = 0 AND `author-link` != ''");
242 while ($item = DBA::fetch($items)) {
243 $contact = ['url' => $item['author-link'], 'name' => $item['author-name'],
244 'photo' => $item['author-avatar'], 'network' => $item['network']];
245 $cid = Contact::getIdForURL($item['author-link'], 0, false, $contact);
249 Item::update(['author-id' => $cid], ['id' => $item['id']]);
253 DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
254 SET `thread`.`author-id` = `item`.`author-id` WHERE `thread`.`author-id` = 0");
256 Config::set('system', 'maintenance', 0);
257 return Update::SUCCESS;
260 function update_1261()
262 // This fixes the results of an issue in the develop branch of 2018-05.
263 DBA::update('contact', ['blocked' => false, 'pending' => false], ['uid' => 0, 'blocked' => true, 'pending' => true]);
264 return Update::SUCCESS;
267 function update_1278()
269 Config::set('system', 'maintenance', 1);
272 'maintenance_reason',
274 '%s: Updating post-type.',
275 DateTimeFormat::utcNow().' '.date('e')
279 Item::update(['post-type' => Item::PT_PAGE], ['bookmark' => true]);
280 Item::update(['post-type' => Item::PT_PERSONAL_NOTE], ['type' => 'note']);
282 Config::set('system', 'maintenance', 0);
284 return Update::SUCCESS;
287 function update_1288()
289 // Updates missing `uri-id` values
291 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");
292 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");
294 return Update::SUCCESS;
298 // Post-update script of PR 5751
299 function update_1293()
301 $allGenders = DBA::select('contact', ['id', 'gender']);
302 $allLangs = L10n::getAvailableLanguages();
305 foreach ($allGenders as $key => $gender) {
306 if ($gender['gender'] != '') {
307 foreach ($allLangs as $key => $lang) {
308 $a = new \stdClass();
311 // First we get the the localizations
312 if (file_exists("view/lang/$lang/strings.php")) {
313 include "view/lang/$lang/strings.php";
315 if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
316 include "addon/morechoice/lang/$lang/strings.php";
319 $localizedStrings = $a->strings;
322 $key = array_search($gender['gender'], $localizedStrings);
323 if ($key !== false) {
327 // defaulting to empty string
334 DBA::update('contact', ['gender' => $key], ['id' => $gender['id']]);
335 logger::log('Updated contact ' . $gender['id'] . ' to gender ' . $key .
336 ' (was: ' . $gender['gender'] . ')');
342 Logger::log("Gender fix completed. Success: $success. Fail: $fail");
343 return Update::SUCCESS;