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