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 string $basePath The base path of this application
18 * @param boolean $via_worker boolean Is the check run via the worker?
19 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
21 public static function check($basePath, $via_worker)
23 if (!DBA::connected()) {
27 $build = Config::get('system', 'build');
30 Config::set('system', 'build', DB_UPDATE_VERSION - 1);
31 $build = DB_UPDATE_VERSION - 1;
34 // We don't support upgrading from very old versions anymore
35 if ($build < NEW_UPDATE_ROUTINE_VERSION) {
36 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.');
39 if ($build < DB_UPDATE_VERSION) {
40 // When we cannot execute the database update via the worker, we will do it directly
41 if (!Worker::add(PRIORITY_CRITICAL, 'DBUpdate') && $via_worker) {
48 * Automatic database updates
50 * @param string $basePath The base path of this application
51 * @param bool $force Force the Update-Check even if the lock is set
52 * @param bool $verbose Run the Update-Check verbose
53 * @param bool $sendMail Sends a Mail to the administrator in case of success/failure
55 * @return string Empty string if the update is successful, error messages otherwise
56 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
58 public static function run($basePath, $force = false, $verbose = false, $sendMail = true)
60 // In force mode, we release the dbupdate lock first
61 // Necessary in case of an stuck update
63 Lock::release('dbupdate');
66 $build = Config::get('system', 'build');
68 if (empty($build) || ($build > DB_UPDATE_VERSION)) {
69 $build = DB_UPDATE_VERSION - 1;
70 Config::set('system', 'build', $build);
73 if ($build != DB_UPDATE_VERSION) {
74 require_once 'update.php';
76 $stored = intval($build);
77 $current = intval(DB_UPDATE_VERSION);
78 if ($stored < $current) {
79 Config::load('database');
81 Logger::log('Update from \'' . $stored . '\' to \'' . $current . '\' - starting', Logger::DEBUG);
83 // Compare the current structure with the defined structure
84 // If the Lock is acquired, never release it automatically to avoid double updates
85 if (Lock::acquire('dbupdate', 120, Cache::INFINITE)) {
87 // run the pre_update_nnnn functions in update.php
88 for ($x = $stored + 1; $x <= $current; $x++) {
89 $r = self::runUpdateFunction($x, 'pre_update');
95 // update the structure in one call
96 $retval = DBStructure::update($basePath, $verbose, true);
104 Logger::log('ERROR: Update from \'' . $stored . '\' to \'' . $current . '\' - failed: ' - $retval, Logger::ALL);
105 Lock::release('dbupdate');
108 Config::set('database', 'last_successful_update', $current);
109 Config::set('database', 'last_successful_update_time', time());
110 Logger::log('Update from \'' . $stored . '\' to \'' . $current . '\' - finished', Logger::DEBUG);
113 // run the update_nnnn functions in update.php
114 for ($x = $stored + 1; $x <= $current; $x++) {
115 $r = self::runUpdateFunction($x, 'update');
121 Logger::log('Update from \'' . $stored . '\' to \'' . $current . '\' - successful', Logger::DEBUG);
123 self::updateSuccessfull($stored, $current);
126 Lock::release('dbupdate');
130 DBStructure::update($basePath, $verbose, true);
137 * Executes a specific update function
139 * @param int $x the DB version number of the function
140 * @param string $prefix the prefix of the function (update, pre_update)
142 * @return bool true, if the update function worked
143 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
145 public static function runUpdateFunction($x, $prefix)
147 $funcname = $prefix . '_' . $x;
149 Logger::log('Update function \'' . $funcname . '\' - start', Logger::DEBUG);
151 if (function_exists($funcname)) {
152 // There could be a lot of processes running or about to run.
153 // We want exactly one process to run the update command.
154 // So store the fact that we're taking responsibility
155 // after first checking to see if somebody else already has.
156 // If the update fails or times-out completely you may need to
157 // delete the config entry to try again.
159 if (Lock::acquire('dbupdate_function', 120,Cache::INFINITE)) {
161 // call the specific update
162 $retval = $funcname();
165 //send the administrator an e-mail
168 L10n::t('Update %s failed. See error logs.', $x)
170 Logger::log('ERROR: Update function \'' . $funcname . '\' - failed: ' . $retval, Logger::ALL);
171 Lock::release('dbupdate_function');
174 Config::set('database', 'last_successful_update_function', $funcname);
175 Config::set('database', 'last_successful_update_function_time', time());
177 if ($prefix == 'update') {
178 Config::set('system', 'build', $x);
181 Lock::release('dbupdate_function');
182 Logger::log('Update function \'' . $funcname . '\' - finished', Logger::DEBUG);
187 Logger::log('Skipping \'' . $funcname . '\' without executing', Logger::DEBUG);
189 Config::set('database', 'last_successful_update_function', $funcname);
190 Config::set('database', 'last_successful_update_function_time', time());
192 if ($prefix == 'update') {
193 Config::set('system', 'build', $x);
201 * send the email and do what is needed to do on update fails
203 * @param int $update_id number of failed update
204 * @param string $error_message error message
205 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
207 private static function updateFailed($update_id, $error_message) {
208 //send the administrators an e-mail
209 $admin_mail_list = "'".implode("','", array_map(['Friendica\Database\DBA', 'escape'], explode(",", str_replace(" ", "", Config::get('config', 'admin_email')))))."'";
210 $adminlist = DBA::select('user', ['uid', 'language', 'email'], ['`email` IN (%s)', $admin_mail_list]);
213 if (!DBA::isResult($adminlist)) {
214 Logger::log(sprintf('Cannot notify administrators about update_id=%d, error_message=%s', $update_id, $error_message), Logger::INFO);
220 // every admin could had different language
221 foreach ($adminlist as $admin) {
222 $lang = (($admin['language'])?$admin['language']:'en');
223 L10n::pushLang($lang);
225 $preamble = Strings::deindent(L10n::t("
226 The friendica developers released update %s recently,
227 but when I tried to install it, something went terribly wrong.
228 This needs to be fixed soon and I can't do it alone. Please contact a
229 friendica developer if you can not help me on your own. My database might be invalid.",
231 $body = L10n::t("The error message is\n[pre]%s[/pre]", $error_message);
234 'uid' => $admin['uid'],
235 'type' => SYSTEM_EMAIL,
236 'to_email' => $admin['email'],
237 'preamble' => $preamble,
245 Logger::log("CRITICAL: Database structure update failed: " . $error_message);
248 private static function updateSuccessfull($from_build, $to_build)
250 //send the administrators an e-mail
251 $admin_mail_list = "'".implode("','", array_map(['Friendica\Database\DBA', 'escape'], explode(",", str_replace(" ", "", Config::get('config', 'admin_email')))))."'";
252 $adminlist = DBA::select('user', ['uid', 'language', 'email'], ['`email` IN (%s)', $admin_mail_list]);
254 if (DBA::isResult($adminlist)) {
255 // every admin could had different language
256 foreach ($adminlist as $admin) {
257 $lang = (($admin['language']) ? $admin['language'] : 'en');
258 L10n::pushLang($lang);
260 $preamble = Strings::deindent(L10n::t("
261 The friendica database was successfully updated from %s to %s.",
262 $from_build, $to_build));
265 'uid' => $admin['uid'],
266 'type' => SYSTEM_EMAIL,
267 'to_email' => $admin['email'],
268 'preamble' => $preamble,
277 Logger::log("Database structure update successful.", Logger::TRACE);