View source
<?php
namespace Drupal\disqus\Plugin\migrate\destination;
use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DisqusComment extends DestinationBase implements ContainerFactoryPluginInterface {
protected $logger;
protected $config;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, LoggerInterface $logger, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
$this->logger = $logger;
$this->config = $config_factory
->get('disqus.settings');
}
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('logger.factory')
->get('disqus'), $container
->get('config.factory'));
}
public function fields(MigrationInterface $migration = NULL) {
return [
'disqus_id' => $this
->t('The disqus ID'),
'message' => $this
->t('The comment body.'),
'parent' => $this
->t('Parent comment ID. If set to null, this comment is not a reply to an existing comment.'),
'identifier' => $this
->t('The disqus identifier to look up the correct thread.'),
'title' => $this
->t("The title of the comment's thread page."),
'author_email' => $this
->t("The comments author's email."),
'author_name' => $this
->t("The comments author's name."),
'author_url' => $this
->t("The comments author's url."),
'date' => $this
->t('The time that the comment was posted as a Unix timestamp.'),
'ip_address' => $this
->t("The IP address that the comment was posted from."),
];
}
public function getIds() {
$ids['disqus_id']['type'] = 'string';
return $ids;
}
public function import(Row $row, array $old_destination_id_values = []) {
$identifier = $row
->getDestinationProperty('identifier');
$disqus = disqus_api();
if ($disqus) {
try {
$thread = $disqus->threads
->details([
'forum' => $this->config
->get('disqus_domain'),
'thread:ident' => $identifier,
'thread' => '1',
]);
} catch (\Exception $exception) {
$this->logger
->error('Error loading thread details for entity : @identifier. Check your API keys.', [
'@identifier' => $identifier,
]);
$thread = NULL;
}
if (!isset($thread->id)) {
try {
$thread = $disqus->threads
->create([
'forum' => $this->config
->get('disqus_domain'),
'access_token' => $this->config
->get('advanced.disqus_useraccesstoken'),
'title' => $row
->getDestinationProperty('title'),
'identifier' => $identifier,
]);
} catch (\Exception $exception) {
$this->logger
->error('Error creating thread for entity : @identifier. Check your user access token.', [
'@identifier' => $identifier,
]);
}
}
try {
$message = $row
->getDestinationProperty('message');
$author_name = $row
->getDestinationProperty('author_name');
$author_email = $row
->getDestinationProperty('author_email');
$author_url = $row
->getDestinationProperty('author_url');
$date = $row
->getDestinationProperty('date');
$ip_address = $row
->getDestinationProperty('ip_address');
$ids = FALSE;
if (empty($author_name) || empty($author_email)) {
$ids = [
'disqus_id' => $disqus->posts
->create([
'message' => $message,
'thread' => $thread->id,
'access_token' => $this->config
->get('advanced.disqus_useraccesstoken'),
'date' => $date,
'ip_address' => $ip_address,
])->id,
];
}
else {
$ids = [
'disqus_id' => $disqus->posts
->create([
'thread' => $thread->id,
'message' => $message,
'author_name' => $author_name,
'author_email' => $author_email,
'author_url' => $author_url,
'api_key' => $this->config
->get('advanced.disqus_publickey'),
]),
];
}
return $ids;
} catch (\Exception $exception) {
$this->logger
->error('Error creating post on thread @thread, error: @error', [
'@thread' => $thread->id,
'@error' => $exception
->getMessage(),
]);
}
return FALSE;
}
}
public function rollback(array $destination_identifier) {
$disqus = disqus_api();
if ($disqus) {
try {
$post = $disqus->posts
->details([
'post' => $destination_identifier['disqus_id'],
]);
} catch (\Exception $exception) {
$this->logger
->error('Error loading thread details for entity : @identifier. Check your API keys.', [
'@identifier' => $destination_identifier['disqus_id'],
]);
$post = NULL;
}
if (!isset($post->id)) {
$this->logger
->notice('Unable to find post: @identifier when trying to delete it. Maybe it was already deleted?', [
'@identifier' => $destination_identifier['disqus_id'],
]);
return;
}
try {
$disqus->posts
->remove([
'access_token' => $this->config
->get('advanced.disqus_useraccesstoken'),
'post' => $destination_identifier['disqus_id'],
]);
} catch (\Exception $exception) {
$this->logger
->error('Error deleting post @identifier.', [
'@identifier' => $destination_identifier['disqus_id'],
]);
}
return FALSE;
}
}
}