Migrate module: migrating poll nodes

Migrate module: migrating poll nodes

Posted by stella on Thu, 2010-03-04 17:07 in

Currently the Migrate module doesn't support full migration of poll nodes. When the poll module is enabled, you can create a content set mapping for a poll node, but you are unable to set the poll status (active or closed) and, more importantly, you can't migrate the poll choices.

Using the hook_migrate_prepare_node() and hook_migrate_complete_node() hooks, I was able to migrate all choices for each node, along with all existing votes stored.

In the example below, I am migrating polls from a Joomla MySQL database and I am using a poll content set which handles the core node field mapping, including the question text.

<?php
/**
* Implements hook_migrate_prepare_node().
*/
function mymodule_migrate_prepare_node(&$node, $tblinfo, &$row) {
 
$errors = array();
 
// Ensure we only work on a particular content set.
 
if ($tblinfo->machine_name == "my_poll_content_set" && $node->type == "poll") {
   
db_set_active('joomla');  // Set active database to be the Joomla db.
   
$node->choice = array();
   
$node->active = $node->status// Set the poll active/closed state to be the same status as the node.
   
$choices = db_query("SELECT text, hits FROM {jos_poll_data} WHERE text != '' AND pollid = %d ORDER BY id", $row->id);
    while (
$choice = db_fetch_object($choices)) {
     
$node->choice[] = array(
       
'chtext' => $choice->text,
       
'chvotes' => $choice->hits,
      );
    }
   
db_set_active('default');  // Restore active database setting to be the Drupal db.
 
}
  return
$errors;
}
?>

The above implementation of hook_migrate_prepare_node() first checks that we're operating on a specific content set which is identified by the machine name. This is the unique name you enter when creating the content set mapping. It then switches to the Joomla database, and fetches the configured choices for the poll record currently being migrated. Finally we just populate the $node->choice array with this data, and the migration module takes care of the rest.

As the Joomla records I'm dealing with have no concept of being active or closed, I've manually set the poll status ($node->active) to be the same as the node status ($node->status). This may not apply to your own records, but if you don't set $node->active somehow, it will default to closed.

<?php
/**
* Implements hook_migrate_complete_node().
*/
function mymodule_migrate_complete_node(&$node, $tblinfo, &$row) {
 
$errors = array();
  if (
$tblinfo->machine_name == "my_poll_content_set" && $node->type == "poll") {
   
// Select across the two databases and match on poll id and choice text.
   
$votes = db_query("SELECT p.chorder, jv.date FROM {poll_choices} p, joomla.jos_poll_data jc, joomla.jos_poll_date jv WHERE jc.text = p.chtext AND jc.pollid = jv.poll_id AND jv.vote_id = jc.id AND p.nid = %d AND jc.pollid = %d", $node->nid, $row->id);
    while (
$vote = db_fetch_object($votes)) {
     
// We have no user id or hostname to identify source of vote.
      // However we need to store something in hostname field so record is unique - using
      // vote date here.
     
db_query("INSERT INTO {poll_votes} (nid, chorder, uid, hostname) VALUES (%d, %d, %d, '%s')", $node->nid, $vote->chorder, 0, $vote->date);
    }
  }
  return
$errors;
}
?>

hook_migrate_complete_node() is only needed if you have individual vote records to migrate in addition to the poll itself. Drupal expects to be able to store the poll id, poll choice id and details on the user who voted. If the user is anonymous, then Drupal will store the user's IP address instead. However in Joomla, no record of the user who voted is kept, but we do have an extra piece of information which is the date and time the vote was recorded. As Drupal's poll_votes table has a primary key which enforces one vote per user (or one vote per anonymous user per IP address), we need to make our vote entries unique somehow. To get around this, I've stored the vote date in the hostname field. I suppose this is a bit "hacky" but it allows the votes to be migrated at least.

The other thing to note about the hook_migrate_complete_node() implementation above is that in the hook_migrate_prepare_node() I didn't store any mapping of a poll choice sourceid to a destid. This means that when pulling out the votes, I needed to join across the two databases in my SELECT and match up the records based on the poll id and the choice text. Again, not ideal, but I wasn't sure how to manage this mapping while in the middle of processing a separate content set.

Collaboration, Features, anyone?

I still have to migrate a couple of Joomla sites too, and therefore wondering whether there are any plans to create specific migrate_* companion modules, potentially based on http://drupal.org/project/features (or not), to ease the migration from X to Drupal, i.e.

migrate_joomla10
migrate_joomla15
migrate_wordpressX
...

Any plans or ideas on that?

If this would exist as joint effort, I'd immediately try to migrate those sites + contribute.

Posted by sun (not verified) on Fri, 2010-03-05 03:12
Database Won't Query

I love this example. It's helped me out tremendously on other content types just seeing how it works. However, I'm completely stuck on the Poll migrate I'm attempting.

Using your code, I've managed to get everything to work except the database query. I have code that queries the database and works fine if i have it in a block. And I have static dummy results that work when i put them in the prepare node code.

For some reason, when I'm doing an import, it will not fetch the database query and let me use the results. It just bypasses it and leaves it blank. I'm putting my code below. I know you are in no way obligated to answer, but i just thought maybe you are someone else might see this and give me a point in the right direction...

<?php

function migrate_image_migrate_prepare_node(&$node, $tblinfo, $row) {

   
$errors = array();
   
    if (
$tblinfo->machine_name == "jx" && $node->type == "poll") {
       
$node->choice = array();
       
//$node->active = $node->status;  // Set the poll active/closed state to be the same status as the node.

       
$choices = db_query("
            SELECT
            ANSWER01, ANSWER02, ANSWER03, ANSWER04, ANSWER05, ANSWER06, ANSWER07, ANSWER08, ANSWER09, ANSWER10,
            ANSWER01TOTAL, ANSWER02TOTAL, ANSWER03TOTAL, ANSWER04TOTAL, ANSWER05TOTAL, ANSWER06TOTAL, ANSWER07TOTAL, ANSWER08TOTAL, ANSWER09TOTAL, ANSWER10TOTAL
            FROM aa_poller 
            WHERE ID = %d
            ORDER BY ID
        "
, $row->id);
       
        while (
$choice = db_fetch_object($choices)) {
           
           
/*
           
            Query database to get all answers and counts.  Create an array that we can loop through below!
            Sort this out on separate page!
           
            */
           
   
           
$labelArray[0][0] = 'testee' /*$choice->ANSWER01*/;
           
$labelArray[0][1] = '34' /*$choice->ANSWER01TOTAL*/;
           
$labelArray[1][0] = 'testx' /*$choice->ANSWER02*/;
           
$labelArray[1][1] = '33' /*$choice->ANSWER02TOTAL*/;
           
$labelArray[2][0] = 'testdf' /*$choice->ANSWER03*/;
           
$labelArray[2][1] = '55' /*$choice->ANSWER03TOTAL*/;
           
$labelArray[3][0] = 'tessdft' /*$choice->ANSWER04*/;
           
$labelArray[3][1] = '32' /*$choice->ANSWER04TOTAL*/;
           
$labelArray[4][0] = 'tessdft' /*$choice->ANSWER05*/;
           
$labelArray[4][1] = '12' /*$choice->ANSWER05TOTAL*/;
           
$labelArray[5][0] = 'teasest' /*$choice->ANSWER06*/;
           
$labelArray[5][1] = '66' /*$choice->ANSWER06TOTAL*/;
           
$labelArray[6][0] = 'tehdfhst' /*$choice->ANSWER07*/;
           
$labelArray[6][1] = '78' /*$choice->ANSWER07TOTAL*/;
           
$labelArray[7][0] = 'tejhyst' /*$choice->ANSWER08*/;
           
$labelArray[7][1] = '23' /*$choice->ANSWER08TOTAL*/;
           
$labelArray[8][0] = '' /*$choice->ANSWER09*/;
           
$labelArray[8][1] = '5' /*$choice->ANSWER09TOTAL*/;
           
$labelArray[9][0] = '' /*$choice->ANSWER10*/;
           
$labelArray[9][1] = '6' /*$choice->ANSWER10TOTAL*/;
           
           
            for(
$z=0; $z<10; $z++) {
                if(
$labelArray[$z][0] != '') {
               
$node->choice[] = array('chtext' => $labelArray[$z][0], 'chvotes' => $labelArray[$z][1]);
                }
            }
           
           
//print_r($node->choice);
           
       
}
    }
   
    return
$errors;
   
}
?>

Posted by Justin (not verified) on Fri, 2010-08-27 20:54
set active database?

which database does your "aa_poller" table reside in? If it's not in the default drupal database, then you may need to configure your 2nd database in your settings.php file too, and use db_set_active() function to skip between them. See my implementation of hook_migrate_prepare_node() above for an example - note the db_set_active('default'); to return to the default drupal database when done.

Posted by stella on Sat, 2010-08-28 16:42
karen millen dresses outlet karen millen promotional code

Do you havesome Michael Kors If you are looking for elegance and sophistication
in the shoe,michael kors sale Michael Kors has the design for you.Shoes are considered an accessory because it helps to finish an outfit.Whether you are dressing up to go out with the evening or you are just relaxing over a walk,michael kors bag you will enjoy the designs of Michael kors.The Michael Kors collection works with several different designs and colors to masteryour comfort and style of each shoe he brings to the market.michael kors bag : http://ww.michaelkorsbagsale.com/ The basic colors of black.orange.burgundy.and brown are used so you can find the particular shoe
you want the best.The choices in footwear include sandals.pumps.wedge heels.boots.and more.You can find everything you need with the Michael Kors collection.Shoes that are in style for youMichael Kors has the shoe for any outfit and any occasion.Some of the shoes available that are the most versatile include a number of the following below.The McGraw leather boot.If you like your boots to go to your knees then this can be the right boot for you.You can not only walk taller but feel alluring too.This boot is made from a soft cognac leather so it will be durable and dependable for a long period.With this type of leather.the more you wear it.the softer it will feel.It features a five-inch platform heel that you can wear for everyday or just on an occasion.Every woman needs a pair of leopard print pumps for all unexpected nights out.The leopard print pumps feature a peep toe as well as a graffiti design.It has a five-inch stacked heel

long year acer trends show only 001 more acelenolysunci topics now pr12 in 2012

Posted by mrphcllpkn (not verified) on Fri, 2012-02-03 17:03