Merge "Move up devunt's name to Developers"
[lhc/web/wiklou.git] / includes / site / SiteExporter.php
1 <?php
2
3 /**
4 * Utility for exporting site entries to XML.
5 * For the output file format, 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 SiteExporter {
31
32 /**
33 * @var resource
34 */
35 private $sink;
36
37 /**
38 * @param resource $sink A file handle open for writing
39 */
40 public function __construct( $sink ) {
41 if ( !is_resource( $sink ) || get_resource_type( $sink ) !== 'stream' ) {
42 throw new InvalidArgumentException( '$sink must be a file handle' );
43 }
44
45 $this->sink = $sink;
46 }
47
48 /**
49 * Writes a <site> tag for each Site object in $sites, and encloses the entire list
50 * between <sites> tags.
51 *
52 * @param Site[]|SiteList $sites
53 */
54 public function exportSites( $sites ) {
55 $attributes = [
56 'version' => '1.0',
57 'xmlns' => 'http://www.mediawiki.org/xml/sitelist-1.0/',
58 ];
59
60 fwrite( $this->sink, Xml::openElement( 'sites', $attributes ) . "\n" );
61
62 foreach ( $sites as $site ) {
63 $this->exportSite( $site );
64 }
65
66 fwrite( $this->sink, Xml::closeElement( 'sites' ) . "\n" );
67 fflush( $this->sink );
68 }
69
70 /**
71 * Writes a <site> tag representing the given Site object.
72 *
73 * @param Site $site
74 */
75 private function exportSite( Site $site ) {
76 if ( $site->getType() !== Site::TYPE_UNKNOWN ) {
77 $siteAttr = [ 'type' => $site->getType() ];
78 } else {
79 $siteAttr = null;
80 }
81
82 fwrite( $this->sink, "\t" . Xml::openElement( 'site', $siteAttr ) . "\n" );
83
84 fwrite( $this->sink, "\t\t" . Xml::element( 'globalid', null, $site->getGlobalId() ) . "\n" );
85
86 if ( $site->getGroup() !== Site::GROUP_NONE ) {
87 fwrite( $this->sink, "\t\t" . Xml::element( 'group', null, $site->getGroup() ) . "\n" );
88 }
89
90 if ( $site->getSource() !== Site::SOURCE_LOCAL ) {
91 fwrite( $this->sink, "\t\t" . Xml::element( 'source', null, $site->getSource() ) . "\n" );
92 }
93
94 if ( $site->shouldForward() ) {
95 fwrite( $this->sink, "\t\t" . Xml::element( 'forward', null, '' ) . "\n" );
96 }
97
98 foreach ( $site->getAllPaths() as $type => $path ) {
99 fwrite( $this->sink, "\t\t" . Xml::element( 'path', [ 'type' => $type ], $path ) . "\n" );
100 }
101
102 foreach ( $site->getLocalIds() as $type => $ids ) {
103 foreach ( $ids as $id ) {
104 fwrite( $this->sink, "\t\t" . Xml::element( 'localid', [ 'type' => $type ], $id ) . "\n" );
105 }
106 }
107
108 // @todo: export <data>
109 // @todo: export <config>
110
111 fwrite( $this->sink, "\t" . Xml::closeElement( 'site' ) . "\n" );
112 }
113
114 }