PDA

View Full Version : SMF 2 Quote Cleaner Code



rsw686
Fri 29th May '09, 5:09pm
I wanted to share this with the community. I wrote this function to fix quotes in imported posts so they wouldn't show the unparsable SMF style quote code and the view post button next to the author's name would work. It uses the importpostid column to lookup the new postid. There are no guarantees on this, so make sure to test it out before doing the real import.

In the # Posts section of cleaner.php find

$text = str_replace(array_keys($replacer), $replacer, $post['pagetext']);Add after

$text = parse_quote($text);In the # PM text section of cleaner.php find

$text = str_replace(array_keys($replacer), $replacer, $pm['message']);Add after

$text = parse_quote($text);Place this at the bottom of cleaner.php


function parse_quote($post)
{
global $Db_target, $targettableprefix;

// no quote tag found, next post
if(($start = strpos($post, '[quote author=')) === false)
return $post;

// no end of tag found, next post
if(($end = strpos($post, ']', $start)) === false)
return $post;

// grab section to replace
$found = substr($post, $start, $end - $start + 1);
echo '<br /><br \>found: ' . $found . "<br \>\n";

$parts = explode(' ', $found);
for($i=0; $i<count($parts); $i++)
{
$part = $parts[$i];

if(($pos = strpos($part, 'author=')) !== false)
{
$filter['author'] = substr($part, $pos + 7);

while(strpos($parts[$i + 1], '=') === false && $i < count($parts))
{
$i++;
$filter['author'] .= ' ' . $parts[$i];
}

$filter['author'] = trim($filter['author']);
if($filter['author'][strlen($filter['author']) - 1] == ']')
$filter['author'] = substr($filter['author'], 0, strlen($filter['author']) - 1);

echo 'filtered: author: ' . $filter['author'] . "<br \>\n";
}

if(($pos = strpos($part, 'link=topic=')) !== false)
{
$tmp = explode('#', substr($part, $pos));
if(isset($post[1]))
{
$tmppost = substr($tmp[1], 3);
echo 'filtered: post: ' . $tmppost . "<br \>\n";

$request = $Db_target->query("
SELECT postid
FROM " . $targettableprefix . "post
WHERE importpostid = " . $tmppost);

if($row = $Db_target->fetch_array($request))
{
$filter['post'] = $row['postid'];
echo 'converted: post: ' . $filter['post'] . "<br \>\n";
}
}
}
}

if(isset($filter['author']) && isset($filter['post']))
{
$replace = '[quote=' . $filter['author'] . ';' . $filter['post'] . ']';
echo 'replacing: post: ' . $replace . "\n";
$post = str_replace($found, $replace, $post);
}
else if(isset($filter['author']))
{
$replace = '[quote=' . $filter['author'] . ']';
echo 'replacing: author: ' . $replace . "\n";
$post = str_replace($found, $replace, $post);
}
else
{
echo "replacing: generic\n";
$post = str_replace($found, '[quote]', $post);
}

return parse_quote($post);
}


If interested I also used the following for the replacer varaibles



// Set true or false as to the data you want to clean
$do_posts = true;
$do_sigs = true;
$do_thread_titles = false;
$do_pm_text = true;
$do_pm_text_title = false;




$replacer = array(
"" => "
",
"" => "",
"&quot;" => "\"",
"&amp;" => "&",

rsw686
Sun 14th Jun '09, 10:45am
I thought I would add an example to clarify what the code does.

After I imported SMF quotes in topics looked like the below. Basically vBulletin does not have BB code for the SMF quote style so it displays as unparsed.





My quote cleaner code formats them to vBulletin style and updates the post reference. By updating the post reference you can click the link to take you to the quoted posted.