Merge pull request #8294 from annando/no-gender
[friendica.git/.git] / update.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  * Automatic post-databse structure change updates
21  *
22  * These functions are responsible for doing critical post update changes to the data (not the structure) in the database.
23  *
24  * Database structure changes are done in static/dbstructure.config.php
25  *
26  * For non-critical database migrations, please add a method in the Database\PostUpdate class
27  *
28  * If there is a need for a post update to a structure change, update this file
29  * by adding a new function at the end with the number of the new DB_UPDATE_VERSION.
30  *
31  * The numbered script in this file has to be exactly like the DB_UPDATE_VERSION
32  *
33  * Example:
34  * You are currently on version 4711 and you are preparing changes that demand an update script.
35  *
36  * 1. Create a function "update_4712()" here in the update.php
37  * 2. Apply the needed structural changes in static/dbStructure.php
38  * 3. Set DB_UPDATE_VERSION in static/dbstructure.config.php to 4712.
39  *
40  * If you need to run a script before the database update, name the function "pre_update_4712()"
41  */
42
43 use Friendica\Core\Addon;
44 use Friendica\Core\Logger;
45 use Friendica\Core\Update;
46 use Friendica\Core\Worker;
47 use Friendica\Database\DBA;
48 use Friendica\DI;
49 use Friendica\Model\Contact;
50 use Friendica\Model\GContact;
51 use Friendica\Model\Item;
52 use Friendica\Model\User;
53 use Friendica\Model\Storage;
54 use Friendica\Util\DateTimeFormat;
55 use Friendica\Worker\Delivery;
56
57 function update_1179()
58 {
59         if (DI::config()->get('system', 'no_community_page')) {
60                 DI::config()->set('system', 'community_page_style', CP_NO_COMMUNITY_PAGE);
61         }
62
63         // Update the central item storage with uid=0
64         Worker::add(PRIORITY_LOW, "threadupdate");
65
66         return Update::SUCCESS;
67 }
68
69 function update_1181()
70 {
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
81         if (strlen(DI::config()->get('system', 'directory_submit_url')) &&
82                 !strlen(DI::config()->get('system', 'directory'))) {
83                 DI::config()->set('system', 'directory', dirname(DI::config()->get('system', 'directory_submit_url')));
84                 DI::config()->delete('system', 'directory_submit_url');
85         }
86
87         return Update::SUCCESS;
88 }
89
90 function update_1191()
91 {
92         DI::config()->set('system', 'maintenance', 1);
93
94         if (Addon::isEnabled('forumlist')) {
95                 $addon = 'forumlist';
96                 $addons = DI::config()->get('system', 'addon');
97                 $addons_arr = [];
98
99                 if ($addons) {
100                         $addons_arr = explode(",", str_replace(" ", "", $addons));
101
102                         $idx = array_search($addon, $addons_arr);
103                         if ($idx !== false) {
104                                 unset($addons_arr[$idx]);
105                                 //delete forumlist manually from addon and hook table
106                                 // since Addon::uninstall() don't work here
107                                 q("DELETE FROM `addon` WHERE `name` = 'forumlist' ");
108                                 q("DELETE FROM `hook` WHERE `file` = 'addon/forumlist/forumlist.php' ");
109                                 DI::config()->set('system', 'addon', implode(", ", $addons_arr));
110                         }
111                 }
112         }
113
114         // select old formlist addon entries
115         $r = q("SELECT `uid`, `cat`, `k`, `v` FROM `pconfig` WHERE `cat` = '%s' ",
116                 DBA::escape('forumlist')
117         );
118
119         // convert old forumlist addon entries in new config entries
120         if (DBA::isResult($r)) {
121                 foreach ($r as $rr) {
122                         $uid = $rr['uid'];
123                         $family = $rr['cat'];
124                         $key = $rr['k'];
125                         $value = $rr['v'];
126
127                         if ($key === 'randomise') {
128                                 DI::pConfig()->delete($uid, $family, $key);
129                         }
130
131                         if ($key === 'show_on_profile') {
132                                 if ($value) {
133                                         DI::pConfig()->set($uid, 'feature', 'forumlist_profile', $value);
134                                 }
135
136                                 DI::pConfig()->delete($uid, $family, $key);
137                         }
138
139                         if ($key === 'show_on_network') {
140                                 if ($value) {
141                                         DI::pConfig()->set($uid, 'feature', 'forumlist_widget', $value);
142                                 }
143
144                                 DI::pConfig()->delete($uid, $family, $key);
145                         }
146                 }
147         }
148
149         DI::config()->set('system', 'maintenance', 0);
150
151         return Update::SUCCESS;
152 }
153
154 function update_1203()
155 {
156         $r = q("UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)",
157                 DBA::escape(User::ACCOUNT_TYPE_COMMUNITY),
158                 DBA::escape(User::PAGE_FLAGS_COMMUNITY),
159                 DBA::escape(User::PAGE_FLAGS_PRVGROUP)
160         );
161 }
162
163 function update_1244()
164 {
165         // Sets legacy_password for all legacy hashes
166         DBA::update('user', ['legacy_password' => true], ['SUBSTR(password, 1, 4) != "$2y$"']);
167
168         // All legacy hashes are re-hashed using the new secure hashing function
169         $stmt = DBA::select('user', ['uid', 'password'], ['legacy_password' => true]);
170         while ($user = DBA::fetch($stmt)) {
171                 DBA::update('user', ['password' => User::hashPassword($user['password'])], ['uid' => $user['uid']]);
172         }
173
174         // Logged in users are forcibly logged out
175         DBA::delete('session', ['1 = 1']);
176
177         return Update::SUCCESS;
178 }
179
180 function update_1245()
181 {
182         $rino = DI::config()->get('system', 'rino_encrypt');
183
184         if (!$rino) {
185                 return Update::SUCCESS;
186         }
187
188         DI::config()->set('system', 'rino_encrypt', 1);
189
190         return Update::SUCCESS;
191 }
192
193 function update_1247()
194 {
195         // Removing hooks with the old name
196         DBA::e("DELETE FROM `hook`
197 WHERE `hook` LIKE 'plugin_%'");
198
199         // Make sure we install the new renamed ones
200         Addon::reload();
201 }
202
203 function update_1260()
204 {
205         DI::config()->set('system', 'maintenance', 1);
206         DI::config()->set(
207                 'system',
208                 'maintenance_reason',
209                 DI::l10n()->t(
210                         '%s: Updating author-id and owner-id in item and thread table. ',
211                         DateTimeFormat::utcNow().' '.date('e')
212                 )
213         );
214
215         $items = DBA::p("SELECT `id`, `owner-link`, `owner-name`, `owner-avatar`, `network` FROM `item`
216                 WHERE `owner-id` = 0 AND `owner-link` != ''");
217         while ($item = DBA::fetch($items)) {
218                 $contact = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
219                         'photo' => $item['owner-avatar'], 'network' => $item['network']];
220                 $cid = Contact::getIdForURL($item['owner-link'], 0, false, $contact);
221                 if (empty($cid)) {
222                         continue;
223                 }
224                 Item::update(['owner-id' => $cid], ['id' => $item['id']]);
225         }
226         DBA::close($items);
227
228         DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
229                 SET `thread`.`owner-id` = `item`.`owner-id` WHERE `thread`.`owner-id` = 0");
230
231         $items = DBA::p("SELECT `id`, `author-link`, `author-name`, `author-avatar`, `network` FROM `item`
232                 WHERE `author-id` = 0 AND `author-link` != ''");
233         while ($item = DBA::fetch($items)) {
234                 $contact = ['url' => $item['author-link'], 'name' => $item['author-name'],
235                         'photo' => $item['author-avatar'], 'network' => $item['network']];
236                 $cid = Contact::getIdForURL($item['author-link'], 0, false, $contact);
237                 if (empty($cid)) {
238                         continue;
239                 }
240                 Item::update(['author-id' => $cid], ['id' => $item['id']]);
241         }
242         DBA::close($items);
243
244         DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
245                 SET `thread`.`author-id` = `item`.`author-id` WHERE `thread`.`author-id` = 0");
246
247         DI::config()->set('system', 'maintenance', 0);
248         return Update::SUCCESS;
249 }
250
251 function update_1261()
252 {
253         // This fixes the results of an issue in the develop branch of 2018-05.
254         DBA::update('contact', ['blocked' => false, 'pending' => false], ['uid' => 0, 'blocked' => true, 'pending' => true]);
255         return Update::SUCCESS;
256 }
257
258 function update_1278()
259 {
260         DI::config()->set('system', 'maintenance', 1);
261         DI::config()->set(
262                 'system',
263                 'maintenance_reason',
264                 DI::l10n()->t(
265                         '%s: Updating post-type.',
266                         DateTimeFormat::utcNow().' '.date('e')
267                 )
268         );
269
270         Item::update(['post-type' => Item::PT_PAGE], ['bookmark' => true]);
271         Item::update(['post-type' => Item::PT_PERSONAL_NOTE], ['type' => 'note']);
272
273         DI::config()->set('system', 'maintenance', 0);
274
275         return Update::SUCCESS;
276 }
277
278 function update_1288()
279 {
280         // Updates missing `uri-id` values
281
282         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");
283         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");
284
285         return Update::SUCCESS;
286 }
287
288 // Post-update script of PR 5751
289 function update_1298()
290 {
291         $keys = ['gender', 'marital', 'sexual'];
292         foreach ($keys as $translateKey) {
293                 $allData = DBA::select('profile', ['id', $translateKey]);
294                 $allLangs = DI::l10n()->getAvailableLanguages();
295                 $success = 0;
296                 $fail = 0;
297                 foreach ($allData as $key => $data) {
298                         $toTranslate = $data[$translateKey];
299                         if ($toTranslate != '') {
300                                 foreach ($allLangs as $key => $lang) {
301                                         $a = new \stdClass();
302                                         $a->strings = [];
303
304                                         // First we get the the localizations
305                                         if (file_exists("view/lang/$lang/strings.php")) {
306                                                 include "view/lang/$lang/strings.php";
307                                         }
308                                         if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
309                                                 include "addon/morechoice/lang/$lang/strings.php";
310                                         }
311
312                                         $localizedStrings = $a->strings;
313                                         unset($a);
314
315                                         $key = array_search($toTranslate, $localizedStrings);
316                                         if ($key !== false) {
317                                                 break;
318                                         }
319
320                                         // defaulting to empty string
321                                         $key = '';
322                                 }
323
324                                 if ($key == '') {
325                                         $fail++;
326                                 } else {
327                                         DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
328                                         Logger::notice('Updated contact', ['action' => 'update', 'contact' => $data['id'], "$translateKey" => $key,
329                                                 'was' => $data[$translateKey]]);
330                                         Worker::add(PRIORITY_LOW, 'ProfileUpdate', $data['id']);
331                                         Contact::updateSelfFromUserID($data['id']);
332                                         GContact::updateForUser($data['id']);
333                                         $success++;
334                                 }
335                         }
336                 }
337
338                 Logger::notice($translateKey . " fix completed", ['action' => 'update', 'translateKey' => $translateKey, 'Success' => $success, 'Fail' => $fail ]);
339         }
340         return Update::SUCCESS;
341 }
342
343 function update_1309()
344 {
345         $queue = DBA::select('queue', ['id', 'cid', 'guid']);
346         while ($entry = DBA::fetch($queue)) {
347                 $contact = DBA::selectFirst('contact', ['uid'], ['id' => $entry['cid']]);
348                 if (!DBA::isResult($contact)) {
349                         continue;
350                 }
351
352                 $item = Item::selectFirst(['id', 'gravity'], ['uid' => $contact['uid'], 'guid' => $entry['guid']]);
353                 if (!DBA::isResult($item)) {
354                         continue;
355                 }
356
357                 $deliver_options = ['priority' => PRIORITY_MEDIUM, 'dont_fork' => true];
358                 Worker::add($deliver_options, 'Delivery', Delivery::POST, $item['id'], $entry['cid']);
359                 Logger::info('Added delivery worker', ['item' => $item['id'], 'contact' => $entry['cid']]);
360                 DBA::delete('queue', ['id' => $entry['id']]);
361         }
362         return Update::SUCCESS;
363 }
364
365 function update_1315()
366 {
367         DBA::delete('item-delivery-data', ['postopts' => '', 'inform' => '', 'queue_count' => 0, 'queue_done' => 0]);
368         return Update::SUCCESS;
369 }
370
371 function update_1318()
372 {
373         DBA::update('profile', ['marital' => "In a relation"], ['marital' => "Unavailable"]);
374         DBA::update('profile', ['marital' => "Single"], ['marital' => "Available"]);
375
376         Worker::add(PRIORITY_LOW, 'ProfileUpdate');
377         return Update::SUCCESS;
378 }
379
380 function update_1323()
381 {
382         $users = DBA::select('user', ['uid']);
383         while ($user = DBA::fetch($users)) {
384                 Contact::updateSelfFromUserID($user['uid']);
385         }
386         DBA::close($users);
387
388         return Update::SUCCESS;
389 }
390
391 function update_1327()
392 {
393         $contacts = DBA::select('contact', ['uid', 'id', 'blocked', 'readonly'], ["`uid` != ? AND (`blocked` OR `readonly`) AND NOT `pending`", 0]);
394         while ($contact = DBA::fetch($contacts)) {
395                 Contact::setBlockedForUser($contact['id'], $contact['uid'], $contact['blocked']);
396                 Contact::setIgnoredForUser($contact['id'], $contact['uid'], $contact['readonly']);
397         }
398         DBA::close($contacts);
399
400         return Update::SUCCESS;
401 }
402
403 function update_1330()
404 {
405         $currStorage = DI::config()->get('storage', 'class', '');
406
407         // set the name of the storage instead of the classpath as config
408         if (!empty($currStorage)) {
409                 /** @var Storage\IStorage $currStorage */
410                 if (!DI::config()->set('storage', 'name', $currStorage::getName())) {
411                         return Update::FAILED;
412                 }
413
414                 // try to delete the class since it isn't needed. This won't work with config files
415                 DI::config()->delete('storage', 'class');
416         }
417
418         // Update attachments and photos
419         if (!DBA::p("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 25) WHERE `photo`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'") ||
420             !DBA::p("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 25) WHERE `attach`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'")) {
421                 return Update::FAILED;
422         };
423
424         return Update::SUCCESS;
425 }
426
427 function update_1332()
428 {
429         $condition = ["`is-default` IS NOT NULL"];
430         $profiles = DBA::select('profile', [], $condition);
431
432         while ($profile = DBA::fetch($profiles)) {
433                 DI::profileField()->migrateFromLegacyProfile($profile);
434         }
435         DBA::close($profiles);
436
437         DBA::update('contact', ['profile-id' => null], ['`profile-id` IS NOT NULL']);
438
439         return Update::SUCCESS;
440 }