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