MediaWiki master
SiteImporter.php
Go to the documentation of this file.
1<?php
6
7namespace MediaWiki\Site;
8
9use DOMDocument;
10use DOMElement;
11use Exception;
12use InvalidArgumentException;
13use RuntimeException;
14use Wikimedia\RequestTimeout\TimeoutException;
15
26
30 private $exceptionCallback;
31
32 public function __construct( private SiteStore $store, private SiteSanitizer $siteSanitizer ) {
33 }
34
38 public function getExceptionCallback() {
39 return $this->exceptionCallback;
40 }
41
45 public function setExceptionCallback( $exceptionCallback ) {
46 $this->exceptionCallback = $exceptionCallback;
47 }
48
52 public function importFromFile( $file ) {
53 $xml = file_get_contents( $file );
54
55 if ( $xml === false ) {
56 throw new RuntimeException( 'Failed to read ' . $file . '!' );
57 }
58
59 $this->importFromXML( $xml );
60 }
61
65 public function importFromXML( $xml ) {
66 $document = new DOMDocument();
67
68 $oldLibXmlErrors = libxml_use_internal_errors( true );
69 // phpcs:ignore Generic.PHP.NoSilencedErrors -- suppress deprecation per T268847
70 $oldDisable = @libxml_disable_entity_loader( true );
71 $ok = $document->loadXML( $xml, LIBXML_NONET );
72
73 if ( !$ok ) {
74 $errors = libxml_get_errors();
75 libxml_use_internal_errors( $oldLibXmlErrors );
76 // phpcs:ignore Generic.PHP.NoSilencedErrors
77 @libxml_disable_entity_loader( $oldDisable );
78
79 foreach ( $errors as $error ) {
81 throw new InvalidArgumentException(
82 'Malformed XML: ' . $error->message . ' in line ' . $error->line
83 );
84 }
85
86 throw new InvalidArgumentException( 'Malformed XML!' );
87 }
88
89 libxml_use_internal_errors( $oldLibXmlErrors );
90 // phpcs:ignore Generic.PHP.NoSilencedErrors
91 @libxml_disable_entity_loader( $oldDisable );
92 $sites = $this->makeSiteList( $document->documentElement );
93 $this->store->saveSites( $sites );
94 }
95
101 private function makeSiteList( DOMElement $root ) {
102 $sites = [];
103
104 // Old sites, to get the row IDs that correspond to the global site IDs.
105 // TODO: Get rid of internal row IDs, they just get in the way. Get rid of ORMRow, too.
106 $oldSites = $this->store->getSites();
107
108 $current = $root->firstChild;
109 while ( $current ) {
110 if ( $current instanceof DOMElement && $current->tagName === 'site' ) {
111 try {
112 $site = $this->makeSite( $current );
113 $key = $site->getGlobalId();
114
115 if ( $oldSites->hasSite( $key ) ) {
116 $oldSite = $oldSites->getSite( $key );
117 $site->setInternalId( $oldSite->getInternalId() );
118 }
119
120 $sites[$key] = $site;
121 } catch ( TimeoutException $e ) {
122 throw $e;
123 } catch ( Exception $ex ) {
124 $this->handleException( $ex );
125 }
126 }
127
128 $current = $current->nextSibling;
129 }
130
131 return $sites;
132 }
133
139 public function makeSite( DOMElement $siteElement ) {
140 if ( $siteElement->tagName !== 'site' ) {
141 throw new InvalidArgumentException( 'Expected <site> tag, found ' . $siteElement->tagName );
142 }
143
144 $type = $this->getAttributeValue( $siteElement, 'type', Site::TYPE_UNKNOWN );
145 $site = $this->siteSanitizer->newSiteForType( $type );
146
147 $site->setGlobalId( $this->getChildText( $siteElement, 'globalid' ) );
148 $site->setGroup( $this->getChildText( $siteElement, 'group', Site::GROUP_NONE ) );
149 $site->setLanguageCode( $this->getChildText( $siteElement, 'language', null ) );
150
151 $pathTags = $siteElement->getElementsByTagName( 'path' );
152 for ( $i = 0; $i < $pathTags->length; $i++ ) {
153 $pathElement = $pathTags->item( $i );
154 '@phan-var DOMElement $pathElement';
155 $pathType = $this->getAttributeValue( $pathElement, 'type' );
156 $path = $pathElement->textContent;
157
158 $site->setPath( $pathType, $path );
159 }
160
161 $idTags = $siteElement->getElementsByTagName( 'localid' );
162 for ( $i = 0; $i < $idTags->length; $i++ ) {
163 $idElement = $idTags->item( $i );
164 '@phan-var DOMElement $idElement';
165 $idType = $this->getAttributeValue( $idElement, 'type' );
166 $id = $idElement->textContent;
167
168 $site->addLocalId( $idType, $id );
169 }
170
171 // @todo: import <data>
172 // @todo: import <config>
173
174 $this->siteSanitizer->sanitizeSite( $site );
175 return $site;
176 }
177
185 private function getAttributeValue( DOMElement $element, $name, $default = false ) {
186 $node = $element->getAttributeNode( $name );
187
188 if ( !$node ) {
189 if ( $default !== false ) {
190 return $default;
191 } else {
192 throw new RuntimeException(
193 'Required ' . $name . ' attribute not found in <' . $element->tagName . '> tag'
194 );
195 }
196 }
197
198 return $node->textContent;
199 }
200
208 private function getChildText( DOMElement $element, $name, $default = false ) {
209 $elements = $element->getElementsByTagName( $name );
210
211 if ( $elements->length < 1 ) {
212 if ( $default !== false ) {
213 return $default;
214 } else {
215 throw new RuntimeException(
216 'Required <' . $name . '> tag not found inside <' . $element->tagName . '> tag'
217 );
218 }
219 }
220
221 $node = $elements->item( 0 );
222 return $node->textContent;
223 }
224
231 private function hasChild( DOMElement $element, $name ) {
232 return $this->getChildText( $element, $name, null ) !== null;
233 }
234
235 private function handleException( Exception $ex ) {
236 if ( $this->exceptionCallback ) {
237 ( $this->exceptionCallback )( $ex );
238 } else {
239 wfLogWarning( $ex->getMessage() );
240 }
241 }
242
243}
244
246class_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)
makeSite(DOMElement $siteElement)
__construct(private SiteStore $store, private SiteSanitizer $siteSanitizer)
Prevent invalid Site attributes.
const TYPE_UNKNOWN
Definition Site.php:21
Interface for storing and retrieving Site objects.
Definition SiteStore.php:18