hangar-wp-theme/hangar/phptest.php.backup

123 lines
4.3 KiB
Plaintext
Raw Permalink Normal View History

2021-02-18 14:09:56 +01:00
<?php
/*
Template Name: PHP Test Template
*/
?>
<?php get_header(); ?>
<div id="content" class="narrowcolumn">
<h2>Hangar Import Drupal -> Wordpress</h2>
<?php
//get post from Drupal
$USERNAME = "hangar-old";
$PASSWORD = "cONt31DVig";
$DBNAME = "hangar_old";
$wpdb_drupal = new wpdb($USERNAME, $PASSWORD, $DBNAME);
$wpdb_drupal->show_errors();
$sql = "SELECT * FROM node WHERE type='story' ORDER BY nid";
$nodes = $wpdb_drupal->get_results($sql);
//print_r($nids);
//echo count($nodes)."<br>";
$i=0;
foreach ($nodes as $node) {
//echo $nid->nid."<br>";
//get content of node
$sql2 = "SELECT * FROM node_revisions WHERE nid=".$node->nid;
$node_content = $wpdb_drupal->get_row($sql2);
//get language information
$sql3 = "SELECT * FROM localizernode WHERE nid=".$node->nid;
$node_lang = $wpdb_drupal->get_row($sql3);
if ($node->nid == $node_lang->pid){
//new base node -> create node object
$n = new stdClass;
$n->nid = $node->nid;
$n->date = date('Y-m-d H:i:s',$node->created);
$n->uid = $node->uid;
}
else {
//translated node: $node->nid == $node->pid;
$n = $langs[$node_lang->pid];
}
//get language version
switch ($node_lang->locale) {
case 'ca':
$n->title_ca = $node->title;
$n->teaser_ca = $node_content->teaser;
$n->body_ca = $node_content->body;
break;
case 'en':
$n->title_en = $node->title;
$n->teaser_en = $node_content->teaser;
$n->body_en = $node_content->body;
break;
case 'es':
$n->title_es = $node->title;
$n->teaser_es = $node_content->teaser;
$n->body_es = $node_content->body;
break;
}
//-> save in associative array
$langs[$n->nid] = $n;
//echo "new node:".$n->nid."<br>";
//echo "- [".$n->nid."] ".$n->title_ca." - created:".$n->date." - user:[".$n->uid."] <strong>[".$node_lang->locale.']</strong> '.$node_lang->pid.'<br>'.$n->teaser_ca.'<br>'; //.$n->body.'<br>';
//limit to few rows (for testing)
//$i++;
//if ($i == 500) break;
}
//print (or save) nodes
foreach ($langs as $node) {
if ($node->nid != ''){
echo "<strong>[".$node->nid."]</strong> - created:".$node->date." - user:[".$node->uid."]<br>";
echo "ca:".$node->title_ca."<br>"; //.$node->teaser_ca.'<br>';
echo "en:".$node->title_en."<br>"; //.$node->teaser_en.'<br>';
echo "es:".$node->title_es."<br>"; //.$node->teaser_es.'<br>'; //.$node->body_ca.'<br>';
$node->title = '<!--:ca-->'.$node->title_ca.'<!--:--><!--:en-->'.$node->title_en.'<!--:--><!--:es-->'.$node->title_es.'<!--:-->';
$node->teaser = '<!--:ca-->'.$node->teaser_ca.'<!--:--><!--:en-->'.$node->teaser_en.'<!--:--><!--:es-->'.$node->teaser_es.'<!--:-->';
$node->content = '<!--:ca-->'.$node->body_ca.'<!--:--><!--:en-->'.$node->body_en.'<!--:--><!--:es-->'.$node->body_es.'<!--:-->';
//replace image route
$node->teaser = str_replace("/drupal/sites/hangar.org/files", "http://hangar.org/webnou/wp-content/uploads/drupal", $node->teaser);
$node->teaser = str_replace('<img ', '<img class="attachment-thumbnail wp-post-image" width="100%" height="100%" ', $node->teaser);
$node->content = str_replace("/drupal/sites/hangar.org/files", "http://hangar.org/webnou/wp-content/uploads/drupal", $node->content);
//insert posts into wordpress
$post = array(
'comment_status' => 'closed', // 'closed' means no comments.
'ping_status' => 'closed', // 'closed' means pingbacks or trackbacks turned off
'post_author' => 11, //The user ID number of the author: 11=gerald
'post_category' => array(767), //Add some categories: 767=arxiu
'post_excerpt' => $node->teaser, //For all your post excerpt needs.
'post_date' => $node->date, //The time post was made.
'post_content' => $node->content, //The full text of the post.
'post_name' => $node->title_ca, // The name (slug) for your post
'post_status' => 'publish', //Set the status of the new post.
'post_title' => $node->title, //The title of your post.
'post_type' => 'post', //You may want to insert a regular post, page, etc.
'tags_input' => 'arxiu, drupal' //For tags.
);
//write to WP
//if ($node->nid == 1190 || $node->nid == 1293 || $node->nid == 1301)
//echo "SAVED WITH ID:".wp_insert_post($post)."<br><br>";
}
}
?>
</div>
<?php get_footer(); ?>