<?php
//define('MULTIPLY_UPGRADE', $x);

/*
v 2.0.1

2.0.1: give user #1 the manage_blogs cap everywhere
*/

function multiply_query_mod($queries) {
	global $multiply_tables, $multiply_real_prefix, $table_prefix;
	foreach ($multiply_tables as $table) {
		$replace = "CREATE TABLE " . $multiply_real_prefix . $table . " (";
		$with =    "CREATE TABLE " . $table_prefix . $table . " ("; 
		$queries = str_replace($replace, $with, $queries);
	}
	return $queries;
}
define('WP_INSTALLING', true);
if (!file_exists('../wp-config.php')) die("There doesn't seem to be a wp-config.php file. Double check that you updated wp-config-sample.php with the proper database connection information and renamed it to wp-config.php.");
require('../wp-config.php');
timer_start();
require_once(ABSPATH . '/wp-admin/upgrade-functions.php');
require_once(ABSPATH . '/wp-content/plugins/000-multiply.php');

$step = $_GET['step'];
if (!$step) $step = 0;
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>
	<title>WordPress Multiply &rsaquo; Upgrade</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<style media="screen" type="text/css">
	<!--
	html {
		background: #eee;
	}
	body {
		background: #fff;
		color: #000;
		font-family: Georgia, "Times New Roman", Times, serif;
		margin-left: 20%;
		margin-right: 20%;
		padding: .2em 2em;
	}
	
	h1 {
		color: #006;
		font-size: 30px;
		font-weight: lighter;
	}
	
	h2 {
		font-size: 16px;
	}
	
	p, li, dt {
		line-height: 140%;
		padding-bottom: 2px;
	}

	ul, ol {
		padding: 5px 5px 5px 20px;
	}
	#logo {
		margin-bottom: 2em;
	}
.step a, .step input {
	font-size: 2em;
}
.step, th {
	text-align: right;
}
#footer {
text-align: center; border-top: 1px solid #ccc; padding-top: 1em; font-style: italic;
}
	-->
	</style>
</head>
<body>
<h1>Upgrade Multiply Blogs</h1>
<?php
switch($step) {

	case 0:
?> 
<p><?php _e('This file upgrades the blogs created and managed by the <a href="http://rephrase.net/box/word/multiply/">Multiply plugin for WordPress</a>.', 'Multiply'); ?></p>
<p><?php _e("Depending on changes in the latest version of WordPress, it's possible that it won't actually work&mdash;so, even moreso than with a regular upgrade, it's vital that you back up your data first. You should also make sure that you have the latest version of this file."); ?></p>

<h2 class="step"><a href="upgrade-multiply.php?step=1"><?php _e('Upgrade &raquo;'); ?></a></h2>
<?php
	break;
	
	case 1:
		$wpdb->hide_errors();

		if (! $wpdb->query("SHOW COLUMNS FROM `$wpdb->multiply` LIKE 'press_type'") ) { 
			$wpdb->query("ALTER TABLE `$wpdb->multiply` ADD `press_type` VARCHAR( 20 ) DEFAULT 'alt' NOT NULL;");
		}
		
		$first = $wpdb->get_row("SELECT * FROM $wpdb->multiply WHERE `press_id`='1'");
		if ('main' != $first->press_type) {
			
			// Dodgy: old Multiply numbered the new blogs starting from mb_id #1;
			// new WordPress ships with a blog_id of #1 for the main blog itself.
			// To make them compatible, we change an Multiply blog with id #1 to
			// something else, and insert a placeholder for the default.
			$first->press_name = $wpdb->escape($first->press_name);
			$wpdb->query("INSERT INTO $wpdb->multiply ( `press_name` ) VALUES ( '$first->press_name' )");	
			$new_id = $wpdb->insert_id;
			
			foreach ($multiply_tables as $table)
				$change[] = $table_prefix . '1_' . $table . ' TO ' . $table_prefix . $new_id . '_' . $table;
			
			$sql = implode(', ', $change);
			
			$wpdb->query("RENAME TABLE $sql");
			
			// default press
			$wpdb->query("DELETE FROM `$wpdb->multiply` WHERE `press_id` = '1'");
			$wpdb->query("
				INSERT INTO $wpdb->multiply ( `press_id`, `press_name`, `press_type` ) 
				VALUES ( '1', '".__('Default/Main', 'Multiply')."', 'main')" 
				);
		}
		
		// If there were custom user levels before, convert the levels
		// to the new cap system.
		if ( $users = $wpdb->get_results("SELECT * FROM $wpdb->muser") ) {
			
			foreach ($users as $user) {
				if ($user->press_id == 1) $user->press_id = $new_id;
				
				$role = translate_level_to_role($user->level);
				update_usermeta( $user->user_id, $table_prefix . $user->press_id . '_capabilities', array($role => true) );
			}
			$wpdb->query("DROP TABLE `$wpdb->muser`");
		}
		
		// We'll need these.
		$roles = Multiply::get_settings($multiply_real_prefix . 'user_roles', $press_id);
		$real_wp_queries = $wp_queries;		

				
		$presses = Multiply::press_list();
		foreach ($presses as $press_id => $press_name) {
			if ($press_id == 1) 
				continue;
			
			if ( defined('MULTIPLY_UPGRADE') && $press_id != MULTIPLY_UPGRADE )
				continue;
			
			Multiply::set_prefix($press_id);
			
			$wp_queries = multiply_query_mod($real_wp_queries);
			echo "<h3>$press_id - $press_name</h3>";
			wp_cache_flush();
			wp_cache_init();
			echo make_db_current();
			upgrade_all();
	
			// user #1 gets an administrator role everywhere
			update_option($table_prefix . 'user_roles', $roles);
			update_usermeta( 1, $table_prefix . 'capabilities', array('administrator' => true, 'manage_blogs' => true) );
			update_usermeta( 1, $table_prefix . 'user_level', 10 );
		}
		Multiply::set_prefix(1);
		wp_cache_init();
		

	
?> 
<h2><?php _e('Step 1'); ?></h2> 
	<p><?php printf(__("There's actually only one step. So if you see this, you're done. <a href='%s'>Have fun</a>!"), '../'); ?></p>

<!--
<pre>
<?php printf(__('%s queries'), $wpdb->num_queries); ?>

<?php printf(__('%s seconds'), timer_stop(0)); ?>
</pre>
-->

<?php
	break;
}
?> 
</body>
</html>
