3 use Friendica\Core\Addon;
4 use Friendica\Core\Config;
5 use Friendica\Core\L10n;
6 use Friendica\Core\PConfig;
7 use Friendica\Core\Update;
8 use Friendica\Core\Worker;
9 use Friendica\Database\DBA;
10 use Friendica\Model\Contact;
11 use Friendica\Model\Item;
12 use Friendica\Model\User;
13 use Friendica\Util\DateTimeFormat;
17 * update.php - automatic system update
19 * This function is responsible for doing post update changes to the data
20 * (not the structure) in the database.
22 * Database structure changes are done in config/dbstructure.config.php
24 * If there is a need for a post process to a structure change, update this file
25 * by adding a new function at the end with the number of the new DB_UPDATE_VERSION.
27 * The numbered script in this file has to be exactly like the DB_UPDATE_VERSION
30 * You are currently on version 4711 and you are preparing changes that demand an update script.
32 * 1. Create a function "update_4712()" here in the update.php
33 * 2. Apply the needed structural changes in config/dbStructure.php
34 * 3. Set DB_UPDATE_VERSION in config/dbstructure.config.php to 4712.
36 * If you need to run a script before the database update, name the function "pre_update_4712()"
39 function update_1178() {
40 require_once 'mod/profiles.php';
42 $profiles = q("SELECT `uid`, `about`, `locality`, `pub_keywords`, `gender` FROM `profile` WHERE `is-default`");
44 foreach ($profiles AS $profile) {
45 if ($profile["about"].$profile["locality"].$profile["pub_keywords"].$profile["gender"] == "")
48 $profile["pub_keywords"] = profile_clean_keywords($profile["pub_keywords"]);
50 $r = q("UPDATE `contact` SET `about` = '%s', `location` = '%s', `keywords` = '%s', `gender` = '%s' WHERE `self` AND `uid` = %d",
51 DBA::escape($profile["about"]),
52 DBA::escape($profile["locality"]),
53 DBA::escape($profile["pub_keywords"]),
54 DBA::escape($profile["gender"]),
55 intval($profile["uid"])
60 function update_1179() {
61 if (Config::get('system','no_community_page'))
62 Config::set('system','community_page_style', CP_NO_COMMUNITY_PAGE);
64 // Update the central item storage with uid=0
65 Worker::add(PRIORITY_LOW, "threadupdate");
67 return Update::SUCCESS;
70 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() {
80 if (strlen(Config::get('system','directory_submit_url')) &&
81 !strlen(Config::get('system','directory'))) {
82 Config::set('system','directory', dirname(Config::get('system','directory_submit_url')));
83 Config::delete('system','directory_submit_url');
86 return Update::SUCCESS;
89 function update_1191() {
90 Config::set('system', 'maintenance', 1);
92 if (Addon::isEnabled('forumlist')) {
94 $addons = Config::get('system', 'addon');
98 $addons_arr = explode(",",str_replace(" ", "", $addons));
100 $idx = array_search($addon, $addons_arr);
102 unset($addons_arr[$idx]);
103 //delete forumlist manually from addon and hook table
104 // since Addon::uninstall() don't work here
105 q("DELETE FROM `addon` WHERE `name` = 'forumlist' ");
106 q("DELETE FROM `hook` WHERE `file` = 'addon/forumlist/forumlist.php' ");
107 Config::set('system','addon', implode(", ", $addons_arr));
112 // select old formlist addon entries
113 $r = q("SELECT `uid`, `cat`, `k`, `v` FROM `pconfig` WHERE `cat` = '%s' ",
114 DBA::escape('forumlist')
117 // convert old forumlist addon entries in new config entries
118 if (DBA::isResult($r)) {
119 foreach ($r as $rr) {
121 $family = $rr['cat'];
125 if ($key === 'randomise')
126 PConfig::delete($uid,$family,$key);
128 if ($key === 'show_on_profile') {
130 PConfig::set($uid,feature,forumlist_profile,$value);
132 PConfig::delete($uid,$family,$key);
135 if ($key === 'show_on_network') {
137 PConfig::set($uid,feature,forumlist_widget,$value);
139 PConfig::delete($uid,$family,$key);
144 Config::set('system', 'maintenance', 0);
146 return Update::SUCCESS;
149 function update_1203() {
150 $r = q("UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)",
151 DBA::escape(Contact::ACCOUNT_TYPE_COMMUNITY), DBA::escape(Contact::PAGE_COMMUNITY), DBA::escape(Contact::PAGE_PRVGROUP));
154 function update_1244() {
155 // Sets legacy_password for all legacy hashes
156 DBA::update('user', ['legacy_password' => true], ['SUBSTR(password, 1, 4) != "$2y$"']);
158 // All legacy hashes are re-hashed using the new secure hashing function
159 $stmt = DBA::select('user', ['uid', 'password'], ['legacy_password' => true]);
160 while($user = DBA::fetch($stmt)) {
161 DBA::update('user', ['password' => User::hashPassword($user['password'])], ['uid' => $user['uid']]);
164 // Logged in users are forcibly logged out
165 DBA::delete('session', ['1 = 1']);
167 return Update::SUCCESS;
170 function update_1245() {
171 $rino = Config::get('system', 'rino_encrypt');
174 return Update::SUCCESS;
177 Config::set('system', 'rino_encrypt', 1);
179 return Update::SUCCESS;
182 function update_1247() {
183 // Removing hooks with the old name
184 DBA::e("DELETE FROM `hook`
185 WHERE `hook` LIKE 'plugin_%'");
187 // Make sure we install the new renamed ones
191 function update_1260() {
192 Config::set('system', 'maintenance', 1);
193 Config::set('system', 'maintenance_reason', L10n::t('%s: Updating author-id and owner-id in item and thread table. ', DateTimeFormat::utcNow().' '.date('e')));
195 $items = DBA::p("SELECT `id`, `owner-link`, `owner-name`, `owner-avatar`, `network` FROM `item`
196 WHERE `owner-id` = 0 AND `owner-link` != ''");
197 while ($item = DBA::fetch($items)) {
198 $contact = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
199 'photo' => $item['owner-avatar'], 'network' => $item['network']];
200 $cid = Contact::getIdForURL($item['owner-link'], 0, false, $contact);
204 Item::update(['owner-id' => $cid], ['id' => $item['id']]);
208 DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
209 SET `thread`.`owner-id` = `item`.`owner-id` WHERE `thread`.`owner-id` = 0");
211 $items = DBA::p("SELECT `id`, `author-link`, `author-name`, `author-avatar`, `network` FROM `item`
212 WHERE `author-id` = 0 AND `author-link` != ''");
213 while ($item = DBA::fetch($items)) {
214 $contact = ['url' => $item['author-link'], 'name' => $item['author-name'],
215 'photo' => $item['author-avatar'], 'network' => $item['network']];
216 $cid = Contact::getIdForURL($item['author-link'], 0, false, $contact);
220 Item::update(['author-id' => $cid], ['id' => $item['id']]);
224 DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
225 SET `thread`.`author-id` = `item`.`author-id` WHERE `thread`.`author-id` = 0");
227 Config::set('system', 'maintenance', 0);
228 return Update::SUCCESS;
231 function update_1261() {
232 // This fixes the results of an issue in the develop branch of 2018-05.
233 DBA::update('contact', ['blocked' => false, 'pending' => false], ['uid' => 0, 'blocked' => true, 'pending' => true]);
234 return Update::SUCCESS;
237 function update_1278() {
238 Config::set('system', 'maintenance', 1);
239 Config::set('system', 'maintenance_reason', L10n::t('%s: Updating post-type.', DateTimeFormat::utcNow().' '.date('e')));
241 Item::update(['post-type' => Item::PT_PAGE], ['bookmark' => true]);
242 Item::update(['post-type' => Item::PT_PERSONAL_NOTE], ['type' => 'note']);
244 Config::set('system', 'maintenance', 0);
246 return Update::SUCCESS;
249 function update_1288() {
250 // Updates missing `uri-id` values
252 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");
253 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");
255 return Update::SUCCESS;