MediaWiki master
SiteExporter.php
Go to the documentation of this file.
1<?php
6
7namespace MediaWiki\Site;
8
9use InvalidArgumentException;
10use MediaWiki\Xml\Xml;
11
22
26 private $sink;
27
31 public function __construct( $sink ) {
32 // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.is_resource
33 if ( !is_resource( $sink ) || get_resource_type( $sink ) !== 'stream' ) {
34 throw new InvalidArgumentException( '$sink must be a file handle' );
35 }
36
37 $this->sink = $sink;
38 }
39
46 public function exportSites( $sites ) {
47 $attributes = [
48 'version' => '1.1',
49 'xmlns' => 'http://www.mediawiki.org/xml/sitelist-1.1/',
50 ];
51
52 fwrite( $this->sink, Xml::openElement( 'sites', $attributes ) . "\n" );
53
54 $orderedSites = [];
55 foreach ( $sites as $site ) {
56 $orderedSites[$site->getGlobalId()] = $site;
57 }
58 ksort( $orderedSites );
59
60 foreach ( $orderedSites as $site ) {
61 $this->exportSite( $site );
62 }
63
64 fwrite( $this->sink, Xml::closeElement( 'sites' ) . "\n" );
65 fflush( $this->sink );
66 }
67
71 private function exportSite( Site $site ) {
72 if ( $site->getType() !== Site::TYPE_UNKNOWN ) {
73 $siteAttr = [ 'type' => $site->getType() ];
74 } else {
75 $siteAttr = null;
76 }
77
78 fwrite( $this->sink, "\t" . Xml::openElement( 'site', $siteAttr ) . "\n" );
79
80 fwrite( $this->sink, "\t\t" . Xml::element( 'globalid', null, $site->getGlobalId() ) . "\n" );
81
82 if ( $site->getGroup() !== Site::GROUP_NONE ) {
83 fwrite( $this->sink, "\t\t" . Xml::element( 'group', null, $site->getGroup() ) . "\n" );
84 }
85
86 if ( $site->getLanguageCode() !== null ) {
87 fwrite( $this->sink, "\t\t" . Xml::element( 'language', null, $site->getLanguageCode() ) . "\n" );
88 }
89
90 foreach ( $site->getAllPaths() as $type => $path ) {
91 fwrite( $this->sink, "\t\t" . Xml::element( 'path', [ 'type' => $type ], $path ) . "\n" );
92 }
93
94 foreach ( $site->getLocalIds() as $type => $ids ) {
95 foreach ( $ids as $id ) {
96 fwrite( $this->sink, "\t\t" . Xml::element( 'localid', [ 'type' => $type ], $id ) . "\n" );
97 }
98 }
99
100 // @todo: export <data>
101 // @todo: export <config>
102
103 fwrite( $this->sink, "\t" . Xml::closeElement( 'site' ) . "\n" );
104 }
105
106}
107
109class_alias( SiteExporter::class, 'SiteExporter' );
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition Html.php:308
static closeElement( $element)
Returns "</$element>".
Definition Html.php:345
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml\element()).
Definition Html.php:284
Utility for exporting site entries to XML.
exportSites( $sites)
Writes a <site> tag for each Site object in $sites, and encloses the entire list between <sites> tags...
Represents a single site.
Definition Site.php:20
getGroup()
Gets the group of the site (ie wikipedia).
Definition Site.php:163
getAllPaths()
Returns the paths as associative array.
Definition Site.php:572
getType()
Returns the type of the site (ie mediawiki).
Definition Site.php:152
getLocalIds()
Returns all local ids.
Definition Site.php:533
getLanguageCode()
Returns language code of the sites primary language.
Definition Site.php:421
const TYPE_UNKNOWN
Definition Site.php:21
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition Site.php:131