You are here

public function WordPressItemMigration::prepareRow in WordPress Migrate 7.2

Same name and namespace in other branches
  1. 7 wordpress_item.inc \WordPressItemMigration::prepareRow()

Data manipulations to be performed before the migrate module applies mappings.

Parameters

stdClass $row:

Return value

string

Overrides Migration::prepareRow

File

./wordpress_item.inc, line 343
Support for migrating posts and pages from a WordPress blog into Drupal.

Class

WordPressItemMigration
Intermediate Migration class, implementing behavior common across different types (post_type) of items.

Code

public function prepareRow($row) {
  $wp_row = $row->xml
    ->children($this->arguments['namespaces']['wp']);
  $content_row = $row->xml
    ->children($this->arguments['namespaces']['content']);

  // Skip any of the wrong post type
  if ($wp_row->post_type != $this->postType) {
    $this->skippedItems[] = $row->{'wp:post_id'};
    return FALSE;
  }

  // Only publish those with wp:status == 'publish'
  if (isset($wp_row->status)) {
    switch ($wp_row->status) {
      case 'publish':
        $row->status = NODE_PUBLISHED;
        break;
      case 'trash':
        return FALSE;
      default:
        $row->status = NODE_NOT_PUBLISHED;
    }
  }
  else {
    $row->status = NODE_NOT_PUBLISHED;
  }

  // If incoming date is zero (indicates unpublished content), use the current time
  if ($wp_row->post_date == '0000-00-00 00:00:00') {
    $row->{'wp:post_date'} = time();
  }

  // If the link has a query string, don't produce a path
  $row->link = (string) $row->xml->link;
  if (strpos($row->link, '?')) {
    unset($row->link);
  }
  else {

    // Otherwise, strip the domain portion of the URL
    $matches = array();
    if (preg_match('|https?://[^/]+/(.*)|', $row->link, $matches)) {
      $row->link = $matches[1];

      // Strip the last slash off of the URL (the Path module can't handle this)
      $row->link = rtrim($row->link, '/');
    }
    else {
      unset($row->link);
    }
  }

  // Translate WordPress comment_status to Drupal values
  if (module_exists('comment')) {
    if ($wp_row->comment_status == 'open') {
      $row->{'wp:comment_status'} = COMMENT_NODE_OPEN;
    }
    else {
      $row->{'wp:comment_status'} = COMMENT_NODE_CLOSED;
    }
  }

  // Pull out teasers out based on <!--more--> tags
  $broken = explode('<!--more-->', $row->content);
  if (count($broken) == 2) {
    $row->excerpt = $broken[0];
  }
  $row->content = $content_row->encoded;

  // Interpret the [caption] tags
  $row->content = preg_replace_callback('|(\\[caption.*?\\])(.*?)(\\[/caption\\])|i', array(
    $this,
    'replaceCaptions',
  ), $row->content);

  // Handle [youtube] tags - convert them to <object> tags. If the media module is
  // installed, the next step will then convert them to media tags
  $replacement = '<object width="425" height="344">' . '<param name="movie" value="http://www.youtube.com/v/$1&#038;fs=1" />' . '<param name="allowFullScreen" value="true" />' . '<param name="allowscriptaccess" value="always" />' . '<embed src="http://www.youtube.com/v/$1&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344">' . '</embed></object>';

  // One form is [youtube dQw4w9WgXcQ]
  $row->content = preg_replace('|\\[youtube (.+?)\\]|i', $replacement, $row->content);

  // Another form is [youtube=https://www.youtube.com/watch?v=dQw4w9WgXcQ]
  $row->content = preg_replace('|\\[youtube=.+?=([a-z0-9_-]+)[^\\]]*\\]|i', $replacement, $row->content);

  // Rewrite embedded video references to media tags
  if (module_exists('media')) {
    $row->content = preg_replace_callback('|<object [^>]*>.*?(<embed [^>]*>).*?</object>|i', array(
      $this,
      'replaceEmbeds',
    ), $row->content);
  }

  // Rewrite (or remember to rewrite) links of the form
  // http://example.wordpress.com/?p=19 to local links of the form /node/35
  $row->content = $this
    ->fixLocalLinks($row->content);

  // Handle Embedit HTML embed
  if (isset($row->HTML1)) {
    $row->content = $this
      ->replaceHTMLEmbeds($row, $row->content);
  }

  // Replace Kimilli Flash Embed tags with <object>
  $row->content = preg_replace_callback('|\\[kml_flashembed *(.*?)/\\]|i', array(
    $this,
    'replaceFlashEmbeds',
  ), $row->content);

  // Remove [gallery] shortcode tags.
  $row->content = preg_replace('|\\[gallery (.+?)\\]|i', '', $row->content);
  $row->{'content:encoded'} = $row->content;
  return TRUE;
}