MediaWiki master
SiteImporter.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Site;
22
23use DOMDocument;
24use DOMElement;
25use Exception;
26use InvalidArgumentException;
27use RuntimeException;
28use Wikimedia\RequestTimeout\TimeoutException;
29
40
44 private $store;
45
49 private $exceptionCallback;
50
51 public function __construct( SiteStore $store ) {
52 $this->store = $store;
53 }
54
58 public function getExceptionCallback() {
59 return $this->exceptionCallback;
60 }
61
65 public function setExceptionCallback( $exceptionCallback ) {
66 $this->exceptionCallback = $exceptionCallback;
67 }
68
72 public function importFromFile( $file ) {
73 $xml = file_get_contents( $file );
74
75 if ( $xml === false ) {
76 throw new RuntimeException( 'Failed to read ' . $file . '!' );
77 }
78
79 $this->importFromXML( $xml );
80 }
81
86 public function importFromXML( $xml ) {
87 $document = new DOMDocument();
88
89 $oldLibXmlErrors = libxml_use_internal_errors( true );
90 // phpcs:ignore Generic.PHP.NoSilencedErrors -- suppress deprecation per T268847
91 $oldDisable = @libxml_disable_entity_loader( true );
92 $ok = $document->loadXML( $xml, LIBXML_NONET );
93
94 if ( !$ok ) {
95 $errors = libxml_get_errors();
96 libxml_use_internal_errors( $oldLibXmlErrors );
97 // phpcs:ignore Generic.PHP.NoSilencedErrors
98 @libxml_disable_entity_loader( $oldDisable );
99
100 foreach ( $errors as $error ) {
102 throw new InvalidArgumentException(
103 'Malformed XML: ' . $error->message . ' in line ' . $error->line
104 );
105 }
106
107 throw new InvalidArgumentException( 'Malformed XML!' );
108 }
109
110 libxml_use_internal_errors( $oldLibXmlErrors );
111 // phpcs:ignore Generic.PHP.NoSilencedErrors
112 @libxml_disable_entity_loader( $oldDisable );
113 $sites = $this->makeSiteList( $document->documentElement );
114 $this->store->saveSites( $sites );
115 }
116
122 private function makeSiteList( DOMElement $root ) {
123 $sites = [];
124
125 // Old sites, to get the row IDs that correspond to the global site IDs.
126 // TODO: Get rid of internal row IDs, they just get in the way. Get rid of ORMRow, too.
127 $oldSites = $this->store->getSites();
128
129 $current = $root->firstChild;
130 while ( $current ) {
131 if ( $current instanceof DOMElement && $current->tagName === 'site' ) {
132 try {
133 $site = $this->makeSite( $current );
134 $key = $site->getGlobalId();
135
136 if ( $oldSites->hasSite( $key ) ) {
137 $oldSite = $oldSites->getSite( $key );
138 $site->setInternalId( $oldSite->getInternalId() );
139 }
140
141 $sites[$key] = $site;
142 } catch ( TimeoutException $e ) {
143 throw $e;
144 } catch ( Exception $ex ) {
145 $this->handleException( $ex );
146 }
147 }
148
149 $current = $current->nextSibling;
150 }
151
152 return $sites;
153 }
154
160 public function makeSite( DOMElement $siteElement ) {
161 if ( $siteElement->tagName !== 'site' ) {
162 throw new InvalidArgumentException( 'Expected <site> tag, found ' . $siteElement->tagName );
163 }
164
165 $type = $this->getAttributeValue( $siteElement, 'type', Site::TYPE_UNKNOWN );
166 $site = Site::newForType( $type );
167
168 $site->setForward( $this->hasChild( $siteElement, 'forward' ) );
169 $site->setGlobalId( $this->getChildText( $siteElement, 'globalid' ) );
170 $site->setGroup( $this->getChildText( $siteElement, 'group', Site::GROUP_NONE ) );
171 $site->setSource( $this->getChildText( $siteElement, 'source', Site::SOURCE_LOCAL ) );
172
173 $pathTags = $siteElement->getElementsByTagName( 'path' );
174 for ( $i = 0; $i < $pathTags->length; $i++ ) {
175 $pathElement = $pathTags->item( $i );
176 '@phan-var DOMElement $pathElement';
177 $pathType = $this->getAttributeValue( $pathElement, 'type' );
178 $path = $pathElement->textContent;
179
180 $site->setPath( $pathType, $path );
181 }
182
183 $idTags = $siteElement->getElementsByTagName( 'localid' );
184 for ( $i = 0; $i < $idTags->length; $i++ ) {
185 $idElement = $idTags->item( $i );
186 '@phan-var DOMElement $idElement';
187 $idType = $this->getAttributeValue( $idElement, 'type' );
188 $id = $idElement->textContent;
189
190 $site->addLocalId( $idType, $id );
191 }
192
193 // @todo: import <data>
194 // @todo: import <config>
195
196 return $site;
197 }
198
206 private function getAttributeValue( DOMElement $element, $name, $default = false ) {
207 $node = $element->getAttributeNode( $name );
208
209 if ( !$node ) {
210 if ( $default !== false ) {
211 return $default;
212 } else {
213 throw new RuntimeException(
214 'Required ' . $name . ' attribute not found in <' . $element->tagName . '> tag'
215 );
216 }
217 }
218
219 return $node->textContent;
220 }
221
229 private function getChildText( DOMElement $element, $name, $default = false ) {
230 $elements = $element->getElementsByTagName( $name );
231
232 if ( $elements->length < 1 ) {
233 if ( $default !== false ) {
234 return $default;
235 } else {
236 throw new RuntimeException(
237 'Required <' . $name . '> tag not found inside <' . $element->tagName . '> tag'
238 );
239 }
240 }
241
242 $node = $elements->item( 0 );
243 return $node->textContent;
244 }
245
252 private function hasChild( DOMElement $element, $name ) {
253 return $this->getChildText( $element, $name, null ) !== null;
254 }
255
256 private function handleException( Exception $ex ) {
257 if ( $this->exceptionCallback ) {
258 ( $this->exceptionCallback )( $ex );
259 } else {
260 wfLogWarning( $ex->getMessage() );
261 }
262 }
263
264}
265
267class_alias( SiteImporter::class, 'SiteImporter' );
wfLogWarning( $msg, $callerOffset=1, $level=E_USER_WARNING)
Send a warning as a PHP error and the debug log.
Utility for importing site entries from XML.
setExceptionCallback( $exceptionCallback)
__construct(SiteStore $store)
makeSite(DOMElement $siteElement)
const SOURCE_LOCAL
Definition Site.php:45
static newForType( $siteType)
Definition Site.php:609
const TYPE_UNKNOWN
Definition Site.php:37
Interface for storing and retrieving Site objects.
Definition SiteStore.php:32