Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
653 views
in Q2A Core by

I want to make an auto-tag editor to clean up duplicate tags, so for example I could set all questions tagged "plugin" to be retagged "plugins".

If I run process_event on the q_post and q_edit events, and change the tags on the question, when the page finishes loading will the new tags be visible? Or is the template all loaded before the event runs?

1 Answer

+1 vote
by

Yes, they run immediately after the corresponding event happened, often in the functions in qa-app-post-create.php and qa-app-post-update.php.

In the case of what you want to do, you'll first need to change the tags in the database, but then also call qa_post_unindex() then qa_post_index(...) using the values passed to your event handler, to make sure everything else is updated.

by
Finally getting round to doing this. One more question: do the events run after the content has already been added to the database? If I have to reindex, it sounds like it. But this would mean on every post, double the number of queries might be running, e.g. a post is saved to the database then I run another query to edit the tags.

This could also pose problems if I want to delete nonsense tags like "i" or "how" because now a question might not have any tags at all. Not sure if there is a way round that though.
by
Also, assuming it's run after content is in the database, is there a method to re-save content? On q_post for example you get a $params array containing the question content. If you edit that array can you call a function to save it back to the database?
by
In general, the events run last, so yes, it's after the content has been posted to the database. For modifying database content, take a look at the functions in qa-app-posts.php - but note these will themselves throw events unless you call qa_suspend_event_reports(...) so be on the lookout for infinite loops.
by
edited by
Is this code correct? It still seems to be doing an infinite loop:

qa_suspend_event_reports(true);
require_once QA_INCLUDE_DIR.'qa-app-posts.php';
qa_post_set_content( $params['postid'], $params['title'], $params['content'], $params['format'], $tags );
qa_suspend_event_reports(false);

EDIT: Scratch that, I think MySQL was still in an infinite loop. Restarted Apache and it seems to be working now. Thanks for your help, Gideon!
...