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