]> git.mxchange.org Git - friendica.git/blob - update.php
Solved warnings
[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\Item;
13 use Friendica\Model\User;
14 use Friendica\Util\DateTimeFormat;
15
16 /**
17  *
18  * update.php - automatic system update
19  *
20  * This function is responsible for doing post update changes to the data
21  * (not the structure) in the database.
22  *
23  * Database structure changes are done in config/dbstructure.config.php
24  *
25  * If there is a need for a post process to a structure change, update this file
26  * by adding a new function at the end with the number of the new DB_UPDATE_VERSION.
27  *
28  * The numbered script in this file has to be exactly like the DB_UPDATE_VERSION
29  *
30  * Example:
31  * You are currently on version 4711 and you are preparing changes that demand an update script.
32  *
33  * 1. Create a function "update_4712()" here in the update.php
34  * 2. Apply the needed structural changes in config/dbStructure.php
35  * 3. Set DB_UPDATE_VERSION in config/dbstructure.config.php to 4712.
36  *
37  * If you need to run a script before the database update, name the function "pre_update_4712()"
38  */
39
40 function update_1178()
41 {
42         require_once 'mod/profiles.php';
43
44         $profiles = q("SELECT `uid`, `about`, `locality`, `pub_keywords`, `gender` FROM `profile` WHERE `is-default`");
45
46         foreach ($profiles as $profile) {
47                 if ($profile["about"].$profile["locality"].$profile["pub_keywords"].$profile["gender"] == "") {
48                         continue;
49                 }
50
51                 $profile["pub_keywords"] = profile_clean_keywords($profile["pub_keywords"]);
52
53                 $r = q(
54                         "UPDATE `contact` SET `about` = '%s', `location` = '%s', `keywords` = '%s', `gender` = '%s'
55                         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(
124                 "SELECT `uid`, `cat`, `k`, `v` FROM `pconfig` WHERE `cat` = '%s' ",
125                 DBA::escape('forumlist')
126         );
127
128         // convert old forumlist addon entries in new config entries
129         if (DBA::isResult($r)) {
130                 foreach ($r as $rr) {
131                         $uid = $rr['uid'];
132                         $family = $rr['cat'];
133                         $key = $rr['k'];
134                         $value = $rr['v'];
135
136                         if ($key === 'randomise') {
137                                 PConfig::delete($uid, $family, $key);
138                         }
139
140                         if ($key === 'show_on_profile') {
141                                 if ($value) {
142                                         PConfig::set($uid, feature, forumlist_profile, $value);
143                                 }
144
145                                 PConfig::delete($uid, $family, $key);
146                         }
147
148                         if ($key === 'show_on_network') {
149                                 if ($value) {
150                                         PConfig::set($uid, feature, forumlist_widget, $value);
151                                 }
152
153                                 PConfig::delete($uid, $family, $key);
154                         }
155                 }
156         }
157
158         Config::set('system', 'maintenance', 0);
159
160         return Update::SUCCESS;
161 }
162
163 function update_1203()
164 {
165         $r = q(
166                 "UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)",
167                 DBA::escape(Contact::ACCOUNT_TYPE_COMMUNITY),
168                 DBA::escape(Contact::PAGE_COMMUNITY),
169                 DBA::escape(Contact::PAGE_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(
265                 'contact',
266                 ['blocked' => false, 'pending' => false],
267                 ['uid' => 0, 'blocked' => true, 'pending' => true]
268         );
269         return Update::SUCCESS;
270 }
271
272 function update_1278()
273 {
274         Config::set('system', 'maintenance', 1);
275         Config::set(
276                 'system',
277                 'maintenance_reason',
278                 L10n::t(
279                         '%s: Updating post-type.',
280                         DateTimeFormat::utcNow().' '.date('e')
281                 )
282         );
283
284         Item::update(['post-type' => Item::PT_PAGE], ['bookmark' => true]);
285         Item::update(['post-type' => Item::PT_PERSONAL_NOTE], ['type' => 'note']);
286
287         Config::set('system', 'maintenance', 0);
288
289         return Update::SUCCESS;
290 }
291
292 function update_1288()
293 {
294         // Updates missing `uri-id` values
295
296         DBA::e("UPDATE `item-activity`
297                 INNER JOIN `item` ON `item`.`iaid` = `item-activity`.`id`
298                 SET `item-activity`.`uri-id` = `item`.`uri-id`
299                 WHERE `item-activity`.`uri-id` IS NULL OR `item-activity`.`uri-id` = 0");
300         DBA::e("UPDATE `item-content`
301                 INNER JOIN `item` ON `item`.`icid` = `item-content`.`id`
302                 SET `item-content`.`uri-id` = `item`.`uri-id`
303                 WHERE `item-content`.`uri-id` IS NULL OR `item-content`.`uri-id` = 0");
304
305         return Update::SUCCESS;
306 }
307
308         
309 // Post-update script of PR 5751
310 function update_1293()
311 {
312         $allGenders = DBA::select('contact', ['id', 'gender']);
313         $allLangs = L10n::getAvailableLanguages();
314         $success = 0;
315         $fail = 0;
316         foreach ($allGenders as $key => $gender) {
317                 if ($gender['gender'] != '') {
318                         foreach ($allLangs as $key => $lang) {
319                                 $a = new \stdClass();
320                                 $a->strings = [];
321
322                                 // First we get the the localizations
323                                 if (file_exists("view/lang/$lang/strings.php")) {
324                                         include "view/lang/$lang/strings.php";
325                                 }
326                                 if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
327                                         include "addon/morechoice/lang/$lang/strings.php";
328                                 }
329
330                                 $localizedStrings = $a->strings;
331                                 unset($a);
332
333                                 $key = array_search($gender['gender'], $localizedStrings);
334                                 if ($key !== false) {
335                                         break;
336                                 }
337
338                                 // defaulting to empty string
339                                 $key = '';
340                         }
341
342                         if ($key == '') {
343                                 $fail++;
344                         } else {
345                                 DBA::update('contact', ['gender' => $key], ['id' => $gender['id']]);
346                                 logger::log('Updated contact ' . $gender['id'] . ' to gender ' . $key .
347                                         ' (was: ' . $gender['gender'] . ')');
348                                 $success++;
349                         }
350                 }
351         }
352
353         Logger::log("Gender fix completed. Success: $success. Fail: $fail");
354         return Update::SUCCESS;
355 }