Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wordpress to MODX migration with xPDO
<?php
/*
* xPDO Wordpress to MODX Migration Code
*
* Original author: James Ehly
* Original source: http://devtrench.com/posts/first-impressions-of-xpdo-wordpress-to-modx-migration-tool
*
* This script uses xpdo to connect to modx and wordpress packages to migrate
* data from a wordpress database to a modx database.
*
* The code below may or may not work for your particular wordpress and MODx install.
* It should be used as a reference point, and you should customize it to your
* specific needs, but I hope that most general cases are covered.
*
* This script is intended to be run on a new install of MODx Revolution. If
* you are using an existing MODx Revo install then the script will probably
* work but might have some unexpected results. So, backup your data first, and
* use at your own risk.
*
*/
// Include the xpdo and modx classes
//include ('core/xpdo/xpdo.class.php');
//include ('core/model/modx/modx.class.php');
$rootPasswd = $modx->getOption('rootPasswd', $scriptProperties, '');
$dbName = $modx->getOption('dbName', $scriptProperties, '');
$dsn = 'mysql:host=localhost; dbname=' . $dbName . '; port=3306; charset=utf8';
$xpdo = new xPDO($dsn,'root', $rootPasswd);
// Instantiate a new modx object. MODx inherits from xpdo so we can use it
// like an xpdo object, but it has the extra functions needed for saving content.
// Thanks to Shaun McCormick for writing docs on this.
$modx = new modX();
$modx->initialize('web');
// Now instantiate a new xpdo object and add our wordpress package. This gives
// us the ability to make queries on the wordpress database as an xpdo object.
$xpdo->addPackage('wordpress','core/model/','wp_');
echo $o=($xpdo->connect()) ? 'Connected<br>' : 'Not Connected<br>';
// Get all wordpress posts. Isn't this so easy?
$posts = $xpdo->getCollection('Posts');
$postmeta = $xpdo->getCollection('Postmeta');
// Iterate over each post and create a new modResource object, mapping our post
// fields to our wordpress fields
foreach ($posts as $post) {
// Strip content of WP formatting and inline styles
$content = $post->get('post_content');
$content = preg_replace('/\[[\s\S]+?]/', '', $content);
$content = preg_replace('/style="[\s\S]+?"/', '', $content);
$content = '<div class="ui vertical stripe segment white"><div class="ui container">' . $content . '</div></div>';
// Remove domain name from internal links
$content = preg_replace('/http:\/\/YOUR-DOMAIN-NAME\.com\//', '', $content);
$content = preg_replace('/https:\/\/YOUR-DOMAIN-NAME\.com\//', '', $content);
// Create resource object
$resource = '';
$resource = $modx->newObject('modResource',
array(
'content' => $content,
'id' => $post->get('ID'),
'pagetitle' => $post->get('post_title'),
'parent' => $post->get('post_parent'),
'template' => 2,
'context_key' => 'web',
'alias' => $post->get('post_name'),
'published' => ($post->get('post_status') == 'publish') ? 1 : 0,
'pub_date' => ($post->get('post_status') == 'publish') ? $post->get('post_date') : 0,
)
);
// Call the save function which inserts our object record into the database
if (
$post->get('post_title')
&& stripos($post->get('post_title'), 'img') === false
&& stripos($post->get('post_title'), 'icon') === false
&& stripos($post->get('post_title'), 'slide') === false
&& stripos($post->get('post_title'), 'button') === false
&& stripos($post->get('post_title'), 'foto') === false
&& stripos($post->get('post_title'), '.jpg') === false
&& stripos($post->get('post_name'), 'img') === false
&& stripos($post->get('post_name'), 'icon') === false
){
$resource->set('id', $post->get('ID'));
$resource->save();
//print_r($resource);
}
}
// Add SEO titles and descriptions
foreach ($postmeta as $meta) {
if ($meta->get('meta_key') === '_yoast_wpseo_title') {
$resource = $modx->getObject('modResource',
array(
'id' => $meta->get('post_id'),
)
);
$resource->set('longtitle', $meta->get('meta_value'));
$resource->save();
}
if ($meta->get('meta_key') === '_yoast_wpseo_metadesc') {
$resource = $modx->getObject('modResource',
array(
'id' => $meta->get('post_id'),
)
);
$resource->set('description', $meta->get('meta_value'));
$resource->save();
}
}
// we're done
return 'Done!';
<?php
/*
* xPDO Wordpress to MODX Migration Code
*
* Original author: James Ehly
* Original source: http://devtrench.com/posts/first-impressions-of-xpdo-wordpress-to-modx-migration-tool
*
* This code is part of the WP to MODx migration and should go in your
* core/model/schema directory. Simply run it from a web browser or cli and
* it will create the wordpress schema from the database and all of the
* xpdo objects in core/model/wordpress
*
* Once this is done we can run the wpImportBuildResources snippet that does the actual
* data migration.
*
* @todo some manual intervention is needed to set up the table relationships we need
*
* Thanks to Jason Coward for posting how do set up a schema from a database:
* http://modxcms.com/forums/index.php/topic,16562.0.html
*/
$mtime= microtime();
$mtime= explode(" ", $mtime);
$mtime= $mtime[1] + $mtime[0];
$tstart= $mtime;
//Customize this line based on the location of your script
include_once (dirname(dirname(dirname(__FILE__))) . '/xpdo/xpdo.class.php');
// set your database credentials to use your wordpress database, and set the wordpress prefix (default is 'wp_')
$rootPasswd = $modx->getOption('rootPasswd', $scriptProperties, '');
$dbName = $modx->getOption('dbName', $scriptProperties, '');
$xpdo= new xPDO('mysql:host=localhost;dbname='.$dbName,'root',$rootPasswd,'wp_');
// Set the package name and root path of that package
$xpdo->setPackage('modx', XPDO_CORE_PATH . '../model/');
$xpdo->setDebug(true);
$manager= $xpdo->getManager();
$generator= $manager->getGenerator();
//Use this to create a schema from an existing database - this will write the wordpress.mysql.schema.xml file
$xml= $generator->writeSchema(XPDO_CORE_PATH . '../model/schema/wordpress.mysql.schema.xml', 'wordpress', 'xPDOObject', 'wp_');
//Use this to generate classes and maps from your schema
// NOTE: by default, only maps are overwritten; delete class files if you want to regenerate classes
// this will generate all of the model code in core/model/wordpress/
$generator->parseSchema(XPDO_CORE_PATH . '../model/schema/wordpress.mysql.schema.xml', XPDO_CORE_PATH . '../model/');
$mtime= microtime();
$mtime= explode(" ", $mtime);
$mtime= $mtime[1] + $mtime[0];
$tend= $mtime;
$totalTime= ($tend - $tstart);
$totalTime= sprintf("%2.4f s", $totalTime);
echo "\nExecution time: {$totalTime}\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment