]> git.mxchange.org Git - friendica.git/blob - update.php
Merge pull request #7988 from friendica/MrPetovan-notice
[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\Model\Contact;
12 use Friendica\Model\GContact;
13 use Friendica\Model\Item;
14 use Friendica\Model\User;
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 (Config::get('system', 'no_community_page')) {
68                 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(Config::get('system', 'directory_submit_url')) &&
90                 !strlen(Config::get('system', 'directory'))) {
91                 Config::set('system', 'directory', dirname(Config::get('system', 'directory_submit_url')));
92                 Config::delete('system', 'directory_submit_url');
93         }
94
95         return Update::SUCCESS;
96 }
97
98 function update_1191()
99 {
100         Config::set('system', 'maintenance', 1);
101
102         if (Addon::isEnabled('forumlist')) {
103                 $addon = 'forumlist';
104                 $addons = 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                                 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                                 PConfig::delete($uid, $family, $key);
137                         }
138
139                         if ($key === 'show_on_profile') {
140                                 if ($value) {
141                                         PConfig::set($uid, feature, forumlist_profile, $value);
142                                 }
143
144                                 PConfig::delete($uid, $family, $key);
145                         }
146
147                         if ($key === 'show_on_network') {
148                                 if ($value) {
149                                         PConfig::set($uid, feature, forumlist_widget, $value);
150                                 }
151
152                                 PConfig::delete($uid, $family, $key);
153                         }
154                 }
155         }
156
157         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 = Config::get('system', 'rino_encrypt');
191
192         if (!$rino) {
193                 return Update::SUCCESS;
194         }
195
196         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         Config::set('system', 'maintenance', 1);
214         Config::set(
215                 'system',
216                 'maintenance_reason',
217                 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         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         Config::set('system', 'maintenance', 1);
269         Config::set(
270                 'system',
271                 'maintenance_reason',
272                 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         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 = 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