How to replace all the aliases

I am working on a site that has quite a bit of data imported by importx.

388 of the resources had commas in the page title, resulting in commas in the alias. After a great deal of editing I have discovered an issue with the commas in the aliases. (using importx for updating) I have since updated the excluded alias characters to include a comma so future resources will not have a comma in the alias.

I can go into each resource delete the alias and resave- that removes the comma. But I am on a time line that makes that unpractical, Is there a way to reset the aliases via a batch operation

Thanks for any help

Roh

The simplest solution is probably an SQL UPDATE statement in PhpMyAdmin that deletes the commas from the columns alias and uri in the database table modx_site_content.

Or maybe you could write a snippet that loops through all the resources and deletes the field alias. And then select ManageClear CacheRefresh URIs in the topmenu to regenerate the uris.

Something like this should work pretty fast

Put this tag on a page:

[[!FixAlias]]

Create a snippet called FixAlias with this code, then visit the page with the tag:

/* FixAlias snippet */
$docs = $modx->getCollection('modResource');
$output = "";
foreach ($docs as $doc) {
    $oldAlias = $doc->get('alias');
    if (strpos($oldAlias, ',') !== false) {
        $newAlias = str_replace(',', "", $oldAlias);
        $doc->set('alias', $newAlias);
        $output .= '<br>Changed ' . $oldAlias . ' to ' . $newAlias;
        $doc->save();
    }
}

return $output;

For a dry-run, you can just comment out the $doc->save() line, like this:

// $doc->save();

After a real run, refresh the URIs as suggested by halftrainedharry.

I am glad this site is on MODXCloud. I just took a snapshot and created two new clouds to test out the suggestions. Both Halftrainedharry’s and bobray’s ideas worked perfectly.

To help others I used MyPHPAdmin to run the following two queries to implement halftrainedharrys idea:

UPDATE `modx_site_content`
SET `uri` = replace( replace(`uri` , ',', ''), '"', '' );
UPDATE `modx_site_content`
SET `alias` = replace( replace(`alias` , ',', ''), '"', '' );

and worked like a charm.

bobray’s snippet was quick and easy to setup/run for those that do not have access to the mysql db.

Thanks for the help.

Roy

I’m glad it worked for you. :slight_smile: