WordPress (サブディレクトリ型マルチサイト) の DB を一気に書き換える奴

#!/usr/bin/php
<?php
switch($argc) {
case 1:
case 2:
case 3:
echo "please input new site url and wp directory name, old site url!\n";
exit();
default:
$old_site = isset($argv[3]) ? $argv[3] : '';
$path = $argv[2];
$new_site = $argv[1];
}
$old_domain = $new_domain = false;
$parsed_old_site = parse_url($old_site);
$parsed_new_site = parse_url($new_site);
if ( isset($parsed_old_site['host']) && isset($parsed_new_site['host'])) {
$old_domain = $parsed_old_site['host'];
$new_domain = $parsed_new_site['host'];
$_SERVER['HTTP_HOST'] = $old_domain;
}
if ( !$old_domain || !$new_domain ) {
exit();
}
if ( !file_exists($path.'wp-load.php') && file_exists($path.'wp-config.php') ) {
echo "Not found wp-config.php!\n";
exit();
}
require_once(file_exists($path.'wp-load.php') ? $path.'wp-load.php' : $path.'wp-config.php');
echo "*** main site ***\n";
if ( $new_domain !== $old_domain ) {
global $wpdb;
$wpdb->query("update {$wpdb->prefix}site set domain = '{$new_domain}' where domain = '{$old_domain}'");
$wpdb->query("update {$wpdb->prefix}blogs set domain = '{$new_domain}' where domain = '{$old_domain}'");
}
replace_site_url($new_site, $path, $old_site, true);
$blogs = $wpdb->get_results("select blog_id, domain, path from {$wpdb->prefix}blogs order by blog_id;");
foreach ($blogs as $blog) {
echo "**** {$blog->blog_id} : {$blog->domain}{$blog->path} *****\n";
switch_to_blog($blog->blog_id);
replace_site_url($new_site, $path, $old_site);
}
function replace_site_url($new_site, $path, $old_site, $main = false) {
global $wpdb;
$replace = new ReplaceSiteURL($new_site, $path, $old_site);
// wp_sitemeta
if ($main) {
echo "{$wpdb->prefix}sitemeta : replace '{$replace->old_site}' to '{$replace->new_site}' …\n";
printf("Result: %d\n\n", $replace->sitemeta());
}
// wp_options
echo "{$wpdb->options} : replace '{$replace->old_site}' to '{$replace->new_site}' …\n";
printf("Result: %d\n\n", $replace->options());
// wp_posts
echo "{$wpdb->posts} : replace '{$replace->old_site}' to '{$replace->new_site}' …\n";
printf("Result: %d\n\n", $replace->posts());
// wp_postmeta
echo "{$wpdb->postmeta} : replace '{$replace->old_site}' to '{$replace->new_site}' …\n";
printf("Result: %d\n\n", $replace->postmeta());
// wp_usermeta
echo "{$wpdb->usermeta} : replace '{$replace->old_site}' to '{$replace->new_site}' …\n";
printf("Result: %d\n\n", $replace->usermeta());
// wp_commentmeta
echo "{$wpdb->commentmeta} : replace '{$replace->old_site}' to '{$replace->new_site}' …\n";
printf("Result: %d\n\n", $replace->commentmeta());
unset($replace);
}
// replace class
class ReplaceSiteURL {
public $new_site;
public $old_site;
public $wp_path;
function __construct($new_site, $path, $old_site = '') {
$this->new_site = untrailingslashit(preg_match('/^https?:\/\//i', $new_site) ? $new_site : 'http://&#39;.$new_site );
$old_site = (!empty($old_site) && !preg_match('/^https?:\/\//i', $old_site) ? 'http://&#39;.$old_site : $old_site );
$this->old_site = untrailingslashit(empty($old_site) ? home_url() : $old_site);
$this->wp_path = $path;
}
// wp_options
public function options() {
global $wpdb;
$count = 0;
$sql = $wpdb->prepare(
"SELECT * from `{$wpdb->options}` where option_value like %s",
'%'.untrailingslashit($this->old_site).'%'
);
$results = $wpdb->get_results($sql);
foreach ($results as $result){
$sql = $wpdb->prepare(
"UPDATE `{$wpdb->options}` SET option_value = %s where option_id = %d",
$this->replace($this->old_site, $this->new_site, $result->option_value) ,
$result->option_id
);
$wpdb->query($sql);
$count++;
}
return $count;
}
// wp_posts
public function posts() {
global $wpdb;
$sql = $wpdb->prepare(
"UPDATE `{$wpdb->posts}` SET post_content=REPLACE(post_content, %s, %s) where post_content like %s",
$this->old_site,
$this->new_site,
"%{$this->old_site}%"
);
return $wpdb->query($sql);
}
// wp_postmeta
public function postmeta() {
global $wpdb;
$count = 0;
$sql = $wpdb->prepare(
"SELECT * from `{$wpdb->postmeta}` where meta_value like %s",
'%'.untrailingslashit($this->old_site).'%'
);
$results = $wpdb->get_results($sql);
foreach ($results as $result){
$sql = $wpdb->prepare(
"UPDATE `{$wpdb->postmeta}` SET meta_value = %s where meta_id = %d",
$this->replace($this->old_site, $this->new_site, $result->meta_value) ,
$result->meta_id
);
$wpdb->query($sql);
$count++;
}
return $count;
}
// wp_usermeta
public function usermeta() {
global $wpdb;
$count = 0;
$sql = $wpdb->prepare(
"SELECT * from `{$wpdb->usermeta}` where meta_value like %s",
'%'.untrailingslashit($this->old_site).'%'
);
$results = $wpdb->get_results($sql);
foreach ($results as $result){
$sql = $wpdb->prepare(
"UPDATE `{$wpdb->usermeta}` SET meta_value = %s where umeta_id = %d",
$this->replace($this->old_site, $this->new_site, $result->meta_value) ,
$result->umeta_id
);
$wpdb->query($sql);
$count++;
}
return $count;
}
// wp_commentmeta
public function commentmeta() {
global $wpdb;
$count = 0;
$sql = $wpdb->prepare(
"SELECT * from `{$wpdb->commentmeta}` where meta_value like %s",
'%'.untrailingslashit($this->old_site).'%'
);
$results = $wpdb->get_results($sql);
foreach ($results as $result){
$sql = $wpdb->prepare(
"UPDATE `{$wpdb->commentmeta}` SET meta_value = %s where meta_id = %d",
$this->replace($this->old_site, $this->new_site, $result->meta_value) ,
$result->meta_id
);
$wpdb->query($sql);
$count++;
}
return $count;
}
// wp_sitemeta
public function sitemeta() {
global $wpdb;
$count = 0;
$sql = $wpdb->prepare(
"SELECT * from `{$wpdb->prefix}sitemeta` where meta_value like %s",
'%'.untrailingslashit($this->old_site).'%'
);
$results = $wpdb->get_results($sql);
foreach ($results as $result){
$sql = $wpdb->prepare(
"UPDATE `{$wpdb->prefix}sitemeta` SET meta_value = %s where meta_id = %d",
$this->replace($this->old_site, $this->new_site, $result->meta_value) ,
$result->meta_id
);
$wpdb->query($sql);
$count++;
}
return $count;
}
private function replace($origin, $replaced, $value) {
if ( is_serialized($value) ) {
$value = maybe_unserialize($value);
$value = $this->deep_replace($origin, $replaced, $value);
$value = maybe_serialize($value);
} else {
$value = str_replace($origin, $replaced, $value);
}
return $value;
}
private function deep_replace($origin, $replaced, $datas) {
if ( is_array($datas) || is_object($datas) ) {
foreach ( $datas as &$data ) {
if ( is_array($data) || is_object($data) ) {
$data = $this->deep_replace($origin, $replaced, $data);
} else {
$data = str_replace($origin, $replaced, $data);
}
}
}
return $datas;
}
}

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト /  変更 )

Google フォト

Google アカウントを使ってコメントしています。 ログアウト /  変更 )

Twitter 画像

Twitter アカウントを使ってコメントしています。 ログアウト /  変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト /  変更 )

%s と連携中