MyBB Community Forums

Full Version: Converting MyCode BBcode Tags in MySQL Tables?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
(2015-02-27, 03:16 PM)doylecc Wrote: [ -> ]
(2015-02-27, 01:27 PM)WebDevandPhoto Wrote: [ -> ]Thank you, yea but if I do this it will put close center on the wrong sections...

Yes, sorry. I forgot to consider the align closing tag is used for left, right and justify too.

Crazycat's suggestion of using a regex pattern is the better way here.
This should work in most editors though I only tested it with sublime text and geany:

search \[(align=(center|justify|left|right))\](.*)\[/(align)\]
replace: [\2]\3[/\2]

Did you have better success with this?
Just not sure why sublime is skipping over so many aligns with this expression.
Yes, I tested it with the posts table of one of my forums.
Though I had to run it twice or three times to get all replaced, since there were nested align tags (e.g. align right between center etc.) in several posts.
I'm not even sure if it will work and you should consider the quantity of posts in your board.
$query = $db->simple_select('posts', 'pid, message', "message LIKE '%[/align]%'", array('limit' => 100));
while($post = $db->fetch_array($query))
{
	while(preg_match("#\[align=(left|center|right|justify)\](.*?)\[/align\]#si", $post['message']))
	{
		$post['message'] = preg_replace("#\[align=(left|center|right|justify)\](.*?)\[/align\]#si", "[$1]$2[/$1]", $post['message']);
	}

	$db->update_query('posts', array('message' => $post['message']), "pid={$post['pid']}");
}
(2015-02-28, 04:48 AM)Omar G. Wrote: [ -> ]I'm not even sure if it will work and you should consider the quantity of posts in your board.

$query = $db->simple_select('posts', 'pid, message', "message LIKE '%[/align]%'");
while($post = $db->fetch_array($query))
{
	while(preg_match("#\[align=(left|center|right|justify)\](.*?)\[/align\]#si", $post['message']))
	{
		$post['message'] = preg_replace("#\[align=(left|center|right|justify)\](.*?)\[/align\]#si", "[$1]$2[/$1]", $post['message']);
	}

	$db->update_query('posts', array('message' => $post['message']), "pid={$post['pid']}");
}

I put this in a PHP file in the root of MyBB and set it as a cron job in webmin.
Didn't work. How else should I try to make it run properly? Thank you Omar, good to see you're a MyBB Staff member now! I'm sure you've been for a while, I've been away for a long time.

Update:
So I got it to work in Sublime Text, thank you everyone, it was hanging in Sublime Text from massive CPU spikes... and I have a new pc... lol. It took many many times to run through every single align pairs,  I lost count. I'm glad I wasn't successful doing this on the server, I'd imagine that it would have taken it down to a crippling crawl.



Thank you a TON guys! You saved my bacon!


Update2:
I wrote prematurely... it looked like it worked but actually made a huge mess of things so I reverted back and am now back to the drawing board.
Thanks.

But it worked for me. Make sure you included the core properly. I'm unsure if how cron jobs work but you can probably set a limit for the query.

Also note that even though the message is properly updated, the mybb editor seems to revert them back to stock MyCode for some reason. You can try quick editing such posts to see the updated message.

I have no idea why it is doing so.
Upate: Never mind Toungue
damn! I may have typed prematurely.. okay so it's doing something weird in both Sublime Text and Text Wrangler with the grep option... long story short it's not working as intended.
So I did a search through the original table rows and found 813 occurrences of the closing align tag.
Notice something.... that's an odd number... which is NOT good... so that means somewhere there's an opening align tag missing in a post.

So I did a search with this expression:
\[/align\](.*)\[/align\]

Which found 6 occurrences...
I know that's not the right expression to use because it's not supposed to count any chunk that has a single align open tag (or pair + the open tag) in the wild card area. Also weird it only found 6 occurrences when there's 813 closing tag occurrences.

So confused I went looking for grep expression generators online...
found this one http://regexr.com/
and started playing with the expression... 
and found that it may be lacking in logic that's needed.
For example -> open align | close align | close align.
The expression
\[align=(center|justify|left|right)\](.*)\[/align\]

Would highlight all 3 instead of skipping to a validly nested tag group.
The expression needs to find validly nested groups, replace those first, then I can search again and manually fix any invalidly nested group after. At which point work should be minimal.

Any ideas on how the logic can be beefed up? I'm researching but as I wrote earlier I totally suck at expressions.
I'm not sure Toungue Hope somebody can help you.
(2015-02-28, 07:15 AM)Omar G. Wrote: [ -> ]I'm not sure Toungue Hope somebody can help you.

Thanks Omar,
I'm studying this article http://caspar.bgsu.edu/~courses/Stats/La...vanced.htm
and the (? if then commands... there's got to be a way to do this.

OR.... it should work from the inside out with nested groups...

Basically only do what's highlighted in purple one by one here:
[attachment=33881]

I found the answer here!
The problem with .* is that it's greedy.
So... adding ? at the end of it makes it not greedy.
So:
\[align=(center|justify|left|right)\](.*?)\[/align\]

Using this new string found 808 occurrences..
which leaves somewhere around 5 tags unaccounted for... which are probably missing their opening or closing tag in a post but that is VERY manageable!

But trying the expression above with the replace command in Text Wrangler replaces the matched string with literally the replace command.. 
I also tried in Sublime Text, so either both these programs don't support expressions in the replace command (which I think it does) or the replace command won't work with the non greedy .*


If so then I need help figuring out what I need to use to work with the find expression for replace command. Any ideas?

Found answers! And within a few mins here
Basically, Sublime Text will work but only if not escaping the brackets... eg:
Search (grep):
\[align\=(left|center|right|justify)\](.*?)\[/align\]
Replace:
[$1]$2[/$1]

And wahla!! Works BEAUTIFULLY!

This does not work in TextWrangler and some online regex tools.
Pages: 1 2