You are here

public function WordPressBlog::__construct in WordPress Migrate 7

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

File

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

Class

WordPressBlog

Code

public function __construct($filename) {
  $this->filename = $filename;
  $row = db_select('wordpress_migrate', 'wm')
    ->fields('wm', array(
    'filename',
    'title',
    'blog_url',
    'uid',
  ))
    ->condition('filename', $filename)
    ->execute()
    ->fetchObject();
  if ($row) {
    $this->title = $row->title;
    $this->blog_url = $row->blog_url;
    $this->uid = $row->uid;
  }
  else {

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

    // Extract the blog url and title
    $xml = simplexml_load_file($this->filename);
    if (!$xml) {
      $message = t('Could not load WXR file - problems reported: ');
      foreach (libxml_get_errors() as $error) {
        $message .= $error->message . t(" at line !line\n", array(
          '!line' => $error->line,
        ));
      }
      throw new Exception($message);
    }

    // Validate that it really is a WXR file
    $wxr_version = $xml
      ->xpath('//channel/wp:wxr_version');
    if (!$wxr_version) {
      throw new Exception(t('The uploaded file is not a valid WordPress export'));
    }
    $title = (string) $xml->channel->title;
    $blog_url = $xml
      ->xpath('//channel/wp:base_blog_url');
    $this->blog_url = (string) $blog_url[0];

    // Keep only alphabetic characters
    $this->title = preg_replace('/[^A-Za-z]/', '', $title);
    if (!$this->title) {
      $this->title = preg_replace('/[^A-Za-z]/', '', $blog_url);
    }
    global $user;
    $this->uid = $user->uid;
    db_insert('wordpress_migrate')
      ->fields(array(
      'filename' => $filename,
      'title' => $this->title,
      'blog_url' => $this->blog_url,
      'uid' => $this->uid,
    ))
      ->execute();
    foreach ($this
      ->migrationClasses() as $class) {
      Migration::registerMigration($class, $this
        ->machineName($class), array(
        'filename' => $filename,
      ));
    }
  }
}