SiteImporter.php 6.6 KB

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