Created
April 23, 2012 00:28
-
-
Save pix0r/2467767 to your computer and use it in GitHub Desktop.
Script to update Magento base URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$opt_arr = array( | |
'u' => 'user', | |
'p' => 'pass', | |
'h' => 'host', | |
'd' => 'dbname', | |
't' => 'table_prefix', | |
'b' => 'base_url', | |
's' => 'secure_url', | |
'x' => 'local_xml', | |
); | |
$options = getopt(implode(':', array_keys($opt_arr)) . ':'); | |
$c = array(); | |
foreach ($opt_arr as $k => $v) { | |
if (!empty($options[$k])) $c[$v] = $options[$k]; | |
} | |
if (empty($c['local_xml'])) $c['local_xml'] = 'app/etc/local.xml'; | |
$local_xml = file_get_contents($c['local_xml']); | |
$root = new SimpleXMLElement($local_xml); | |
if (empty($c['base_url'])) usage(); | |
if (empty($c['secure_url'])) $c['secure_url'] = $c['base_url']; | |
if (empty($c['host'])) $c['host'] = $root->global->resources->default_setup->connection->host; | |
if (empty($c['user'])) $c['user'] = $root->global->resources->default_setup->connection->username; | |
if (empty($c['pass'])) $c['pass'] = $root->global->resources->default_setup->connection->password; | |
if (empty($c['dbname'])) $c['dbname'] = $root->global->resources->default_setup->connection->dbname; | |
if (empty($c['table_prefix'])) $c['table_prefix'] = $root->global->resources->db->table_prefix; | |
function usage() { | |
global $opt_arr; | |
$usage = "Usage: " . $_SERVER['argv'][0] . " -b <base_url>"; | |
foreach ($opt_arr as $k => $v) { | |
if ($k == 'b') continue; | |
$usage .= " [-$k <$v>]"; | |
} | |
die($usage . "\n"); | |
} | |
$db = mysql_connect($c['host'], $c['user'], $c['pass']) or die("Error connecting: " . mysql_error()); | |
mysql_select_db($c['dbname']) or die("Error selecting db: " . mysql_error()); | |
$config_tbl = "{$c['table_prefix']}core_config_data"; | |
$sql = array( | |
"UPDATE `$config_tbl` SET value='" . mysql_real_escape_string($c['base_url']) . "' WHERE path = 'web/unsecure/base_url'", | |
"UPDATE `$config_tbl` SET value='" . mysql_real_escape_string($c['secure_url']) . "' WHERE path = 'web/secure/base_url'", | |
); | |
foreach ($sql as $s) { | |
echo "Running $s\n"; | |
$result = mysql_query($s); | |
if (!$result) { | |
die("SQL Error: " . mysql_error() . "\n"); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment