]> git.mxchange.org Git - friendica.git/blob - src/Core/Update.php
Merge pull request #13019 from MrPetovan/bug/deprecated
[friendica.git] / src / Core / Update.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core;
23
24 use Friendica\App;
25 use Friendica\App\Mode;
26 use Friendica\Database\DBA;
27 use Friendica\Database\DBStructure;
28 use Friendica\DI;
29 use Friendica\Model\User;
30 use Friendica\Network\HTTPException\InternalServerErrorException;
31 use Friendica\Util\DateTimeFormat;
32 use Friendica\Util\Strings;
33
34 class Update
35 {
36         const SUCCESS = 0;
37         const FAILED  = 1;
38
39         const NEW_TABLE_STRUCTURE_VERSION = 1288;
40
41         /**
42          * Function to check if the Database structure needs an update.
43          *
44          * @param string   $basePath   The base path of this application
45          * @param boolean  $via_worker Is the check run via the worker?
46          * @param App\Mode $mode       The current app mode
47          * @return void
48          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
49          */
50         public static function check(string $basePath, bool $via_worker)
51         {
52                 if (!DBA::connected()) {
53                         return;
54                 }
55
56                 // Don't check the status if the last update was failed
57                 if (DI::config()->get('system', 'update', Update::SUCCESS) == Update::FAILED) {
58                         return;
59                 }
60
61                 $build = DI::config()->get('system', 'build');
62
63                 if (empty($build)) {
64                         // legacy option - check if there's something in the Config table
65                         if (DBStructure::existsTable('config')) {
66                                 $dbConfig = DBA::selectFirst('config', ['v'], ['cat' => 'system', 'k' => 'build']);
67                                 if (!empty($dbConfig)) {
68                                         $build = $dbConfig['v'];
69                                 }
70                         }
71
72                         if (empty($build)) {
73                                 DI::config()->set('system', 'build', DB_UPDATE_VERSION - 1);
74                                 $build = DB_UPDATE_VERSION - 1;
75                         }
76                 }
77
78                 // We don't support upgrading from very old versions anymore
79                 if ($build < self::NEW_TABLE_STRUCTURE_VERSION) {
80                         $error = DI::l10n()->t('Updates from version %s are not supported. Please update at least to version 2021.01 and wait until the postupdate finished version 1383.', $build);
81                         if (DI::mode()->getExecutor() == Mode::INDEX) {
82                                 die($error);
83                         } else {
84                                 throw new InternalServerErrorException($error);
85                         }
86                 }
87
88                 // The postupdate has to completed version 1288 for the new post views to take over
89                 $postupdate = DI::keyValue()->get('post_update_version') ?? self::NEW_TABLE_STRUCTURE_VERSION;
90                 if ($postupdate < self::NEW_TABLE_STRUCTURE_VERSION) {
91                         $error = DI::l10n()->t('Updates from postupdate version %s are not supported. Please update at least to version 2021.01 and wait until the postupdate finished version 1383.', $postupdate);
92                         if (DI::mode()->getExecutor() == Mode::INDEX) {
93                                 die($error);
94                         } else {
95                                 throw new InternalServerErrorException($error);
96                         }
97                 }
98
99                 if ($build < DB_UPDATE_VERSION) {
100                         if ($via_worker) {
101                                 /*
102                                  * Calling the database update directly via the worker enables us to perform database changes to the workerqueue table itself.
103                                  * This is a fallback, since normally the database update will be performed by a worker job.
104                                  * This worker job doesn't work for changes to the "workerqueue" table itself.
105                                  */
106                                 self::run($basePath);
107                         } else {
108                                 Worker::add(Worker::PRIORITY_CRITICAL, 'DBUpdate');
109                         }
110                 }
111         }
112
113         /**
114          * Automatic database updates
115          *
116          * @param string $basePath The base path of this application
117          * @param bool   $force    Force the Update-Check even if the database version doesn't match
118          * @param bool   $override Overrides any running/stuck updates
119          * @param bool   $verbose  Run the Update-Check verbose
120          * @param bool   $sendMail Sends a Mail to the administrator in case of success/failure
121          * @return string Empty string if the update is successful, error messages otherwise
122          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
123          */
124         public static function run(string $basePath, bool $force = false, bool $override = false, bool $verbose = false, bool $sendMail = true): string
125         {
126                 // In force mode, we release the dbupdate lock first
127                 // Necessary in case of an stuck update
128                 if ($override) {
129                         DI::lock()->release('dbupdate', true);
130                 }
131
132                 if (!DBStructure::existsTable('config')) {
133                         DBA::e(<<<EOF
134 CREATE TABLE IF NOT EXISTS `config` (
135         `id` int unsigned NOT NULL auto_increment COMMENT '',
136         `cat` varbinary(50) NOT NULL DEFAULT '' COMMENT 'The category of the entry',
137         `k` varbinary(50) NOT NULL DEFAULT '' COMMENT 'The key of the entry',
138         `v` mediumtext COMMENT '',
139          PRIMARY KEY(`id`),
140          UNIQUE INDEX `cat_k` (`cat`,`k`)
141 ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='main configuration storage';
142 EOF
143 );
144                 }
145
146                 $build = DI::config()->get('system', 'build');
147
148                 if (empty($build)) {
149                         $dbConfig = DBA::selectFirst('config', ['v'], ['cat' => 'system', 'k' => 'build']);
150                         if (!empty($dbConfig)) {
151                                 $build = $dbConfig['v'];
152                         }
153
154                         if (empty($build) || ($build > DB_UPDATE_VERSION)) {
155                                 DI::config()->set('system', 'build', DB_UPDATE_VERSION - 1);
156                                 $build = DB_UPDATE_VERSION - 1;
157                         }
158                 }
159
160                 if ($build != DB_UPDATE_VERSION || $force) {
161                         require_once 'update.php';
162
163                         $stored = intval($build);
164                         $current = intval(DB_UPDATE_VERSION);
165                         if ($stored < $current || $force) {
166                                 DI::config()->reload();
167
168                                 // Compare the current structure with the defined structure
169                                 // If the Lock is acquired, never release it automatically to avoid double updates
170                                 if (DI::lock()->acquire('dbupdate', 0, Cache\Enum\Duration::INFINITE)) {
171
172                                         Logger::notice('Update starting.', ['from' => $stored, 'to' => $current]);
173
174                                         // Checks if the build changed during Lock acquiring (so no double update occurs)
175                                         $retryBuild = DI::config()->get('system', 'build');
176                                         if ($retryBuild != $build) {
177                                                 // legacy option - check if there's something in the Config table
178                                                 if (DBStructure::existsTable('config')) {
179                                                         $dbConfig = DBA::selectFirst('config', ['v'], ['cat' => 'system', 'k' => 'build']);
180                                                         if (!empty($dbConfig)) {
181                                                                 $retryBuild = intval($dbConfig['v']);
182                                                         }
183                                                 }
184
185                                                 if ($retryBuild != $build) {
186                                                         Logger::notice('Update already done.', ['from' => $build, 'retry' => $retryBuild, 'to' => $current]);
187                                                         DI::lock()->release('dbupdate');
188                                                         return '';
189                                                 }
190                                         }
191
192                                         DI::config()->set('system', 'maintenance', 1);
193
194                                         // run the pre_update_nnnn functions in update.php
195                                         for ($version = $stored + 1; $version <= $current; $version++) {
196                                                 Logger::notice('Execute pre update.', ['version' => $version]);
197                                                 DI::config()->set('system', 'maintenance_reason', DI::l10n()->t('%s: executing pre update %d',
198                                                         DateTimeFormat::utcNow() . ' ' . date('e'), $version));
199                                                 $r = self::runUpdateFunction($version, 'pre_update', $sendMail);
200                                                 if (!$r) {
201                                                         Logger::warning('Pre update failed', ['version' => $version]);
202                                                         DI::config()->set('system', 'update', Update::FAILED);
203                                                         DI::lock()->release('dbupdate');
204                                                         DI::config()->beginTransaction()
205                                                                                 ->set('system', 'maintenance', false)
206                                                                                 ->delete('system', 'maintenance_reason')
207                                                                                 ->commit();
208                                                         return $r;
209                                                 } else {
210                                                         Logger::notice('Pre update executed.', ['version' => $version]);
211                                                 }
212                                         }
213
214                                         // update the structure in one call
215                                         Logger::notice('Execute structure update');
216                                         $retval = DBStructure::performUpdate(false, $verbose);
217                                         if (!empty($retval)) {
218                                                 if ($sendMail) {
219                                                         self::updateFailed(
220                                                                 DB_UPDATE_VERSION,
221                                                                 $retval
222                                                         );
223                                                 }
224                                                 Logger::error('Update ERROR.', ['from' => $stored, 'to' => $current, 'retval' => $retval]);
225                                                 DI::config()->set('system', 'update', Update::FAILED);
226                                                 DI::lock()->release('dbupdate');
227                                                 DI::config()->beginTransaction()
228                                                                         ->set('system', 'maintenance', false)
229                                                                         ->delete('system', 'maintenance_reason')
230                                                                         ->commit();
231                                                 return $retval;
232                                         } else {
233                                                 Logger::notice('Database structure update finished.', ['from' => $stored, 'to' => $current]);
234                                         }
235
236                                         // run the update_nnnn functions in update.php
237                                         for ($version = $stored + 1; $version <= $current; $version++) {
238                                                 Logger::notice('Execute post update.', ['version' => $version]);
239                                                 DI::config()->set('system', 'maintenance_reason', DI::l10n()->t('%s: executing post update %d',
240                                                         DateTimeFormat::utcNow() . ' ' . date('e'), $version));
241                                                 $r = self::runUpdateFunction($version, 'update', $sendMail);
242                                                 if (!$r) {
243                                                         Logger::warning('Post update failed', ['version' => $version]);
244                                                         DI::config()->set('system', 'update', Update::FAILED);
245                                                         DI::lock()->release('dbupdate');
246                                                         DI::config()->beginTransaction()
247                                                                                 ->set('system', 'maintenance', false)
248                                                                                 ->delete('system', 'maintenance_reason')
249                                                                                 ->commit();
250                                                         return $r;
251                                                 } else {
252                                                         DI::config()->set('system', 'build', $version);
253                                                         Logger::notice('Post update executed.', ['version' => $version]);
254                                                 }
255                                         }
256
257                                         DI::config()->set('system', 'build', $current);
258                                         DI::config()->set('system', 'update', Update::SUCCESS);
259                                         DI::lock()->release('dbupdate');
260                                         DI::config()->beginTransaction()
261                                                                 ->set('system', 'maintenance', false)
262                                                                 ->delete('system', 'maintenance_reason')
263                                                                 ->commit();
264
265                                         Logger::notice('Update success.', ['from' => $stored, 'to' => $current]);
266                                         if ($sendMail) {
267                                                 self::updateSuccessful($stored, $current);
268                                         }
269                                 } else {
270                                         Logger::warning('Update lock could not be acquired');
271                                 }
272                         }
273                 }
274
275                 return '';
276         }
277
278         /**
279          * Executes a specific update function
280          *
281          * @param int    $version  the DB version number of the function
282          * @param string $prefix   the prefix of the function (update, pre_update)
283          * @param bool   $sendMail whether to send emails on success/failure
284          * @return bool true, if the update function worked
285          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
286          */
287         public static function runUpdateFunction(int $version, string $prefix, bool $sendMail = true): bool
288         {
289                 $funcname = $prefix . '_' . $version;
290
291                 Logger::notice('Update function start.', ['function' => $funcname]);
292
293                 if (function_exists($funcname)) {
294                         // There could be a lot of processes running or about to run.
295                         // We want exactly one process to run the update command.
296                         // So store the fact that we're taking responsibility
297                         // after first checking to see if somebody else already has.
298                         // If the update fails or times-out completely you may need to
299                         // delete the config entry to try again.
300
301                         if (DI::lock()->acquire('dbupdate_function', 120, Cache\Enum\Duration::INFINITE)) {
302
303                                 // call the specific update
304                                 Logger::notice('Pre update function start.', ['function' => $funcname]);
305                                 $retval = $funcname();
306                                 Logger::notice('Update function done.', ['function' => $funcname]);
307
308                                 if ($retval) {
309                                         if ($sendMail) {
310                                                 //send the administrator an e-mail
311                                                 self::updateFailed(
312                                                         $version,
313                                                         DI::l10n()->t('Update %s failed. See error logs.', $version)
314                                                 );
315                                         }
316                                         Logger::error('Update function ERROR.', ['function' => $funcname, 'retval' => $retval]);
317                                         DI::lock()->release('dbupdate_function');
318                                         return false;
319                                 } else {
320                                         DI::lock()->release('dbupdate_function');
321                                         Logger::notice('Update function finished.', ['function' => $funcname]);
322                                         return true;
323                                 }
324                         } else {
325                                 Logger::error('Locking failed.', ['function' => $funcname]);
326                                 return false;
327                         }
328                 } else {
329                         Logger::notice('Update function skipped.', ['function' => $funcname]);
330                         return true;
331                 }
332         }
333
334         /**
335          * send the email and do what is needed to do on update fails
336          *
337          * @param int    $update_id     number of failed update
338          * @param string $error_message error message
339          * @return void
340          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
341          */
342         private static function updateFailed(int $update_id, string $error_message)
343         {
344                 $adminEmails = User::getAdminListForEmailing(['uid', 'language', 'email']);
345                 if (!$adminEmails) {
346                         Logger::warning('Cannot notify administrators .', ['update' => $update_id, 'message' => $error_message]);
347                         return;
348                 }
349
350                 foreach($adminEmails as $admin) {
351                         $l10n = DI::l10n()->withLang($admin['language'] ?: 'en');
352
353                         $preamble = Strings::deindent($l10n->t("
354                                 The friendica developers released update %s recently,
355                                 but when I tried to install it, something went terribly wrong.
356                                 This needs to be fixed soon and I can't do it alone. Please contact a
357                                 friendica developer if you can not help me on your own. My database might be invalid.",
358                                 $update_id));
359                         $body     = $l10n->t('The error message is\n[pre]%s[/pre]', $error_message);
360
361                         $email = DI::emailer()
362                                 ->newSystemMail()
363                                 ->withMessage($l10n->t('[Friendica Notify] Database update'), $preamble, $body)
364                                 ->forUser($admin)
365                                 ->withRecipient($admin['email'])
366                                 ->build();
367                         DI::emailer()->send($email);
368                 }
369
370                 Logger::alert('Database structure update failed.', ['error' => $error_message]);
371         }
372
373         /**
374          * Send a mail to the administrator about the successful update
375          *
376          * @param integer $from_build
377          * @param integer $to_build
378          * @return void
379          */
380         private static function updateSuccessful(int $from_build, int $to_build)
381         {
382                 foreach(User::getAdminListForEmailing(['uid', 'language', 'email']) as $admin) {
383                         $l10n = DI::l10n()->withLang($admin['language'] ?: 'en');
384
385                         $preamble = Strings::deindent($l10n->t('
386                                 The friendica database was successfully updated from %s to %s.',
387                                 $from_build, $to_build));
388
389                         $email = DI::emailer()
390                                 ->newSystemMail()
391                                 ->withMessage($l10n->t('[Friendica Notify] Database update'), $preamble)
392                                 ->forUser($admin)
393                                 ->withRecipient($admin['email'])
394                                 ->build();
395                         DI::emailer()->send($email);
396                 }
397
398                 Logger::debug('Database structure update successful.');
399         }
400 }