Recent Topics

1 Jan 23, 2009 22:01    

My b2evolution Version: Not Entered

Hello and thanks to B2 Evolution for this great software really enjoying it.

However ,I need to upload quite a bit of data. Is it possible to import a text file to create a post per line (subject + body + language + category)

Many thanks for your help in advance

Regards

Mark

2 Jan 25, 2009 01:35

That's not something b2evolution can do itself.

But you can import data directly into your database if you know about mySQL and understand what you're doing. You'd need to be very careful to make it in a format b2e will like. I don't know how to do this, but others on this forum might...

3 Jan 25, 2009 04:47

Try this
Create a new file hacks.php with the following content and put it in /config directory

if( isset($_GET['create_post']) )
{
	global $timestamp;
	
	load_class( 'items/model/_item.class.php' );
			   
	$post_title = 'Title';
	$post_content = 'Post content';
	$post_status = 'published';
	$post_locale = '#';					// Post locale en-US
	$author_user_ID = 1;				// Author ID
	$main_cat_ID = 6;					// Main cat ID
	$extra_cat_IDs = array();			// Extra cat IDs array(3,6,10,14)
	
	
	// Insert a post:
	$now = date('Y-m-d H:i:s',time());
	$edited_Item = & new Item();
	$edited_Item->insert( $author_user_ID, $post_title, $post_content, $now,
					 		$main_cat_ID, $extra_cat_IDs, $post_status, $post_locale );
}

Then you have to break your text file by lines with something like this explode( "\n", $content ), preg_match the text you want to put in (subject + body + language + category) and make foreach loop to add new posts.

4 Jan 26, 2009 11:50

Thanks for the reply with code, but I dont think I can do that. I sure want to but it would take too much spare brain power at the moment.

5 Jan 26, 2009 15:10

Can you post 2 lines from your text file, I'll try to make it.

6 Jan 27, 2009 06:36

Oh, thats really kind, thanks

Here would be my text file example

Title, Detail, Language, Category
Title1, Detail1, Language1, Category1

Regards

Mark

7 Jan 27, 2009 15:45

Is it separated with [comma space]? If your title or detail contains comma and space the script will break.

Title, Detail, Language, Category = Good
Title, with comma, Detail, more detail, Language, Category = Bad

So can you rebuild the file and either escape commas in title and details \, or use another separator like "Title ~|~ Details ~|~ Language"?

8 Jan 31, 2009 21:30

Here is the full script, you can change line endings and string separator

The text file should look like this

Title, Detail, en-US, 1
Title1, Detail1, ru-RU, 2
Title3, Detail3, en-US, 3

if( isset($_GET['create_posts']) )
{
    global $Messages, $timestamp;
	
	$Messages->clear('all');
	
	$file = dirname(__FILE__).'/file.txt';	// Path to text file
	$NL = "\n";								// New line
	$separator = ',';						// String separator
	$author_user_ID = 1;					// Author ID
	$post_status = 'published';				// Post status
	$extra_cat_IDs = array();				// Extra cat IDs array(3,6,10,14)
	
	$content = @file_get_contents($file);
	$parts = explode( $NL, $content );	
	if( empty($content) || empty($parts) ) die('Check the file <b>'.$file.'</b>');
	
	$ChapterCache = & get_Cache( 'ChapterCache' );
	load_class( 'items/model/_item.class.php' );	
	
	$items = array();
	foreach( $parts as $part ) $items[] = explode( $separator, $part );

	foreach( $items as $item )
	{
		$post_title		= trim($item[0]);		// Post title
		$post_content	= trim($item[1]);		// Post content
		$post_locale	= trim($item[2]);		// Post locale en-US
		$main_cat_ID	= trim($item[3]);		// Main cat ID		
		
		if( ($Chapter = & $ChapterCache->get_by_ID( $main_cat_ID, false, false )) === false )
		{
			$Messages->add( 'Requested category '.$main_cat_ID.' does not exist!<br/>Bad line: '.implode( ', ', $item ) );
			continue;
		}
		
		// Insert a post:
		$now = date('Y-m-d H:i:s',time());
		$edited_Item = & new Item();
		
		if( $ID = $edited_Item->insert( $author_user_ID, $post_title, $post_content,
					$now, $main_cat_ID, $extra_cat_IDs, $post_status, $post_locale ) )
		{
			$Messages->add( 'New post created: '.$ID, 'success' );
		}
		else
		{
			$Messages->add( 'Bad line: '.implode( ', ', $item ) );
		}
	}
}


Form is loading...