3 namespace Friendica\Core;
6 use Friendica\Database\DBA;
7 use Friendica\Database\DBStructure;
8 use Friendica\Util\Strings;
16 * @brief Function to check if the Database structure needs an update.
18 * @param string $basePath The base path of this application
19 * @param boolean $via_worker Is the check run via the worker?
20 * @param App\Mode $mode The current app mode
22 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
24 public static function check($basePath, $via_worker, App\Mode $mode)
26 if (!DBA::connected()) {
30 // Check if the config files are set correctly
31 self::checkBaseSettings($_SERVER);
33 // Don't check the status if the last update was failed
34 if (Config::get('system', 'update', Update::SUCCESS, true) == Update::FAILED) {
38 $build = Config::get('system', 'build');
41 Config::set('system', 'build', DB_UPDATE_VERSION - 1);
42 $build = DB_UPDATE_VERSION - 1;
45 // We don't support upgrading from very old versions anymore
46 if ($build < NEW_UPDATE_ROUTINE_VERSION) {
47 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.');
50 if ($build < DB_UPDATE_VERSION) {
52 // Calling the database update directly via the worker enables us to perform database changes to the workerqueue table itself.
53 // This is a fallback, since normally the database update will be performed by a worker job.
54 // This worker job doesn't work for changes to the "workerqueue" table itself.
57 Worker::add(PRIORITY_CRITICAL, 'DBUpdate');
63 * Automatic database updates
65 * @param string $basePath The base path of this application
66 * @param bool $force Force the Update-Check even if the database version doesn't match
67 * @param bool $override Overrides any running/stuck updates
68 * @param bool $verbose Run the Update-Check verbose
69 * @param bool $sendMail Sends a Mail to the administrator in case of success/failure
71 * @return string Empty string if the update is successful, error messages otherwise
72 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
74 public static function run($basePath, $force = false, $override = false, $verbose = false, $sendMail = true)
76 // In force mode, we release the dbupdate lock first
77 // Necessary in case of an stuck update
79 Lock::release('dbupdate', true);
82 $build = Config::get('system', 'build', null, true);
84 if (empty($build) || ($build > DB_UPDATE_VERSION)) {
85 $build = DB_UPDATE_VERSION - 1;
86 Config::set('system', 'build', $build);
89 if ($build != DB_UPDATE_VERSION || $force) {
90 require_once 'update.php';
92 $stored = intval($build);
93 $current = intval(DB_UPDATE_VERSION);
94 if ($stored < $current || $force) {
95 Config::load('database');
97 Logger::info('Update starting.', ['from' => $stored, 'to' => $current]);
99 // Compare the current structure with the defined structure
100 // If the Lock is acquired, never release it automatically to avoid double updates
101 if (Lock::acquire('dbupdate', 120, Cache::INFINITE)) {
103 // Checks if the build changed during Lock acquiring (so no double update occurs)
104 $retryBuild = Config::get('system', 'build', null, true);
105 if ($retryBuild !== $build) {
106 Logger::info('Update already done.', ['from' => $stored, 'to' => $current]);
107 Lock::release('dbupdate');
111 // run the pre_update_nnnn functions in update.php
112 for ($x = $stored + 1; $x <= $current; $x++) {
113 $r = self::runUpdateFunction($x, 'pre_update');
115 Config::set('system', 'update', Update::FAILED);
116 Lock::release('dbupdate');
121 // update the structure in one call
122 $retval = DBStructure::update($basePath, $verbose, true);
123 if (!empty($retval)) {
130 Logger::error('Update ERROR.', ['from' => $stored, 'to' => $current, 'retval' => $retval]);
131 Config::set('system', 'update', Update::FAILED);
132 Lock::release('dbupdate');
135 Config::set('database', 'last_successful_update', $current);
136 Config::set('database', 'last_successful_update_time', time());
137 Logger::info('Update finished.', ['from' => $stored, 'to' => $current]);
140 // run the update_nnnn functions in update.php
141 for ($x = $stored + 1; $x <= $current; $x++) {
142 $r = self::runUpdateFunction($x, 'update');
144 Config::set('system', 'update', Update::FAILED);
145 Lock::release('dbupdate');
150 Logger::notice('Update success.', ['from' => $stored, 'to' => $current]);
152 self::updateSuccessfull($stored, $current);
155 Config::set('system', 'update', Update::SUCCESS);
156 Lock::release('dbupdate');
165 * Executes a specific update function
167 * @param int $x the DB version number of the function
168 * @param string $prefix the prefix of the function (update, pre_update)
170 * @return bool true, if the update function worked
171 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
173 public static function runUpdateFunction($x, $prefix)
175 $funcname = $prefix . '_' . $x;
177 Logger::info('Update function start.', ['function' => $funcname]);
179 if (function_exists($funcname)) {
180 // There could be a lot of processes running or about to run.
181 // We want exactly one process to run the update command.
182 // So store the fact that we're taking responsibility
183 // after first checking to see if somebody else already has.
184 // If the update fails or times-out completely you may need to
185 // delete the config entry to try again.
187 if (Lock::acquire('dbupdate_function', 120,Cache::INFINITE)) {
189 // call the specific update
190 $retval = $funcname();
193 //send the administrator an e-mail
196 L10n::t('Update %s failed. See error logs.', $x)
198 Logger::error('Update function ERROR.', ['function' => $funcname, 'retval' => $retval]);
199 Lock::release('dbupdate_function');
202 Config::set('database', 'last_successful_update_function', $funcname);
203 Config::set('database', 'last_successful_update_function_time', time());
205 if ($prefix == 'update') {
206 Config::set('system', 'build', $x);
209 Lock::release('dbupdate_function');
210 Logger::info('Update function finished.', ['function' => $funcname]);
215 Logger::info('Update function skipped.', ['function' => $funcname]);
217 Config::set('database', 'last_successful_update_function', $funcname);
218 Config::set('database', 'last_successful_update_function_time', time());
220 if ($prefix == 'update') {
221 Config::set('system', 'build', $x);
228 public static function checkBaseSettings(array $server)
234 * send the email and do what is needed to do on update fails
236 * @param int $update_id number of failed update
237 * @param string $error_message error message
238 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
240 private static function updateFailed($update_id, $error_message) {
241 //send the administrators an e-mail
242 $condition = ['email' => explode(",", str_replace(" ", "", Config::get('config', 'admin_email'))), 'parent-uid' => 0];
243 $adminlist = DBA::select('user', ['uid', 'language', 'email'], $condition, ['order' => ['uid']]);
246 if (!DBA::isResult($adminlist)) {
247 Logger::warning('Cannot notify administrators .', ['update' => $update_id, 'message' => $error_message]);
255 // every admin could had different language
256 while ($admin = DBA::fetch($adminlist)) {
257 if (in_array($admin['email'], $sent)) {
260 $sent[] = $admin['email'];
262 $lang = (($admin['language'])?$admin['language']:'en');
263 L10n::pushLang($lang);
265 $preamble = Strings::deindent(L10n::t("
266 The friendica developers released update %s recently,
267 but when I tried to install it, something went terribly wrong.
268 This needs to be fixed soon and I can't do it alone. Please contact a
269 friendica developer if you can not help me on your own. My database might be invalid.",
271 $body = L10n::t("The error message is\n[pre]%s[/pre]", $error_message);
274 'uid' => $admin['uid'],
275 'type' => SYSTEM_EMAIL,
276 'to_email' => $admin['email'],
277 'subject' => l10n::t('[Friendica Notify] Database update'),
278 'preamble' => $preamble,
286 Logger::alert('Database structure update FAILED.', ['error' => $error_message]);
289 private static function updateSuccessfull($from_build, $to_build)
291 //send the administrators an e-mail
292 $condition = ['email' => explode(",", str_replace(" ", "", Config::get('config', 'admin_email'))), 'parent-uid' => 0];
293 $adminlist = DBA::select('user', ['uid', 'language', 'email'], $condition, ['order' => ['uid']]);
295 if (DBA::isResult($adminlist)) {
298 // every admin could had different language
299 while ($admin = DBA::fetch($adminlist)) {
300 if (in_array($admin['email'], $sent)) {
303 $sent[] = $admin['email'];
305 $lang = (($admin['language']) ? $admin['language'] : 'en');
306 L10n::pushLang($lang);
308 $preamble = Strings::deindent(L10n::t("
309 The friendica database was successfully updated from %s to %s.",
310 $from_build, $to_build));
313 'uid' => $admin['uid'],
314 'type' => SYSTEM_EMAIL,
315 'to_email' => $admin['email'],
316 'preamble' => $preamble,
325 Logger::debug('Database structure update successful.');