yj-t
7/7/2016 - 11:00 PM

Example of export script from MODX Resources to JSON, for importing to another site.

Example of export script from MODX Resources to JSON, for importing to another site.

<?php
// Only executable via SSH
if (PHP_SAPI !== 'cli') exit();
// Instantiate MODX
@include(dirname(__FILE__) . '/config.core.php');
if (!defined('MODX_CORE_PATH')) define('MODX_CORE_PATH', dirname(__FILE__) . '/core/');
include_once (MODX_CORE_PATH . "model/modx/modx.class.php");
$modx= new modX();
$modx->initialize('web');

// Target export file
$exportPath = dirname(__FILE__);
$exportFilename = 'export.json';

// Allowed object classes
$allowed = array(
    'modResource' => 1,
    'modSnippet' => 1,
    'modPlugin' => 1,
    'modChunk' => 1,
    'modTemplate' => 1,
);
// Config file location
$configPath = dirname(__FILE__);
$configFile = 'config.json';
/**
 * config.json
 * @description: JSON array of objects with three required keys
 * "class" is the class of MODX object you wish to export, from the array $allowed
 * "where" is the criteria on which to search for the object. 
 * "toWhere" is the criteria on which to search for the object to replace in the target site.
 * Special flags for "toWhere" include: 
 * "create" which will discard the ID and create a new object using fromArray(). 
 * NOTE: issues may arise from duplicate field values, such as "name".
 * "update" which will use the same criteria as supplied in "where" on the target site.
 * Any fals-ey value, or non-array, other than the special flags, will result in the object
 * being excluded from the export.
 */
$config = file_get_contents($configPath . '/' . $configFile);
if (!$config) {
    echo 'could not load config file';
    exit();
}
$config = $modx->fromJSON($config);
if (!is_array($config)) {
    echo 'bad config data';
    exit();
}
// Begin iteration
$export = array();
foreach ($config as $item) {
    
    // Check class
    if (!isset($allowed[$item['class']]) || !$allowed[$item['class']]) continue;
    
    // Check where
    if (!is_array($item['where'])) continue;
    
    // Process toWhere
    if (is_array($item['toWhere'])) {
        $toWhere = $item['toWhere'];
    } elseif ($item['toWhere'] === 'update') {
        $toWhere = $item['where'];
    } elseif ($item['toWhere'] === 'create') {
        $toWhere = false;
    } else {
        continue;
    }
    
    // Fetch object and convert to array (might be faster to use straight PDO)
    $object = $modx->getObject($item['class'], $item['where'])->toArray();
    if (!$object) continue;
    
    // Add to export array
    $export[$item['class']][] = array(
        'where' => $toWhere,
        'id' => $object['id'],
        'object' => $object,
    );
}

// Prep for output. FR for xPDO: expose json_encode flags :P
$export = json_encode($export, JSON_PRETTY_PRINT);
// Save to file
if ($export) {
    file_put_contents($exportPath . '/' . $exportFilename, $export);
    // It's always a good idea to manually examine the export file to ensure it has the objects you expect
    // Very easy to select the wrong object if you aren't very, very careful with your query criteria ;)
} else {
    echo 'Export failed';
}