Skip to content

Instantly share code, notes, and snippets.

@sepiariver
Last active December 22, 2023 12:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sepiariver/bf18d571c676035ab9f3487eaab7c6dd to your computer and use it in GitHub Desktop.
Save sepiariver/bf18d571c676035ab9f3487eaab7c6dd to your computer and use it in GitHub Desktop.
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';
}
@mikelweb
Copy link

Hi,
How do I execute this via SSH in a localhost (XAMPP) ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment