MediaWiki  1.34.0
SiteImporter.php
Go to the documentation of this file.
1 <?php
2 
30 class SiteImporter {
31 
35  private $store;
36 
41 
45  public function __construct( SiteStore $store ) {
46  $this->store = $store;
47  }
48 
52  public function getExceptionCallback() {
54  }
55 
60  $this->exceptionCallback = $exceptionCallback;
61  }
62 
66  public function importFromFile( $file ) {
67  $xml = file_get_contents( $file );
68 
69  if ( $xml === false ) {
70  throw new RuntimeException( 'Failed to read ' . $file . '!' );
71  }
72 
73  $this->importFromXML( $xml );
74  }
75 
81  public function importFromXML( $xml ) {
82  $document = new DOMDocument();
83 
84  $oldLibXmlErrors = libxml_use_internal_errors( true );
85  $ok = $document->loadXML( $xml, LIBXML_NONET );
86 
87  if ( !$ok ) {
88  $errors = libxml_get_errors();
89  libxml_use_internal_errors( $oldLibXmlErrors );
90 
91  foreach ( $errors as $error ) {
93  throw new InvalidArgumentException(
94  'Malformed XML: ' . $error->message . ' in line ' . $error->line
95  );
96  }
97 
98  throw new InvalidArgumentException( 'Malformed XML!' );
99  }
100 
101  libxml_use_internal_errors( $oldLibXmlErrors );
102  $this->importFromDOM( $document->documentElement );
103  }
104 
108  private function importFromDOM( DOMElement $root ) {
109  $sites = $this->makeSiteList( $root );
110  $this->store->saveSites( $sites );
111  }
112 
118  private function makeSiteList( DOMElement $root ) {
119  $sites = [];
120 
121  // Old sites, to get the row IDs that correspond to the global site IDs.
122  // TODO: Get rid of internal row IDs, they just get in the way. Get rid of ORMRow, too.
123  $oldSites = $this->store->getSites();
124 
125  $current = $root->firstChild;
126  while ( $current ) {
127  if ( $current instanceof DOMElement && $current->tagName === 'site' ) {
128  try {
129  $site = $this->makeSite( $current );
130  $key = $site->getGlobalId();
131 
132  if ( $oldSites->hasSite( $key ) ) {
133  $oldSite = $oldSites->getSite( $key );
134  $site->setInternalId( $oldSite->getInternalId() );
135  }
136 
137  $sites[$key] = $site;
138  } catch ( Exception $ex ) {
139  $this->handleException( $ex );
140  }
141  }
142 
143  $current = $current->nextSibling;
144  }
145 
146  return $sites;
147  }
148 
155  public function makeSite( DOMElement $siteElement ) {
156  if ( $siteElement->tagName !== 'site' ) {
157  throw new InvalidArgumentException( 'Expected <site> tag, found ' . $siteElement->tagName );
158  }
159 
160  $type = $this->getAttributeValue( $siteElement, 'type', Site::TYPE_UNKNOWN );
161  $site = Site::newForType( $type );
162 
163  $site->setForward( $this->hasChild( $siteElement, 'forward' ) );
164  $site->setGlobalId( $this->getChildText( $siteElement, 'globalid' ) );
165  $site->setGroup( $this->getChildText( $siteElement, 'group', Site::GROUP_NONE ) );
166  $site->setSource( $this->getChildText( $siteElement, 'source', Site::SOURCE_LOCAL ) );
167 
168  $pathTags = $siteElement->getElementsByTagName( 'path' );
169  for ( $i = 0; $i < $pathTags->length; $i++ ) {
170  $pathElement = $pathTags->item( $i );
171  '@phan-var DOMElement $pathElement';
172  $pathType = $this->getAttributeValue( $pathElement, 'type' );
173  $path = $pathElement->textContent;
174 
175  $site->setPath( $pathType, $path );
176  }
177 
178  $idTags = $siteElement->getElementsByTagName( 'localid' );
179  for ( $i = 0; $i < $idTags->length; $i++ ) {
180  $idElement = $idTags->item( $i );
181  '@phan-var DOMElement $idElement';
182  $idType = $this->getAttributeValue( $idElement, 'type' );
183  $id = $idElement->textContent;
184 
185  $site->addLocalId( $idType, $id );
186  }
187 
188  // @todo: import <data>
189  // @todo: import <config>
190 
191  return $site;
192  }
193 
202  private function getAttributeValue( DOMElement $element, $name, $default = false ) {
203  $node = $element->getAttributeNode( $name );
204 
205  if ( !$node ) {
206  if ( $default !== false ) {
207  return $default;
208  } else {
209  throw new MWException(
210  'Required ' . $name . ' attribute not found in <' . $element->tagName . '> tag'
211  );
212  }
213  }
214 
215  return $node->textContent;
216  }
217 
226  private function getChildText( DOMElement $element, $name, $default = false ) {
227  $elements = $element->getElementsByTagName( $name );
228 
229  if ( $elements->length < 1 ) {
230  if ( $default !== false ) {
231  return $default;
232  } else {
233  throw new MWException(
234  'Required <' . $name . '> tag not found inside <' . $element->tagName . '> tag'
235  );
236  }
237  }
238 
239  $node = $elements->item( 0 );
240  return $node->textContent;
241  }
242 
250  private function hasChild( DOMElement $element, $name ) {
251  return $this->getChildText( $element, $name, null ) !== null;
252  }
253 
257  private function handleException( Exception $ex ) {
258  if ( $this->exceptionCallback ) {
259  call_user_func( $this->exceptionCallback, $ex );
260  } else {
261  wfLogWarning( $ex->getMessage() );
262  }
263  }
264 
265 }
SiteImporter\getExceptionCallback
getExceptionCallback()
Definition: SiteImporter.php:52
SiteImporter\$exceptionCallback
callable null $exceptionCallback
Definition: SiteImporter.php:40
SiteImporter\getAttributeValue
getAttributeValue(DOMElement $element, $name, $default=false)
Definition: SiteImporter.php:202
SiteImporter\hasChild
hasChild(DOMElement $element, $name)
Definition: SiteImporter.php:250
SiteImporter\importFromXML
importFromXML( $xml)
Definition: SiteImporter.php:81
SiteImporter\makeSiteList
makeSiteList(DOMElement $root)
Definition: SiteImporter.php:118
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
SiteImporter
Definition: SiteImporter.php:30
wfLogWarning
wfLogWarning( $msg, $callerOffset=1, $level=E_USER_WARNING)
Send a warning as a PHP error and the debug log.
Definition: GlobalFunctions.php:1078
SiteImporter\__construct
__construct(SiteStore $store)
Definition: SiteImporter.php:45
SiteImporter\$store
SiteStore $store
Definition: SiteImporter.php:35
Site\newForType
static newForType( $siteType)
Definition: Site.php:646
SiteStore
Definition: SiteStore.php:29
MWException
MediaWiki exception.
Definition: MWException.php:26
Site\TYPE_UNKNOWN
const TYPE_UNKNOWN
Definition: Site.php:30
SiteImporter\getChildText
getChildText(DOMElement $element, $name, $default=false)
Definition: SiteImporter.php:226
SiteImporter\setExceptionCallback
setExceptionCallback( $exceptionCallback)
Definition: SiteImporter.php:59
SiteImporter\handleException
handleException(Exception $ex)
Definition: SiteImporter.php:257
$path
$path
Definition: NoLocalSettings.php:25
Site\GROUP_NONE
const GROUP_NONE
Definition: Site.php:33
Site\SOURCE_LOCAL
const SOURCE_LOCAL
Definition: Site.php:38
SiteImporter\importFromDOM
importFromDOM(DOMElement $root)
Definition: SiteImporter.php:108
SiteImporter\importFromFile
importFromFile( $file)
Definition: SiteImporter.php:66
SiteImporter\makeSite
makeSite(DOMElement $siteElement)
Definition: SiteImporter.php:155
$type
$type
Definition: testCompression.php:48