You are here

protected function ChunkedXMLFIReader::read in Feed Import 8

Populates the $items array with xml nodes.

1 call to ChunkedXMLFIReader::read()
ChunkedXMLFIReader::get in feed_import_base/src/ChunkedXMLFIReader.php
This method returns the next available item or NULL if there are no items left.

File

feed_import_base/src/ChunkedXMLFIReader.php, line 109

Class

ChunkedXMLFIReader
CunkedXML Reader class, used for huge XML files.

Namespace

Drupal\feed_import_base

Code

protected function read() {
  $substr =& $this->substr;

  // To the end of file.
  while (!feof($this->fh)) {

    // Append read content.
    if (!($this->content .= fread($this->fh, $this->size))) {
      continue;
    }

    // Loop until match some elements.
    while (TRUE) {

      // Tag open position.
      $openpos = strpos($this->content, $this->tagOpen);

      // Tag close position.
      $openposclose = $openpos + $this->tagLen + 1;
      if ($openpos === FALSE || !isset($this->content[$openposclose]) || !($closepos = strpos($this->content, $this->tagClose, $openposclose))) {
        if ($this->items) {

          // We have some items.
          break;
        }
        else {

          // Read more data.
          continue 2;
        }
      }
      elseif (isset($this->content[$openposclose]) && $this->content[$openposclose] != ' ' && $this->content[$openposclose] != '>') {

        // Remove data read so far to save memory.
        $this->content = $substr($this->content, $openposclose);

        // Not searched tag, keep looking.
        continue;
      }

      // We have data.
      $closepos += $this->tagCloseLen;

      // Create item.
      $item = $this->properties . $substr($this->content, $openpos, $closepos - $openpos);

      // Remove read data.
      $this->content = $substr($this->content, $closepos);

      // Create xml object.
      try {
        $item = simplexml_load_string($item, $this->sxclass, LIBXML_NOCDATA);
      } catch (Exception $e) {
        continue;
      }

      // Apply root.
      if ($item = $item
        ->xpath($this->root)) {

        // Add to items.
        $this->items[] = reset($item);
      }
      unset($item);
    }

    // We have part of items so just return OK.
    if ($this->items) {
      return TRUE;
    }
  }
  $this->content = '';
  return FALSE;
}