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
54 public function __construct( SiteStore $store ) {
55 $this->store = $store;
56 }
57
61 public function getExceptionCallback() {
62 return $this->exceptionCallback;
63 }
64
68 public function setExceptionCallback( $exceptionCallback ) {
69 $this->exceptionCallback = $exceptionCallback;
70 }
71
75 public function importFromFile( $file ) {
76 $xml = file_get_contents( $file );
77
78 if ( $xml === false ) {
79 throw new RuntimeException( 'Failed to read ' . $file . '!' );
80 }
81
82 $this->importFromXML( $xml );
83 }
84
89 public function importFromXML( $xml ) {
90 $document = new DOMDocument();
91
92 $oldLibXmlErrors = libxml_use_internal_errors( true );
93 // phpcs:ignore Generic.PHP.NoSilencedErrors -- suppress deprecation per T268847
94 $oldDisable = @libxml_disable_entity_loader( true );
95 $ok = $document->loadXML( $xml, LIBXML_NONET );
96
97 if ( !$ok ) {
98 $errors = libxml_get_errors();
99 libxml_use_internal_errors( $oldLibXmlErrors );
100 // phpcs:ignore Generic.PHP.NoSilencedErrors
101 @libxml_disable_entity_loader( $oldDisable );
102
103 foreach ( $errors as $error ) {
105 throw new InvalidArgumentException(
106 'Malformed XML: ' . $error->message . ' in line ' . $error->line
107 );
108 }
109
110 throw new InvalidArgumentException( 'Malformed XML!' );
111 }
112
113 libxml_use_internal_errors( $oldLibXmlErrors );
114 // phpcs:ignore Generic.PHP.NoSilencedErrors
115 @libxml_disable_entity_loader( $oldDisable );
116 $sites = $this->makeSiteList( $document->documentElement );
117 $this->store->saveSites( $sites );
118 }
119
125 private function makeSiteList( DOMElement $root ) {
126 $sites = [];
127
128 // Old sites, to get the row IDs that correspond to the global site IDs.
129 // TODO: Get rid of internal row IDs, they just get in the way. Get rid of ORMRow, too.
130 $oldSites = $this->store->getSites();
131
132 $current = $root->firstChild;
133 while ( $current ) {
134 if ( $current instanceof DOMElement && $current->tagName === 'site' ) {
135 try {
136 $site = $this->makeSite( $current );
137 $key = $site->getGlobalId();
138
139 if ( $oldSites->hasSite( $key ) ) {
140 $oldSite = $oldSites->getSite( $key );
141 $site->setInternalId( $oldSite->getInternalId() );
142 }
143
144 $sites[$key] = $site;
145 } catch ( TimeoutException $e ) {
146 throw $e;
147 } catch ( Exception $ex ) {
148 $this->handleException( $ex );
149 }
150 }
151
152 $current = $current->nextSibling;
153 }
154
155 return $sites;
156 }
157
163 public function makeSite( DOMElement $siteElement ) {
164 if ( $siteElement->tagName !== 'site' ) {
165 throw new InvalidArgumentException( 'Expected <site> tag, found ' . $siteElement->tagName );
166 }
167
168 $type = $this->getAttributeValue( $siteElement, 'type', Site::TYPE_UNKNOWN );
169 $site = Site::newForType( $type );
170
171 $site->setForward( $this->hasChild( $siteElement, 'forward' ) );
172 $site->setGlobalId( $this->getChildText( $siteElement, 'globalid' ) );
173 $site->setGroup( $this->getChildText( $siteElement, 'group', Site::GROUP_NONE ) );
174 $site->setSource( $this->getChildText( $siteElement, 'source', Site::SOURCE_LOCAL ) );
175
176 $pathTags = $siteElement->getElementsByTagName( 'path' );
177 for ( $i = 0; $i < $pathTags->length; $i++ ) {
178 $pathElement = $pathTags->item( $i );
179 '@phan-var DOMElement $pathElement';
180 $pathType = $this->getAttributeValue( $pathElement, 'type' );
181 $path = $pathElement->textContent;
182
183 $site->setPath( $pathType, $path );
184 }
185
186 $idTags = $siteElement->getElementsByTagName( 'localid' );
187 for ( $i = 0; $i < $idTags->length; $i++ ) {
188 $idElement = $idTags->item( $i );
189 '@phan-var DOMElement $idElement';
190 $idType = $this->getAttributeValue( $idElement, 'type' );
191 $id = $idElement->textContent;
192
193 $site->addLocalId( $idType, $id );
194 }
195
196 // @todo: import <data>
197 // @todo: import <config>
198
199 return $site;
200 }
201
209 private function getAttributeValue( DOMElement $element, $name, $default = false ) {
210 $node = $element->getAttributeNode( $name );
211
212 if ( !$node ) {
213 if ( $default !== false ) {
214 return $default;
215 } else {
216 throw new RuntimeException(
217 'Required ' . $name . ' attribute not found in <' . $element->tagName . '> tag'
218 );
219 }
220 }
221
222 return $node->textContent;
223 }
224
232 private function getChildText( DOMElement $element, $name, $default = false ) {
233 $elements = $element->getElementsByTagName( $name );
234
235 if ( $elements->length < 1 ) {
236 if ( $default !== false ) {
237 return $default;
238 } else {
239 throw new RuntimeException(
240 'Required <' . $name . '> tag not found inside <' . $element->tagName . '> tag'
241 );
242 }
243 }
244
245 $node = $elements->item( 0 );
246 return $node->textContent;
247 }
248
255 private function hasChild( DOMElement $element, $name ) {
256 return $this->getChildText( $element, $name, null ) !== null;
257 }
258
262 private function handleException( Exception $ex ) {
263 if ( $this->exceptionCallback ) {
264 call_user_func( $this->exceptionCallback, $ex );
265 } else {
266 wfLogWarning( $ex->getMessage() );
267 }
268 }
269
270}
271
273class_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