Automatic upgrade, first cut. see #5560

git-svn-id: http://svn.automattic.com/wordpress/trunk@8595 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2008-08-08 22:49:35 +00:00
parent eb6e891041
commit 05305465e4
6 changed files with 208 additions and 14 deletions

View File

@ -408,8 +408,10 @@ function copy_dir($from, $to) {
return new WP_Error('copy_failed', __('Could not copy file'), $to . $filename);
$wp_filesystem->chmod($to . $filename, 0644);
} elseif ( 'd' == $fileinfo['type'] ) {
if ( !$wp_filesystem->mkdir($to . $filename, 0755) )
return new WP_Error('mkdir_failed', __('Could not create directory'), $to . $filename);
if ( !$wp_filesystem->is_dir($to . $filename) ) {
if ( !$wp_filesystem->mkdir($to . $filename, 0755) )
return new WP_Error('mkdir_failed', __('Could not create directory'), $to . $filename);
}
$result = copy_dir($from . $filename, $to . $filename);
if ( is_wp_error($result) )
return $result;

View File

@ -0,0 +1,50 @@
<?php
function update_core($from, $to) {
global $wp_filesystem;
// Sanity check the unzipped distribution
apply_filters('update_feedback', __('Verifying the unpacked files'));
if ( !file_exists($from . '/wordpress/wp-settings.php') || !file_exists($from . '/wordpress/wp-admin/admin.php') ||
!file_exists($from . '/wordpress/wp-includes/functions.php') ) {
$wp_filesystem->delete($from, true);
return new WP_Error('insane_distro', __('The update could not be unpacked') );
}
apply_filters('update_feedback', __('Installing the latest version'));
// Create maintenance file to signal that we are upgrading
$maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
$maintenance_file = $to . '.maintenance';
$wp_filesystem->delete($maintenance_file);
$wp_filesystem->put_contents($maintenance_file, $maintenance_string, 0644);
// Copy new versions of WP files into place.
$result = copy_dir($from . '/wordpress', $to);
if ( is_wp_error($result) ) {
$wp_filesystem->delete($maintenance_file);
//$wp_filesystem->delete($working_dir, true); //TODO: Uncomment? This DOES mean that the new files are available in the upgrade folder if it fails.
return $result;
}
// Might have to do upgrade in a separate step.
apply_filters('update_feedback', __('Upgrading database'));
// Get new db version
global $wp_db_version;
require (ABSPATH . WPINC . '/version.php');
// Upgrade db
define('WP_INSTALLING', true);
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
wp_upgrade();
// Remove working directory
$wp_filesystem->delete($from, true);
// Remove maintenance file, we're done.
$wp_filesystem->delete($maintenance_file);
// Force refresh of update information
delete_option('update_core');
}
?>

View File

@ -15,18 +15,18 @@ function core_update_footer( $msg = '' ) {
switch ( $cur->response ) {
case 'development' :
return sprintf( '| '.__( 'You are using a development version (%s). Cool! Please <a href="%s">stay updated</a>.' ), $GLOBALS['wp_version'], $cur->url, $cur->current );
return sprintf( '| '.__( 'You are using a development version (%1$s). Cool! Please <a href="%2$s">stay updated</a>.' ), $GLOBALS['wp_version'], wp_nonce_url('update.php?action=upgrade-core', 'upgrade-core'));
break;
case 'upgrade' :
if ( current_user_can('manage_options') ) {
return sprintf( '| <strong>'.__( '<a href="%2$s">Get Version %3$s</a>' ).'</strong>', $GLOBALS['wp_version'], $cur->url, $cur->current );
return sprintf( '| <strong>'.__( '<a href="%1$s">Get Version %2$s</a>' ).'</strong>', wp_nonce_url('update.php?action=upgrade-core', 'upgrade-core'), $cur->current);
break;
}
case 'latest' :
default :
return sprintf( '| '.__( 'Version %s' ), $GLOBALS['wp_version'], $cur->url, $cur->current );
return sprintf( '| '.__( 'Version %s' ), $GLOBALS['wp_version'] );
break;
}
}
@ -39,9 +39,9 @@ function update_nag() {
return false;
if ( current_user_can('manage_options') )
$msg = sprintf( __('WordPress %2$s is available! <a href="%1$s">Please update now</a>.'), $cur->url, $cur->current );
$msg = sprintf( __('WordPress %1$s is available! <a href="%2$s">Please update now</a>.'), $cur->current, wp_nonce_url('update.php?action=upgrade-core', 'upgrade-core') );
else
$msg = sprintf( __('WordPress %2$s is available! Please notify the site administrator.'), $cur->url, $cur->current );
$msg = sprintf( __('WordPress %1$s is available! Please notify the site administrator.'), $cur->current );
echo "<div id='update-nag'>$msg</div>";
}
@ -53,7 +53,7 @@ function update_right_now_message() {
$msg = sprintf( __('This is WordPress version %s.'), $GLOBALS['wp_version'] );
if ( isset( $cur->response ) && $cur->response == 'upgrade' && current_user_can('manage_options') )
$msg .= " <a href='$cur->url' class='rbutton'>" . sprintf( __('Update to %s'), $cur->current ? $cur->current : __( 'Latest' ) ) . '</a>';
$msg .= " <a href='" . wp_nonce_url('update.php?action=upgrade-core', 'upgrade-core') . "' class='rbutton'>" . sprintf( __('Update to %s'), $cur->current ? $cur->current : __( 'Latest' ) ) . '</a>';
echo "<span id='wp-version-message'>$msg</span>";
}
@ -193,4 +193,77 @@ function wp_update_plugin($plugin, $feedback = '') {
return $folder . '/' . $pluginfiles[0];
}
function wp_update_core($feedback = '') {
global $wp_filesystem;
if ( !empty($feedback) )
add_filter('update_feedback', $feedback);
// Is an update available?
$current = get_option( 'update_core' );
if ( !isset( $current->response ) || $current->response == 'latest' )
return new WP_Error('up_to_date', __('WordPress is at the latest version.'));
// Is a filesystem accessor setup?
if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
WP_Filesystem();
if ( ! is_object($wp_filesystem) )
return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
if ( $wp_filesystem->errors->get_error_code() )
return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
// Get the base WP folder
$wp_dir = $wp_filesystem->abspath();
if ( empty($wp_dir) )
return new WP_Error('fs_no_wp_dir', __('Unable to locate WordPress directory.'));
// And the same for the Content directory.
$content_dir = $wp_filesystem->wp_content_dir();
if( empty($content_dir) )
return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
$wp_dir = trailingslashit( $wp_dir );
$content_dir = trailingslashit( $content_dir );
// Get the URL to the zip file
$package = $current->package;
// Download the package
apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
$download_file = download_url($package);
if ( is_wp_error($download_file) )
return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
$working_dir = $content_dir . 'upgrade/core';
// Clean up working directory
if ( $wp_filesystem->is_dir($working_dir) )
$wp_filesystem->delete($working_dir, true);
apply_filters('update_feedback', __('Unpacking the update'));
// Unzip package to working directory
$result = unzip_file($download_file, $working_dir);
// Once extracted, delete the package
unlink($download_file);
if ( is_wp_error($result) ) {
$wp_filesystem->delete($working_dir, true);
return $result;
}
// Copy update-core.php from the new version into place.
if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) {
$wp_filesystem->delete($working_dir, true);
return new WP_Error('copy_failed', __('Could not copy files'));
}
require(ABSPATH . 'wp-admin/includes/update-core.php');
return update_core($working_dir, $wp_dir);
}
?>

View File

@ -44,6 +44,39 @@ function do_plugin_upgrade($plugin) {
echo '</div>';
}
function do_core_upgrade() {
global $wp_filesystem;
$url = wp_nonce_url("update.php?action=upgrade-core", "upgrade-core");
if ( false === ($credentials = request_filesystem_credentials($url)) )
return;
if ( ! WP_Filesystem($credentials) ) {
request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
return;
}
echo '<div class="wrap">';
echo '<h2>' . __('Upgrade WordPress') . '</h2>';
if ( $wp_filesystem->errors->get_error_code() ) {
foreach ( $wp_filesystem->errors->get_error_messages() as $message )
show_message($message);
echo '</div>';
return;
}
$result = wp_update_core('show_message');
if ( is_wp_error($result) ) {
show_message($result);
if ('up_to_date' != $result->get_error_code() )
show_message( __('Installation Failed') );
} else {
show_message( __('WordPress upgraded successfully') );
}
echo '</div>';
}
if ( isset($_GET['action']) ) {
$plugin = isset($_GET['plugin']) ? trim($_GET['plugin']) : '';
@ -84,6 +117,13 @@ wp_admin_css( 'colors', true );
include(WP_PLUGIN_DIR . '/' . $plugin);
}
echo "</body></html>";
} elseif ( 'upgrade-core' == $_GET['action'] ) {
//check_admin_referer('upgrade-core');
$title = __('Upgrade WordPress');
$parent_file = 'index.php';
require_once('admin-header.php');
do_core_upgrade();
include('admin-footer.php');
}
}

View File

@ -40,7 +40,7 @@ function wp_version_check() {
$new_option->last_checked = time(); // this gets set whether we get a response or not, so if something is down or misconfigured it won't delay the page load for more than 3 seconds, twice a day
$new_option->version_checked = $wp_version;
$url = "http://api.wordpress.org/core/version-check/1.1/?version=$wp_version&php=$php_version&locale=$locale";
$url = "http://api.wordpress.org/core/version-check/1.2/?version=$wp_version&php=$php_version&locale=$locale";
$options = array('timeout' => 3);
$headers = array(
@ -50,21 +50,23 @@ function wp_version_check() {
$response = wp_remote_request($url, $options, $headers);
if( 200 != $response['response']['code'] )
if ( 200 != $response['response']['code'] )
return false;
$body = $response['body'];
$body = trim( $response[1] );
$body = trim( $body );
$body = str_replace(array("\r\n", "\r"), "\n", $body);
$returns = explode("\n", $body);
$new_option->response = attribute_escape( $returns[0] );
if ( isset( $returns[1] ) )
$new_option->url = clean_url( $returns[1] );
if ( isset( $returns[2] ) )
$new_option->current = attribute_escape( $returns[2] );
$new_option->package = clean_url( $returns[2] );
if ( isset( $returns[3] ) )
$new_option->current = attribute_escape( $returns[3] );
if ( isset( $returns[4] ) )
$new_option->locale = attribute_escape( $returns[4] );
update_option( 'update_core', $new_option );
}

View File

@ -107,6 +107,33 @@ if ( version_compare( '4.3', phpversion(), '>' ) ) {
if ( !defined('WP_CONTENT_DIR') )
define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' ); // no trailing slash, full paths only - WP_CONTENT_URL is defined further down
if ( file_exists(ABSPATH . '.maintenance') ) {
if ( file_exists( WP_CONTENT_DIR . '/maintenance.php' ) ) {
require_once( WP_CONTENT_DIR . '/maintenance.php' );
die();
}
$protocol = $_SERVER["SERVER_PROTOCOL"];
if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol )
$protocol = 'HTTP/1.0';
header( "$protocol 503 Service Unavailable", true, 503 );
header( 'Content-Type: text/html; charset=utf-8' );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Maintenance</title>
</head>
<body>
<h1>Briefly unavailable for scheduled maintenance. Check back in a minute.</h1>
</body>
</html>
<?php
die();
}
if ( !extension_loaded('mysql') && !file_exists(WP_CONTENT_DIR . '/db.php') )
die( /*WP_I18N_OLD_MYSQL*/'Your PHP installation appears to be missing the MySQL extension which is required by WordPress.'/*/WP_I18N_OLD_MYSQL*/ );