82 $document =
new DOMDocument();
84 $oldLibXmlErrors = libxml_use_internal_errors(
true );
86 $oldDisable = @libxml_disable_entity_loader(
true );
87 $ok = $document->loadXML( $xml, LIBXML_NONET );
90 $errors = libxml_get_errors();
91 libxml_use_internal_errors( $oldLibXmlErrors );
93 @libxml_disable_entity_loader( $oldDisable );
95 foreach ( $errors as $error ) {
97 throw new InvalidArgumentException(
98 'Malformed XML: ' . $error->message .
' in line ' . $error->line
102 throw new InvalidArgumentException(
'Malformed XML!' );
105 libxml_use_internal_errors( $oldLibXmlErrors );
107 @libxml_disable_entity_loader( $oldDisable );
129 $oldSites = $this->store->getSites();
131 $current = $root->firstChild;
133 if ( $current instanceof DOMElement && $current->tagName ===
'site' ) {
135 $site = $this->
makeSite( $current );
136 $key = $site->getGlobalId();
138 if ( $oldSites->hasSite( $key ) ) {
139 $oldSite = $oldSites->getSite( $key );
140 $site->setInternalId( $oldSite->getInternalId() );
143 $sites[$key] = $site;
144 }
catch ( Exception $ex ) {
149 $current = $current->nextSibling;
161 public function makeSite( DOMElement $siteElement ) {
162 if ( $siteElement->tagName !==
'site' ) {
163 throw new InvalidArgumentException(
'Expected <site> tag, found ' . $siteElement->tagName );
169 $site->setForward( $this->
hasChild( $siteElement,
'forward' ) );
170 $site->setGlobalId( $this->
getChildText( $siteElement,
'globalid' ) );
174 $pathTags = $siteElement->getElementsByTagName(
'path' );
175 for ( $i = 0; $i < $pathTags->length; $i++ ) {
176 $pathElement = $pathTags->item( $i );
177 '@phan-var DOMElement $pathElement';
179 $path = $pathElement->textContent;
181 $site->setPath( $pathType,
$path );
184 $idTags = $siteElement->getElementsByTagName(
'localid' );
185 for ( $i = 0; $i < $idTags->length; $i++ ) {
186 $idElement = $idTags->item( $i );
187 '@phan-var DOMElement $idElement';
189 $id = $idElement->textContent;
191 $site->addLocalId( $idType, $id );
232 private function getChildText( DOMElement $element, $name, $default =
false ) {
233 $elements = $element->getElementsByTagName( $name );
235 if ( $elements->length < 1 ) {
236 if ( $default !==
false ) {
240 'Required <' . $name .
'> tag not found inside <' . $element->tagName .
'> tag'
245 $node = $elements->item( 0 );
246 return $node->textContent;