]> git.mxchange.org Git - friendica.git/blob - update.php
Add Database storage backend
[friendica.git] / update.php
1 <?php
2
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;
14
15 /**
16  *
17  * update.php - automatic system update
18  *
19  * This function is responsible for doing post update changes to the data
20  * (not the structure) in the database.
21  *
22  * Database structure changes are done in config/dbstructure.config.php
23  *
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.
26  *
27  * The numbered script in this file has to be exactly like the DB_UPDATE_VERSION
28  *
29  * Example:
30  * You are currently on version 4711 and you are preparing changes that demand an update script.
31  *
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.
35  *
36  * If you need to run a script before the database update, name the function "pre_update_4712()"
37  */
38
39 function update_1178() {
40         require_once 'mod/profiles.php';
41
42         $profiles = q("SELECT `uid`, `about`, `locality`, `pub_keywords`, `gender` FROM `profile` WHERE `is-default`");
43
44         foreach ($profiles AS $profile) {
45                 if ($profile["about"].$profile["locality"].$profile["pub_keywords"].$profile["gender"] == "")
46                         continue;
47
48                 $profile["pub_keywords"] = profile_clean_keywords($profile["pub_keywords"]);
49
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"])
56                         );
57         }
58 }
59
60 function update_1179() {
61         if (Config::get('system','no_community_page'))
62                 Config::set('system','community_page_style', CP_NO_COMMUNITY_PAGE);
63
64         // Update the central item storage with uid=0
65         Worker::add(PRIORITY_LOW, "threadupdate");
66
67         return Update::SUCCESS;
68 }
69
70 function update_1181() {
71
72         // Fill the new fields in the term table.
73         Worker::add(PRIORITY_LOW, "TagUpdate");
74
75         return Update::SUCCESS;
76 }
77
78 function update_1189() {
79
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');
84         }
85
86         return Update::SUCCESS;
87 }
88
89 function update_1191() {
90         Config::set('system', 'maintenance', 1);
91
92         if (Addon::isEnabled('forumlist')) {
93                 $addon = 'forumlist';
94                 $addons = Config::get('system', 'addon');
95                 $addons_arr = [];
96
97                 if ($addons) {
98                         $addons_arr = explode(",",str_replace(" ", "", $addons));
99
100                         $idx = array_search($addon, $addons_arr);
101                         if ($idx !== false){
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));
108                         }
109                 }
110         }
111
112         // select old formlist addon entries
113         $r = q("SELECT `uid`, `cat`, `k`, `v` FROM `pconfig` WHERE `cat` = '%s' ",
114                 DBA::escape('forumlist')
115         );
116
117         // convert old forumlist addon entries in new config entries
118         if (DBA::isResult($r)) {
119                 foreach ($r as $rr) {
120                         $uid = $rr['uid'];
121                         $family = $rr['cat'];
122                         $key = $rr['k'];
123                         $value = $rr['v'];
124
125                         if ($key === 'randomise')
126                                 PConfig::delete($uid,$family,$key);
127
128                         if ($key === 'show_on_profile') {
129                                 if ($value)
130                                         PConfig::set($uid,feature,forumlist_profile,$value);
131
132                                 PConfig::delete($uid,$family,$key);
133                         }
134
135                         if ($key === 'show_on_network') {
136                                 if ($value)
137                                         PConfig::set($uid,feature,forumlist_widget,$value);
138
139                                 PConfig::delete($uid,$family,$key);
140                         }
141                 }
142         }
143
144         Config::set('system', 'maintenance', 0);
145
146         return Update::SUCCESS;
147 }
148
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));
152 }
153
154 function update_1244() {
155         // Sets legacy_password for all legacy hashes
156         DBA::update('user', ['legacy_password' => true], ['SUBSTR(password, 1, 4) != "$2y$"']);
157
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']]);
162         }
163
164         // Logged in users are forcibly logged out
165         DBA::delete('session', ['1 = 1']);
166
167         return Update::SUCCESS;
168 }
169
170 function update_1245() {
171         $rino = Config::get('system', 'rino_encrypt');
172
173         if (!$rino) {
174                 return Update::SUCCESS;
175         }
176
177         Config::set('system', 'rino_encrypt', 1);
178
179         return Update::SUCCESS;
180 }
181
182 function update_1247() {
183         // Removing hooks with the old name
184         DBA::e("DELETE FROM `hook`
185 WHERE `hook` LIKE 'plugin_%'");
186
187         // Make sure we install the new renamed ones
188         Addon::reload();
189 }
190
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')));
194
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);
201                 if (empty($cid)) {
202                         continue;
203                 }
204                 Item::update(['owner-id' => $cid], ['id' => $item['id']]);
205         }
206         DBA::close($items);
207
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");
210
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);
217                 if (empty($cid)) {
218                         continue;
219                 }
220                 Item::update(['author-id' => $cid], ['id' => $item['id']]);
221         }
222         DBA::close($items);
223
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");
226
227         Config::set('system', 'maintenance', 0);
228         return Update::SUCCESS;
229 }
230
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;
235 }
236
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')));
240
241         Item::update(['post-type' => Item::PT_PAGE], ['bookmark' => true]);
242         Item::update(['post-type' => Item::PT_PERSONAL_NOTE], ['type' => 'note']);
243
244         Config::set('system', 'maintenance', 0);
245
246         return Update::SUCCESS;
247 }
248
249 function update_1288() {
250         // Updates missing `uri-id` values
251
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");
254
255         return Update::SUCCESS;
256 }