| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Site; |
| 22 |
|
| 23 |
use DOMDocument; |
| 24 |
use DOMElement; |
| 25 |
use Exception; |
| 26 |
use InvalidArgumentException; |
| 27 |
use RuntimeException; |
| 28 |
use Wikimedia\RequestTimeout\TimeoutException; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class SiteImporter { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
private $store; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
private $exceptionCallback; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function __construct( SiteStore $store ) { |
| 55 |
$this->store = $store; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function getExceptionCallback() { |
| 62 |
return $this->exceptionCallback; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function setExceptionCallback( $exceptionCallback ) { |
| 69 |
$this->exceptionCallback = $exceptionCallback; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function importFromFile( $file ) { |
| 76 |
$xml = file_get_contents( $file ); |
| 77 |
|
| 78 |
if ( $xml === false ) { |
| 79 |
throw new RuntimeException( 'Failed to read ' . $file . '!' ); |
| 80 |
} |
| 81 |
|
| 82 |
$this->importFromXML( $xml ); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function importFromXML( $xml ) { |
| 90 |
$document = new DOMDocument(); |
| 91 |
|
| 92 |
$oldLibXmlErrors = libxml_use_internal_errors( true ); |
| 93 |
|
| 94 |
$oldDisable = @libxml_disable_entity_loader( true ); |
| 95 |
$ok = $document->loadXML( $xml, LIBXML_NONET ); |
| 96 |
|
| 97 |
if ( !$ok ) { |
| 98 |
$errors = libxml_get_errors(); |
| 99 |
libxml_use_internal_errors( $oldLibXmlErrors ); |
| 100 |
|
| 101 |
@libxml_disable_entity_loader( $oldDisable ); |
| 102 |
|
| 103 |
foreach ( $errors as $error ) { |
| 104 |
|
| 105 |
throw new InvalidArgumentException( |
| 106 |
'Malformed XML: ' . $error->message . ' in line ' . $error->line |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
throw new InvalidArgumentException( 'Malformed XML!' ); |
| 111 |
} |
| 112 |
|
| 113 |
libxml_use_internal_errors( $oldLibXmlErrors ); |
| 114 |
|
| 115 |
@libxml_disable_entity_loader( $oldDisable ); |
| 116 |
$sites = $this->makeSiteList( $document->documentElement ); |
| 117 |
$this->store->saveSites( $sites ); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
private function makeSiteList( DOMElement $root ) { |
| 126 |
$sites = []; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
$oldSites = $this->store->getSites(); |
| 131 |
|
| 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(); |
| 138 |
|
| 139 |
if ( $oldSites->hasSite( $key ) ) { |
| 140 |
$oldSite = $oldSites->getSite( $key ); |
| 141 |
$site->setInternalId( $oldSite->getInternalId() ); |
| 142 |
} |
| 143 |
|
| 144 |
$sites[$key] = $site; |
| 145 |
} catch ( TimeoutException $e ) { |
| 146 |
throw $e; |
| 147 |
} catch ( Exception $ex ) { |
| 148 |
$this->handleException( $ex ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
$current = $current->nextSibling; |
| 153 |
} |
| 154 |
|
| 155 |
return $sites; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function makeSite( DOMElement $siteElement ) { |
| 164 |
if ( $siteElement->tagName !== 'site' ) { |
| 165 |
throw new InvalidArgumentException( 'Expected <site> tag, found ' . $siteElement->tagName ); |
| 166 |
} |
| 167 |
|
| 168 |
$type = $this->getAttributeValue( $siteElement, 'type', Site::TYPE_UNKNOWN ); |
| 169 |
$site = Site::newForType( $type ); |
| 170 |
|
| 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 ) ); |
| 175 |
|
| 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; |
| 182 |
|
| 183 |
$site->setPath( $pathType, $path ); |
| 184 |
} |
| 185 |
|
| 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; |
| 192 |
|
| 193 |
$site->addLocalId( $idType, $id ); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
return $site; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
private function getAttributeValue( DOMElement $element, $name, $default = false ) { |
| 210 |
$node = $element->getAttributeNode( $name ); |
| 211 |
|
| 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' |
| 218 |
); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
return $node->textContent; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 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 RuntimeException( |
| 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 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
private function hasChild( DOMElement $element, $name ) { |
| 256 |
return $this->getChildText( $element, $name, null ) !== null; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
private function handleException( Exception $ex ) { |
| 263 |
if ( $this->exceptionCallback ) { |
| 264 |
call_user_func( $this->exceptionCallback, $ex ); |
| 265 |
} else { |
| 266 |
wfLogWarning( $ex->getMessage() ); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
class_alias( SiteImporter::class, 'SiteImporter' ); |