EclipseGc

Untitled

Mar 29th, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.86 KB | None | 0 0
  1. abstract class NodesMigration extends Migration {
  2.   public function __construct(array $arguments) {
  3.     parent::__construct();
  4.     $type = isset($arguments['type']) ? $arguments['type'] : NULL;
  5.     $new_type = isset($arguments['new type']) ? $arguments['new type'] : $type;
  6.     $this->description = t('Migrate nodes');
  7.     $this->map = new MigrateSQLMap($this->machineName,
  8.       array(
  9.         'nid' => array(
  10.           'type' => 'int',
  11.           'unsigned' => TRUE,
  12.           'not null' => TRUE,
  13.           'description' => 'd6 Unique User ID',
  14.           'alias' => 'n',
  15.         )
  16.       ),
  17.       MigrateDestinationNode::getKeySchema()
  18.     );
  19.  
  20.     $query = $this->node_query($type);
  21.     $this->set_highwater_field();
  22.     $this->set_source($query);
  23.     $this->destination = new MigrateDestinationNode($new_type);
  24.     $this->node_field_mapping();
  25.   }
  26.  
  27.   public function set_highwater_field() {
  28.     $this->highwaterField = array(
  29.       'name' => 'changed',
  30.       'alias' => 'n',
  31.     );
  32.   }
  33.  
  34.   public function node_query($type) {
  35.     $query = Database::getConnection('d6')
  36.       ->select('node', 'n')
  37.       ->fields('n', array('nid', 'vid', 'title', 'uid', 'status', 'created', 'changed', 'comment', 'promote', 'moderate', 'sticky'))
  38.       ->condition('n.type', $type);
  39.     $query->join('node_revisions', 'nr', 'nr.vid = n.vid');
  40.     $query->fields('nr', array('body', 'teaser', 'format'));
  41.     return $query;
  42.   }
  43.  
  44.   public function set_source($query) {
  45.     $this->source = new MigrateSourceSQL($query);
  46.     $this->source->setMapJoinable(FALSE);
  47.   }
  48.  
  49.   public function node_field_mapping() {
  50.     $body_arguments = MigrateTextFieldHandler::arguments(array('source_field' => 'teaser'), array('source_field' => 'format'), NULL);
  51.     // Make the mappings
  52.     $this->addFieldMapping('nid', 'nid');
  53.     $this->addFieldMapping('tnid', 'nid');
  54.     $this->addFieldMapping('vid', 'vid');
  55.     $this->addFieldMapping('title', 'title');
  56.     $this->addFieldMapping('uid', 'uid');
  57.     $this->addFieldMapping('status', 'status');
  58.     $this->addFieldMapping('created', 'created');
  59.     $this->addFieldMapping('changed', 'changed');
  60.     $this->addFieldMapping('comment', 'comment');
  61.     $this->addFieldMapping('promote', 'promote');
  62.     $this->addFieldMapping('moderate', 'moderate');
  63.     $this->addFieldMapping('sticky', 'sticky');
  64.     $this->addFieldMapping('body', 'body')->arguments($body_arguments);
  65.     $this->addFieldMapping('language')->defaultValue('en');
  66.     $this->addFieldMapping('is_new')->defaultValue(TRUE);
  67.   }
  68.  
  69.   public function prepareRow($current_row) {
  70.     $formats = array(
  71.       '1' => 'filtered_html',
  72.       '2' => 'php_code',
  73.       '3' => 'full_html',
  74.     );
  75.     $current_row->format = isset($formats[$current_row->format]) ? $formats[$current_row->format] : 'plain_text';
  76.   }
  77. }
  78.  
  79. abstract class NodesBookMigration extends NodesMigration {
  80.   public function __construct() {
  81.     parent::__construct(array('type' => 'book'));
  82.   }
  83.  
  84.   public function node_query($type) {
  85.     $query = parent::node_query($type);
  86.     $query->join('book', 'b', 'n.nid = b.nid');
  87.     $query->fields('b', array('bid'));
  88.     $query->join('menu_links', 'ml', 'ml.mlid = b.mlid');
  89.     $query->fields('ml', array('mlid', 'plid'));
  90.     return $query;
  91.   }
  92.  
  93.   public function node_field_mapping() {
  94.     parent::node_field_mapping();
  95.     $this->addFieldMapping('book', 'book');
  96.   }
  97.  
  98.   public function complete($node, $row) {
  99.     $var = variable_get('book_mlids', array());
  100.     $query = Database::getConnection('d6')
  101.       ->select('menu_links', 'ml')
  102.       ->fields('ml');
  103.     $query->join('book', 'b', 'ml.mlid = b.mlid');
  104.     $query->fields('b', array('bid'));
  105.     $query->condition('b.nid', $node->nid);
  106.     $result = $query->execute()->fetchObject();
  107.     if ($result) {
  108.       $node->book['bid'] = $result->bid;
  109.       $node->book['nid'] = $node->nid;
  110.       $node->book['plid'] = isset($var[$result->menu_name][$result->plid]) ? $var[$result->menu_name][$result->plid] : $result->plid;
  111.       $node->book['menu_name'] = $result->menu_name;
  112.     }
  113.     _book_update_outline($node);
  114.     node_save($node);
  115.     $var[$result->menu_name][$result->mlid] = $node->book['mlid'];
  116.     variable_set('book_mlids', $var);
  117.   }
  118. }
  119.  
  120. class NodesBook102Migration extends NodesBookMigration {
  121.   public function node_query($type) {
  122.     $query = parent::node_query($type);
  123.     $query->condition('b.bid', 102);
  124.     $query->orderBy('FIELD(b.mlid,
  125.      1138,
  126.      277,
  127.      1343,
  128.      232,
  129.      1139,
  130.      1343,
  131.      1138,
  132.      1141,
  133.      1342,
  134.      1140
  135.    )', '');
  136.     return $query;
  137.   }
  138. }
  139.  
  140. class NodesBookNot102Migration extends NodesBookMigration {
  141.   public function node_query($type) {
  142.     $query = parent::node_query($type);
  143.     $query->condition('b.bid', 102, '<>');
  144.     $query->orderBy('b.bid')
  145.       ->orderBy('ml.plid')
  146.       ->orderBy('b.nid');
  147.     return $query;
  148.   }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment