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