1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Site;
use DOMDocument;
use DOMElement;
use Exception;
use InvalidArgumentException;
use RuntimeException;
use Wikimedia\RequestTimeout\TimeoutException;
/**
* Utility for importing site entries from XML.
*
* For the expected format of the input, see docs/sitelist.md and docs/sitelist-1.0.xsd.
*
* @since 1.25
* @ingroup Site
* @author Daniel Kinzler
*/
class SiteImporter {
/**
* @var SiteStore
*/
private $store;
/**
* @var callable|null
*/
private $exceptionCallback;
/**
* @param SiteStore $store
*/
public function __construct( SiteStore $store ) {
$this->store = $store;
}
/**
* @return callable
*/
public function getExceptionCallback() {
return $this->exceptionCallback;
}
/**
* @param callable $exceptionCallback
*/
public function setExceptionCallback( $exceptionCallback ) {
$this->exceptionCallback = $exceptionCallback;
}
/**
* @param string $file
*/
public function importFromFile( $file ) {
$xml = file_get_contents( $file );
if ( $xml === false ) {
throw new RuntimeException( 'Failed to read ' . $file . '!' );
}
$this->importFromXML( $xml );
}
/**
* @param string $xml
*
*/
public function importFromXML( $xml ) {
$document = new DOMDocument();
$oldLibXmlErrors = libxml_use_internal_errors( true );
// phpcs:ignore Generic.PHP.NoSilencedErrors -- suppress deprecation per T268847
$oldDisable = @libxml_disable_entity_loader( true );
$ok = $document->loadXML( $xml, LIBXML_NONET );
if ( !$ok ) {
$errors = libxml_get_errors();
libxml_use_internal_errors( $oldLibXmlErrors );
// phpcs:ignore Generic.PHP.NoSilencedErrors
@libxml_disable_entity_loader( $oldDisable );
foreach ( $errors as $error ) {
/** @var LibXMLError $error */
throw new InvalidArgumentException(
'Malformed XML: ' . $error->message . ' in line ' . $error->line
);
}
throw new InvalidArgumentException( 'Malformed XML!' );
}
libxml_use_internal_errors( $oldLibXmlErrors );
// phpcs:ignore Generic.PHP.NoSilencedErrors
@libxml_disable_entity_loader( $oldDisable );
$sites = $this->makeSiteList( $document->documentElement );
$this->store->saveSites( $sites );
}
/**
* @param DOMElement $root
*
* @return Site[]
*/
private function makeSiteList( DOMElement $root ) {
$sites = [];
// Old sites, to get the row IDs that correspond to the global site IDs.
// TODO: Get rid of internal row IDs, they just get in the way. Get rid of ORMRow, too.
$oldSites = $this->store->getSites();
$current = $root->firstChild;
while ( $current ) {
if ( $current instanceof DOMElement && $current->tagName === 'site' ) {
try {
$site = $this->makeSite( $current );
$key = $site->getGlobalId();
if ( $oldSites->hasSite( $key ) ) {
$oldSite = $oldSites->getSite( $key );
$site->setInternalId( $oldSite->getInternalId() );
}
$sites[$key] = $site;
} catch ( TimeoutException $e ) {
throw $e;
} catch ( Exception $ex ) {
$this->handleException( $ex );
}
}
$current = $current->nextSibling;
}
return $sites;
}
/**
* @param DOMElement $siteElement
*
* @return Site
*/
public function makeSite( DOMElement $siteElement ) {
if ( $siteElement->tagName !== 'site' ) {
throw new InvalidArgumentException( 'Expected <site> tag, found ' . $siteElement->tagName );
}
$type = $this->getAttributeValue( $siteElement, 'type', Site::TYPE_UNKNOWN );
$site = Site::newForType( $type );
$site->setForward( $this->hasChild( $siteElement, 'forward' ) );
$site->setGlobalId( $this->getChildText( $siteElement, 'globalid' ) );
$site->setGroup( $this->getChildText( $siteElement, 'group', Site::GROUP_NONE ) );
$site->setSource( $this->getChildText( $siteElement, 'source', Site::SOURCE_LOCAL ) );
$pathTags = $siteElement->getElementsByTagName( 'path' );
for ( $i = 0; $i < $pathTags->length; $i++ ) {
$pathElement = $pathTags->item( $i );
'@phan-var DOMElement $pathElement';
$pathType = $this->getAttributeValue( $pathElement, 'type' );
$path = $pathElement->textContent;
$site->setPath( $pathType, $path );
}
$idTags = $siteElement->getElementsByTagName( 'localid' );
for ( $i = 0; $i < $idTags->length; $i++ ) {
$idElement = $idTags->item( $i );
'@phan-var DOMElement $idElement';
$idType = $this->getAttributeValue( $idElement, 'type' );
$id = $idElement->textContent;
$site->addLocalId( $idType, $id );
}
// @todo: import <data>
// @todo: import <config>
return $site;
}
/**
* @param DOMElement $element
* @param string $name
* @param string|null|false $default
*
* @return null|string
*/
private function getAttributeValue( DOMElement $element, $name, $default = false ) {
$node = $element->getAttributeNode( $name );
if ( !$node ) {
if ( $default !== false ) {
return $default;
} else {
throw new RuntimeException(
'Required ' . $name . ' attribute not found in <' . $element->tagName . '> tag'
);
}
}
return $node->textContent;
}
/**
* @param DOMElement $element
* @param string $name
* @param string|null|false $default
*
* @return null|string
*/
private function getChildText( DOMElement $element, $name, $default = false ) {
$elements = $element->getElementsByTagName( $name );
if ( $elements->length < 1 ) {
if ( $default !== false ) {
return $default;
} else {
throw new RuntimeException(
'Required <' . $name . '> tag not found inside <' . $element->tagName . '> tag'
);
}
}
$node = $elements->item( 0 );
return $node->textContent;
}
/**
* @param DOMElement $element
* @param string $name
*
* @return bool
*/
private function hasChild( DOMElement $element, $name ) {
return $this->getChildText( $element, $name, null ) !== null;
}
/**
* @param Exception $ex
*/
private function handleException( Exception $ex ) {
if ( $this->exceptionCallback ) {
call_user_func( $this->exceptionCallback, $ex );
} else {
wfLogWarning( $ex->getMessage() );
}
}
}
/** @deprecated class alias since 1.42 */
class_alias( SiteImporter::class, 'SiteImporter' );
|