Jump to content

Import Posts with API


kreativmonkey
 Share

Recommended Posts

Hi,

i want to import stuff from Drupal to Processwire and have create an importscript but some things doesn't work.

I import a list of questions from users and i will manipulate the page created date (thats work fine) and change the User to the right one. If the User dosn't exist, i will creat a new one. But this is the first problem i have, no user will be change or create...

The second problem is to find out if the page does alrady exist or not. If the Page exist i will only modify some fields, otherwise i will create a new page.....

To find out if the Page exist and whats the Page ID do i also need to import the Comments.....

readCSV.php:

// Read the CSV file and get an array of rows
function readCSV($file){
	$lines = array();
	
	foreach(file($file, FILE_IGNORE_NEW_LINES) as $line){
		$lines[] = str_getcsv($line);
	}	

	return $lines;
}

import.php:

while($i < $countArray){
  // Initzalisierung der Variablen
	$title = $data[$i][0];
	$date = $data[$i][6];
	#$summary = '';
	$body = $data[$i][1];
	$categorys = $data[$i][4];
	$tags = $data[$i][3];
	#$images = '';
	$authorName = $data[$i][5];

	$sanitizTitle = $sanitizer->pageName($title);
	$post = $pages->get("template=$template, name=$sanitizTitle");
	if(empty($post)){
	  $p = new Page(); // create new page object
	  $p->template= $template; //set template post
	  $p->parent = wire('pages')->get('/$parent/'); //set the parent
	  $p->name = $sanitizTitle; // SANITIZE!!!!
	  $p->title = $title;
	  // added by Ryan: save page in preparation for adding files (#1)
	  $p->save();

	  $p->created = $date; // Timestamp!!!
		$p->save(array('quiet' => true)); // To save the created Date

		$p->body = $body;
	  // $p->summary = $summary;
	  // Looking for the inizialisation
	  $p->tags = $tags;
	  $p->categories = $categorys;

		$p->save();
		echo 'Created Page: '. $title .'<br>';
		$authorNameSanitiz = $sanitizer->name($authorName);

	  if($users->get($authorNameSanitiz)){
	    $authorID = $users->get($authorNameSanitiz);
			$p->createdUser->name = $authorID;
	  } else {
	    $u = new User();
	    $u->name = $authorNameSanitiz;
	    $u->addRole("author");
	    $u->addRole("guest");
	    $u->save();

			echo 'creat User: '. $authorName .'<br>';

			$authorID = $users->get($authorNameSanitiz);
	    $p->createdUser = $authorID;
	  }

	  $p->save();

	#}

  // populate fields
  //$p->images->add('path/to/image1.jpg'); // add multiple to images field
	$i++;
}

The CSV:

"Titel","Inhalt","E-Mail","Schlagworte","Thema","Benutzername","Beitragsdatum"
"digitale Filme in der Strahlentherapie","Hi,<br>zur Zeit suchen wir in unserer Abteilung für Strahlentherapie in Regensburg eine digitales Filmsystem.<br>Welche Firmen bieten den solche Systeme für die Strahlentherapie an?<br>MfG<br>Jan<br>","janmee@web.de","","Strahlentherapie","pete","1437587189"
"Gehalt MTA R","Hallo,<br>ich interessiere mich für den Beruf. Wo kann ich erfahren, was ich ausgelernt verdiene? Gibt es einen Tarifvertrag? In welchem ist man da wie eingruppiert? Vielen Dank<br> <br> <br> <br> <br> <br>","sweetmic@web.de","Gehalt","Radiologie","guest","1437303471"

what am i doing wrong?

Link to comment
Share on other sites

Quick response: 

For changing the user of a page try:

$p->created_users_id = $authorID;
$p->save(array('quiet' => true));

If you also want to set the modified user you may need to use some SQL, although I think quiet save works for this also in newer dev versions of PW:

$sql = "UPDATE `pages` SET `modified_users_id` = '".$authorID;."' WHERE `id` = '".$p->id."';";
$update = wire('db')->query($sql);

Keep in mind that if you find you need to use the SQL approach, it will need to be executed after the last saving of the page or it will be overwritten.

  • Like 1
Link to comment
Share on other sites

this line will not work I believe:

 $p->parent = wire('pages')->get('/$parent/'); //set the parent

'/$parent/' : if you use it with a variable as shown in your code, you must surround it with doublequotes: "/$parent/". Variables will not be populated within singlequotes.

------

Where is the code (line) where you check for existing records? Are there unique fields in your drupal data with the records that you can use?

steps to do:

1) read (next) record from drupal source

2) fetch and prepare data for a unique identifier from it

This can be a unique field within the drupal data, or, if this is not present, create a selector that may contain sanitized values for name / author / date or what is the best for your data.

3) do a get() with this selector

If the page exists, modify it where needed or move on to the next record.

If it do not exist, create it and populate fields.

  • Like 1
Link to comment
Share on other sites

Quick response: 

For changing the user of a page try:

$p->created_users_id = $authorID;
$p->save(array('quiet' => true));
....

Thanks, the first issue are solved!

The next Problem is to set the User i get following issue:

Notice: Object of class User could not be converted to int i

but the output of $authorID is a int ... 1370 or something else.

for testing i use this code:

$authorNameSanitiz = $sanitizer->name($authorName);
	$authorID = $users->get($authorNameSanitiz);
	if($authorID instanceof NullPage && $authorNameSanitiz != 'guest'){
		# Creat new User
		echo 'creat User: '. $authorName .'<br>';
	} elseif($authorNameSanitiz != 'guest') {
		echo "Existiert: " . $users->get($authorID)->name . " id= " . $authorID . "<br>";
	} else {
		echo "Gast <br>";
	}
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

×
×
  • Create New...