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