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
21 namespace MediaWiki\Site
;
26 use InvalidArgumentException
;
28 use Wikimedia\RequestTimeout\TimeoutException
;
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.
37 * @author Daniel Kinzler
49 private $exceptionCallback;
51 public function __construct( SiteStore
$store ) {
52 $this->store
= $store;
58 public function getExceptionCallback() {
59 return $this->exceptionCallback
;
63 * @param callable $exceptionCallback
65 public function setExceptionCallback( $exceptionCallback ) {
66 $this->exceptionCallback
= $exceptionCallback;
72 public function importFromFile( $file ) {
73 $xml = file_get_contents( $file );
75 if ( $xml === false ) {
76 throw new RuntimeException( 'Failed to read ' . $file . '!' );
79 $this->importFromXML( $xml );
86 public function importFromXML( $xml ) {
87 $document = new DOMDocument();
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
);
95 $errors = libxml_get_errors();
96 libxml_use_internal_errors( $oldLibXmlErrors );
97 // phpcs:ignore Generic.PHP.NoSilencedErrors
98 @libxml_disable_entity_loader
( $oldDisable );
100 foreach ( $errors as $error ) {
101 /** @var LibXMLError $error */
102 throw new InvalidArgumentException(
103 'Malformed XML: ' . $error->message
. ' in line ' . $error->line
107 throw new InvalidArgumentException( 'Malformed XML!' );
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 );
118 * @param DOMElement $root
122 private function makeSiteList( DOMElement
$root ) {
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();
129 $current = $root->firstChild
;
131 if ( $current instanceof DOMElement
&& $current->tagName
=== 'site' ) {
133 $site = $this->makeSite( $current );
134 $key = $site->getGlobalId();
136 if ( $oldSites->hasSite( $key ) ) {
137 $oldSite = $oldSites->getSite( $key );
138 $site->setInternalId( $oldSite->getInternalId() );
141 $sites[$key] = $site;
142 } catch ( TimeoutException
$e ) {
144 } catch ( Exception
$ex ) {
145 $this->handleException( $ex );
149 $current = $current->nextSibling
;
156 * @param DOMElement $siteElement
160 public function makeSite( DOMElement
$siteElement ) {
161 if ( $siteElement->tagName
!== 'site' ) {
162 throw new InvalidArgumentException( 'Expected <site> tag, found ' . $siteElement->tagName
);
165 $type = $this->getAttributeValue( $siteElement, 'type', Site
::TYPE_UNKNOWN
);
166 $site = Site
::newForType( $type );
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
) );
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
;
180 $site->setPath( $pathType, $path );
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
;
190 $site->addLocalId( $idType, $id );
193 // @todo: import <data>
194 // @todo: import <config>
200 * @param DOMElement $element
201 * @param string $name
202 * @param string|null|false $default
204 * @return null|string
206 private function getAttributeValue( DOMElement
$element, $name, $default = false ) {
207 $node = $element->getAttributeNode( $name );
210 if ( $default !== false ) {
213 throw new RuntimeException(
214 'Required ' . $name . ' attribute not found in <' . $element->tagName
. '> tag'
219 return $node->textContent
;
223 * @param DOMElement $element
224 * @param string $name
225 * @param string|null|false $default
227 * @return null|string
229 private function getChildText( DOMElement
$element, $name, $default = false ) {
230 $elements = $element->getElementsByTagName( $name );
232 if ( $elements->length
< 1 ) {
233 if ( $default !== false ) {
236 throw new RuntimeException(
237 'Required <' . $name . '> tag not found inside <' . $element->tagName
. '> tag'
242 $node = $elements->item( 0 );
243 return $node->textContent
;
247 * @param DOMElement $element
248 * @param string $name
252 private function hasChild( DOMElement
$element, $name ) {
253 return $this->getChildText( $element, $name, null ) !== null;
256 private function handleException( Exception
$ex ) {
257 if ( $this->exceptionCallback
) {
258 call_user_func( $this->exceptionCallback
, $ex );
260 wfLogWarning( $ex->getMessage() );
266 /** @deprecated class alias since 1.42 */
267 class_alias( SiteImporter
::class, 'SiteImporter' );