We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22303 MODX Staff
    • 10,725 Posts
    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();
    
      • 25663 MODX Staff
      • 12,272 Posts
      Very cool Jason... thanks! smiley
        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
        • 7923
        • 4,213 Posts
        I’ll try to convert MaxiGallery to 097 with this and the modOutputFilter.. I’ll report results here, thanks alot for your hard work Jason!

        EDIT: This will be delayed a bit as I encountered slight problems when implementing it.. waiting for some new code from Jason wink


          "He can have a lollipop any time he wants to. That's what it means to be a programmer."
          • 18397
          • 3,250 Posts
          2 quick questions:

          1. What is modOutputFilter?

          2. Is the tag translation tool for changing snippet calls and document object references [*field*] or something else?
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: Mark at Feb 25, 2007, 02:25 PM

            1. What is modOutputFilter?
            modInputFilter and modOutputFilter are meant to replace the modifier functionality of phX. They can be used with all tag elements in 0.9.7. It is experimental and needs a lot of work, as many of the modifier implementations are obviously broken (i.e. I just copied and pasted the code from phX, but have not tested or worked on any of the modifier command implementations).

            Quote from: Mark at Feb 25, 2007, 02:25 PM

            2. Is the tag translation tool for changing snippet calls and document object references [*field*] or something else?
            It’s for identifying/changing any instances of old style tags to new style tags in any kind of content, including references in document pagetitles or content, snippet content, PHP files included from snippets, language files, everything. In other words, it’s a tool to help you convert your site content and your add-on components that have hardcoded references to any kind of element tag, either in code or in your output templates. For a quick reference, here are the new tags with their legacy counterparts...

            [*] [[snippet]] (no change)
            [*] [[*fieldOrTV]] formerly [*fieldOrTV*]
            [*] [[+placeholder]] formerly [+placeholder+]
            [*] [[$chunk]] formerly {{chunk}}
            [*] [[++system_settings]] formerly [(system_settings)]
            [*] [[~link]] formerly [~link~]
            [*] [[%lang]] new tag to present language (a.k.a. lexicon or dictionary) values
              • 18397
              • 3,250 Posts
              Will this replace the tags in chunks?
                • 22303 MODX Staff
                • 10,725 Posts
                Quote from: Mark at Apr 17, 2007, 11:16 PM

                Will this replace the tags in chunks?
                It should replace tags in all the database content fields where you might expect to find them, as well as in any physical files you point it to.
                  • 23491 ☆ A M B ☆
                  • 1,056 Posts
                  Quote from: OpenGeek at Apr 18, 2007, 09:50 AM

                  Quote from: Mark at Apr 17, 2007, 11:16 PM

                  Will this replace the tags in chunks?
                  It should replace tags in all the database content fields where you might expect to find them, as well as in any physical files you point it to.

                  Wow! Great work, Jason!

                  I was discussing this on Friday regarding future plans/options to upgrading from 095+ to 097... Tag Translation being one of my bigger concerns, but it looks like now I can feel a more secure! Once I am in position to test this, I’ll be sure to provide feedback.

                  Thanks!
                    Mike Reid - www.pixelchutes.com
                    MODx Ambassador / Contributor
                    [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
                    ________________________________
                    Where every pixel matters.
                    • 4172
                    • 5,888 Posts
                    ok, this is an older topic, but found it very usefull and created a snippet on basis of that.
                    You can find it here: http://modxcms.com/forums/index.php/topic,42321.0.html
                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!