Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.67% covered (success)
96.67%
29 / 30
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
SiteExporter
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
3 / 3
13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 exportSites
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 exportSite
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
8
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 InvalidArgumentException;
24use MediaWiki\Xml\Xml;
25
26/**
27 * Utility for exporting site entries to XML.
28 *
29 * For the output file format, see docs/sitelist.md and docs/sitelist-1.0.xsd.
30 *
31 * @since 1.25
32 * @ingroup Site
33 * @author Daniel Kinzler
34 */
35class SiteExporter {
36
37    /**
38     * @var resource
39     */
40    private $sink;
41
42    /**
43     * @param resource $sink A file handle open for writing
44     */
45    public function __construct( $sink ) {
46        // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.is_resource
47        if ( !is_resource( $sink ) || get_resource_type( $sink ) !== 'stream' ) {
48            throw new InvalidArgumentException( '$sink must be a file handle' );
49        }
50
51        $this->sink = $sink;
52    }
53
54    /**
55     * Writes a <site> tag for each Site object in $sites, and encloses the entire list
56     * between <sites> tags.
57     *
58     * @param Site[]|SiteList $sites
59     */
60    public function exportSites( $sites ) {
61        $attributes = [
62            'version' => '1.0',
63            'xmlns' => 'http://www.mediawiki.org/xml/sitelist-1.0/',
64        ];
65
66        fwrite( $this->sink, Xml::openElement( 'sites', $attributes ) . "\n" );
67
68        foreach ( $sites as $site ) {
69            $this->exportSite( $site );
70        }
71
72        fwrite( $this->sink, Xml::closeElement( 'sites' ) . "\n" );
73        fflush( $this->sink );
74    }
75
76    /**
77     * Writes a <site> tag representing the given Site object.
78     */
79    private function exportSite( Site $site ) {
80        if ( $site->getType() !== Site::TYPE_UNKNOWN ) {
81            $siteAttr = [ 'type' => $site->getType() ];
82        } else {
83            $siteAttr = null;
84        }
85
86        fwrite( $this->sink, "\t" . Xml::openElement( 'site', $siteAttr ) . "\n" );
87
88        fwrite( $this->sink, "\t\t" . Xml::element( 'globalid', null, $site->getGlobalId() ) . "\n" );
89
90        if ( $site->getGroup() !== Site::GROUP_NONE ) {
91            fwrite( $this->sink, "\t\t" . Xml::element( 'group', null, $site->getGroup() ) . "\n" );
92        }
93
94        if ( $site->getSource() !== Site::SOURCE_LOCAL ) {
95            fwrite( $this->sink, "\t\t" . Xml::element( 'source', null, $site->getSource() ) . "\n" );
96        }
97
98        if ( $site->shouldForward() ) {
99            fwrite( $this->sink, "\t\t" . Xml::element( 'forward', null, '' ) . "\n" );
100        }
101
102        foreach ( $site->getAllPaths() as $type => $path ) {
103            fwrite( $this->sink, "\t\t" . Xml::element( 'path', [ 'type' => $type ], $path ) . "\n" );
104        }
105
106        foreach ( $site->getLocalIds() as $type => $ids ) {
107            foreach ( $ids as $id ) {
108                fwrite( $this->sink, "\t\t" . Xml::element( 'localid', [ 'type' => $type ], $id ) . "\n" );
109            }
110        }
111
112        // @todo: export <data>
113        // @todo: export <config>
114
115        fwrite( $this->sink, "\t" . Xml::closeElement( 'site' ) . "\n" );
116    }
117
118}
119
120/** @deprecated class alias since 1.42 */
121class_alias( SiteExporter::class, 'SiteExporter' );