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'
55 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
124 "SELECT `uid`, `cat`, `k`, `v` FROM `pconfig` WHERE `cat` = '%s' ",
125 DBA::escape('forumlist')
128 // convert old forumlist addon entries in new config entries
129 if (DBA::isResult($r)) {
130 foreach ($r as $rr) {
132 $family = $rr['cat'];
136 if ($key === 'randomise') {
137 PConfig::delete($uid, $family, $key);
140 if ($key === 'show_on_profile') {
142 PConfig::set($uid, feature, forumlist_profile, $value);
145 PConfig::delete($uid, $family, $key);
148 if ($key === 'show_on_network') {
150 PConfig::set($uid, feature, forumlist_widget, $value);
153 PConfig::delete($uid, $family, $key);
158 Config::set('system', 'maintenance', 0);
160 return Update::SUCCESS;
163 function update_1203()
166 "UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)",
167 DBA::escape(Contact::ACCOUNT_TYPE_COMMUNITY),
168 DBA::escape(Contact::PAGE_COMMUNITY),
169 DBA::escape(Contact::PAGE_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.
266 ['blocked' => false, 'pending' => false],
267 ['uid' => 0, 'blocked' => true, 'pending' => true]
269 return Update::SUCCESS;
272 function update_1278()
274 Config::set('system', 'maintenance', 1);
277 'maintenance_reason',
279 '%s: Updating post-type.',
280 DateTimeFormat::utcNow().' '.date('e')
284 Item::update(['post-type' => Item::PT_PAGE], ['bookmark' => true]);
285 Item::update(['post-type' => Item::PT_PERSONAL_NOTE], ['type' => 'note']);
287 Config::set('system', 'maintenance', 0);
289 return Update::SUCCESS;
292 function update_1288()
294 // Updates missing `uri-id` values
296 DBA::e("UPDATE `item-activity`
297 INNER JOIN `item` ON `item`.`iaid` = `item-activity`.`id`
298 SET `item-activity`.`uri-id` = `item`.`uri-id`
299 WHERE `item-activity`.`uri-id` IS NULL OR `item-activity`.`uri-id` = 0");
300 DBA::e("UPDATE `item-content`
301 INNER JOIN `item` ON `item`.`icid` = `item-content`.`id`
302 SET `item-content`.`uri-id` = `item`.`uri-id`
303 WHERE `item-content`.`uri-id` IS NULL OR `item-content`.`uri-id` = 0");
305 return Update::SUCCESS;
309 // Post-update script of PR 5751
310 function update_1293()
312 $allGenders = DBA::select('contact', ['id', 'gender']);
313 $allLangs = L10n::getAvailableLanguages();
316 foreach ($allGenders as $key => $gender) {
317 if ($gender['gender'] != '') {
318 foreach ($allLangs as $key => $lang) {
319 $a = new \stdClass();
322 // First we get the the localizations
323 if (file_exists("view/lang/$lang/strings.php")) {
324 include "view/lang/$lang/strings.php";
326 if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
327 include "addon/morechoice/lang/$lang/strings.php";
330 $localizedStrings = $a->strings;
333 $key = array_search($gender['gender'], $localizedStrings);
334 if ($key !== false) {
338 // defaulting to empty string
345 DBA::update('contact', ['gender' => $key], ['id' => $gender['id']]);
346 logger::log('Updated contact ' . $gender['id'] . ' to gender ' . $key .
347 ' (was: ' . $gender['gender'] . ')');
353 Logger::log("Gender fix completed. Success: $success. Fail: $fail");
354 return Update::SUCCESS;