3 namespace Friendica\Core;
5 use Friendica\Database\DBA;
6 use Friendica\Database\DBStructure;
14 * @brief Function to check if the Database structure needs an update.
16 * @param boolean $via_worker boolean Is the check run via the worker?
18 public static function check($via_worker)
20 $build = Config::get('system', 'build');
23 Config::set('system', 'build', DB_UPDATE_VERSION - 1);
24 $build = DB_UPDATE_VERSION - 1;
27 // We don't support upgrading from very old versions anymore
28 if ($build < NEW_UPDATE_ROUTINE_VERSION) {
29 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.');
32 if ($build < DB_UPDATE_VERSION) {
33 // When we cannot execute the database update via the worker, we will do it directly
34 if (!Worker::add(PRIORITY_CRITICAL, 'DBUpdate') && $via_worker) {
41 * Automatic database updates
43 * @param bool $force Force the Update-Check even if the lock is set
44 * @param bool $verbose Run the Update-Check verbose
45 * @param bool $sendMail Sends a Mail to the administrator in case of success/failure
47 * @return string Empty string if the update is successful, error messages otherwise
49 public static function run($force = false, $verbose = false, $sendMail = true)
51 // In force mode, we release the dbupdate lock first
52 // Necessary in case of an stuck update
54 Lock::release('dbupdate');
57 $build = Config::get('system', 'build');
59 if (empty($build) || ($build > DB_UPDATE_VERSION)) {
60 $build = DB_UPDATE_VERSION - 1;
61 Config::set('system', 'build', $build);
64 if ($build != DB_UPDATE_VERSION) {
65 require_once 'update.php';
67 $stored = intval($build);
68 $current = intval(DB_UPDATE_VERSION);
69 if ($stored < $current) {
70 Config::load('database');
72 // Compare the current structure with the defined structure
73 // If the Lock is acquired, never release it automatically to avoid double updates
74 if (Lock::acquire('dbupdate', 120, Cache::INFINITE)) {
76 // run the pre_update_nnnn functions in update.php
77 for ($x = $stored + 1; $x <= $current; $x++) {
78 $r = self::runUpdateFunction($x, 'pre_update');
84 // update the structure in one call
85 $retval = DBStructure::update($verbose, true);
93 Lock::release('dbupdate');
96 Config::set('database', 'last_successful_update', $current);
97 Config::set('database', 'last_successful_update_time', time());
100 // run the update_nnnn functions in update.php
101 for ($x = $stored + 1; $x <= $current; $x++) {
102 $r = self::runUpdateFunction($x, 'update');
109 self::updateSuccessfull($stored, $current);
112 Lock::release('dbupdate');
121 * Executes a specific update function
123 * @param int $x the DB version number of the function
124 * @param string $prefix the prefix of the function (update, pre_update)
126 * @return bool true, if the update function worked
128 public static function runUpdateFunction($x, $prefix)
130 $funcname = $prefix . '_' . $x;
132 if (function_exists($funcname)) {
133 // There could be a lot of processes running or about to run.
134 // We want exactly one process to run the update command.
135 // So store the fact that we're taking responsibility
136 // after first checking to see if somebody else already has.
137 // If the update fails or times-out completely you may need to
138 // delete the config entry to try again.
140 if (Lock::acquire('dbupdate_function', 120,Cache::INFINITE)) {
142 // call the specific update
143 $retval = $funcname();
146 //send the administrator an e-mail
149 L10n::t('Update %s failed. See error logs.', $x)
151 Lock::release('dbupdate_function');
154 Config::set('database', 'last_successful_update_function', $funcname);
155 Config::set('database', 'last_successful_update_function_time', time());
157 if ($prefix == 'update') {
158 Config::set('system', 'build', $x);
161 Lock::release('dbupdate_function');
166 Logger::log('Skipping \'' . $funcname . '\' without executing', Logger::DEBUG);
168 Config::set('database', 'last_successful_update_function', $funcname);
169 Config::set('database', 'last_successful_update_function_time', time());
171 if ($prefix == 'update') {
172 Config::set('system', 'build', $x);
180 * send the email and do what is needed to do on update fails
182 * @param int $update_id number of failed update
183 * @param string $error_message error message
185 private static function updateFailed($update_id, $error_message) {
186 //send the administrators an e-mail
187 $admin_mail_list = "'".implode("','", array_map(['Friendica\Database\DBA', 'escape'], explode(",", str_replace(" ", "", Config::get('config', 'admin_email')))))."'";
188 $adminlist = DBA::select('user', ['uid', 'language', 'email'], ['`email` IN (%s)', $admin_mail_list]);
191 if (!DBA::isResult($adminlist)) {
192 Logger::log(sprintf('Cannot notify administrators about update_id=%d, error_message=%s', $update_id, $error_message), Logger::INFO);
198 // every admin could had different language
199 foreach ($adminlist as $admin) {
200 $lang = (($admin['language'])?$admin['language']:'en');
201 L10n::pushLang($lang);
203 $preamble = deindent(L10n::t("
204 The friendica developers released update %s recently,
205 but when I tried to install it, something went terribly wrong.
206 This needs to be fixed soon and I can't do it alone. Please contact a
207 friendica developer if you can not help me on your own. My database might be invalid.",
209 $body = L10n::t("The error message is\n[pre]%s[/pre]", $error_message);
212 'uid' => $admin['uid'],
213 'type' => SYSTEM_EMAIL,
214 'to_email' => $admin['email'],
215 'preamble' => $preamble,
223 Logger::log("CRITICAL: Database structure update failed: " . $error_message);
226 private static function updateSuccessfull($from_build, $to_build)
228 //send the administrators an e-mail
229 $admin_mail_list = "'".implode("','", array_map(['Friendica\Database\DBA', 'escape'], explode(",", str_replace(" ", "", Config::get('config', 'admin_email')))))."'";
230 $adminlist = DBA::select('user', ['uid', 'language', 'email'], ['`email` IN (%s)', $admin_mail_list]);
232 if (DBA::isResult($adminlist)) {
233 // every admin could had different language
234 foreach ($adminlist as $admin) {
235 $lang = (($admin['language']) ? $admin['language'] : 'en');
236 L10n::pushLang($lang);
238 $preamble = deindent(L10n::t("
239 The friendica database was successfully updated from %s to %s.",
240 $from_build, $to_build));
243 'uid' => $admin['uid'],
244 'type' => SYSTEM_EMAIL,
245 'to_email' => $admin['email'],
246 'preamble' => $preamble,
255 Logger::log("Database structure update successful.", Logger::TRACE);