You are here

class WordPressCommentXMLReader in WordPress Migrate 7.2

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

Hierarchy

Expanded class hierarchy of WordPressCommentXMLReader

File

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

View source
class WordPressCommentXMLReader extends MigrateXMLReader {

  /**
   * The last post ID we've seen - saved so we can apply it to comments.
   * @var int
   */
  protected $postId;

  /**
   * Implementation of Iterator::next(). We need to preserve the ID of
   * the parent element.
   *
   * @return void
   */
  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');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateXMLReader::$attributeName protected property If the element query is filtering by an attribute name=value, the name of the attribute in question.
MigrateXMLReader::$attributeValue protected property If the element query is filtering by an attribute name=value, the value of the attribute in question.
MigrateXMLReader::$currentElement protected property Current element object when iterating.
MigrateXMLReader::$currentId protected property Value of the ID for the current element when iterating.
MigrateXMLReader::$currentPath protected property Array representing the path to the current element as we traverse the XML. For example, if in an XML string like '<file><article>...</article></file>' we are positioned within the article element, currentPath will…
MigrateXMLReader::$elementQuery public property Query string used to retrieve the elements from the XML file.
MigrateXMLReader::$elementsToMatch protected property Array of the element names from the query, 0-based from the first (root) element. For example, '//file/article' would be stored as array(0 => 'file', 1 => 'article').
MigrateXMLReader::$idQuery public property Xpath query string used to retrieve the primary key value from each element.
MigrateXMLReader::$prefixedName protected property When matching element names, whether to compare to the namespace-prefixed name, or the local name.
MigrateXMLReader::$reader public property The XMLReader we are encapsulating.
MigrateXMLReader::$url public property URL of the source XML file.
MigrateXMLReader::current public function Implementation of Iterator::current().
MigrateXMLReader::key public function Implementation of Iterator::key().
MigrateXMLReader::rewind public function Implementation of Iterator::rewind().
MigrateXMLReader::valid public function Implementation of Iterator::valid().
MigrateXMLReader::__construct public function Prepares our extensions to the XMLReader object.
WordPressCommentXMLReader::$postId protected property The last post ID we've seen - saved so we can apply it to comments.
WordPressCommentXMLReader::next public function Implementation of Iterator::next(). We need to preserve the ID of the parent element. Overrides MigrateXMLReader::next