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.
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.
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
21 namespace MediaWiki\Site
;
23 use InvalidArgumentException
;
24 use MediaWiki\Xml\Xml
;
27 * Utility for exporting site entries to XML.
29 * For the output file format, see docs/sitelist.md and docs/sitelist-1.0.xsd.
33 * @author Daniel Kinzler
43 * @param resource $sink A file handle open for writing
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' );
55 * Writes a <site> tag for each Site object in $sites, and encloses the entire list
56 * between <sites> tags.
58 * @param Site[]|SiteList $sites
60 public function exportSites( $sites ) {
63 'xmlns' => 'http://www.mediawiki.org/xml/sitelist-1.0/',
66 fwrite( $this->sink
, Xml
::openElement( 'sites', $attributes ) . "\n" );
68 foreach ( $sites as $site ) {
69 $this->exportSite( $site );
72 fwrite( $this->sink
, Xml
::closeElement( 'sites' ) . "\n" );
73 fflush( $this->sink
);
77 * Writes a <site> tag representing the given Site object.
81 private function exportSite( Site
$site ) {
82 if ( $site->getType() !== Site
::TYPE_UNKNOWN
) {
83 $siteAttr = [ 'type' => $site->getType() ];
88 fwrite( $this->sink
, "\t" . Xml
::openElement( 'site', $siteAttr ) . "\n" );
90 fwrite( $this->sink
, "\t\t" . Xml
::element( 'globalid', null, $site->getGlobalId() ) . "\n" );
92 if ( $site->getGroup() !== Site
::GROUP_NONE
) {
93 fwrite( $this->sink
, "\t\t" . Xml
::element( 'group', null, $site->getGroup() ) . "\n" );
96 if ( $site->getSource() !== Site
::SOURCE_LOCAL
) {
97 fwrite( $this->sink
, "\t\t" . Xml
::element( 'source', null, $site->getSource() ) . "\n" );
100 if ( $site->shouldForward() ) {
101 fwrite( $this->sink
, "\t\t" . Xml
::element( 'forward', null, '' ) . "\n" );
104 foreach ( $site->getAllPaths() as $type => $path ) {
105 fwrite( $this->sink
, "\t\t" . Xml
::element( 'path', [ 'type' => $type ], $path ) . "\n" );
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" );
114 // @todo: export <data>
115 // @todo: export <config>
117 fwrite( $this->sink
, "\t" . Xml
::closeElement( 'site' ) . "\n" );
122 /** @deprecated class alias since 1.42 */
123 class_alias( SiteExporter
::class, 'SiteExporter' );