You are here

public function WordPressCommentXMLReader::next in WordPress Migrate 7.2

Implementation of Iterator::next(). We need to preserve the ID of the parent element.

Return value

void

Overrides MigrateXMLReader::next

File

./wordpress_comment.inc, line 26
Support for migrating comments from a WordPress blog into Drupal.

Class

WordPressCommentXMLReader
Override of MigrateXMLReader, so we can track the parent posts for comments.

Code

public function next() {
  migrate_instrument_start('WordPressCommentXMLReader::next');
  $this->currentElement = $this->currentId = NULL;

  // Loop over each node in the XML file, looking for elements at a path
  // matching the input query string (represented in $this->elementsToMatch).
  while ($this->reader
    ->read()) {
    if ($this->reader->nodeType == XMLREADER::ELEMENT) {
      $this->currentPath[$this->reader->depth] = $this->reader->localName;

      // Save the last post_id, so comments can use it to find their parent
      if ($this->reader->name == 'wp:post_id') {
        $this->postId = WordPressBlog::readString($this->reader);
      }
      elseif ($this->reader->name == 'wp:post_type') {
        $this->currentPostType = WordPressBlog::readString($this->reader);
      }
      if ($this->currentPath == $this->elementsToMatch) {
        if ($this->postType != $this->currentPostType) {
          continue;
        }

        // We're positioned to the right element path - if filtering on an
        // attribute, check that as well before accepting this element.
        if (empty($this->attributeName) || $this->reader
          ->getAttribute($this->attributeName) == $this->attributeValue) {

          // We've found a matching element - get a SimpleXML object representing it.
          // We must associate the DOMNode with a DOMDocument to be able to import
          // it into SimpleXML.
          // Despite appearances, this is almost twice as fast as
          // simplexml_load_string($this->readOuterXML());
          $node = $this->reader
            ->expand();
          if ($node) {
            $dom = new DOMDocument();
            $node = $dom
              ->importNode($node, TRUE);
            $dom
              ->appendChild($node);
            $this->currentElement = simplexml_import_dom($node);
            if ($this->reader->name == 'wp:comment') {
              $this->currentElement->post_id = $this->postId;
            }
            $idnode = $this->currentElement
              ->xpath($this->idQuery);
            $this->currentId = (string) reset($idnode);
            break;
          }
          else {
            foreach (libxml_get_errors() as $error) {
              $error_string = MigrateItemsXML::parseLibXMLError($error);
              if ($migration = Migration::currentMigration()) {
                $migration
                  ->saveMessage($error_string);
              }
              else {
                Migration::displayMessage($error_string);
              }
            }
          }
        }
      }
    }
    elseif ($this->reader->nodeType == XMLREADER::END_ELEMENT) {

      // Trim currentPath as we exit each element
      unset($this->currentPath[$this->reader->depth]);
    }
  }
  migrate_instrument_stop('WordPressCommentXMLReader::next');
}