You are here

public static function ApacheSolrDocument::documentToXml in Apache Solr Search 6.3

Same name and namespace in other branches
  1. 8 Apache_Solr_Document.php \ApacheSolrDocument::documentToXml()
  2. 7 Apache_Solr_Document.php \ApacheSolrDocument::documentToXml()

Create an XML fragment from a ApacheSolrDocument instance appropriate for use inside a Solr add call

Return value

string

1 call to ApacheSolrDocument::documentToXml()
DrupalApacheSolrService::addDocuments in ./Drupal_Apache_Solr_Service.php
Add an array of Solr Documents to the index all at once

File

./Apache_Solr_Document.php, line 352

Class

ApacheSolrDocument
Holds Key / Value pairs that represent a Solr Document along with any associated boost values. Field values can be accessed by direct dereferencing such as: <code> ... $document->title = 'Something'; echo…

Code

public static function documentToXml(ApacheSolrDocument $document) {
  $xml = '<doc';
  if ($document
    ->getBoost() !== FALSE) {
    $xml .= ' boost="' . $document
      ->getBoost() . '"';
  }
  $xml .= '>';
  foreach ($document as $key => $value) {
    $key = htmlspecialchars($key, ENT_QUOTES, 'UTF-8');
    $fieldBoost = $document
      ->getFieldBoost($key);
    if (is_array($value)) {
      foreach ($value as $multivalue) {
        $xml .= '<field name="' . $key . '"';
        if ($fieldBoost !== FALSE) {
          $xml .= ' boost="' . $fieldBoost . '"';

          // Only set the boost for the first field in the set
          $fieldBoost = FALSE;
        }
        $xml .= '>' . htmlspecialchars($multivalue, ENT_NOQUOTES, 'UTF-8') . '</field>';
      }
    }
    else {
      $xml .= '<field name="' . $key . '"';
      if ($fieldBoost !== FALSE) {
        $xml .= ' boost="' . $fieldBoost . '"';
      }
      $xml .= '>' . htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8') . '</field>';
    }
  }
  $xml .= '</doc>';

  // Remove any control characters to avoid Solr XML parser exception
  return self::stripCtrlChars($xml);
}