View source
<?php
namespace Drupal\node\Plugin\migrate\source\d6;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\State\StateInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Node extends DrupalSqlBase {
const JOIN = '[n].[vid] = [nr].[vid]';
protected $filterDefaultFormat;
protected $fieldInfo;
protected $moduleHandler;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityTypeManagerInterface $entity_type_manager, ModuleHandler $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_type_manager);
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('state'), $container
->get('entity_type.manager'), $container
->get('module_handler'));
}
public function query() {
$query = $this
->select('node_revisions', 'nr');
$query
->innerJoin('node', 'n', static::JOIN);
$this
->handleTranslations($query);
$query
->fields('n', [
'nid',
'type',
'language',
'status',
'created',
'changed',
'comment',
'promote',
'moderate',
'sticky',
'tnid',
'translate',
])
->fields('nr', [
'title',
'body',
'teaser',
'log',
'timestamp',
'format',
'vid',
]);
$query
->addField('n', 'uid', 'node_uid');
$query
->addField('nr', 'uid', 'revision_uid');
if ($this->moduleHandler
->moduleExists('content_translation')) {
$query
->leftJoin('node', 'nt', '[n].[tnid] = [nt].[nid]');
$query
->addField('nt', 'language', 'source_langcode');
}
if (isset($this->configuration['node_type'])) {
$query
->condition('n.type', (array) $this->configuration['node_type'], 'IN');
}
return $query;
}
protected function initializeIterator() {
$this->filterDefaultFormat = $this
->variableGet('filter_default_format', '1');
return parent::initializeIterator();
}
public function fields() {
$fields = [
'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);
}
}
if ($row
->getSourceProperty('tnid') == 0) {
$row
->setSourceProperty('tnid', $row
->getSourceProperty('nid'));
}
return parent::prepareRow($row);
}
protected function getFieldValues(Row $node) {
$values = [];
foreach ($this
->getFieldInfo($node
->getSourceProperty('type')) as $field => $info) {
$values[$field] = $this
->getFieldData($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 $this->fieldInfo[$node_type] ?? [];
}
protected function getFieldData(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']);
if (empty($columns)) {
return [];
}
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'))
->orderBy('delta')
->execute()
->fetchAllAssoc('delta');
}
else {
return [];
}
}
public function getIds() {
$ids['nid']['type'] = 'integer';
$ids['nid']['alias'] = 'n';
return $ids;
}
protected function handleTranslations(SelectInterface $query) {
if (empty($this->configuration['translations'])) {
$query
->where('[n].[tnid] = 0 OR [n].[tnid] = [n].[nid]');
}
else {
$query
->where('[n].[tnid] <> 0 AND [n].[tnid] <> [n].[nid]');
}
}
}