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