We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40186
    • 28 Posts
    Hi!

    Running Revolution 2.2.2-pl

    I want to replace a piece of code with a chunk call in all resources containing that piece of code.

    Basically a "find all occurrences of this text and replace it with that text" throughout my resources (especially Articles!)

    Batcher doesn't seem to have that flexibility.

    Doc Finder, I think, is an Evolution add-on, because it refers to Modules and I see no Modules in Revolution.

    Advice? Suggestions?

    I'd really rather not change these entries one at a time (there are about 400).

    Thanks!

    Edward
    • Is the code you want to replace all in the "content" field of the Resources? Or does it appear in various fields, Template Variables, existing Chunks, etc.?

      If it's all in one field, you might be able to do it with a MySQL query if you have access to PHPMyAdmin, the mysql CLI, or another mysql client app, e.g.

      UPDATE `modx_site_content` SET `content` = REPLACE('<foo>...</foo>', '[[$foo]]', `content`) WHERE LOCATE('<foo>...</foo>', `content`) > 0
        • 40186
        • 28 Posts
        It's all in the content field.

        Oh man, I wouldn't feel confident pushing MYSQL queries through the system. I'm not that sophisticated with databases at all.
        • That's exactly what any global search-and-replace feature would do. And you would be well advised to back up the database first, no matter what the interface to the query.

          Feel free to post your query here before running it.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 3749
            • 24,544 Posts
            It would be a safer to use a snippet like this:

            Put this on a page. View it to run the snippet:

            [[!SearchReplaceContent? &SearchTerm=`Something` &ReplaceTerm=`Something else`]]


            Create a page called TestPage containing the code you want to change. Copy the content to the clipboard so you can restore it the replacement doesn't work correctly.

            <?php
            /* SearchReplaceContent snippet */
            
            /* try it on one resource first like this */
            
            $resources = $modx->getCollection('modResource', array ('pagetitle' => 'TestPage'));
            
            /* comment out the line above and use this line once it's working:
            
            $resources = $modx->getIterator('modResource', array ('deleted' => '0'));
            
            */
            
            if (empty($resources)) {
              return 'Failed to get any resources';
            }
            
            $totalPages = 0;
            $totalReplacements = 0;
            foreach ($resources as $resource) {
                $count = 0;
                $content = $resource->getContent();
                $content = str_replace($scriptProperties['searchTerm'], $scriptProperties['replaceTerm'], $content, $count);
                if ($count) {
                    $resource->setContent($content);
                    $resource->save();
                    $totalReplacements += $count;
                    $totalPages++;
                }
            }
            
            return $totalPages . " Resources changed" . " --- " . $totalReplacements . ' Replacements';




            ------------------------------------------------------------------------------------------
            PLEASE, PLEASE specify the version of MODX you are using.
            MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 11 years, 9 months ago.]
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting