Skip to content

Instantly share code, notes, and snippets.

@derak-kilgo
Created October 23, 2013 14:12
Show Gist options
  • Save derak-kilgo/7119545 to your computer and use it in GitHub Desktop.
Save derak-kilgo/7119545 to your computer and use it in GitHub Desktop.
cli tool to replace domain in wordpress content. Requires Zend Framework 1.x
<?php
/*
* Only allow script to run via command line interface.
*/
if(php_sapi_name() != 'cli'){
die('This script can only be run from the command line');
}
/*
* Tool makes use of Zend Framework 1.x
* Change settings/paths as needed for your environment.
*/
date_default_timezone_set('America/Chicago');
$doc_root = '/Users/dkilgo/Zend/workspaces/DefaultWorkspace7/';
set_include_path('.' . PATH_SEPARATOR . $doc_root . '/ZendFramework-1.12.3/library/');
require_once 'Zend/Loader/Autoloader.php';
$zend_autoloader = Zend_Loader_Autoloader::getInstance();
$zend_autoloader->setFallbackAutoloader(true);
/*
* configure database adapter
* use settings found in your wp-config.php file.
*/
$dbConfig = array('host'=>'127.0.0.1',
'username'=>'root',
'password'=>'root',
'dbname'=>'wp_blog');
$adapter = new Zend_Db_Adapter_Mysqli($dbConfig);
Zend_Db_Table::setDefaultAdapter($adapter);
//require_once('./db-config.php');
//Esc13DbConfig::load_qa();
/*
* Begin search replace operation.
* These options are passed into str_replace() so scalar or array values can be used
* if you want to replace more than one string at a time.
* Be as specific as possible with your string selection.
*/
$search = array('www-qa.sample.org','www.sample.org');
$replace = 'www-qa.newsite.org';
try{
db_replace($search,$replace,'wp_options',array('option_value'));
db_replace($search,$replace,'wp_postmeta',array('meta_value'));
db_replace($search,$replace,'wp_posts',array('post_content','post_title','guid'));
//Add more db_replace statements if other custom modules embed content you'd like to replace.
}catch(Exception $e){
echo get_class($e) . ': ' . $e->getMessage() . "\n" . $e->getTraceAsString();
die("\n");
}
/**
* A generic function to scan in a database table and do a string replace.
* Also works on serialized arrays or stdClass objects stored in the database.
*
* search and replace are args passed into str_replace() so arrays or strings are valid.
* @see str_replace();
*
* @param $search
* @param $replace
* @param $tableName
* @param $arrFields
*/
function db_replace($search,$replace,$tableName,$arrFields){
$dbtable = new Zend_Db_Table($tableName);
$info = $dbtable->info();
$primary_key = $info['primary'][1];
echo "\n Scanning $tableName table...\n";
$result = $dbtable->fetchAll();
$intFixedCount = 0;
$intRowsTotal = count($arrFields) * count($result);
foreach($result as $row){
foreach($arrFields as $curColumn){
$str = $row->$curColumn;
$data = @unserialize($str);
if ($str === 'b:0;' || $data !== false) {
//is serialized data.
array_walk_recursive($data,'str_replace_recursive',array('search'=>$search,'replace'=>$replace));
$fixed = serialize($data);
} else {
$fixed = str_replace($search,$replace,$str);
}
if( ($fixed !== $str)&& (!empty($fixed)) ){
$row->$curColumn = $fixed;
//save modified data. Comment out next line to do a 'dry run'.
$row->save();
$intFixedCount++;
echo "ID:" . $row->$primary_key .'|Column:'. $curColumn . " was changed.\n";
}
}
}
echo "\nFixed $intFixedCount fields of $intRowsTotal scanned in $tableName \n Done\n";
}
/**
* Callback function to do string replace on multi-dimension arrays.
* @see array_walk_recursive();
*
* @param $item
* @param $key
* @param $data
*/
function str_replace_recursive(&$item,$key,$data){
if(is_scalar($item)){
str_replace($data['search'],$data['replace'],$item);
}else{
array_walk_recursive($item,'str_replace_recursive',$data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment