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 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     * @param Site $site
80     */
81    private function exportSite( Site $site ) {
82        if ( $site->getType() !== Site::TYPE_UNKNOWN ) {
83            $siteAttr = [ 'type' => $site->getType() ];
84        } else {
85            $siteAttr = null;
86        }
87
88        fwrite( $this->sink, "\t" . Xml::openElement( 'site', $siteAttr ) . "\n" );
89
90        fwrite( $this->sink, "\t\t" . Xml::element( 'globalid', null, $site->getGlobalId() ) . "\n" );
91
92        if ( $site->getGroup() !== Site::GROUP_NONE ) {
93            fwrite( $this->sink, "\t\t" . Xml::element( 'group', null, $site->getGroup() ) . "\n" );
94        }
95
96        if ( $site->getSource() !== Site::SOURCE_LOCAL ) {
97            fwrite( $this->sink, "\t\t" . Xml::element( 'source', null, $site->getSource() ) . "\n" );
98        }
99
100        if ( $site->shouldForward() ) {
101            fwrite( $this->sink, "\t\t" . Xml::element( 'forward', null, '' ) . "\n" );
102        }
103
104        foreach ( $site->getAllPaths() as $type => $path ) {
105            fwrite( $this->sink, "\t\t" . Xml::element( 'path', [ 'type' => $type ], $path ) . "\n" );
106        }
107
108        foreach ( $site->getLocalIds() as $type => $ids ) {
109            foreach ( $ids as $id ) {
110                fwrite( $this->sink, "\t\t" . Xml::element( 'localid', [ 'type' => $type ], $id ) . "\n" );
111            }
112        }
113
114        // @todo: export <data>
115        // @todo: export <config>
116
117        fwrite( $this->sink, "\t" . Xml::closeElement( 'site' ) . "\n" );
118    }
119
120}
121
122/** @deprecated class alias since 1.41 */
123class_alias( SiteExporter::class, 'SiteExporter' );