3 namespace Friendica\Core;
6 use Friendica\Database\DBA;
7 use Friendica\Database\DBStructure;
9 use Friendica\Util\Strings;
10 use Friendica\Core\Cache\Cache;
18 * @brief Function to check if the Database structure needs an update.
20 * @param string $basePath The base path of this application
21 * @param boolean $via_worker Is the check run via the worker?
22 * @param App\Mode $mode The current app mode
24 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
26 public static function check($basePath, $via_worker, App\Mode $mode)
28 if (!DBA::connected()) {
32 // Don't check the status if the last update was failed
33 if (Config::get('system', 'update', Update::SUCCESS, true) == Update::FAILED) {
37 $build = Config::get('system', 'build');
40 Config::set('system', 'build', DB_UPDATE_VERSION - 1);
41 $build = DB_UPDATE_VERSION - 1;
44 // We don't support upgrading from very old versions anymore
45 if ($build < NEW_UPDATE_ROUTINE_VERSION) {
46 die('You try to update from a version prior to database version 1170. The direct upgrade path is not supported. Please update to version 3.5.4 before updating to this version.');
49 if ($build < DB_UPDATE_VERSION) {
51 // Calling the database update directly via the worker enables us to perform database changes to the workerqueue table itself.
52 // This is a fallback, since normally the database update will be performed by a worker job.
53 // This worker job doesn't work for changes to the "workerqueue" table itself.
56 Worker::add(PRIORITY_CRITICAL, 'DBUpdate');
62 * Automatic database updates
64 * @param string $basePath The base path of this application
65 * @param bool $force Force the Update-Check even if the database version doesn't match
66 * @param bool $override Overrides any running/stuck updates
67 * @param bool $verbose Run the Update-Check verbose
68 * @param bool $sendMail Sends a Mail to the administrator in case of success/failure
70 * @return string Empty string if the update is successful, error messages otherwise
71 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
73 public static function run($basePath, $force = false, $override = false, $verbose = false, $sendMail = true)
75 // In force mode, we release the dbupdate lock first
76 // Necessary in case of an stuck update
78 DI::lock()->release('dbupdate', true);
81 $build = Config::get('system', 'build', null, true);
83 if (empty($build) || ($build > DB_UPDATE_VERSION)) {
84 $build = DB_UPDATE_VERSION - 1;
85 Config::set('system', 'build', $build);
88 if ($build != DB_UPDATE_VERSION || $force) {
89 require_once 'update.php';
91 $stored = intval($build);
92 $current = intval(DB_UPDATE_VERSION);
93 if ($stored < $current || $force) {
94 Config::load('database');
96 Logger::info('Update starting.', ['from' => $stored, 'to' => $current]);
98 // Compare the current structure with the defined structure
99 // If the Lock is acquired, never release it automatically to avoid double updates
100 if (DI::lock()->acquire('dbupdate', 120, Cache::INFINITE)) {
102 // Checks if the build changed during Lock acquiring (so no double update occurs)
103 $retryBuild = Config::get('system', 'build', null, true);
104 if ($retryBuild !== $build) {
105 Logger::info('Update already done.', ['from' => $stored, 'to' => $current]);
106 DI::lock()->release('dbupdate');
110 // run the pre_update_nnnn functions in update.php
111 for ($x = $stored + 1; $x <= $current; $x++) {
112 $r = self::runUpdateFunction($x, 'pre_update');
114 Config::set('system', 'update', Update::FAILED);
115 DI::lock()->release('dbupdate');
120 // update the structure in one call
121 $retval = DBStructure::update($basePath, $verbose, true);
122 if (!empty($retval)) {
129 Logger::error('Update ERROR.', ['from' => $stored, 'to' => $current, 'retval' => $retval]);
130 Config::set('system', 'update', Update::FAILED);
131 DI::lock()->release('dbupdate');
134 Config::set('database', 'last_successful_update', $current);
135 Config::set('database', 'last_successful_update_time', time());
136 Logger::info('Update finished.', ['from' => $stored, 'to' => $current]);
139 // run the update_nnnn functions in update.php
140 for ($x = $stored + 1; $x <= $current; $x++) {
141 $r = self::runUpdateFunction($x, 'update');
143 Config::set('system', 'update', Update::FAILED);
144 DI::lock()->release('dbupdate');
149 Logger::notice('Update success.', ['from' => $stored, 'to' => $current]);
151 self::updateSuccessfull($stored, $current);
154 Config::set('system', 'update', Update::SUCCESS);
155 DI::lock()->release('dbupdate');
164 * Executes a specific update function
166 * @param int $x the DB version number of the function
167 * @param string $prefix the prefix of the function (update, pre_update)
169 * @return bool true, if the update function worked
170 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
172 public static function runUpdateFunction($x, $prefix)
174 $funcname = $prefix . '_' . $x;
176 Logger::info('Update function start.', ['function' => $funcname]);
178 if (function_exists($funcname)) {
179 // There could be a lot of processes running or about to run.
180 // We want exactly one process to run the update command.
181 // So store the fact that we're taking responsibility
182 // after first checking to see if somebody else already has.
183 // If the update fails or times-out completely you may need to
184 // delete the config entry to try again.
186 if (DI::lock()->acquire('dbupdate_function', 120,Cache::INFINITE)) {
188 // call the specific update
189 $retval = $funcname();
192 //send the administrator an e-mail
195 L10n::t('Update %s failed. See error logs.', $x)
197 Logger::error('Update function ERROR.', ['function' => $funcname, 'retval' => $retval]);
198 DI::lock()->release('dbupdate_function');
201 Config::set('database', 'last_successful_update_function', $funcname);
202 Config::set('database', 'last_successful_update_function_time', time());
204 if ($prefix == 'update') {
205 Config::set('system', 'build', $x);
208 DI::lock()->release('dbupdate_function');
209 Logger::info('Update function finished.', ['function' => $funcname]);
214 Logger::info('Update function skipped.', ['function' => $funcname]);
216 Config::set('database', 'last_successful_update_function', $funcname);
217 Config::set('database', 'last_successful_update_function_time', time());
219 if ($prefix == 'update') {
220 Config::set('system', 'build', $x);
228 * send the email and do what is needed to do on update fails
230 * @param int $update_id number of failed update
231 * @param string $error_message error message
232 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
234 private static function updateFailed($update_id, $error_message) {
235 //send the administrators an e-mail
236 $condition = ['email' => explode(",", str_replace(" ", "", Config::get('config', 'admin_email'))), 'parent-uid' => 0];
237 $adminlist = DBA::select('user', ['uid', 'language', 'email'], $condition, ['order' => ['uid']]);
240 if (!DBA::isResult($adminlist)) {
241 Logger::warning('Cannot notify administrators .', ['update' => $update_id, 'message' => $error_message]);
249 // every admin could had different language
250 while ($admin = DBA::fetch($adminlist)) {
251 if (in_array($admin['email'], $sent)) {
254 $sent[] = $admin['email'];
256 $lang = (($admin['language'])?$admin['language']:'en');
257 $l10n = L10n::withLang($lang);
259 $preamble = Strings::deindent($l10n->t("
260 The friendica developers released update %s recently,
261 but when I tried to install it, something went terribly wrong.
262 This needs to be fixed soon and I can't do it alone. Please contact a
263 friendica developer if you can not help me on your own. My database might be invalid.",
265 $body = $l10n->t("The error message is\n[pre]%s[/pre]", $error_message);
268 'uid' => $admin['uid'],
269 'type' => SYSTEM_EMAIL,
270 'to_email' => $admin['email'],
271 'subject' => $l10n->t('[Friendica Notify] Database update'),
272 'preamble' => $preamble,
279 Logger::alert('Database structure update FAILED.', ['error' => $error_message]);
282 private static function updateSuccessfull($from_build, $to_build)
284 //send the administrators an e-mail
285 $condition = ['email' => explode(",", str_replace(" ", "", Config::get('config', 'admin_email'))), 'parent-uid' => 0];
286 $adminlist = DBA::select('user', ['uid', 'language', 'email'], $condition, ['order' => ['uid']]);
288 if (DBA::isResult($adminlist)) {
291 // every admin could had different language
292 while ($admin = DBA::fetch($adminlist)) {
293 if (in_array($admin['email'], $sent)) {
296 $sent[] = $admin['email'];
298 $lang = (($admin['language']) ? $admin['language'] : 'en');
299 $l10n = L10n::withLang($lang);
301 $preamble = Strings::deindent($l10n->t("
302 The friendica database was successfully updated from %s to %s.",
303 $from_build, $to_build));
306 'uid' => $admin['uid'],
307 'type' => SYSTEM_EMAIL,
308 'to_email' => $admin['email'],
309 'subject' => l10n::t('[Friendica Notify] Database update'),
310 'preamble' => $preamble,
318 Logger::debug('Database structure update successful.');