I'm migrating from Vivvo CMS, and it also counts nodes just like Drupal.

- nid
- totalcount
- daycount
- timestamp (time of latest view)

So Id like to request the feature, to add this as optional. Should be easy I think.

Comments

Anonymous’s picture

I added:

	  'totalcount' => t('Node: Total count'),
	  'daycount' => t('Node: Day count'),
	  'timestamp' => t('Node: Timestamp'),

to node.migrate.inc in node_migrate_fields_node, but I think node_save() doesn't save these stats?
How to programmatically set a node's daycount?

Anonymous’s picture

Found a workaround,

Add before timer_stop('node_save'):

db_query('INSERT INTO {node_counter} VALUES ("'.$node->nid.'", "'.$node->totalcount.'", "'.$node->daycount.'", "'.$node->timestamp.'")'); 

With code from #1

mikeryan’s picture

Version: 6.x-1.0 » 7.x-2.x-dev
StuartDH’s picture

How do we get this to work in v2?...migrating statistics for each node into D7's node_counter table

It looks like I need to add something like the following to migrate/plugins/destinations/nodes.inc

$fields['totalcount'] = t('Node: Total number of node views counter');
$fields['daycount'] = t('Node: Number of node views day count');
$fields['timestamp'] = t('Node: Timestamp');

but I don't know what to do wth #2 as node.migrate.inc isn't in v2 and timer_stop('node_save') doesn't appear to be in the migrate.module or any includes or plugins

Am I barking up the wrong tree? Should I be using a handler in a migration class to somehow expose the node_counter table to the node destination list, rather than hacking the module code?

mikeryan’s picture

Just ignore all references to the Migrate V1 code - the node migration in Migrate V2 is in node.inc, and that's where writing the counters would be implemented.

mototribe’s picture

hi Mike,
I need to implement this too. Do you have a patch or more detailed instructions of where to add the code.

If you roll back and reimport a node wouldn't you have to manually remove any orphaned values from node_counter? Or does Drupal do an automatic garbage collection?

thanks!

mikeryan’s picture

The node counters are managed by the core statistics module, thus we only want to deal with them when it is enabled. The clean way to do this is to implement a new statistics.inc, similar to path.inc - it would add the fields as StuartDH suggests in its fields() methods, then you would implement a complete() handler to write the values to the node_counter table.

xandeadx’s picture

+1 for feature

jordanmagnuson’s picture

I'd also love to know how to do this.

jordanmagnuson’s picture

Status: Needs review » Active

Here's a statistics.inc that works for me. Maybe others can offer feedback/improvement?

/**
 * @file
 * Support for node_counter statistics in core Drupal nodes
 */

class MigrateStatisticsEntityHandler extends MigrateDestinationHandler {
  public function __construct() {
    $this->registerTypes(array('node'));
  }

  public function fields() {
    if (module_exists('statistics')) {
      $fields = array(
        'totalcount' => t('Node: The total number of times the node has been viewed.'),
        'daycount' => t('Node: The total number of times the node has been viewed today.'),
        'timestamp' => t('Node: The most recent time the node has been viewed.'),
      );
    }
    else {
      $fields = array();
    }
    return $fields;
  }

  public function complete($entity, stdClass $row) {
    if (module_exists('statistics') && isset($entity->nid)) {
      $totalcount = $row->totalcount ? $row->totalcount : 0;
      $daycount = $row->daycount ? $row->daycount : 0;
      $timestamp = $row->timestamp ? $row->timestamp : 0;
      db_insert('node_counter')
        ->fields(array(
          'nid' => $entity->nid,
          'totalcount' => $totalcount,
          'daycount' => $daycount,
          'timestamp' => $timestamp,
        ))
        ->execute();
    }
  }
}
jordanmagnuson’s picture

Status: Active » Needs review
mikeryan’s picture

Status: Active » Needs review
Issue tags: +Migrate 2.4

OK, will review the pasted code for Migrate 2.4. In the future, though, please attach a proper patch.

Thanks.

mikeryan’s picture

Status: Needs review » Fixed

Committed to D6 and D7, with one key change - the values need to come from $node, not $row, to support migration from arbitrary sources (not just another Drupal installation).

Thanks.

jordanmagnuson’s picture

Ah, okay. Thanks Mike. And next time I will attempt to submit a real patch!

Status: Fixed » Closed (fixed)
Issue tags: -Migrate 2.4

Automatically closed -- issue fixed for 2 weeks with no activity.