admin/upgrade_unattended.php
Rama master · réplica de lectura del repositorio original.
<?php
# MantisBT - A PHP based bugtracking system
# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
/**
* This file handles unattended upgrades of Mantis.
*
* @package MantisBT
* @copyright Copyright 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
* @copyright Copyright 2002 - 2026 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link https://mantisbt.org
*
* @noinspection PhpUnused
*/
set_time_limit( 0 );
# Load the MantisDB core in maintenance mode. This mode will assume that
# config_inc.php hasn't been specified. Thus the database will not be opened
# and plugins will not be loaded.
const MANTIS_MAINTENANCE_MODE = true;
require_once( dirname( __DIR__ ) . '/core.php' );
$g_error_send_page_header = false; # suppress page headers in the error handler
$g_failed = false;
# This script is probably meant to be executed from PHP CLI and hence should
# not be interpreted as text/html. However saying that, we do call gpc_
# functions that only make sense in PHP CGI mode. Given this mismatch we'll
# just assume for now that this script is meant to be used from PHP CGI and
# the output is meant to be text/plain. We also need to prevent Internet
# Explorer from ignoring our MIME type and using it's own MIME sniffing.
header( 'Content-Type: text/plain' );
header( 'X-Content-Type-Options: nosniff' );
/**
* Print the result of an upgrade step.
*
* @param int $p_result GOOD or BAD.
* @param bool $p_hard_fail If result is BAD, sets the global failure flag.
* @param string $p_message The message describing the upgrade step.
*
* @return void
*/
function print_test_result( $p_result, $p_hard_fail = true, $p_message = '' ) {
global $g_failed;
if( BAD == $p_result ) {
if( $p_hard_fail ) {
$g_failed = true;
echo '- ERROR: ';
} else {
echo '- WARNING: ';
}
if( '' != $p_message ) {
echo $p_message;
}
}
if( GOOD == $p_result ) {
echo '- GOOD';
}
echo "\n";
}
# Check database type before attempting to connect
$t_db_type = config_get_global( 'db_type' );
if( db_get_type( $t_db_type ) == DB_TYPE_UNDEFINED ) {
echo "Invalid db type '$t_db_type'.\n";
exit( 1 );
}
# read control variables with defaults
$t_dsn = config_get_global( 'dsn', false );
$t_hostname = config_get_global( 'hostname' );
$t_db_username = config_get_global( 'db_username' );
$t_db_password = config_get_global( 'db_password' );
$t_database_name = config_get_global( 'database_name' );
# Attempt to connect
echo "Connecting to database... ";
$t_save = error_reporting( error_reporting() & ~E_USER_ERROR );
$t_result = @db_connect( $t_dsn, $t_hostname, $t_db_username, $t_db_password, $t_database_name );
if( !$t_result ) {
echo "FAILED\n";
echo "Error opening connection to database "
. "'$t_database_name' on host '$t_hostname' with username '$t_db_username':\n"
. db_error_msg() . "\n";
exit( 1 );
}
error_reporting( $t_save );
echo "OK\n";
# install the tables
require_once( __DIR__ . '/schema.php' );
global $g_db, $g_upgrade;
echo "\nPost 1.0 schema changes\n";
$t_last_update = config_get( 'database_version', -1, ALL_USERS, ALL_PROJECTS );
$t_last_id = count( $g_upgrade ) - 1;
$i = $t_last_update + 1;
$t_count_done = 0;
$t_dict = NewDataDictionary( $g_db );
echo "Current schema version: $t_last_update\n";
echo "Target schema version: $t_last_id\n";
echo "\n";
while( $i <= $t_last_id && !$g_failed ) {
if ( $g_upgrade[$i] === null ) {
$i++;
$t_count_done++;
continue;
}
$t_sql = true;
$t_target = $g_upgrade[$i][1][0];
if( $g_upgrade[$i][0] == 'InsertData' ) {
$t_sqlarray = call_user_func_array( $g_upgrade[$i][0], $g_upgrade[$i][1] );
} else if( $g_upgrade[$i][0] == 'UpdateSQL' ) {
$t_sqlarray = array(
$g_upgrade[$i][1],
);
$t_target = $g_upgrade[$i][1];
} else if( $g_upgrade[$i][0] == 'UpdateFunction' ) {
$t_sqlarray = array(
$g_upgrade[$i][1],
);
if( isset( $g_upgrade[$i][2] ) ) {
$t_sqlarray[] = $g_upgrade[$i][2];
}
$t_sql = false;
$t_target = $g_upgrade[$i][1];
} else {
# 0: function to call, 1: function params, 2: function to evaluate before calling upgrade, if false, skip upgrade.
if( isset( $g_upgrade[$i][2] ) ) {
if( call_user_func_array( $g_upgrade[$i][2][0], $g_upgrade[$i][2][1] ) ) {
$t_sqlarray = call_user_func_array( array( $t_dict, $g_upgrade[$i][0] ), $g_upgrade[$i][1] );
} else {
$t_sqlarray = array();
}
} else {
$t_sqlarray = call_user_func_array( array( $t_dict, $g_upgrade[$i][0] ), $g_upgrade[$i][1] );
}
}
echo 'Schema step ' . $i . ': ' . $g_upgrade[$i][0] . ' ( ' . $t_target . ' ) ';
if( $t_sql ) {
$t_ret = $t_dict->ExecuteSQLArray( $t_sqlarray, false );
} else {
if( isset( $t_sqlarray[1] ) ) {
$t_ret = call_user_func( 'install_' . $t_sqlarray[0], $t_sqlarray[1] );
} else {
$t_ret = call_user_func( 'install_' . $t_sqlarray[0] );
}
}
if( $t_ret == 2 ) {
print_test_result( GOOD );
config_set( 'database_version', $i, ALL_USERS, ALL_PROJECTS );
} else {
$t_msg = $t_sqlarray[0];
if( !is_blank( $g_db->ErrorMsg() ) ) {
$t_msg .= "\n" . $g_db->ErrorMsg();
}
print_test_result( BAD, true, $t_msg );
}
$i++;
$t_count_done++;
}
if( $t_count_done ) {
echo "\n$t_count_done schema upgrades executed.\n";
if( $g_failed ) {
echo "Failed.\n";
exit( 1 );
}
echo "Done.\n";
} else {
echo "Nothing to do.\n";
}