You are here

class DomXMLFIReader in Feed Import 8

DomDocument XML reader

Hierarchy

Expanded class hierarchy of DomXMLFIReader

File

feed_import_base/src/DomXMLFIReader.php, line 7

Namespace

Drupal\feed_import_base
View source
class DomXMLFIReader extends FeedImportDomXPathReader {

  // Number of items returned by parent xpath
  protected $totalItems = 0;

  // Current item.
  protected $currentItem = 0;

  /**
   * {@inheritdoc}
   */
  public function init() {
    if (empty($this->options['parent']) || empty($this->options['format'])) {
      return FALSE;
    }
    $this->options += array(
      'url' => NULL,
      'raw' => NULL,
      'options' => array(
        LIBXML_NOCDATA,
      ),
      'php_func' => NULL,
      'recover' => FALSE,
      'normalizeDocument' => FALSE,
      'strictErrorChecking' => FALSE,
      'preserveWhiteSpace' => FALSE,
      'resolveExternals' => FALSE,
      'stream' => NULL,
      'silence_load_errors' => FALSE,
    );
    $opts = 0;
    foreach ($this->options['options'] as $opt) {
      $opts |= $opt;
    }
    list($majorv, $minorv) = explode('.', phpversion());
    $htmlok = $majorv >= 5 && $minorv >= 4;
    $doc = new DOMDocument();
    $doc->strictErrorChecking = $this->options['strictErrorChecking'];
    $doc->preserveWhiteSpace = $this->options['preserveWhiteSpace'];
    $doc->resolveExternals = $this->options['resolveExternals'];
    $silence_load = $this->options['silence_load_errors'];
    $loaded = FALSE;
    if ($this->options['url']) {
      if ($ctx = $this
        ->getStreamContext($this->options['stream'])) {
        libxml_set_streams_context($ctx);
      }
      if ($this->options['format'] == 'html') {
        if ($htmlok) {
          $loaded = $silence_load ? @$doc
            ->loadHTMLFile($this->options['url'], $opts) : $doc
            ->loadHTMLFile($this->options['url'], $opts);
        }
        else {
          $loaded = $silence_load ? @$doc
            ->loadHTMLFile($this->options['url']) : $doc
            ->loadHTMLFile($this->options['url']);
        }
      }
      else {
        $loaded = $silence_load ? @$doc
          ->load($this->options['url'], $opts) : $doc
          ->load($this->options['url'], $opts);
      }
    }
    elseif ($this->options['raw']) {
      if ($this->options['format'] == 'html') {
        if ($htmlok) {
          $loaded = $silence_load ? @$doc
            ->loadHTML($this->options['raw'], $opts) : $doc
            ->loadHTML($this->options['raw'], $opts);
        }
        else {
          $loaded = $silence_load ? @$doc
            ->loadHTML($this->options['raw']) : $doc
            ->loadHTML($this->options['raw']);
        }
      }
      else {
        $loaded = $silence_load ? @$doc
          ->loadXML($this->options['raw'], $opts) : $doc
          ->loadXML($this->options['raw'], $opts);
      }
    }
    if (!$loaded) {
      return FALSE;
    }

    // Add X inclusions
    if ($opts & LIBXML_XINCLUDE) {
      $doc
        ->xinclude();
    }
    unset($this->options['raw'], $opts, $opt, $htmlok, $loaded, $e);
    $doc->recover = $this->options['recover'];
    if ($this->options['normalizeDocument']) {
      $doc
        ->normalizeDocument();
    }

    // Create xpath object.
    $this->xpath = new DOMXPath($doc);
    if ($this->options['php_func']) {
      if (!is_array($this->options['php_func'])) {
        $this->options['php_func'] = static::cleanLines($this->options['php_func']);
      }
      $this->xpath
        ->registerNamespace('php', 'http://php.net/xpath');
      $this->xpath
        ->registerPhpFunctions($this->options['php_func']);
    }

    // Check for namespace settings.
    if (!empty($this->options['namespaces'])) {
      if (!is_array($this->options['namespaces'])) {
        $this->options['namespaces'] = static::cleanLines($this->options['namespaces']);
      }
      foreach ($this->options['namespaces'] as $key => &$ns) {
        $ns = explode('=', $ns, 2);
        if (count($ns) == 2 && !empty($ns[0]) && !empty($ns[1])) {
          $this->xpath
            ->registerNamespace($ns[0], $ns[1]);
        }
      }

      // Not needed anymore.
      unset($this->options['namespaces'], $ns);
    }
    $this->items = $this->xpath
      ->query($this->options['parent']);
    if ($this->items === FALSE) {
      return FALSE;
    }
    $this->totalItems = $this->items->length;
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function get() {
    if ($this->currentItem < $this->totalItems) {
      return $this->items
        ->item($this->currentItem++);
    }
    unset($this->items);
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DomXMLFIReader::$currentItem protected property
DomXMLFIReader::$totalItems protected property
DomXMLFIReader::get public function This method returns the next available item or NULL if there are no items left. Overrides FeedImportReader::get
DomXMLFIReader::init public function Here you'll init your reader. Overrides FeedImportReader::init
FeedImportConfigurable::$options protected property
FeedImportConfigurable::cleanLines public static function Helper function to get lines of a string
FeedImportConfigurable::setOptions public function Sets options for this instance 4
FeedImportDomXPathReader::$xpath protected property
FeedImportDomXPathReader::$xpathRegisterNodeNS protected property
FeedImportDomXPathReader::map public function Returns a value mapped from obj by path. Overrides FeedImportReader::map
FeedImportReader::$items protected property
FeedImportReader::formatPath public function Override this to preprocess your paths before they are used in map(). 2
FeedImportReader::getStreamContext public function Returns a stream context
FeedImportReader::__construct final public function Constructor of reader. Constructor is final but you'll have to implement init() to init your reader.
FeedImportReader::__destruct public function Destructor. 2