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    public function __construct( SiteStore $store ) {
52        $this->store = $store;
53    }
54
55    /**
56     * @return callable
57     */
58    public function getExceptionCallback() {
59        return $this->exceptionCallback;
60    }
61
62    /**
63     * @param callable $exceptionCallback
64     */
65    public function setExceptionCallback( $exceptionCallback ) {
66        $this->exceptionCallback = $exceptionCallback;
67    }
68
69    /**
70     * @param string $file
71     */
72    public function importFromFile( $file ) {
73        $xml = file_get_contents( $file );
74
75        if ( $xml === false ) {
76            throw new RuntimeException( 'Failed to read ' . $file . '!' );
77        }
78
79        $this->importFromXML( $xml );
80    }
81
82    /**
83     * @param string $xml
84     *
85     */
86    public function importFromXML( $xml ) {
87        $document = new DOMDocument();
88
89        $oldLibXmlErrors = libxml_use_internal_errors( true );
90        // phpcs:ignore Generic.PHP.NoSilencedErrors -- suppress deprecation per T268847
91        $oldDisable = @libxml_disable_entity_loader( true );
92        $ok = $document->loadXML( $xml, LIBXML_NONET );
93
94        if ( !$ok ) {
95            $errors = libxml_get_errors();
96            libxml_use_internal_errors( $oldLibXmlErrors );
97            // phpcs:ignore Generic.PHP.NoSilencedErrors
98            @libxml_disable_entity_loader( $oldDisable );
99
100            foreach ( $errors as $error ) {
101                /** @var LibXMLError $error */
102                throw new InvalidArgumentException(
103                    'Malformed XML: ' . $error->message . ' in line ' . $error->line
104                );
105            }
106
107            throw new InvalidArgumentException( 'Malformed XML!' );
108        }
109
110        libxml_use_internal_errors( $oldLibXmlErrors );
111        // phpcs:ignore Generic.PHP.NoSilencedErrors
112        @libxml_disable_entity_loader( $oldDisable );
113        $sites = $this->makeSiteList( $document->documentElement );
114        $this->store->saveSites( $sites );
115    }
116
117    /**
118     * @param DOMElement $root
119     *
120     * @return Site[]
121     */
122    private function makeSiteList( DOMElement $root ) {
123        $sites = [];
124
125        // Old sites, to get the row IDs that correspond to the global site IDs.
126        // TODO: Get rid of internal row IDs, they just get in the way. Get rid of ORMRow, too.
127        $oldSites = $this->store->getSites();
128
129        $current = $root->firstChild;
130        while ( $current ) {
131            if ( $current instanceof DOMElement && $current->tagName === 'site' ) {
132                try {
133                    $site = $this->makeSite( $current );
134                    $key = $site->getGlobalId();
135
136                    if ( $oldSites->hasSite( $key ) ) {
137                        $oldSite = $oldSites->getSite( $key );
138                        $site->setInternalId( $oldSite->getInternalId() );
139                    }
140
141                    $sites[$key] = $site;
142                } catch ( TimeoutException $e ) {
143                    throw $e;
144                } catch ( Exception $ex ) {
145                    $this->handleException( $ex );
146                }
147            }
148
149            $current = $current->nextSibling;
150        }
151
152        return $sites;
153    }
154
155    /**
156     * @param DOMElement $siteElement
157     *
158     * @return Site
159     */
160    public function makeSite( DOMElement $siteElement ) {
161        if ( $siteElement->tagName !== 'site' ) {
162            throw new InvalidArgumentException( 'Expected <site> tag, found ' . $siteElement->tagName );
163        }
164
165        $type = $this->getAttributeValue( $siteElement, 'type', Site::TYPE_UNKNOWN );
166        $site = Site::newForType( $type );
167
168        $site->setForward( $this->hasChild( $siteElement, 'forward' ) );
169        $site->setGlobalId( $this->getChildText( $siteElement, 'globalid' ) );
170        $site->setGroup( $this->getChildText( $siteElement, 'group', Site::GROUP_NONE ) );
171        $site->setSource( $this->getChildText( $siteElement, 'source', Site::SOURCE_LOCAL ) );
172
173        $pathTags = $siteElement->getElementsByTagName( 'path' );
174        for ( $i = 0; $i < $pathTags->length; $i++ ) {
175            $pathElement = $pathTags->item( $i );
176            '@phan-var DOMElement $pathElement';
177            $pathType = $this->getAttributeValue( $pathElement, 'type' );
178            $path = $pathElement->textContent;
179
180            $site->setPath( $pathType, $path );
181        }
182
183        $idTags = $siteElement->getElementsByTagName( 'localid' );
184        for ( $i = 0; $i < $idTags->length; $i++ ) {
185            $idElement = $idTags->item( $i );
186            '@phan-var DOMElement $idElement';
187            $idType = $this->getAttributeValue( $idElement, 'type' );
188            $id = $idElement->textContent;
189
190            $site->addLocalId( $idType, $id );
191        }
192
193        // @todo: import <data>
194        // @todo: import <config>
195
196        return $site;
197    }
198
199    /**
200     * @param DOMElement $element
201     * @param string $name
202     * @param string|null|false $default
203     *
204     * @return null|string
205     */
206    private function getAttributeValue( DOMElement $element, $name, $default = false ) {
207        $node = $element->getAttributeNode( $name );
208
209        if ( !$node ) {
210            if ( $default !== false ) {
211                return $default;
212            } else {
213                throw new RuntimeException(
214                    'Required ' . $name . ' attribute not found in <' . $element->tagName . '> tag'
215                );
216            }
217        }
218
219        return $node->textContent;
220    }
221
222    /**
223     * @param DOMElement $element
224     * @param string $name
225     * @param string|null|false $default
226     *
227     * @return null|string
228     */
229    private function getChildText( DOMElement $element, $name, $default = false ) {
230        $elements = $element->getElementsByTagName( $name );
231
232        if ( $elements->length < 1 ) {
233            if ( $default !== false ) {
234                return $default;
235            } else {
236                throw new RuntimeException(
237                    'Required <' . $name . '> tag not found inside <' . $element->tagName . '> tag'
238                );
239            }
240        }
241
242        $node = $elements->item( 0 );
243        return $node->textContent;
244    }
245
246    /**
247     * @param DOMElement $element
248     * @param string $name
249     *
250     * @return bool
251     */
252    private function hasChild( DOMElement $element, $name ) {
253        return $this->getChildText( $element, $name, null ) !== null;
254    }
255
256    private function handleException( Exception $ex ) {
257        if ( $this->exceptionCallback ) {
258            ( $this->exceptionCallback )( $ex );
259        } else {
260            wfLogWarning( $ex->getMessage() );
261        }
262    }
263
264}
265
266/** @deprecated class alias since 1.42 */
267class_alias( SiteImporter::class, 'SiteImporter' );