View source
<?php
namespace Drupal\node\Plugin\migrate\source\d6;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
class Node extends DrupalSqlBase {
const JOIN = 'n.vid = nr.vid';
protected $filterDefaultFormat;
protected $fieldInfo;
public function query() {
$query = $this
->select('node_revisions', 'nr')
->fields('n', array(
'nid',
'type',
'language',
'status',
'created',
'changed',
'comment',
'promote',
'moderate',
'sticky',
'tnid',
'translate',
))
->fields('nr', array(
'vid',
'title',
'body',
'teaser',
'log',
'timestamp',
'format',
));
$query
->addField('n', 'uid', 'node_uid');
$query
->addField('nr', 'uid', 'revision_uid');
$query
->innerJoin('node', 'n', static::JOIN);
if (isset($this->configuration['node_type'])) {
$query
->condition('type', $this->configuration['node_type']);
}
return $query;
}
protected function initializeIterator() {
$this->filterDefaultFormat = $this
->variableGet('filter_default_format', '1');
return parent::initializeIterator();
}
public function fields() {
$fields = array(
'nid' => $this
->t('Node ID'),
'type' => $this
->t('Type'),
'title' => $this
->t('Title'),
'body' => $this
->t('Body'),
'format' => $this
->t('Format'),
'teaser' => $this
->t('Teaser'),
'node_uid' => $this
->t('Node authored by (uid)'),
'revision_uid' => $this
->t('Revision authored by (uid)'),
'created' => $this
->t('Created timestamp'),
'changed' => $this
->t('Modified timestamp'),
'status' => $this
->t('Published'),
'promote' => $this
->t('Promoted to front page'),
'sticky' => $this
->t('Sticky at top of lists'),
'revision' => $this
->t('Create new revision'),
'language' => $this
->t('Language (fr, en, ...)'),
'tnid' => $this
->t('The translation set id for this node'),
'timestamp' => $this
->t('The timestamp the latest revision of this node was created.'),
);
return $fields;
}
public function prepareRow(Row $row) {
if ($row
->getSourceProperty('format') === '0') {
$row
->setSourceProperty('format', $this->filterDefaultFormat);
}
if ($this
->moduleExists('content') && $this
->getModuleSchemaVersion('content') >= 6001) {
foreach ($this
->getFieldValues($row) as $field => $values) {
$row
->setSourceProperty($field, $values);
}
}
return parent::prepareRow($row);
}
protected function getFieldValues(Row $node) {
$values = [];
foreach ($this
->getFieldInfo($node
->getSourceProperty('type')) as $field => $info) {
$values[$field] = $this
->getCckData($info, $node);
}
return $values;
}
protected function getFieldInfo($node_type) {
if (!isset($this->fieldInfo)) {
$this->fieldInfo = [];
$query = $this
->select('content_node_field_instance', 'cnfi');
$query
->join('content_node_field', 'cnf', 'cnf.field_name = cnfi.field_name');
$query
->fields('cnfi');
$query
->fields('cnf');
foreach ($query
->execute() as $field) {
$this->fieldInfo[$field['type_name']][$field['field_name']] = $field;
}
foreach ($this->fieldInfo as $type => $fields) {
foreach ($fields as $field => $info) {
foreach ($info as $property => $value) {
if ($property == 'db_columns' || preg_match('/_settings$/', $property)) {
$this->fieldInfo[$type][$field][$property] = unserialize($value);
}
}
}
}
}
return isset($this->fieldInfo[$node_type]) ? $this->fieldInfo[$node_type] : [];
}
protected function getCckData(array $field, Row $node) {
$field_table = 'content_' . $field['field_name'];
$node_table = 'content_type_' . $node
->getSourceProperty('type');
$db = $this
->getDatabase()
->schema();
if ($db
->tableExists($field_table)) {
$query = $this
->select($field_table, 't');
if ($db
->fieldExists($field_table, 'delta')) {
$query
->addField('t', 'delta');
}
else {
$query
->addExpression(0, 'delta');
}
}
elseif ($db
->tableExists($node_table)) {
$query = $this
->select($node_table, 't');
$query
->addExpression(0, 'delta');
}
if (isset($query)) {
$columns = array_keys($field['db_columns']);
foreach ($columns as $column) {
$query
->addField('t', $field['field_name'] . '_' . $column, $column);
}
return $query
->isNotNull($field['field_name'] . '_' . $columns[0])
->condition('nid', $node
->getSourceProperty('nid'))
->condition('vid', $node
->getSourceProperty('vid'))
->execute()
->fetchAllAssoc('delta');
}
else {
return [];
}
}
public function getIds() {
$ids['nid']['type'] = 'integer';
$ids['nid']['alias'] = 'n';
return $ids;
}
}