3 namespace Friendica\Core;
6 use Friendica\Database\DBA;
7 use Friendica\Database\DBStructure;
8 use Friendica\Util\Strings;
9 use Friendica\Core\Cache\Cache;
17 * @brief Function to check if the Database structure needs an update.
19 * @param string $basePath The base path of this application
20 * @param boolean $via_worker Is the check run via the worker?
21 * @param App\Mode $mode The current app mode
23 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
25 public static function check($basePath, $via_worker, App\Mode $mode)
27 if (!DBA::connected()) {
31 // Don't check the status if the last update was failed
32 if (Config::get('system', 'update', Update::SUCCESS, true) == Update::FAILED) {
36 $build = Config::get('system', 'build');
39 Config::set('system', 'build', DB_UPDATE_VERSION - 1);
40 $build = DB_UPDATE_VERSION - 1;
43 // We don't support upgrading from very old versions anymore
44 if ($build < NEW_UPDATE_ROUTINE_VERSION) {
45 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.');
48 if ($build < DB_UPDATE_VERSION) {
50 // Calling the database update directly via the worker enables us to perform database changes to the workerqueue table itself.
51 // This is a fallback, since normally the database update will be performed by a worker job.
52 // This worker job doesn't work for changes to the "workerqueue" table itself.
55 Worker::add(PRIORITY_CRITICAL, 'DBUpdate');
61 * Automatic database updates
63 * @param string $basePath The base path of this application
64 * @param bool $force Force the Update-Check even if the database version doesn't match
65 * @param bool $override Overrides any running/stuck updates
66 * @param bool $verbose Run the Update-Check verbose
67 * @param bool $sendMail Sends a Mail to the administrator in case of success/failure
69 * @return string Empty string if the update is successful, error messages otherwise
70 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
72 public static function run($basePath, $force = false, $override = false, $verbose = false, $sendMail = true)
74 // In force mode, we release the dbupdate lock first
75 // Necessary in case of an stuck update
77 Lock::release('dbupdate', true);
80 $build = Config::get('system', 'build', null, true);
82 if (empty($build) || ($build > DB_UPDATE_VERSION)) {
83 $build = DB_UPDATE_VERSION - 1;
84 Config::set('system', 'build', $build);
87 if ($build != DB_UPDATE_VERSION || $force) {
88 require_once 'update.php';
90 $stored = intval($build);
91 $current = intval(DB_UPDATE_VERSION);
92 if ($stored < $current || $force) {
93 Config::load('database');
95 Logger::info('Update starting.', ['from' => $stored, 'to' => $current]);
97 // Compare the current structure with the defined structure
98 // If the Lock is acquired, never release it automatically to avoid double updates
99 if (Lock::acquire('dbupdate', 120, Cache::INFINITE)) {
101 // Checks if the build changed during Lock acquiring (so no double update occurs)
102 $retryBuild = Config::get('system', 'build', null, true);
103 if ($retryBuild !== $build) {
104 Logger::info('Update already done.', ['from' => $stored, 'to' => $current]);
105 Lock::release('dbupdate');
109 // run the pre_update_nnnn functions in update.php
110 for ($x = $stored + 1; $x <= $current; $x++) {
111 $r = self::runUpdateFunction($x, 'pre_update');
113 Config::set('system', 'update', Update::FAILED);
114 Lock::release('dbupdate');
119 // update the structure in one call
120 $retval = DBStructure::update($basePath, $verbose, true);
121 if (!empty($retval)) {
128 Logger::error('Update ERROR.', ['from' => $stored, 'to' => $current, 'retval' => $retval]);
129 Config::set('system', 'update', Update::FAILED);
130 Lock::release('dbupdate');
133 Config::set('database', 'last_successful_update', $current);
134 Config::set('database', 'last_successful_update_time', time());
135 Logger::info('Update finished.', ['from' => $stored, 'to' => $current]);
138 // run the update_nnnn functions in update.php
139 for ($x = $stored + 1; $x <= $current; $x++) {
140 $r = self::runUpdateFunction($x, 'update');
142 Config::set('system', 'update', Update::FAILED);
143 Lock::release('dbupdate');
148 Logger::notice('Update success.', ['from' => $stored, 'to' => $current]);
150 self::updateSuccessfull($stored, $current);
153 Config::set('system', 'update', Update::SUCCESS);
154 Lock::release('dbupdate');
163 * Executes a specific update function
165 * @param int $x the DB version number of the function
166 * @param string $prefix the prefix of the function (update, pre_update)
168 * @return bool true, if the update function worked
169 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
171 public static function runUpdateFunction($x, $prefix)
173 $funcname = $prefix . '_' . $x;
175 Logger::info('Update function start.', ['function' => $funcname]);
177 if (function_exists($funcname)) {
178 // There could be a lot of processes running or about to run.
179 // We want exactly one process to run the update command.
180 // So store the fact that we're taking responsibility
181 // after first checking to see if somebody else already has.
182 // If the update fails or times-out completely you may need to
183 // delete the config entry to try again.
185 if (Lock::acquire('dbupdate_function', 120,Cache::INFINITE)) {
187 // call the specific update
188 $retval = $funcname();
191 //send the administrator an e-mail
194 L10n::t('Update %s failed. See error logs.', $x)
196 Logger::error('Update function ERROR.', ['function' => $funcname, 'retval' => $retval]);
197 Lock::release('dbupdate_function');
200 Config::set('database', 'last_successful_update_function', $funcname);
201 Config::set('database', 'last_successful_update_function_time', time());
203 if ($prefix == 'update') {
204 Config::set('system', 'build', $x);
207 Lock::release('dbupdate_function');
208 Logger::info('Update function finished.', ['function' => $funcname]);
213 Logger::info('Update function skipped.', ['function' => $funcname]);
215 Config::set('database', 'last_successful_update_function', $funcname);
216 Config::set('database', 'last_successful_update_function_time', time());
218 if ($prefix == 'update') {
219 Config::set('system', 'build', $x);
227 * send the email and do what is needed to do on update fails
229 * @param int $update_id number of failed update
230 * @param string $error_message error message
231 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
233 private static function updateFailed($update_id, $error_message) {
234 //send the administrators an e-mail
235 $condition = ['email' => explode(",", str_replace(" ", "", Config::get('config', 'admin_email'))), 'parent-uid' => 0];
236 $adminlist = DBA::select('user', ['uid', 'language', 'email'], $condition, ['order' => ['uid']]);
239 if (!DBA::isResult($adminlist)) {
240 Logger::warning('Cannot notify administrators .', ['update' => $update_id, 'message' => $error_message]);
248 // every admin could had different language
249 while ($admin = DBA::fetch($adminlist)) {
250 if (in_array($admin['email'], $sent)) {
253 $sent[] = $admin['email'];
255 $lang = (($admin['language'])?$admin['language']:'en');
256 $l10n = L10n::withLang($lang);
258 $preamble = Strings::deindent($l10n->t("
259 The friendica developers released update %s recently,
260 but when I tried to install it, something went terribly wrong.
261 This needs to be fixed soon and I can't do it alone. Please contact a
262 friendica developer if you can not help me on your own. My database might be invalid.",
264 $body = $l10n->t("The error message is\n[pre]%s[/pre]", $error_message);
267 'uid' => $admin['uid'],
268 'type' => SYSTEM_EMAIL,
269 'to_email' => $admin['email'],
270 'subject' => $l10n->t('[Friendica Notify] Database update'),
271 'preamble' => $preamble,
278 Logger::alert('Database structure update FAILED.', ['error' => $error_message]);
281 private static function updateSuccessfull($from_build, $to_build)
283 //send the administrators an e-mail
284 $condition = ['email' => explode(",", str_replace(" ", "", Config::get('config', 'admin_email'))), 'parent-uid' => 0];
285 $adminlist = DBA::select('user', ['uid', 'language', 'email'], $condition, ['order' => ['uid']]);
287 if (DBA::isResult($adminlist)) {
290 // every admin could had different language
291 while ($admin = DBA::fetch($adminlist)) {
292 if (in_array($admin['email'], $sent)) {
295 $sent[] = $admin['email'];
297 $lang = (($admin['language']) ? $admin['language'] : 'en');
298 $l10n = L10n::withLang($lang);
300 $preamble = Strings::deindent($l10n->t("
301 The friendica database was successfully updated from %s to %s.",
302 $from_build, $to_build));
305 'uid' => $admin['uid'],
306 'type' => SYSTEM_EMAIL,
307 'to_email' => $admin['email'],
308 'subject' => l10n::t('[Friendica Notify] Database update'),
309 'preamble' => $preamble,
317 Logger::debug('Database structure update successful.');