MediaWiki REL1_37
SiteImporter.php
Go to the documentation of this file.
1<?php
2
31
35 private $store;
36
41
45 public function __construct( SiteStore $store ) {
46 $this->store = $store;
47 }
48
52 public function getExceptionCallback() {
54 }
55
60 $this->exceptionCallback = $exceptionCallback;
61 }
62
66 public function importFromFile( $file ) {
67 $xml = file_get_contents( $file );
68
69 if ( $xml === false ) {
70 throw new RuntimeException( 'Failed to read ' . $file . '!' );
71 }
72
73 $this->importFromXML( $xml );
74 }
75
81 public function importFromXML( $xml ) {
82 $document = new DOMDocument();
83
84 $oldLibXmlErrors = libxml_use_internal_errors( true );
85 // phpcs:ignore Generic.PHP.NoSilencedErrors -- suppress deprecation per T268847
86 $oldDisable = @libxml_disable_entity_loader( true );
87 $ok = $document->loadXML( $xml, LIBXML_NONET );
88
89 if ( !$ok ) {
90 $errors = libxml_get_errors();
91 libxml_use_internal_errors( $oldLibXmlErrors );
92 // phpcs:ignore Generic.PHP.NoSilencedErrors
93 @libxml_disable_entity_loader( $oldDisable );
94
95 foreach ( $errors as $error ) {
97 throw new InvalidArgumentException(
98 'Malformed XML: ' . $error->message . ' in line ' . $error->line
99 );
100 }
101
102 throw new InvalidArgumentException( 'Malformed XML!' );
103 }
104
105 libxml_use_internal_errors( $oldLibXmlErrors );
106 // phpcs:ignore Generic.PHP.NoSilencedErrors
107 @libxml_disable_entity_loader( $oldDisable );
108 $this->importFromDOM( $document->documentElement );
109 }
110
114 private function importFromDOM( DOMElement $root ) {
115 $sites = $this->makeSiteList( $root );
116 $this->store->saveSites( $sites );
117 }
118
124 private function makeSiteList( DOMElement $root ) {
125 $sites = [];
126
127 // Old sites, to get the row IDs that correspond to the global site IDs.
128 // TODO: Get rid of internal row IDs, they just get in the way. Get rid of ORMRow, too.
129 $oldSites = $this->store->getSites();
130
131 $current = $root->firstChild;
132 while ( $current ) {
133 if ( $current instanceof DOMElement && $current->tagName === 'site' ) {
134 try {
135 $site = $this->makeSite( $current );
136 $key = $site->getGlobalId();
137
138 if ( $oldSites->hasSite( $key ) ) {
139 $oldSite = $oldSites->getSite( $key );
140 $site->setInternalId( $oldSite->getInternalId() );
141 }
142
143 $sites[$key] = $site;
144 } catch ( Exception $ex ) {
145 $this->handleException( $ex );
146 }
147 }
148
149 $current = $current->nextSibling;
150 }
151
152 return $sites;
153 }
154
161 public function makeSite( DOMElement $siteElement ) {
162 if ( $siteElement->tagName !== 'site' ) {
163 throw new InvalidArgumentException( 'Expected <site> tag, found ' . $siteElement->tagName );
164 }
165
166 $type = $this->getAttributeValue( $siteElement, 'type', Site::TYPE_UNKNOWN );
167 $site = Site::newForType( $type );
168
169 $site->setForward( $this->hasChild( $siteElement, 'forward' ) );
170 $site->setGlobalId( $this->getChildText( $siteElement, 'globalid' ) );
171 $site->setGroup( $this->getChildText( $siteElement, 'group', Site::GROUP_NONE ) );
172 $site->setSource( $this->getChildText( $siteElement, 'source', Site::SOURCE_LOCAL ) );
173
174 $pathTags = $siteElement->getElementsByTagName( 'path' );
175 for ( $i = 0; $i < $pathTags->length; $i++ ) {
176 $pathElement = $pathTags->item( $i );
177 '@phan-var DOMElement $pathElement';
178 $pathType = $this->getAttributeValue( $pathElement, 'type' );
179 $path = $pathElement->textContent;
180
181 $site->setPath( $pathType, $path );
182 }
183
184 $idTags = $siteElement->getElementsByTagName( 'localid' );
185 for ( $i = 0; $i < $idTags->length; $i++ ) {
186 $idElement = $idTags->item( $i );
187 '@phan-var DOMElement $idElement';
188 $idType = $this->getAttributeValue( $idElement, 'type' );
189 $id = $idElement->textContent;
190
191 $site->addLocalId( $idType, $id );
192 }
193
194 // @todo: import <data>
195 // @todo: import <config>
196
197 return $site;
198 }
199
208 private function getAttributeValue( DOMElement $element, $name, $default = false ) {
209 $node = $element->getAttributeNode( $name );
210
211 if ( !$node ) {
212 if ( $default !== false ) {
213 return $default;
214 } else {
215 throw new MWException(
216 'Required ' . $name . ' attribute not found in <' . $element->tagName . '> tag'
217 );
218 }
219 }
220
221 return $node->textContent;
222 }
223
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 MWException(
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
256 private function hasChild( DOMElement $element, $name ) {
257 return $this->getChildText( $element, $name, null ) !== null;
258 }
259
263 private function handleException( Exception $ex ) {
264 if ( $this->exceptionCallback ) {
265 call_user_func( $this->exceptionCallback, $ex );
266 } else {
267 wfLogWarning( $ex->getMessage() );
268 }
269 }
270
271}
wfLogWarning( $msg, $callerOffset=1, $level=E_USER_WARNING)
Send a warning as a PHP error and the debug log.
MediaWiki exception.
SiteStore $store
importFromDOM(DOMElement $root)
makeSiteList(DOMElement $root)
hasChild(DOMElement $element, $name)
__construct(SiteStore $store)
callable null $exceptionCallback
getAttributeValue(DOMElement $element, $name, $default=false)
handleException(Exception $ex)
importFromFile( $file)
importFromXML( $xml)
setExceptionCallback( $exceptionCallback)
makeSite(DOMElement $siteElement)
getChildText(DOMElement $element, $name, $default=false)
const GROUP_NONE
Definition Site.php:36
static newForType( $siteType)
Definition Site.php:662
const TYPE_UNKNOWN
Definition Site.php:33
const SOURCE_LOCAL
Definition Site.php:41
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42