Here is an example script showing how you can use the MODx tag translation class (modTranslate095 - will probably be renamed, but...) to either create a log of or actually perform conversion on any xPDO-controlled database fields or any file content containing old-style tags. The comments should guide you through what you need to know, but feel free to ask any questions, make suggestions for improvement, or additional features to add...
Please note, it’s always best to review what it’s going to do first by passing false as the first parameter and writing the log to file. There are occasions where PHP and Javascript syntax will return a match on a tag, so pay particular attention if you have tags inside these files that need to be translated.
<?php
$mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $tstart = $mtime;
$modx_core_dir_name = 'core';
$modx_core_path = strtr(realpath(dirname(__FILE__)), '\\', '/') . '/' . $modx_core_dir_name . '/';
// include the modX class
include_once ($modx_core_path . "model/modx/modx.class.php");
// create the modX object
$modx = new modX($modx_core_path . 'config/');
// initialize the web context
$modx->initialize('web');
// set the parser to the modParser095 class so we have access to the translate() function
$modx->config['modParser.class']= 'modParser095';
// load the modTranslate095 utility class and get an instance
$modx->loadClass('modTranslate095', '', false, true);
$translator= new modTranslate095($modx);
// an array of files (with full paths) to be translated
$files= array (
MODX_BASE_PATH . 'install/snippet.ditto.tpl',
);
// file patterns to limit the file types to be included for translation in the paths
$patterns= array ('php', 'txt', 'tpl', 'inc', 'js', 'sql', 'html');
// add paths of files, according to the patterns, to be translated (not recursive)
$paths= array ();
$paths[]= MODX_BASE_PATH . 'assets/snippets/ditto/';
$paths[]= MODX_BASE_PATH . 'assets/snippets/ditto/lang/';
//$paths[]= MODX_BASE_PATH . 'install/';
//$paths[]= MODX_BASE_PATH . 'manager/media/style/MODx/';
//$paths[]= MODX_BASE_PATH . 'manager/media/style/MODxGreen/';
//$paths[]= MODX_BASE_PATH . 'manager/media/style/MODxLight/';
if ($paths) {
foreach ($paths as $path) {
$directory= opendir($path);
if ($directory) {
while (false !== ($filename= @ readdir($directory))) {
$extension= substr($filename, strrpos($filename, '.') + 1);
if ($filename != '.' && $filename != '..' && in_array($extension, $patterns)) {
$file= $path . $filename;
if (!is_dir($file)) {
$files[]= $file;
}
}
}
}
}
}
// pass null to skip classes or an empty array to process all standard content fields for MODx
// you can also build your own array of classname => fields to translate
$classes= null;
// translate the site
// first parameter determines if translation is written to the database tables and/or files (changes are permanent if true)
// second parameter is an array of classname => fields to translate
// third parameter is an array of files to translate
// last parameter, if included, should be a path to a file where a log of the translation session is written
$translator->translateSite(false, $classes, $files, $modx->getCachePath() . 'logs/my.translation.log');
$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";
exit();