SHARE
TWEET

Untitled

a guest Feb 4th, 2016 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. // Original script by robrotheram from discuss.flarum.org
  3. // Modified by VIRUXE
  4.  
  5. set_time_limit(0);
  6. ini_set('memory_limit', -1);
  7.  
  8. ini_set("log_errors", 1);
  9. ini_set("error_log", "php-error.log");
  10.  
  11. // Configs for PHPBB database
  12. $servername = "localhost";
  13. $username = "root";
  14. $password = "";
  15. $exportDBName = "phpbb_name";
  16. $importDBName = "flarum_name";
  17.  
  18. // Establish a connection to the server where the PHPBB database exists
  19. $exportDbConnection = new mysqli($servername, $username, $password, $exportDBName);
  20. $importDbConnection = new mysqli($servername, $username, $password, $importDBName);
  21.  
  22. // Check connection
  23. if ($exportDbConnection->connect_error)
  24.     die("Export - Connection failed: " . $exportDbConnection->connect_error);
  25. else
  26. {
  27.     echo "Export - Connected successfully<br>";
  28.  
  29.     if(!$exportDbConnection->set_charset("utf8"))
  30.     {
  31.         printf("Error loading character set utf8: %s\n", $exportDbConnection->error);
  32.         exit();
  33.     }
  34.     else
  35.         printf("Current character set: %s\n", $exportDbConnection->character_set_name());
  36. }
  37.  
  38. if ($importDbConnection->connect_error)
  39.     die("Import - Connection failed: " . $importDbConnection->connect_error);
  40. else
  41. {
  42.     echo "Import - Connected successfully<br>";
  43.  
  44.     if(!$importDbConnection->set_charset("utf8"))
  45.     {
  46.         printf("Error loading character set utf8: %s\n", $importDbConnection->error);
  47.         exit();
  48.     }
  49.     else
  50.         printf("Current character set: %s\n", $importDbConnection->character_set_name());
  51. }
  52.  
  53. //Convert Users
  54. $sqlScript = fopen('flarum_users.sql', 'w');
  55.  
  56. echo "<hr>Step 1 - Users<hr>";
  57. $result = $exportDbConnection->query("SELECT user_id, from_unixtime(user_regdate) as user_regdate, username_clean, user_email FROM phpbb_users");
  58. $totalUsers = $result->num_rows;
  59. if ($totalUsers)
  60. {
  61.     fwrite($sqlScript, "INSERT INTO users (id, username, email, password, join_time, is_activated) VALUES \n");
  62.  
  63.     $i = 0;
  64.     $usersIgnored = 0;
  65.     while($row = $result->fetch_assoc())
  66.     {
  67.         $i++;
  68.  
  69.         if($row["user_email"] != NULL)
  70.         {
  71.             $username = $row["username_clean"];
  72.             $usernameHasSpace = strpos($username, " ");
  73.  
  74.             if($usernameHasSpace > 0)
  75.                 $formatedUsername = str_replace(" ", NULL, $username);
  76.             else
  77.                 $formatedUsername = $username;
  78.  
  79.             $valuesStr = sprintf("\t(%d, '%s', '%s', '%s', '%s', 1)%s\n", $row["user_id"], $formatedUsername, $row["user_email"], sha1(md5(time())), $row['user_regdate'], $i == $totalUsers ? ";" : ",");
  80.             fwrite($sqlScript, $valuesStr);
  81.         }
  82.         else
  83.             $usersIgnored++;
  84.     }
  85.     echo $i-$usersIgnored . ' out of '. $totalUsers .' Total Users converted';
  86. }
  87. else
  88.     echo "Something went wrong.";
  89.  
  90. fclose($sqlScript);
  91.  
  92. //Convert Categories to Tags
  93. $sqlScript = fopen('flarum_tags.sql', 'w');
  94.  
  95. echo "<hr>Step 2 - Categories<hr>";
  96. $result = $exportDbConnection->query("SELECT forum_id, forum_name, forum_desc  FROM phpbb_forums");
  97. $totalCategories = $result->num_rows;
  98. if ($totalCategories)
  99. {
  100.     fwrite($sqlScript, "INSERT INTO tags (id, name, description, slug, color, position) VALUES \n");
  101.  
  102.     $i = 1;
  103.     while($row = $result->fetch_assoc())
  104.     {
  105.         $slug = slugify($row["forum_name"]);
  106.         $valuesStr = sprintf("\t(%d, '%s', '%s', '%s', '%s', %d)%s\n", $row["forum_id"], mysql_escape_mimic($row["forum_name"]), mysql_escape_mimic(strip_tags(stripBBCode($row["forum_desc"]))), mysql_escape_mimic($slug), rand_color(), $i, $i == $totalCategories ? ";" : ",");
  107.         fwrite($sqlScript, $valuesStr);
  108.  
  109.         $i++;
  110.     }
  111.     echo $totalCategories . ' Categories converted.';
  112. }
  113. else
  114.     echo "Something went wrong.";
  115.  
  116. fclose($sqlScript);
  117.  
  118.  
  119. // Convert Topics to Discussions
  120. $sqlScript_discussions = fopen('flarum_discussions.sql', 'w');
  121. $sqlScript_discussions_tags = fopen('flarum_discussions_tags.sql', 'w');
  122.  
  123. echo "<hr>Step 3 - Topics<hr>";
  124. $topicsQuery = $exportDbConnection->query("SELECT topic_id, topic_poster, forum_id, topic_title, topic_time FROM phpbb_topics ORDER BY topic_id DESC;");
  125. $topicCount = $topicsQuery->num_rows;
  126. if($topicCount)
  127. {
  128.     $curTopicCount = 0;
  129.     $insertString = "INSERT INTO posts (id, user_id, discussion_id, time, type, content) VALUES \n";
  130.  
  131.     fwrite($sqlScript_discussions, "INSERT INTO discussions (id, title, start_time, comments_count, participants_count, start_post_id, last_post_id, start_user_id, last_user_id, last_time) VALUES \n");
  132.     fwrite($sqlScript_discussions_tags, "INSERT INTO discussions_tags (discussion_id, tag_id) VALUES \n");
  133.    
  134.     //  Loop trough all PHPBB topics
  135.     while($topic = $topicsQuery->fetch_assoc())
  136.     {
  137.         //  Convert posts per topic
  138.         $participantsArr = [];
  139.         $lastPosterID = 0;
  140.  
  141.         $sqlQuery = sprintf("SELECT * FROM phpbb_posts WHERE topic_id = %d;", $topic["topic_id"]);
  142.         $postsQuery = $exportDbConnection->query($sqlQuery);
  143.         $postCount = $postsQuery->num_rows;
  144.        
  145.         if($postCount)
  146.         {
  147.             $curPost = 0;
  148.  
  149.             //fwrite($sqlScript_posts, $insertString);
  150.             while($post = $postsQuery->fetch_assoc())
  151.             {
  152.                 $curPost++;
  153.  
  154.                 $posterID = 0;
  155.                 $date = new DateTime();
  156.                 $date->setTimestamp($post["post_time"]);
  157.                 $postDate =  $date->format('Y-m-d H:i:s');
  158.                 $postText = formatText($exportDbConnection, $post['post_text']);
  159.  
  160.                 if(empty($post['post_username']))// If the post_username field has text it means it's a "ghost" post. Therefore we should set the poster id to 0 so Flarum knows it's an invalid user
  161.                 {
  162.                     $posterID = $post['poster_id'];
  163.  
  164.                     // Add to the array only if unique
  165.                     if(!in_array($posterID, $participantsArr))
  166.                         $participantsArr[] = $posterID;
  167.                 }
  168.  
  169.                 if($curPost == $postCount)// Check if it's the last post in the discussion and save the poster id
  170.                     $lastPosterID = $posterID;
  171.  
  172.                 // Write post values to SQL Script
  173.                 //fwrite($sqlScript_posts, sprintf("\t(%d, %d, %d, '%s', 'comment', '%s')%s\n", $post['post_id'], $posterID, $topic['topic_id'], $postDate, $postText, $curPost != $postCount ? "," : ";"));
  174.  
  175.                 // Execute the insert query in the desired database.
  176.                 $formattedValuesStr = sprintf("(%d, %d, %d, '%s', 'comment', '%s');", $post['post_id'], $posterID, $topic['topic_id'], $postDate, $postText);
  177.                 $importDbConnection->query($insertString . $formattedValuesStr);
  178.             }
  179.         }
  180.         //else
  181.         //  echo "<br>Topic ". $topic['topic_id'] ." has zero posts.<br>";
  182.  
  183.         //  Convert topic to Flarum format
  184.         //
  185.         //  This needs to be done at the end because we need to get the post count first
  186.         //     
  187.         $date = new DateTime();
  188.         $date->setTimestamp($topic["topic_time"]);
  189.         $discussionDate = $date->format('Y-m-d H:i:s');
  190.         $topicTitle = formatText($exportDbConnection, $topic["topic_title"]);
  191.  
  192.         // Link Discussion/Topic to a Tag/Category
  193.         fwrite($sqlScript_discussions_tags, sprintf("\t(%d, %d),\n", $topic["topic_id"], $topic["forum_id"]));
  194.  
  195.         // Check for parent forums
  196.         $parentForum = $exportDbConnection->query("SELECT parent_id FROM phpbb_forums WHERE forum_id = " . $topic["forum_id"]);
  197.         $result = $parentForum->fetch_assoc();
  198.         if($result['parent_id'] > 0)
  199.             fwrite($sqlScript_discussions_tags, sprintf("\t(%d, %d),\n", $topic["topic_id"], $result['parent_id']));
  200.  
  201.         if($lastPosterID == 0)// Just to make sure it displays an actual username if the topic doesn't have posts? Not sure about this.
  202.             $lastPosterID = $topic["topic_poster"];
  203.  
  204.         // id, title, start_time, comments_count, participants_count, start_post_id, last_post_id, start_user_id, last_user_id, last_time
  205.         $valuesStr = sprintf("\t(%d, '%s', '%s', %d, %d, %d, %d, %d, %d, '%s'),\n", $topic["topic_id"], $topicTitle, $discussionDate, $postCount, count($participantsArr), 1, 1, $topic["topic_poster"], $lastPosterID, $discussionDate);
  206.        
  207.         fwrite($sqlScript_discussions, $valuesStr);
  208.     }
  209. }
  210.  
  211. //fclose($sqlScript_posts);
  212. fclose($sqlScript_discussions_tags);
  213. fclose($sqlScript_discussions);
  214.  
  215. // Convert user posted topics to user discussions?
  216. $sqlScript = fopen('flarum_user_discussions.sql', 'w');
  217. echo "<hr>Last Step - User Discussions<hr/>";
  218. $result = $exportDbConnection->query("SELECT user_id, topic_id FROM phpbb_topics_posted");
  219. if ($result->num_rows > 0)
  220. {
  221.     fwrite($sqlScript, "INSERT INTO users_discussions (user_id, discussion_id) VALUES ");
  222.  
  223.     while($row = $result->fetch_assoc())
  224.         fwrite($sqlScript, "(".($row["user_id"]).", ".($row["topic_id"])."),");
  225.  
  226.     echo "Success";
  227. }
  228. else
  229.     echo "Table is empty";
  230.  
  231. // Close connection to the database
  232. $exportDbConnection->close();
  233. $importDbConnection->close();
  234.  
  235. // Functions
  236. function print_r2($val)
  237. {
  238.     echo '<pre>';
  239.     print_r($val);
  240.     echo  '</pre>';
  241. }
  242.  
  243. function slugify($text)
  244. {
  245.     $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
  246.     $text = trim($text, '-');
  247.     $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  248.     $text = strtolower($text);
  249.     $text = preg_replace('~[^-\w]+~', '', $text);
  250.  
  251.     if (empty($text))
  252.         return 'n-a';
  253.  
  254.     return $text;
  255. }
  256.  
  257. function rand_color()
  258. {
  259.     return '#' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT);
  260. }
  261.  
  262. function mysql_escape_mimic($inp)
  263. {
  264.     if(is_array($inp))
  265.             return array_map(__METHOD__, $inp);
  266.  
  267.     if(!empty($inp) && is_string($inp)) {
  268.             return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $inp);
  269.     }
  270.     return $inp;
  271. }
  272.  
  273. // Formats PHPBB's text to Flarum's text format
  274. function formatText($connection, $text)
  275. {
  276.     $text = preg_replace('#\:\w+#', '', $text);
  277.     $text = convertBBCodeToHTML($text);
  278.     $text = str_replace(""","\"",$text);
  279.     $text = preg_replace('|[[\/\!]*?[^\[\]]*?]|si', '', $text);
  280.     $text = trimSmilies($text);
  281.  
  282.     // Wrap text lines with paragraph tags
  283.     $explodedText = explode("\n", $text);
  284.     foreach ($explodedText as $key => $value)
  285.     {
  286.         if(strlen($value) > 1)// Only wrap in a paragraph tag if the line has actual text
  287.             $explodedText[$key] = '<p>' . $value . '</p>';
  288.     }
  289.     $text = implode("\n", $explodedText);
  290.  
  291.     $wrapTag = strpos($text, '>') > 0 ? "r" : "t";// Posts with quotes need to be 'richtext'
  292.     $text = sprintf('<%s>%s</%s>', $wrapTag, $text, $wrapTag);
  293.  
  294.     return $connection->real_escape_string($text);
  295. }
  296.  
  297. // Used to convert Categories to Tags
  298. function stripBBCode($text_to_search) {
  299.     $pattern = '|[[\/\!]*?[^\[\]]*?]|si';
  300.     $replace = '';
  301.     return preg_replace($pattern, $replace, $text_to_search);
  302. }
  303.  
  304. function convertBBCodeToHTML($bbcode)
  305. {
  306.     $bbcode = preg_replace('#\[b](.+)\[\/b]#', "<b>$1</b>", $bbcode);
  307.     $bbcode = preg_replace('#\[i](.+)\[\/i]#', "<i>$1</i>", $bbcode);
  308.     $bbcode = preg_replace('#\[u](.+)\[\/u]#', "<u>$1</u>", $bbcode);
  309.     $bbcode = preg_replace('#\[img](.+?)\[\/img]#is', "<img src='$1'\>", $bbcode);
  310.     $bbcode = preg_replace('#\[quote=(.+?)](.+?)\[\/quote]#is', "<QUOTE><i>></i>$2</QUOTE>", $bbcode);
  311.     $bbcode = preg_replace('#\[code:\w+](.+?)\[\/code:\w+]#is', "<CODE>$1<CODE>", $bbcode);
  312.     $bbcode = preg_replace('#\[\*](.+?)\[\/\*]#is', "<li>$1</li>", $bbcode);
  313.     $bbcode = preg_replace('#\[color=\#\w+](.+?)\[\/color]#is', "$1", $bbcode);
  314.     $bbcode = preg_replace('#\[url=(.+?)](.+?)\[\/url]#is', "<a href='$1'>$2</a>", $bbcode);
  315.     $bbcode = preg_replace('#\[url](.+?)\[\/url]#is', "<a href='$1'>$1</a>", $bbcode);
  316.     $bbcode = preg_replace('#\[list](.+?)\[\/list]#is', "<ul>$1</ul>", $bbcode);
  317.     $bbcode = preg_replace('#\[size=200](.+?)\[\/size]#is', "<h1>$1</h1>", $bbcode);
  318.     $bbcode = preg_replace('#\[size=170](.+?)\[\/size]#is', "<h2>$1</h2>", $bbcode);
  319.     $bbcode = preg_replace('#\[size=150](.+?)\[\/size]#is', "<h3>$1</h3>", $bbcode);
  320.     $bbcode = preg_replace('#\[size=120](.+?)\[\/size]#is', "<h4>$1</h4>", $bbcode);
  321.     $bbcode = preg_replace('#\[size=85](.+?)\[\/size]#is', "<h5>$1</h5>", $bbcode);
  322.  
  323.     return $bbcode;
  324. }
  325.  
  326. function trimSmilies($postText)
  327. {
  328.     $startStr = "<!--";
  329.     $endStr = 'alt="';
  330.  
  331.     $startStr1 = '" title';
  332.     $endStr1 = " -->";
  333.  
  334.     $emoticonsCount = substr_count($postText, '<img src="{SMILIES_PATH}');
  335.  
  336.     for ($i=0; $i < $emoticonsCount; $i++)
  337.     {
  338.         $startPos = strpos($postText, $startStr);
  339.         $endPos = strpos($postText, $endStr);
  340.  
  341.         $postText = substr_replace($postText, NULL, $startPos, $endPos-$startPos+strlen($endStr));
  342.  
  343.         $startPos1 = strpos($postText, $startStr1);
  344.         $endPos1 = strpos($postText, $endStr1);
  345.  
  346.         $postText = substr_replace($postText, NULL, $startPos1, $endPos1-$startPos1+strlen($endStr1);
  347.     }
  348.  
  349.     return $postText;
  350. }
  351. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top