Fixes some more things
[friendica.git/.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\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;
15
16 /**
17  *
18  * update.php - automatic system update
19  *
20  * This function is responsible for doing post update changes to the data
21  * (not the structure) in the database.
22  *
23  * Database structure changes are done in config/dbstructure.config.php
24  *
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.
27  *
28  * The numbered script in this file has to be exactly like the DB_UPDATE_VERSION
29  *
30  * Example:
31  * You are currently on version 4711 and you are preparing changes that demand an update script.
32  *
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.
36  *
37  * If you need to run a script before the database update, name the function "pre_update_4712()"
38  */
39
40 function update_1178()
41 {
42         require_once 'mod/profiles.php';
43
44         $profiles = q("SELECT `uid`, `about`, `locality`, `pub_keywords`, `gender` FROM `profile` WHERE `is-default`");
45
46         foreach ($profiles as $profile) {
47                 if ($profile["about"].$profile["locality"].$profile["pub_keywords"].$profile["gender"] == "") {
48                         continue;
49                 }
50
51                 $profile["pub_keywords"] = profile_clean_keywords($profile["pub_keywords"]);
52
53                 $r = q("UPDATE `contact` SET `about` = '%s', `location` = '%s', `keywords` = '%s', `gender` = '%s' WHERE `self` AND `uid` = %d",
54                         DBA::escape($profile["about"]),
55                         DBA::escape($profile["locality"]),
56                         DBA::escape($profile["pub_keywords"]),
57                         DBA::escape($profile["gender"]),
58                         intval($profile["uid"])
59                 );
60         }
61 }
62
63 function update_1179()
64 {
65         if (Config::get('system', 'no_community_page')) {
66                 Config::set('system', 'community_page_style', CP_NO_COMMUNITY_PAGE);
67         }
68
69         // Update the central item storage with uid=0
70         Worker::add(PRIORITY_LOW, "threadupdate");
71
72         return Update::SUCCESS;
73 }
74
75 function update_1181()
76 {
77
78         // Fill the new fields in the term table.
79         Worker::add(PRIORITY_LOW, "TagUpdate");
80
81         return Update::SUCCESS;
82 }
83
84 function update_1189()
85 {
86
87         if (strlen(Config::get('system', 'directory_submit_url')) &&
88                 !strlen(Config::get('system', 'directory'))) {
89                 Config::set('system', 'directory', dirname(Config::get('system', 'directory_submit_url')));
90                 Config::delete('system', 'directory_submit_url');
91         }
92
93         return Update::SUCCESS;
94 }
95
96 function update_1191()
97 {
98         Config::set('system', 'maintenance', 1);
99
100         if (Addon::isEnabled('forumlist')) {
101                 $addon = 'forumlist';
102                 $addons = Config::get('system', 'addon');
103                 $addons_arr = [];
104
105                 if ($addons) {
106                         $addons_arr = explode(",", str_replace(" ", "", $addons));
107
108                         $idx = array_search($addon, $addons_arr);
109                         if ($idx !== false) {
110                                 unset($addons_arr[$idx]);
111                                 //delete forumlist manually from addon and hook table
112                                 // since Addon::uninstall() don't work here
113                                 q("DELETE FROM `addon` WHERE `name` = 'forumlist' ");
114                                 q("DELETE FROM `hook` WHERE `file` = 'addon/forumlist/forumlist.php' ");
115                                 Config::set('system', 'addon', implode(", ", $addons_arr));
116                         }
117                 }
118         }
119
120         // select old formlist addon entries
121         $r = q("SELECT `uid`, `cat`, `k`, `v` FROM `pconfig` WHERE `cat` = '%s' ",
122                 DBA::escape('forumlist')
123         );
124
125         // convert old forumlist addon entries in new config entries
126         if (DBA::isResult($r)) {
127                 foreach ($r as $rr) {
128                         $uid = $rr['uid'];
129                         $family = $rr['cat'];
130                         $key = $rr['k'];
131                         $value = $rr['v'];
132
133                         if ($key === 'randomise') {
134                                 PConfig::delete($uid, $family, $key);
135                         }
136
137                         if ($key === 'show_on_profile') {
138                                 if ($value) {
139                                         PConfig::set($uid, feature, forumlist_profile, $value);
140                                 }
141
142                                 PConfig::delete($uid, $family, $key);
143                         }
144
145                         if ($key === 'show_on_network') {
146                                 if ($value) {
147                                         PConfig::set($uid, feature, forumlist_widget, $value);
148                                 }
149
150                                 PConfig::delete($uid, $family, $key);
151                         }
152                 }
153         }
154
155         Config::set('system', 'maintenance', 0);
156
157         return Update::SUCCESS;
158 }
159
160 function update_1203()
161 {
162         $r = q("UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)",
163                 DBA::escape(Contact::ACCOUNT_TYPE_COMMUNITY),
164                 DBA::escape(Contact::PAGE_COMMUNITY),
165                 DBA::escape(Contact::PAGE_PRVGROUP)
166         );
167 }
168
169 function update_1244()
170 {
171         // Sets legacy_password for all legacy hashes
172         DBA::update('user', ['legacy_password' => true], ['SUBSTR(password, 1, 4) != "$2y$"']);
173
174         // All legacy hashes are re-hashed using the new secure hashing function
175         $stmt = DBA::select('user', ['uid', 'password'], ['legacy_password' => true]);
176         while ($user = DBA::fetch($stmt)) {
177                 DBA::update('user', ['password' => User::hashPassword($user['password'])], ['uid' => $user['uid']]);
178         }
179
180         // Logged in users are forcibly logged out
181         DBA::delete('session', ['1 = 1']);
182
183         return Update::SUCCESS;
184 }
185
186 function update_1245()
187 {
188         $rino = Config::get('system', 'rino_encrypt');
189
190         if (!$rino) {
191                 return Update::SUCCESS;
192         }
193
194         Config::set('system', 'rino_encrypt', 1);
195
196         return Update::SUCCESS;
197 }
198
199 function update_1247()
200 {
201         // Removing hooks with the old name
202         DBA::e("DELETE FROM `hook`
203 WHERE `hook` LIKE 'plugin_%'");
204
205         // Make sure we install the new renamed ones
206         Addon::reload();
207 }
208
209 function update_1260()
210 {
211         Config::set('system', 'maintenance', 1);
212         Config::set(
213                 'system',
214                 'maintenance_reason',
215                 L10n::t(
216                         '%s: Updating author-id and owner-id in item and thread table. ',
217                         DateTimeFormat::utcNow().' '.date('e')
218                 )
219         );
220
221         $items = DBA::p("SELECT `id`, `owner-link`, `owner-name`, `owner-avatar`, `network` FROM `item`
222                 WHERE `owner-id` = 0 AND `owner-link` != ''");
223         while ($item = DBA::fetch($items)) {
224                 $contact = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
225                         'photo' => $item['owner-avatar'], 'network' => $item['network']];
226                 $cid = Contact::getIdForURL($item['owner-link'], 0, false, $contact);
227                 if (empty($cid)) {
228                         continue;
229                 }
230                 Item::update(['owner-id' => $cid], ['id' => $item['id']]);
231         }
232         DBA::close($items);
233
234         DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
235                 SET `thread`.`owner-id` = `item`.`owner-id` WHERE `thread`.`owner-id` = 0");
236
237         $items = DBA::p("SELECT `id`, `author-link`, `author-name`, `author-avatar`, `network` FROM `item`
238                 WHERE `author-id` = 0 AND `author-link` != ''");
239         while ($item = DBA::fetch($items)) {
240                 $contact = ['url' => $item['author-link'], 'name' => $item['author-name'],
241                         'photo' => $item['author-avatar'], 'network' => $item['network']];
242                 $cid = Contact::getIdForURL($item['author-link'], 0, false, $contact);
243                 if (empty($cid)) {
244                         continue;
245                 }
246                 Item::update(['author-id' => $cid], ['id' => $item['id']]);
247         }
248         DBA::close($items);
249
250         DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
251                 SET `thread`.`author-id` = `item`.`author-id` WHERE `thread`.`author-id` = 0");
252
253         Config::set('system', 'maintenance', 0);
254         return Update::SUCCESS;
255 }
256
257 function update_1261()
258 {
259         // This fixes the results of an issue in the develop branch of 2018-05.
260         DBA::update('contact', ['blocked' => false, 'pending' => false], ['uid' => 0, 'blocked' => true, 'pending' => true]);
261         return Update::SUCCESS;
262 }
263
264 function update_1278()
265 {
266         Config::set('system', 'maintenance', 1);
267         Config::set(
268                 'system',
269                 'maintenance_reason',
270                 L10n::t(
271                         '%s: Updating post-type.',
272                         DateTimeFormat::utcNow().' '.date('e')
273                 )
274         );
275
276         Item::update(['post-type' => Item::PT_PAGE], ['bookmark' => true]);
277         Item::update(['post-type' => Item::PT_PERSONAL_NOTE], ['type' => 'note']);
278
279         Config::set('system', 'maintenance', 0);
280
281         return Update::SUCCESS;
282 }
283
284 function update_1288()
285 {
286         // Updates missing `uri-id` values
287
288         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");
289         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");
290
291         return Update::SUCCESS;
292 }
293
294 // Post-update script of PR 5751
295 function update_1296()
296 {
297         $translateKey = 'gender';
298         $allData = DBA::select('profile', ['id', $translateKey]);
299         $allLangs = L10n::getAvailableLanguages();
300         $success = 0;
301         $fail = 0;
302         foreach ($allData as $key => $data) {
303                 $toTranslate = $data[$translateKey];
304                 if ($toTranslate != '') {
305                         foreach ($allLangs as $key => $lang) {
306                                 $a = new \stdClass();
307                                 $a->strings = [];
308
309                                 // First we get the the localizations
310                                 if (file_exists("view/lang/$lang/strings.php")) {
311                                         include "view/lang/$lang/strings.php";
312                                 }
313                                 if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
314                                         include "addon/morechoice/lang/$lang/strings.php";
315                                 }
316
317                                 $localizedStrings = $a->strings;
318                                 unset($a);
319
320                                 $key = array_search($toTranslate, $localizedStrings);
321                                 if ($key !== false) {
322                                         break;
323                                 }
324
325                                 // defaulting to empty string
326                                 $key = '';
327                         }
328
329                         if ($key == '') {
330                                 $fail++;
331                         } else {
332                                 DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
333                                 logger::log('Updated contact ' . $data['id'] . " to $translateKey " . $key .
334                                         ' (was: ' . $data[$translateKey] . ')');
335                                 Worker::add(PRIORITY_LOW, 'ProfileUpdate', $data['id']);                
336                                 Contact::updateSelfFromUserID($data['id']);
337                                 GContact::updateForUser($data['id']);
338                                 $success++;
339                         }
340                 }
341         }
342
343         Logger::log($translateKey . " fix completed. Success: $success. Fail: $fail");
344         return Update::SUCCESS;
345 }
346 // Post-update script of PR 5751
347 function update_1297()
348 {
349         $translateKey = 'marital';
350         $allData = DBA::select('profile', ['id', $translateKey]);
351         $allLangs = L10n::getAvailableLanguages();
352         $success = 0;
353         $fail = 0;
354         foreach ($allData as $key => $data) {
355                 $toTranslate = $data[$translateKey];
356                 if ($toTranslate != '') {
357                         foreach ($allLangs as $key => $lang) {
358                                 $a = new \stdClass();
359                                 $a->strings = [];
360
361                                 // First we get the the localizations
362                                 if (file_exists("view/lang/$lang/strings.php")) {
363                                         include "view/lang/$lang/strings.php";
364                                 }
365                                 if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
366                                         include "addon/morechoice/lang/$lang/strings.php";
367                                 }
368
369                                 $localizedStrings = $a->strings;
370                                 unset($a);
371
372                                 $key = array_search($toTranslate, $localizedStrings);
373                                 if ($key !== false) {
374                                         break;
375                                 }
376
377                                 // defaulting to empty string
378                                 $key = '';
379                         }
380
381                         if ($key == '') {
382                                 $fail++;
383                         } else {
384                                 DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
385                                 logger::log('Updated contact ' . $data['id'] . " to $translateKey " . $key .
386                                         ' (was: ' . $data[$translateKey] . ')');
387                                 Worker::add(PRIORITY_LOW, 'ProfileUpdate', $data['id']);                
388                                 Contact::updateSelfFromUserID($data['id']);
389                                 GContact::updateForUser($data['id']);
390                                 $success++;
391                         }
392                 }
393         }
394
395         Logger::log($translateKey . " fix completed. Success: $success. Fail: $fail");
396         return Update::SUCCESS;
397 }
398
399 // Post-update script of PR 5751
400 function update_1298()
401 {
402         $translateKey = 'sexual';
403         $allData = DBA::select('profile', ['id', $translateKey]);
404         $allLangs = L10n::getAvailableLanguages();
405         $success = 0;
406         $fail = 0;
407         foreach ($allData as $key => $data) {
408                 $toTranslate = $data[$translateKey];
409                 if ($toTranslate != '') {
410                         foreach ($allLangs as $key => $lang) {
411                                 $a = new \stdClass();
412                                 $a->strings = [];
413
414                                 // First we get the the localizations
415                                 if (file_exists("view/lang/$lang/strings.php")) {
416                                         include "view/lang/$lang/strings.php";
417                                 }
418                                 if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
419                                         include "addon/morechoice/lang/$lang/strings.php";
420                                 }
421
422                                 $localizedStrings = $a->strings;
423                                 unset($a);
424
425                                 $key = array_search($toTranslate, $localizedStrings);
426                                 if ($key !== false) {
427                                         break;
428                                 }
429
430                                 // defaulting to empty string
431                                 $key = '';
432                         }
433
434                         if ($key == '') {
435                                 $fail++;
436                         } else {
437                                 DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
438                                 logger::log('Updated contact ' . $data['id'] . " to $translateKey " . $key .
439                                         ' (was: ' . $data[$translateKey] . ')');
440                                 Worker::add(PRIORITY_LOW, 'ProfileUpdate', $data['id']);                
441                                 Contact::updateSelfFromUserID($data['id']);
442                                 GContact::updateForUser($data['id']);
443                                 $success++;
444                         }
445                 }
446         }
447
448         Logger::log($translateKey . " fix completed. Success: $success. Fail: $fail");
449         return Update::SUCCESS;
450 }