Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / site / SiteImporter.php
blob6be9366f3edaf7295887c9ee3e4f6081f2126657
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 namespace MediaWiki\Site;
23 use DOMDocument;
24 use DOMElement;
25 use Exception;
26 use InvalidArgumentException;
27 use RuntimeException;
28 use Wikimedia\RequestTimeout\TimeoutException;
30 /**
31 * Utility for importing site entries from XML.
33 * For the expected format of the input, see docs/sitelist.md and docs/sitelist-1.0.xsd.
35 * @since 1.25
36 * @ingroup Site
37 * @author Daniel Kinzler
39 class SiteImporter {
41 /**
42 * @var SiteStore
44 private $store;
46 /**
47 * @var callable|null
49 private $exceptionCallback;
51 /**
52 * @param SiteStore $store
54 public function __construct( SiteStore $store ) {
55 $this->store = $store;
58 /**
59 * @return callable
61 public function getExceptionCallback() {
62 return $this->exceptionCallback;
65 /**
66 * @param callable $exceptionCallback
68 public function setExceptionCallback( $exceptionCallback ) {
69 $this->exceptionCallback = $exceptionCallback;
72 /**
73 * @param string $file
75 public function importFromFile( $file ) {
76 $xml = file_get_contents( $file );
78 if ( $xml === false ) {
79 throw new RuntimeException( 'Failed to read ' . $file . '!' );
82 $this->importFromXML( $xml );
85 /**
86 * @param string $xml
89 public function importFromXML( $xml ) {
90 $document = new DOMDocument();
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 );
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 );
103 foreach ( $errors as $error ) {
104 /** @var LibXMLError $error */
105 throw new InvalidArgumentException(
106 'Malformed XML: ' . $error->message . ' in line ' . $error->line
110 throw new InvalidArgumentException( 'Malformed XML!' );
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 );
121 * @param DOMElement $root
123 * @return Site[]
125 private function makeSiteList( DOMElement $root ) {
126 $sites = [];
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();
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();
139 if ( $oldSites->hasSite( $key ) ) {
140 $oldSite = $oldSites->getSite( $key );
141 $site->setInternalId( $oldSite->getInternalId() );
144 $sites[$key] = $site;
145 } catch ( TimeoutException $e ) {
146 throw $e;
147 } catch ( Exception $ex ) {
148 $this->handleException( $ex );
152 $current = $current->nextSibling;
155 return $sites;
159 * @param DOMElement $siteElement
161 * @return Site
163 public function makeSite( DOMElement $siteElement ) {
164 if ( $siteElement->tagName !== 'site' ) {
165 throw new InvalidArgumentException( 'Expected <site> tag, found ' . $siteElement->tagName );
168 $type = $this->getAttributeValue( $siteElement, 'type', Site::TYPE_UNKNOWN );
169 $site = Site::newForType( $type );
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 ) );
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;
183 $site->setPath( $pathType, $path );
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;
193 $site->addLocalId( $idType, $id );
196 // @todo: import <data>
197 // @todo: import <config>
199 return $site;
203 * @param DOMElement $element
204 * @param string $name
205 * @param string|null|false $default
207 * @return null|string
209 private function getAttributeValue( DOMElement $element, $name, $default = false ) {
210 $node = $element->getAttributeNode( $name );
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'
222 return $node->textContent;
226 * @param DOMElement $element
227 * @param string $name
228 * @param string|null|false $default
230 * @return null|string
232 private function getChildText( DOMElement $element, $name, $default = false ) {
233 $elements = $element->getElementsByTagName( $name );
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'
245 $node = $elements->item( 0 );
246 return $node->textContent;
250 * @param DOMElement $element
251 * @param string $name
253 * @return bool
255 private function hasChild( DOMElement $element, $name ) {
256 return $this->getChildText( $element, $name, null ) !== null;
260 * @param Exception $ex
262 private function handleException( Exception $ex ) {
263 if ( $this->exceptionCallback ) {
264 call_user_func( $this->exceptionCallback, $ex );
265 } else {
266 wfLogWarning( $ex->getMessage() );
272 /** @deprecated class alias since 1.42 */
273 class_alias( SiteImporter::class, 'SiteImporter' );