Is this a module I would need to create, or is it an advanced action I need to create (haven't read into that yet, reading after this)?

The problem:
I have 'speaker' and 'agenda event' content types. Links to speakers are manually sprinkled in agenda event content. When a speaker name is updated, that speaker's url also changes. This happens at least once a day when a name spelling is corrected, etc. The person maintaining speakers and agenda has headaches as a result, and I want to remove them.

The goal:
Every time a speaker node is saved, I want to run a search and replace query on agenda (or all) node content, something like this:

UPDATE node_revisions SET body = REPLACE(body,'[!! get old url here somehow !!]','[!! get new url here somehow !!]');
UPDATE node_revisions SET teaser = REPLACE(teaser,'[!! get old url here somehow !!]','[!! get new url here somehow !!]');

Of course on top of calling the query, I need to figure out what the old and new urls were (url-safe vversion of the speaker 'name'/node title.

I don't expect all the answers, but any idea of where to start looking?

I appreciate the help. :)

EDIT:
This page has been helpful. I could create a module and put some action hooks in place. I've been wanting to learn module development, and it's starting to click together. Getting the arguments will still be a challenge. I'm still open to any thoughts.:
http://drupal.org/node/172152

Comments

dave reid’s picture

I would recommend using the Pathologic module and linking to the node ID url of the content (something http://yoursite.com/node/3462). The Pathologic module will link to the latest URL alias for internal links and helps prevent link breakage.

owntheweb’s picture

That is an option. However, I don't feel the editing staff will get it. heh. Making them copy the node id and placing it in their content will give them a new linking 'issue' to inquire about.

From my edit above, I'm open to create a module for learning purposes. The reading process has started. I welcome any additional feedback.

Worlds to explore. Worlds to create.
Blog: http://www.christopherstevens.cc/blog
Twitter: http://www.twitter.com/owntheweb

Anonymous’s picture

there's a straightforward way to do this using the hook mechanism in a custom module. If your module has defined a node type, you can use hook_update and hook_insert. For example, here's an example hook_update for a module called 'speaker' that defines your speaker node type. Something like this (splicing in your pseudo-code):

function speaker_update($node) {
  UPDATE node_revisions SET body = REPLACE(body,'[!! get old url here somehow !!]','[!! get new url here somehow !!]');
  UPDATE node_revisions SET teaser = REPLACE(teaser,'[!! get old url here somehow !!]','[!! get new url here somehow !!]');
}

but I wonder what you're really trying to do. What is the processing you want to do on the body--a preg_replace? What you might want to do is change the body of the node BEFORE hook_update is called, which you can do using hook_nodeapi with the 'presave' op. Here's a possibility (you'd need to specify the regex patterns for preg_match or use str_replace):

function speaker_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'presave':
      // Modify the body of the node.
      preg_replace('/pattern/','/replacement/', $node->body); 
      break;
    case 'insert':
    case 'update':
    case 'view':
  }
}

this way you change the node's body and let the node machinery do the actual saves into the database. THere's usually no need to manipulate the {node_revision} table directly, just use one of the hooks before the save/update is called.

owntheweb’s picture

Doug,
Thanks for the great response. It sheds light on things as I dig deeper into modules.

Here's the full story. My co-workers maintain the speakers here, and make reference to them where appropriate here with no specific presentation pattern. Whenever someone updates a speaker name, they have to go into the agenda and manually update the links to match the newly generated speaker url, not fool proof. There's an option that let's me keep the urls the same if the titles change, but that is not the preferred option (don't want misspelled urls)

I guess another option could be to generate permanent redirects every time a node is updated. That could work even better. Hmmm. In any case, I'm learning a lot! :D

Worlds to explore. Worlds to create.
Blog: http://www.christopherstevens.cc/blog
Twitter: http://www.twitter.com/owntheweb

Anonymous’s picture

i'd suggest looking deeper into the nodereference and token modules.