Advertisement
EclipseGc

Untitled

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