some updates for the work over the wekend
[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::uninstall('forumlist');
96         }
97
98         // select old formlist addon entries
99         $r = q("SELECT `uid`, `cat`, `k`, `v` FROM `pconfig` WHERE `cat` = '%s' ",
100                 DBA::escape('forumlist')
101         );
102
103         // convert old forumlist addon entries in new config entries
104         if (DBA::isResult($r)) {
105                 foreach ($r as $rr) {
106                         $uid = $rr['uid'];
107                         $family = $rr['cat'];
108                         $key = $rr['k'];
109                         $value = $rr['v'];
110
111                         if ($key === 'randomise') {
112                                 DI::pConfig()->delete($uid, $family, $key);
113                         }
114
115                         if ($key === 'show_on_profile') {
116                                 if ($value) {
117                                         DI::pConfig()->set($uid, 'feature', 'forumlist_profile', $value);
118                                 }
119
120                                 DI::pConfig()->delete($uid, $family, $key);
121                         }
122
123                         if ($key === 'show_on_network') {
124                                 if ($value) {
125                                         DI::pConfig()->set($uid, 'feature', 'forumlist_widget', $value);
126                                 }
127
128                                 DI::pConfig()->delete($uid, $family, $key);
129                         }
130                 }
131         }
132
133         DI::config()->set('system', 'maintenance', 0);
134
135         return Update::SUCCESS;
136 }
137
138 function update_1203()
139 {
140         $r = q("UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)",
141                 DBA::escape(User::ACCOUNT_TYPE_COMMUNITY),
142                 DBA::escape(User::PAGE_FLAGS_COMMUNITY),
143                 DBA::escape(User::PAGE_FLAGS_PRVGROUP)
144         );
145 }
146
147 function update_1244()
148 {
149         // Sets legacy_password for all legacy hashes
150         DBA::update('user', ['legacy_password' => true], ['SUBSTR(password, 1, 4) != "$2y$"']);
151
152         // All legacy hashes are re-hashed using the new secure hashing function
153         $stmt = DBA::select('user', ['uid', 'password'], ['legacy_password' => true]);
154         while ($user = DBA::fetch($stmt)) {
155                 DBA::update('user', ['password' => User::hashPassword($user['password'])], ['uid' => $user['uid']]);
156         }
157
158         // Logged in users are forcibly logged out
159         DBA::delete('session', ['1 = 1']);
160
161         return Update::SUCCESS;
162 }
163
164 function update_1245()
165 {
166         $rino = DI::config()->get('system', 'rino_encrypt');
167
168         if (!$rino) {
169                 return Update::SUCCESS;
170         }
171
172         DI::config()->set('system', 'rino_encrypt', 1);
173
174         return Update::SUCCESS;
175 }
176
177 function update_1247()
178 {
179         // Removing hooks with the old name
180         DBA::e("DELETE FROM `hook`
181 WHERE `hook` LIKE 'plugin_%'");
182
183         // Make sure we install the new renamed ones
184         Addon::reload();
185 }
186
187 function update_1260()
188 {
189         DI::config()->set('system', 'maintenance', 1);
190         DI::config()->set(
191                 'system',
192                 'maintenance_reason',
193                 DI::l10n()->t(
194                         '%s: Updating author-id and owner-id in item and thread table. ',
195                         DateTimeFormat::utcNow().' '.date('e')
196                 )
197         );
198
199         $items = DBA::p("SELECT `id`, `owner-link`, `owner-name`, `owner-avatar`, `network` FROM `item`
200                 WHERE `owner-id` = 0 AND `owner-link` != ''");
201         while ($item = DBA::fetch($items)) {
202                 $contact = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
203                         'photo' => $item['owner-avatar'], 'network' => $item['network']];
204                 $cid = Contact::getIdForURL($item['owner-link'], 0, false, $contact);
205                 if (empty($cid)) {
206                         continue;
207                 }
208                 Item::update(['owner-id' => $cid], ['id' => $item['id']]);
209         }
210         DBA::close($items);
211
212         DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
213                 SET `thread`.`owner-id` = `item`.`owner-id` WHERE `thread`.`owner-id` = 0");
214
215         $items = DBA::p("SELECT `id`, `author-link`, `author-name`, `author-avatar`, `network` FROM `item`
216                 WHERE `author-id` = 0 AND `author-link` != ''");
217         while ($item = DBA::fetch($items)) {
218                 $contact = ['url' => $item['author-link'], 'name' => $item['author-name'],
219                         'photo' => $item['author-avatar'], 'network' => $item['network']];
220                 $cid = Contact::getIdForURL($item['author-link'], 0, false, $contact);
221                 if (empty($cid)) {
222                         continue;
223                 }
224                 Item::update(['author-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`.`author-id` = `item`.`author-id` WHERE `thread`.`author-id` = 0");
230
231         DI::config()->set('system', 'maintenance', 0);
232         return Update::SUCCESS;
233 }
234
235 function update_1261()
236 {
237         // This fixes the results of an issue in the develop branch of 2018-05.
238         DBA::update('contact', ['blocked' => false, 'pending' => false], ['uid' => 0, 'blocked' => true, 'pending' => true]);
239         return Update::SUCCESS;
240 }
241
242 function update_1278()
243 {
244         DI::config()->set('system', 'maintenance', 1);
245         DI::config()->set(
246                 'system',
247                 'maintenance_reason',
248                 DI::l10n()->t(
249                         '%s: Updating post-type.',
250                         DateTimeFormat::utcNow().' '.date('e')
251                 )
252         );
253
254         Item::update(['post-type' => Item::PT_PAGE], ['bookmark' => true]);
255         Item::update(['post-type' => Item::PT_PERSONAL_NOTE], ['type' => 'note']);
256
257         DI::config()->set('system', 'maintenance', 0);
258
259         return Update::SUCCESS;
260 }
261
262 function update_1288()
263 {
264         // Updates missing `uri-id` values
265
266         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");
267         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");
268
269         return Update::SUCCESS;
270 }
271
272 // Post-update script of PR 5751
273 function update_1298()
274 {
275         $keys = ['gender', 'marital', 'sexual'];
276         foreach ($keys as $translateKey) {
277                 $allData = DBA::select('profile', ['id', $translateKey]);
278                 $allLangs = DI::l10n()->getAvailableLanguages();
279                 $success = 0;
280                 $fail = 0;
281                 foreach ($allData as $key => $data) {
282                         $toTranslate = $data[$translateKey];
283                         if ($toTranslate != '') {
284                                 foreach ($allLangs as $key => $lang) {
285                                         $a = new \stdClass();
286                                         $a->strings = [];
287
288                                         // First we get the the localizations
289                                         if (file_exists("view/lang/$lang/strings.php")) {
290                                                 include "view/lang/$lang/strings.php";
291                                         }
292                                         if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
293                                                 include "addon/morechoice/lang/$lang/strings.php";
294                                         }
295
296                                         $localizedStrings = $a->strings;
297                                         unset($a);
298
299                                         $key = array_search($toTranslate, $localizedStrings);
300                                         if ($key !== false) {
301                                                 break;
302                                         }
303
304                                         // defaulting to empty string
305                                         $key = '';
306                                 }
307
308                                 if ($key == '') {
309                                         $fail++;
310                                 } else {
311                                         DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
312                                         Logger::notice('Updated contact', ['action' => 'update', 'contact' => $data['id'], "$translateKey" => $key,
313                                                 'was' => $data[$translateKey]]);
314                                         Worker::add(PRIORITY_LOW, 'ProfileUpdate', $data['id']);
315                                         Contact::updateSelfFromUserID($data['id']);
316                                         GContact::updateForUser($data['id']);
317                                         $success++;
318                                 }
319                         }
320                 }
321
322                 Logger::notice($translateKey . " fix completed", ['action' => 'update', 'translateKey' => $translateKey, 'Success' => $success, 'Fail' => $fail ]);
323         }
324         return Update::SUCCESS;
325 }
326
327 function update_1309()
328 {
329         $queue = DBA::select('queue', ['id', 'cid', 'guid']);
330         while ($entry = DBA::fetch($queue)) {
331                 $contact = DBA::selectFirst('contact', ['uid'], ['id' => $entry['cid']]);
332                 if (!DBA::isResult($contact)) {
333                         continue;
334                 }
335
336                 $item = Item::selectFirst(['id', 'gravity'], ['uid' => $contact['uid'], 'guid' => $entry['guid']]);
337                 if (!DBA::isResult($item)) {
338                         continue;
339                 }
340
341                 $deliver_options = ['priority' => PRIORITY_MEDIUM, 'dont_fork' => true];
342                 Worker::add($deliver_options, 'Delivery', Delivery::POST, $item['id'], $entry['cid']);
343                 Logger::info('Added delivery worker', ['item' => $item['id'], 'contact' => $entry['cid']]);
344                 DBA::delete('queue', ['id' => $entry['id']]);
345         }
346         return Update::SUCCESS;
347 }
348
349 function update_1315()
350 {
351         DBA::delete('item-delivery-data', ['postopts' => '', 'inform' => '', 'queue_count' => 0, 'queue_done' => 0]);
352         return Update::SUCCESS;
353 }
354
355 function update_1318()
356 {
357         DBA::update('profile', ['marital' => "In a relation"], ['marital' => "Unavailable"]);
358         DBA::update('profile', ['marital' => "Single"], ['marital' => "Available"]);
359
360         Worker::add(PRIORITY_LOW, 'ProfileUpdate');
361         return Update::SUCCESS;
362 }
363
364 function update_1323()
365 {
366         $users = DBA::select('user', ['uid']);
367         while ($user = DBA::fetch($users)) {
368                 Contact::updateSelfFromUserID($user['uid']);
369         }
370         DBA::close($users);
371
372         return Update::SUCCESS;
373 }
374
375 function update_1327()
376 {
377         $contacts = DBA::select('contact', ['uid', 'id', 'blocked', 'readonly'], ["`uid` != ? AND (`blocked` OR `readonly`) AND NOT `pending`", 0]);
378         while ($contact = DBA::fetch($contacts)) {
379                 Contact::setBlockedForUser($contact['id'], $contact['uid'], $contact['blocked']);
380                 Contact::setIgnoredForUser($contact['id'], $contact['uid'], $contact['readonly']);
381         }
382         DBA::close($contacts);
383
384         return Update::SUCCESS;
385 }
386
387 function update_1330()
388 {
389         $currStorage = DI::config()->get('storage', 'class', '');
390
391         // set the name of the storage instead of the classpath as config
392         if (!empty($currStorage)) {
393                 /** @var Storage\IStorage $currStorage */
394                 if (!DI::config()->set('storage', 'name', $currStorage::getName())) {
395                         return Update::FAILED;
396                 }
397
398                 // try to delete the class since it isn't needed. This won't work with config files
399                 DI::config()->delete('storage', 'class');
400         }
401
402         // Update attachments and photos
403         if (!DBA::p("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 25) WHERE `photo`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'") ||
404             !DBA::p("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 25) WHERE `attach`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'")) {
405                 return Update::FAILED;
406         };
407
408         return Update::SUCCESS;
409 }
410
411 function update_1332()
412 {
413         $condition = ["`is-default` IS NOT NULL"];
414         $profiles = DBA::select('profile', [], $condition);
415
416         while ($profile = DBA::fetch($profiles)) {
417                 DI::profileField()->migrateFromLegacyProfile($profile);
418         }
419         DBA::close($profiles);
420
421         DBA::update('contact', ['profile-id' => null], ['`profile-id` IS NOT NULL']);
422
423         return Update::SUCCESS;
424 }