streams/Code/Lib/DB_Upgrade.php

120 lines
4.5 KiB
PHP
Raw Normal View History

2017-03-24 04:49:20 +00:00
<?php
2022-02-16 04:08:28 +00:00
namespace Code\Lib;
2017-03-24 04:49:20 +00:00
2021-12-02 22:33:36 +00:00
use App;
2022-02-16 04:08:28 +00:00
use Code\Render\Theme;
2022-02-12 20:43:29 +00:00
2021-12-02 22:33:36 +00:00
2021-12-02 23:02:31 +00:00
class DB_Upgrade
{
public $config_name = '';
public $func_prefix = '';
public function __construct($db_revision)
{
$this->config_name = 'db_version';
$this->func_prefix = '_';
$build = get_config('system', 'db_version', 0);
2023-08-15 11:01:27 +00:00
if (!(int)$build) {
2021-12-02 23:02:31 +00:00
$build = set_config('system', 'db_version', $db_revision);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
2023-08-15 10:11:46 +00:00
if ((int)$build === (int)$db_revision) {
2021-12-02 23:02:31 +00:00
// Nothing to be done.
return;
} else {
2023-08-15 10:11:46 +00:00
$storedVersion = (int)$build;
if (!$storedVersion) {
2021-12-02 23:02:31 +00:00
logger('Critical: check_config unable to determine database schema version');
return;
}
2023-08-15 10:11:46 +00:00
$newVersion = (int)$db_revision;
2021-12-02 23:02:31 +00:00
2023-08-15 10:11:46 +00:00
if ($storedVersion < $newVersion) {
2021-12-02 23:02:31 +00:00
// The last update we performed was $stored.
// Start at $stored + 1 and continue until we have completed $current
2023-08-15 10:11:46 +00:00
for ($currentUpdate = $storedVersion + 1; $currentUpdate <= $newVersion; $currentUpdate++) {
$configString = '_' . $currentUpdate;
$updateClass = '\\Code\Update\\' . $configString;
if (!class_exists($updateClass)) {
2021-12-02 23:02:31 +00:00
return;
}
// There could be a lot of processes running or about to run.
// We want exactly one process to run the update command.
// So store the fact that we're taking responsibility
// after first checking to see if somebody else already has.
// If the update fails or times-out completely you may need to
// delete the config entry to try again.
Config::Load('database');
2023-08-15 10:11:46 +00:00
if (get_config('database', $configString)) {
2021-12-02 23:02:31 +00:00
break;
2021-12-03 03:01:39 +00:00
}
2023-08-15 10:11:46 +00:00
set_config('database', $configString, '1');
2021-12-02 23:02:31 +00:00
2023-08-15 10:11:46 +00:00
$update = new $updateClass();
2021-12-02 23:02:31 +00:00
2023-08-15 10:11:46 +00:00
$retval = $update->run();
2021-12-02 23:02:31 +00:00
if ($retval != UPDATE_SUCCESS) {
2023-08-15 10:11:46 +00:00
$source = t('Source code of failed update: ') . "\n\n" . @file_get_contents('Code/Update/' . $configString . '.php');
2021-12-02 23:02:31 +00:00
// Prevent sending hundreds of thousands of emails by creating
// a lockfile.
$lockfile = 'cache/mailsent';
2021-12-03 03:01:39 +00:00
if ((file_exists($lockfile)) && (filemtime($lockfile) > (time() - 86400))) {
2021-12-02 23:02:31 +00:00
return;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
@unlink($lockfile);
//send the administrator an e-mail
2023-08-15 10:11:46 +00:00
file_put_contents($lockfile, $currentUpdate);
2021-12-02 23:02:31 +00:00
2021-12-03 03:01:39 +00:00
$r = q(
"select account_language from account where account_email = '%s' limit 1",
2021-12-02 23:02:31 +00:00
dbesc(App::$config['system']['admin_email'])
);
push_lang(($r) ? $r[0]['account_language'] : 'en');
z_mail(
[
'toEmail' => App::$config['system']['admin_email'],
'messageSubject' => sprintf(t('Update Error at %s'), z_root()),
2021-12-03 03:01:39 +00:00
'textVersion' => replace_macros(
2022-02-12 20:43:29 +00:00
Theme::get_email_template('update_fail_eml.tpl'),
2021-12-02 23:02:31 +00:00
[
'$sitename' => App::$config['system']['sitename'],
'$siteurl' => z_root(),
2023-08-15 10:11:46 +00:00
'$update' => $currentUpdate,
'$error' => sprintf(t('Update %s failed. See error logs.'), $currentUpdate),
2021-12-02 23:02:31 +00:00
'$baseurl' => z_root(),
'$source' => $source
]
)
]
);
//try the logger
2023-08-15 10:11:46 +00:00
logger('CRITICAL: Update Failed: ' . $currentUpdate);
2021-12-02 23:02:31 +00:00
pop_lang();
} else {
2023-08-15 10:11:46 +00:00
set_config('database', $configString, 'success');
2021-12-02 23:02:31 +00:00
}
}
}
set_config('system', 'db_version', $db_revision);
}
}
2021-12-03 03:01:39 +00:00
}