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;
52 * @param SiteStore $store
54 public function __construct( SiteStore
$store ) {
55 $this->store
= $store;
61 public function getExceptionCallback() {
62 return $this->exceptionCallback
;
66 * @param callable $exceptionCallback
68 public function setExceptionCallback( $exceptionCallback ) {
69 $this->exceptionCallback
= $exceptionCallback;
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 );
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
);
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
125 private function makeSiteList( DOMElement
$root ) {
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
;
134 if ( $current instanceof DOMElement
&& $current->tagName
=== 'site' ) {
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 ) {
147 } catch ( Exception
$ex ) {
148 $this->handleException( $ex );
152 $current = $current->nextSibling
;
159 * @param DOMElement $siteElement
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>
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 );
213 if ( $default !== false ) {
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 ) {
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
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 );
266 wfLogWarning( $ex->getMessage() );
272 /** @deprecated class alias since 1.42 */
273 class_alias( SiteImporter
::class, 'SiteImporter' );