Created
September 12, 2019 06:57
-
-
Save GerB/badc893f7baf161162bbb416ec85c5d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Create topics for each line from a text file; reading 1 line for each topic | |
* Set file path, post text and config below | |
* | |
* You should be logged in with founder or administrator permissions to be able | |
* to use this script | |
* It is recommended to delete this script and the text file after use | |
* | |
* This script takes a long time when importing large text files, | |
* typical speed is 500 - 1000 rows per minute. | |
* DO NOT REFRESH OR INTERRUPT! | |
* | |
* @author Ger Bruinsma | |
* @date 2019-09-11 | |
* | |
*/ | |
// Path to source file. For ease of use, place it in the same folder as this script file | |
$file_path = 'test.txt'; | |
// Replace default topic text with your desired content. Write down als you would in a generic phpBB post form field | |
$post_text = 'This is a default post text. | |
You can use linebreaks | |
[b]and BBcode if desired and enabled[/b]. | |
'; | |
// General config | |
define('GB_POST_USER_ID', 2); // User id that posts message | |
define('GB_POST_FORUM_ID', 1); // Forum id to create topic in - Note that given user should have posting permissions in this forum | |
define('GB_POST_FORUM_NAME', 'Your Forum'); // Forum name to create topic in (used in notifications if enabled below) | |
define('GB_POST_TEXT_ADD_TITLE', 'prepend'); // Add the title to the post text (with linebreak between) Either 'prepend', 'append', 'none' | |
define('GB_ENABLE_BBCODE', true); // Enable BBcode in posts | |
define('GB_ENABLE_SMILIES', true); // Enable smilies in posts | |
define('GB_ENABLE_URLS', true); // Enable urls in posts | |
define('GB_ENABLE_SIG', true); // Enable signature in posts | |
define('GB_ENABLE_INDEXING', true); // Enable indexing | |
/* ******************************************************************* | |
* * * ===== DO NOT CHANGE ANYTHING BELOW THIS LINE ===== ! ! ! * * * | |
* *******************************************************************/ | |
// Fire up phpBB | |
define('IN_PHPBB', true); | |
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './'; | |
$phpEx = substr(strrchr(__FILE__, '.'), 1); | |
include($phpbb_root_path . 'common.' . $phpEx); | |
include_once($phpbb_root_path . 'includes/functions_content.' . $phpEx); | |
include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx); | |
// Start session management | |
$user->session_begin(); | |
$auth->acl($user->data); | |
/* If ANONYMOUS = login box */ | |
if ((int) $user->data['user_id'] == ANONYMOUS) { | |
login_box(request_var('redirect', '')); | |
} | |
/* Limit this to founders or admins */ | |
if ((int) $user->data['user_type'] !== USER_FOUNDER || !$auth->acl_get('a_')) { | |
trigger_error('You don\'t have permission to alter the database. You need to be logged in as a founder or administrator.'); | |
} | |
// Pose as given user | |
$original_user_id = $user->data['user_id']; | |
switch_user(GB_POST_USER_ID); | |
// Read the file | |
$tmpNameSrc = '_tmpSrc_' . $file_path; | |
$tmpNameHandle = '_tmpDst_' . $file_path; | |
copy($phpbb_root_path . $file_path, $phpbb_root_path . $tmpNameSrc); | |
$handle = fopen($phpbb_root_path . $tmpNameSrc, 'r'); | |
if ($handle) { | |
// First part: make sure we get proper line endings | |
$remain = ''; | |
while (($cont = fgets($handle, 1000000)) !== false) { | |
set_time_limit(30); | |
// Detect line ending | |
if (stripos($cont, "\r\n")!== false) { | |
$eol = "\r\n"; | |
} elseif (stripos($cont, "\r")!== false) { | |
$eol = "\r"; | |
} elseif (stripos($cont, "\n")!== false) { | |
$eol = "\r"; | |
} else { | |
$eol = PHP_EOL; | |
} | |
$lines = explode($eol, $cont); | |
$lines[0] = $remain . $lines[0]; | |
$remain = array_pop($lines); | |
append_source_file($lines, $phpbb_root_path . $tmpNameHandle); | |
} | |
// also add the latest line | |
$lastLine = [$remain]; | |
append_source_file($lastLine, $phpbb_root_path . $tmpNameHandle); | |
fclose($handle); | |
} else { | |
// error opening the file. | |
trigger_error('Source file not found'); | |
} | |
// Now we should be able to open the file at hand and know what to expect | |
$handle = fopen($phpbb_root_path . $tmpNameHandle, 'r'); | |
$success = $error = 0; | |
if ($handle) { | |
while (($title = fgets($handle)) !== false) { | |
set_time_limit(30); | |
if (post_message(trim($title), $post_text)) { | |
$success++; | |
} else { | |
$error++; | |
} | |
} | |
fclose($handle); | |
} else { | |
// error opening the file. | |
trigger_error('Handle file not found'); | |
} | |
// Revert back to original user | |
switch_user($original_user_id); | |
// Unlink temporary files | |
unlink($phpbb_root_path . $tmpNameSrc); | |
unlink($phpbb_root_path . $tmpNameHandle); | |
// And be done with it | |
trigger_error('The import script is finished. <br>' . $success . ' posts succesfully created, ' . $error . ' errors.<br><b>Remember to delete this script!</b>'); | |
/* Functions below */ | |
/** | |
* Write the source file | |
* @param array $lines | |
* @param string $filepath | |
* @return void | |
*/ | |
function append_source_file($lines, $filepath) | |
{ | |
foreach ($lines as $line) | |
{ | |
file_put_contents($filepath, trim($line) . PHP_EOL, FILE_APPEND); | |
} | |
} | |
/** | |
* Post the message | |
* @global obje $user | |
* @param string $title | |
* @param string $post_text | |
* @return bool | |
*/ | |
function post_message($title, $post_text) { | |
global $user; | |
switch (GB_POST_TEXT_ADD_TITLE) { | |
case 'append': | |
$text = $post_text . "\n\n" . $title; | |
break; | |
case 'prepend': | |
$text = $title . "\n\n" . $post_text; | |
break; | |
case 'none': | |
default: | |
$text = $post_text; | |
break; | |
} | |
// Prep posting | |
$poll = $uid = $bitfield = $options = ''; | |
$allow_bbcode = GB_ENABLE_BBCODE; | |
$allow_urls = GB_ENABLE_URLS; | |
$allow_smilies = GB_ENABLE_SMILIES; | |
generate_text_for_storage($text, $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies); | |
$data = array( | |
'forum_id' => GB_POST_FORUM_ID, | |
'topic_id' => 0, | |
'icon_id' => false, | |
'enable_bbcode' => GB_ENABLE_BBCODE, | |
'enable_smilies' => GB_ENABLE_SMILIES, | |
'enable_urls' => GB_ENABLE_URLS, | |
'enable_sig' => GB_ENABLE_SIG, | |
'message' => $text, | |
'message_md5' => md5($text), | |
'bbcode_bitfield' => $bitfield, | |
'bbcode_uid' => $uid, | |
'post_edit_locked' => 0, | |
'topic_title' => $title, | |
'notify_set' => true, | |
'notify' => true, | |
'post_time' => 0, | |
'forum_name' => GB_POST_FORUM_NAME, | |
'enable_indexing' => GB_ENABLE_INDEXING, | |
); | |
return submit_post('post', $title, $user->data['username'], POST_NORMAL, $poll, $data); | |
} | |
/** | |
* Switch to the given user | |
* @param int $new_user_id | |
* @return bool | |
*/ | |
function switch_user($new_user_id) { | |
global $user, $db, $auth; | |
if ($user->data['user_id'] == $new_user_id) { | |
return true; | |
} | |
$sql = 'SELECT * | |
FROM ' . USERS_TABLE . ' | |
WHERE user_id = ' . (int) $new_user_id; | |
$result = $db->sql_query($sql); | |
$row = $db->sql_fetchrow($result); | |
$db->sql_freeresult($result); | |
$row['is_registered'] = true; | |
$user->data = array_merge($user->data, $row); | |
$user->timezone = $row['user_timezone']; | |
$auth->acl($user->data); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment