Skip to content

Instantly share code, notes, and snippets.

@rossriley
Created April 14, 2017 13:03
Show Gist options
  • Save rossriley/c1eceae2c43cea57c1acc9deccaa899e to your computer and use it in GitHub Desktop.
Save rossriley/c1eceae2c43cea57c1acc9deccaa899e to your computer and use it in GitHub Desktop.
<?php
namespace MyNewApp\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Bolt\Application;
class WordpressImport extends Command
{
public $app;
public function __construct(Application $app)
{
$this->app = $app;
$this->app['request'] = false;
parent::__construct();
}
protected function configure()
{
$this->setName('content:wordpress-import')
->setDescription('This command imports content from a WXR export file')
->setHelp('')
->addOption(
'file', 'f', InputOption::VALUE_REQUIRED, 'WXR File to import from', null
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$file = $input->getOption('file');
$xml = $this->parse($file);
$items = $this->getBlogItems($xml);
$this->importBlogItems($items, $output);
}
protected function importBlogItems($items, OutputInterface $output)
{
foreach ($items as $item) {
$output->writeln('Importing blog item: '.$item['item']->title);
$content = $this->app['storage']->getContent('blog', ['importedfrom'=>$item['item']->link, 'returnsingle' => true]);
if (!$content) {
$content = $this->app['storage']->getEmptyContent('blog');
}
$url = parse_url($item['item']->link, PHP_URL_PATH);
$url = str_replace('/blog/', '', $url);
$content->setValues([
'importedfrom' => $item['item']->link,
'title' => $item['item']->title,
'status' => 'published',
'datepublish' => date('Y-m-d H:i:s', strtotime($item['item']->pubDate)),
'body' => $this->nl2p( (string)$item['content']->encoded ),
'slug' => $url,
'teaser' => (string)$item['excerpt']->encoded,
'image' => $item['attachment'],
'ownerid' => $this->getAuthor($item)
]);
foreach ($item['item']->category as $cat) {
$content->setTaxonomy('categories', $cat->attributes()->nicename);
}
$this->app['storage']->saveContent($content);
}
}
protected function getBlogItems($xml)
{
$ns = $xml->getNamespaces(true);
$items = [];
foreach ($xml->channel->item as $item) {
$wp = $item->children($ns['wp']);
$dc = $item->children($ns['dc']);
$content = $item->children($ns['content']);
$excerpt = $item->children($ns['excerpt']);
if ( (string)$wp->post_type == 'post' &&
(string)$wp->status =='publish'
) {
$originalAttachment = $this->getAttachment($wp, $xml);
if ($originalAttachment) {
$image = $this->saveAttachment($originalAttachment);
}
$items[] = [
'item'=>$item,
'wp'=>$wp,
'dc'=>$dc,
'content'=>$content,
'excerpt'=>$excerpt,
'attachment'=> $image
];
}
}
return $items;
}
protected function getAttachment($item, $xml)
{
foreach ($item->postmeta as $meta) {
if((string)$meta->meta_key == '_thumbnail_id') {
$attachid = (string)$meta->meta_value;
if ($attachid) {
$ns = $xml->getNamespaces(true);
foreach ($xml->channel->item as $item) {
$wp = $item->children($ns['wp']);
if ( (string)$wp->post_type == 'attachment' &&
$wp->post_id == $attachid
) {
return $wp->attachment_url;
}
}
}
}
}
}
protected function saveAttachment($original, $newDir = 'blog/')
{
$relative = $newDir.basename($original);
$destination = $this->app['resources']->getPath('files') . "/" . $relative;
if (file_exists($destination)) {
return ['file'=>$relative];
} else {
@mkdir(dirname($destination), 0777, true);
$imgcontent = file_get_contents($original);
file_put_contents($destination, $imgcontent);
return ['file'=>$relative];
}
return false;
}
public function getAuthor($item)
{
$creator = (string)$item['dc']->creator;
$all = $this->app['users']->getUsers();
foreach ($all as $user) {
if ($user['username'] == $creator) {
return $user['id'];
}
}
return false;
}
protected function nl2p($string, $line_breaks = true, $xml = true)
{
$string = preg_replace("/(\<h[0-6]\>)/", "\n$1", $string);
$string = preg_replace("/(\<\/h[0-6]\>)/", "$1\n", $string);
$string = str_replace(array('<p>', '</p>', '<br>', '<br />'), '', $string);
$string = preg_replace("/\<h[3-6]\>\s?\<em\>(.*)\<\/em\>\<\/h[3-6]\>/", "<blockquote><p>$1</p></blockquote>", $string);
// It is conceivable that people might still want single line-breaks
// without breaking into a new paragraph.
if ($line_breaks == true) {
return '<p>' . preg_replace(array("/([\n]{2,})/i", "/([^>])\n([^<])/i"),
array("</p>\n<p>", '$1<br' . ($xml == true ? ' /' : '') . '>$2'), trim($string)) . '</p>';
} else {
return '<p>' . preg_replace(
array("/([\n]{2,})/i", "/([\r\n]{3,})/i", "/([^>])\n([^<])/i"),
array("</p>\n<p>", "</p>\n<p>", '$1<br' . ($xml == true ? ' /' : '') . '>$2'),
trim($string)) . '</p>';
}
}
protected function parse($file)
{
$xml = simplexml_load_file( $file );
return $xml;
}
}
@nlemoine
Copy link

Do I have to setup a local extension to use it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment