Skip to content

Instantly share code, notes, and snippets.

@kode54
Last active Dec 26, 2015
Embed
What would you like to do?

This script is part of an import series, designed to remove Invision Board (v2.3.x)'s awful BBcode translation from the database and emit it as an SQL script capable of being imported into an ElkArte database. The amount of IP Board code which this requires to run is minimal, and seems to run fine on PHP 5.5. So, if you have a lingering forum on an old server, feel free to mysqldump the database, and feed the script files and database to a newer server.

<?php
define( 'IPB_THIS_SCRIPT', 'public' );
define( 'IPB_LOAD_SQL' , 'queries' );
require_once( '/var/www/path/to/ipb/init.php' );
require_once ROOT_PATH . "sources/ipsclass.php";
require_once KERNEL_PATH . "class_converge.php";
if ( file_exists( ROOT_PATH . "conf_global.php" ) )
{
require_once ROOT_PATH . "conf_global.php";
}
require_once ROOT_PATH . "sources/handlers/han_parse_bbcode.php";
/* Check if a string is valid UTF-8 */
function is_utf8($str) {
return (bool) preg_match('//u', $str);
}
/* This block of callbacks and function convert quotes from IP Board syntax
to ElkArte syntax */
function cb_fix_quotes_inner($matches)
{
$v = substr($matches[2], 1, -1);
switch ($matches[1])
{
case 'name':
return 'author=' . $v;
case 'post':
return 'link=msg=' . $v;
case 'date':
return 'date=' . strtotime( str_replace( '&#58;', ':', $v ) );
}
}
function cb_fix_quotes_outer($matches)
{
return preg_replace_callback(
"/([^= ]*)=('(?:\\\\.|[^'\\\\]+)*'|[^ ']*)/",
'cb_fix_quotes_inner',
$matches[0]
);
}
function fix_quotes($line)
{
return preg_replace_callback(
'|\[quote (.*)\]|',
'cb_fix_quotes_outer',
$line
);
}
function cb_fix_urls($matches)
{
return '[url=' . substr( $matches[1], 1, -1 ) . ']';
}
function fix_urls($line)
{
return preg_replace_callback(
'/\[url=("(?:\\\\.|[^"\\\\]+)*"|[^ "]*)\]/',
'cb_fix_urls',
$line
);
}
$ipsclass = new ipsclass();
$ipsclass->vars = $INFO;
$parser = new parse_bbcode();
$parser->ipsclass =& $ipsclass;
$parser->allow_update_caches = 0;
$parser->bypass_badwords = 1;
$parser->parse_bbcode = 1;
$parser->parse_nl2br = 1;
$ipsclass->init_db_connection();
$first_post = 0;
while (1)
{
$ipsclass->DB->build_query( array( 'select' => '*', 'from' => 'posts', 'limit' => array($first_post, '200') ) );
$ipsclass->DB->simple_exec();
if ( ! $ipsclass->DB->get_num_rows() ) break;
$posts = array();
while ( $row = $ipsclass->DB->fetch_row() )
{
$posts[] = $row;
}
$ipsclass->DB->free_result();
foreach ( $posts as $row )
{
$row['post_htmlstate'] = isset($row['post_htmlstate']) ? $row['post_htmlstate'] : 0;
$parser->parse_html = intval($row['post_htmlstate']);
$parser->parse_smilies = intval($row['use_emo']);
if ( $parser->parse_html )
{
$row['post'] = str_replace( "<#EMO_DIR#>", "&lt;#EMO_DIR&gt;", $row['post'] );
$row['post'] = preg_replace( "#(\s)?<([^>]+?)emoid=\"(.+?)\"([^>]*?)".">(\s)?#is", "\\1\\3\\5", $row['post'] );
$row['post'] = str_replace( "&lt;#EMO_DIR&gt;", "<#EMO_DIR#>", $row['post'] );
$row['post'] = $parser->convert_ipb_html_to_html( $row['post'] );
$row['post'] = htmlspecialchars( $row['post'] );
if ( $parser->parse_nl2br )
{
$row['post'] = str_replace( '&lt;br&gt;', "\n", $row['post'] );
$row['post'] = str_replace( '&lt;br /&gt;', "\n", $row['post'] );
}
}
$row['post'] = $parser->pre_edit_parse( $row['post'] );
$row['post'] = fix_quotes($row['post']);
$row['post'] = fix_urls($row['post']);
$row['post'] = str_replace(
array("\000", "\r", "\n" , "'" ),
array('' , '' , '<br>', '&#39;'),
$row['post']
);
echo 'UPDATE `elkarte_messages` SET `body` = \'' . $row['post'] . '\' WHERE `id_msg` = ' . intval($row['pid']) . ';' . "\n";
}
$first_post += 200;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment