You are here

public function DomXMLFIReader::init in Feed Import 8

Here you'll init your reader.

Overrides FeedImportReader::init

File

feed_import_base/src/DomXMLFIReader.php, line 17

Class

DomXMLFIReader
DomDocument XML reader

Namespace

Drupal\feed_import_base

Code

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;
}