3 namespace Friendica\Core;
5 use Friendica\Database\DBA;
6 use Friendica\Database\DBStructure;
7 use Friendica\Util\Strings;
15 * @brief Function to check if the Database structure needs an update.
17 * @param boolean $via_worker boolean Is the check run via the worker?
19 public static function check($via_worker)
21 $build = Config::get('system', 'build');
24 Config::set('system', 'build', DB_UPDATE_VERSION - 1);
25 $build = DB_UPDATE_VERSION - 1;
28 // We don't support upgrading from very old versions anymore
29 if ($build < NEW_UPDATE_ROUTINE_VERSION) {
30 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.');
33 if ($build < DB_UPDATE_VERSION) {
34 // When we cannot execute the database update via the worker, we will do it directly
35 if (!Worker::add(PRIORITY_CRITICAL, 'DBUpdate') && $via_worker) {
42 * Automatic database updates
44 * @param bool $force Force the Update-Check even if the lock is set
45 * @param bool $verbose Run the Update-Check verbose
46 * @param bool $sendMail Sends a Mail to the administrator in case of success/failure
48 * @return string Empty string if the update is successful, error messages otherwise
50 public static function run($force = false, $verbose = false, $sendMail = true)
52 // In force mode, we release the dbupdate lock first
53 // Necessary in case of an stuck update
55 Lock::release('dbupdate');
58 $build = Config::get('system', 'build');
60 if (empty($build) || ($build > DB_UPDATE_VERSION)) {
61 $build = DB_UPDATE_VERSION - 1;
62 Config::set('system', 'build', $build);
65 if ($build != DB_UPDATE_VERSION) {
66 require_once 'update.php';
68 $stored = intval($build);
69 $current = intval(DB_UPDATE_VERSION);
70 if ($stored < $current) {
71 Config::load('database');
73 Logger::log('Update from \'' . $stored . '\' to \'' . $current . '\' - starting', Logger::DEBUG);
75 // Compare the current structure with the defined structure
76 // If the Lock is acquired, never release it automatically to avoid double updates
77 if (Lock::acquire('dbupdate', 120, Cache::INFINITE)) {
79 // run the pre_update_nnnn functions in update.php
80 for ($x = $stored + 1; $x <= $current; $x++) {
81 $r = self::runUpdateFunction($x, 'pre_update');
87 // update the structure in one call
88 $retval = DBStructure::update($verbose, true);
96 Logger::log('ERROR: Update from \'' . $stored . '\' to \'' . $current . '\' - failed: ' - $retval, Logger::ALL);
97 Lock::release('dbupdate');
100 Config::set('database', 'last_successful_update', $current);
101 Config::set('database', 'last_successful_update_time', time());
102 Logger::log('Update from \'' . $stored . '\' to \'' . $current . '\' - finished', Logger::DEBUG);
105 // run the update_nnnn functions in update.php
106 for ($x = $stored + 1; $x <= $current; $x++) {
107 $r = self::runUpdateFunction($x, 'update');
113 Logger::log('Update from \'' . $stored . '\' to \'' . $current . '\' - successful', Logger::DEBUG);
115 self::updateSuccessfull($stored, $current);
118 Lock::release('dbupdate');
127 * Executes a specific update function
129 * @param int $x the DB version number of the function
130 * @param string $prefix the prefix of the function (update, pre_update)
132 * @return bool true, if the update function worked
134 public static function runUpdateFunction($x, $prefix)
136 $funcname = $prefix . '_' . $x;
138 Logger::log('Update function \'' . $funcname . '\' - start', Logger::DEBUG);
140 if (function_exists($funcname)) {
141 // There could be a lot of processes running or about to run.
142 // We want exactly one process to run the update command.
143 // So store the fact that we're taking responsibility
144 // after first checking to see if somebody else already has.
145 // If the update fails or times-out completely you may need to
146 // delete the config entry to try again.
148 if (Lock::acquire('dbupdate_function', 120,Cache::INFINITE)) {
150 // call the specific update
151 $retval = $funcname();
154 //send the administrator an e-mail
157 L10n::t('Update %s failed. See error logs.', $x)
159 Logger::log('ERROR: Update function \'' . $funcname . '\' - failed: ' . $retval, Logger::ALL);
160 Lock::release('dbupdate_function');
163 Config::set('database', 'last_successful_update_function', $funcname);
164 Config::set('database', 'last_successful_update_function_time', time());
166 if ($prefix == 'update') {
167 Config::set('system', 'build', $x);
170 Lock::release('dbupdate_function');
171 Logger::log('Update function \'' . $funcname . '\' - finished', Logger::DEBUG);
176 Logger::log('Skipping \'' . $funcname . '\' without executing', Logger::DEBUG);
178 Config::set('database', 'last_successful_update_function', $funcname);
179 Config::set('database', 'last_successful_update_function_time', time());
181 if ($prefix == 'update') {
182 Config::set('system', 'build', $x);
190 * send the email and do what is needed to do on update fails
192 * @param int $update_id number of failed update
193 * @param string $error_message error message
195 private static function updateFailed($update_id, $error_message) {
196 //send the administrators an e-mail
197 $admin_mail_list = "'".implode("','", array_map(['Friendica\Database\DBA', 'escape'], explode(",", str_replace(" ", "", Config::get('config', 'admin_email')))))."'";
198 $adminlist = DBA::select('user', ['uid', 'language', 'email'], ['`email` IN (%s)', $admin_mail_list]);
201 if (!DBA::isResult($adminlist)) {
202 Logger::log(sprintf('Cannot notify administrators about update_id=%d, error_message=%s', $update_id, $error_message), Logger::INFO);
208 // every admin could had different language
209 foreach ($adminlist as $admin) {
210 $lang = (($admin['language'])?$admin['language']:'en');
211 L10n::pushLang($lang);
213 $preamble = Strings::deindent(L10n::t("
214 The friendica developers released update %s recently,
215 but when I tried to install it, something went terribly wrong.
216 This needs to be fixed soon and I can't do it alone. Please contact a
217 friendica developer if you can not help me on your own. My database might be invalid.",
219 $body = L10n::t("The error message is\n[pre]%s[/pre]", $error_message);
222 'uid' => $admin['uid'],
223 'type' => SYSTEM_EMAIL,
224 'to_email' => $admin['email'],
225 'preamble' => $preamble,
233 Logger::log("CRITICAL: Database structure update failed: " . $error_message);
236 private static function updateSuccessfull($from_build, $to_build)
238 //send the administrators an e-mail
239 $admin_mail_list = "'".implode("','", array_map(['Friendica\Database\DBA', 'escape'], explode(",", str_replace(" ", "", Config::get('config', 'admin_email')))))."'";
240 $adminlist = DBA::select('user', ['uid', 'language', 'email'], ['`email` IN (%s)', $admin_mail_list]);
242 if (DBA::isResult($adminlist)) {
243 // every admin could had different language
244 foreach ($adminlist as $admin) {
245 $lang = (($admin['language']) ? $admin['language'] : 'en');
246 L10n::pushLang($lang);
248 $preamble = Strings::deindent(L10n::t("
249 The friendica database was successfully updated from %s to %s.",
250 $from_build, $to_build));
253 'uid' => $admin['uid'],
254 'type' => SYSTEM_EMAIL,
255 'to_email' => $admin['email'],
256 'preamble' => $preamble,
265 Logger::log("Database structure update successful.", Logger::TRACE);