Jump to content

Import comments


Metaphora
 Share

Recommended Posts

Hi,

I've never done it myself so I cannot present some pre-made solutions, but there seem to be discussions about it in the forum:

https://www.google.hu/search?as_sitesearch=processwire.com%2Ftalk&as_q=migrate#q=+Import+comments+site:processwire.com%2Ftalk

especially:

Also, there are various discussions about using an existing module to import vs implement your on solution. I also recommend checking out @adrian's 
Batch Child Editor which can also import data (note, that I do not know if you can use it for importing comments or not, but he will be able to tell us...):

 

  • Like 1
Link to comment
Share on other sites

BCE won't work for comments, but you can certainly write your own API script to import comments from another source. What format do you have them in?

The only gotcha you will come across is this bug (https://github.com/ryancramerdesign/ProcessWire/issues/1034) which prevents you from setting the status via the API, so currently you will need some SQL to fix that.

Let us know if you need any help.

  • Like 1
Link to comment
Share on other sites

The import modules I found (BCE and ImportPagesCSV) only work for pages.

This is just a private blog I want to migrate. Comments are stored in a CSV file. Since my coding skills are limited I'm not sure if I manage to write my own API script. This is why a code snippet to start from would be helpful.

Link to comment
Share on other sites

This is a start for you that comes from the Migrator module:

$p = $pages->get() // use this to get the page that you are importing comments to

foreach($comments as $comment){
    $c = new Comment();
    $p->of(false);
    $c->text = $comment['text'];
    $c->cite = $comment['cite'];
    $c->email = $comment['email'];
    $c->ip = $comment['ip'];
    $c->website = $comment['website'];
    $p->fieldname->add($c);
    $p->save();
    // setting the status doesn't current work due to this: https://github.com/ryancramerdesign/ProcessWire/issues/1034
    $c->status = $comment->status ? $comment->status : 0; //need to set after saving to allow setting status without being subject to moderation settings
    $p->save();
}

This assumes that you have already parsed the rows of the CSV file into the $comments array. To do that, take a look at: http://php.net/manual/en/function.str-getcsv.php

Hope that helps to get you going.

  • Like 4
Link to comment
Share on other sites

That was helpful, indeed! I changed the import file from csv to xml.

// K.P. See if the export file is where we expect it to be
if (file_exists('comments.xml')) {
     print_r('File exists :-)');
} else {
    exit('File not found :-(');
}

$file = 'comments.xml';
$doc = new DOMDocument();
$doc->load($file);

$posts = $doc->getElementsByTagName('table');

foreach($posts as $post) {

	/* K.P. Show all items */
	echo $post->getElementsByTagName('column')->item(0)->nodeValue;
	echo "<br>";
	echo $post->getElementsByTagName('column')->item(1)->nodeValue;
	echo "<br>";
	echo $post->getElementsByTagName('column')->item(2)->nodeValue;
    /* ... */
	echo "<hr>";

	$comment = array();

	$comment['id'] = $post->getElementsByTagName('column')->item(0)->nodeValue;
	$comment['thread'] = $post->getElementsByTagName('column')->item(1)->nodeValue;
	$comment['aid'] = $post->getElementsByTagName('column')->item(2)->nodeValue;
	$comment['date'] = $post->getElementsByTagName('column')->item(3)->nodeValue;
	$comment['ip'] = $post->getElementsByTagName('column')->item(4)->nodeValue;
	$comment['title'] = $post->getElementsByTagName('column')->item(5)->nodeValue;
	$comment['text'] = $post->getElementsByTagName('column')->item(6)->nodeValue;
	$comment['mail'] = $post->getElementsByTagName('column')->item(7)->nodeValue;
	$comment['name'] = $post->getElementsByTagName('column')->item(8)->nodeValue;
	$comment['url'] = $post->getElementsByTagName('column')->item(9)->nodeValue;


 	// K.P. the page the comment is imported to
	$p = $pages->get("xarid={$comment['aid']}");

    $c = new Comment();

    $p->of(false);

    $c->text = $comment['text'];
    $c->cite = $comment['name'];
    $c->email = $comment['mail'];
    $c->ip = $comment['ip'];
    $c->website = $comment['url'];
    $c->created = $comment['date'];
    $c->pages_id = $comment['aid'];
    $c->created_users_id = "40";

    $p->comments->add($c);
    $p->save('comments');

}

 

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

Anybody know if there's a way to set upvotes and downvotes via the API? I'm not using the voting features, but I'm importing a nested comment structure and thought I would temporarily use those fields to store legacy IDs. I tried the expected $c->upvotes = $value, both before and after save, but they don't get stored in the database.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...