82 $document =
new DOMDocument();
84 $oldLibXmlErrors = libxml_use_internal_errors(
true );
85 $oldDisable = libxml_disable_entity_loader(
true );
86 $ok = $document->loadXML( $xml, LIBXML_NONET );
89 $errors = libxml_get_errors();
90 libxml_use_internal_errors( $oldLibXmlErrors );
91 libxml_disable_entity_loader( $oldDisable );
93 foreach ( $errors as $error ) {
95 throw new InvalidArgumentException(
96 'Malformed XML: ' . $error->message .
' in line ' . $error->line
100 throw new InvalidArgumentException(
'Malformed XML!' );
103 libxml_use_internal_errors( $oldLibXmlErrors );
104 libxml_disable_entity_loader( $oldDisable );
126 $oldSites = $this->store->getSites();
128 $current = $root->firstChild;
130 if ( $current instanceof DOMElement && $current->tagName ===
'site' ) {
132 $site = $this->
makeSite( $current );
133 $key = $site->getGlobalId();
135 if ( $oldSites->hasSite( $key ) ) {
136 $oldSite = $oldSites->getSite( $key );
137 $site->setInternalId( $oldSite->getInternalId() );
140 $sites[$key] = $site;
141 }
catch ( Exception $ex ) {
146 $current = $current->nextSibling;
158 public function makeSite( DOMElement $siteElement ) {
159 if ( $siteElement->tagName !==
'site' ) {
160 throw new InvalidArgumentException(
'Expected <site> tag, found ' . $siteElement->tagName );
166 $site->setForward( $this->
hasChild( $siteElement,
'forward' ) );
167 $site->setGlobalId( $this->
getChildText( $siteElement,
'globalid' ) );
171 $pathTags = $siteElement->getElementsByTagName(
'path' );
172 for ( $i = 0; $i < $pathTags->length; $i++ ) {
173 $pathElement = $pathTags->item( $i );
174 '@phan-var DOMElement $pathElement';
176 $path = $pathElement->textContent;
178 $site->setPath( $pathType,
$path );
181 $idTags = $siteElement->getElementsByTagName(
'localid' );
182 for ( $i = 0; $i < $idTags->length; $i++ ) {
183 $idElement = $idTags->item( $i );
184 '@phan-var DOMElement $idElement';
186 $id = $idElement->textContent;
188 $site->addLocalId( $idType, $id );
229 private function getChildText( DOMElement $element, $name, $default =
false ) {
230 $elements = $element->getElementsByTagName( $name );
232 if ( $elements->length < 1 ) {
233 if ( $default !==
false ) {
237 'Required <' . $name .
'> tag not found inside <' . $element->tagName .
'> tag'
242 $node = $elements->item( 0 );
243 return $node->textContent;