You are here

public function WordPressBlog::__construct in WordPress Migrate 7.2

Same name and namespace in other branches
  1. 7 wordpress.inc \WordPressBlog::__construct()

File

./wordpress.inc, line 190
Implementation of migration from WordPress into Drupal

Class

WordPressBlog

Code

public function __construct($filename, $arguments = array()) {
  $this->filename = $filename;
  $this->arguments = $arguments;

  // Make sure the upload directory is properly protected
  file_create_htaccess('wordpress://', TRUE);

  // Suppress errors during parsing, so we can pick them up after
  libxml_use_internal_errors(TRUE);

  // Get the blog_url, which is our unique determiner of which blog we're
  // talking about
  $title = '';
  $reader = new XMLReader();
  $status = $reader
    ->open($this->filename);
  if ($status) {
    $this->blog_url = '';
    while ($reader
      ->read()) {
      if ($reader->nodeType == XMLREADER::ELEMENT) {
        switch ($reader->name) {
          case 'title':
            $title = WordPressBlog::readString($reader);
            $this->displayTitle = $title;
            break;
          case 'wp:wxr_version':
            $this->wxrVersion = WordPressBlog::readString($reader);
            break;
          case 'wp:base_blog_url':
            $this->blog_url = WordPressBlog::readString($reader);
            break;
          case 'link':
            $this->link = WordPressBlog::readString($reader);

            // Catch only the first link
            if (empty($this->link)) {
              $this->link = $reader
                ->readString();
            }
            break;
        }
      }
      if (!empty($title) && !empty($this->blog_url) && !empty($this->link)) {
        break;
      }
    }
  }
  else {
    throw new Exception(t('Could not open XML file !url', array(
      '!url' => $this->filename,
    )));
  }

  // Validate that it really is a WXR file
  if (empty($this->blog_url)) {

    // Older WP versions did not have a blog_url but used link instead.
    if (!empty($this->link)) {
      $this->blog_url = $this->link;
    }
    else {
      throw new Exception(t('The uploaded file is not a valid WordPress export'));
    }
  }

  // Keep only alphabetic characters
  $this->title = preg_replace('/[^A-Za-z]/', '', $title);
  if (!$this->title) {
    $this->title = preg_replace('/[^A-Za-z]/', '', $this->blog_url);
  }
  global $user;
  $this->uid = $user->uid;
  $status = db_merge('wordpress_migrate')
    ->key(array(
    'blog_url' => $this->blog_url,
  ))
    ->fields(array(
    'title' => $this->title,
    'uid' => $this->uid,
    'link' => $this->link,
    'filename' => $this->filename,
    'wxr_version' => $this->wxrVersion,
  ))
    ->execute();
  $this->blogID = db_select('wordpress_migrate', 'wm')
    ->fields('wm', array(
    'blog_id',
  ))
    ->condition('blog_url', $this->blog_url)
    ->execute()
    ->fetchField();
}