X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/includes/site/SiteExporter.php diff --git a/includes/site/SiteExporter.php b/includes/site/SiteExporter.php new file mode 100644 index 00000000..01b838ef --- /dev/null +++ b/includes/site/SiteExporter.php @@ -0,0 +1,114 @@ +sink = $sink; + } + + /** + * Writes a tag for each Site object in $sites, and encloses the entire list + * between tags. + * + * @param Site[]|SiteList $sites + */ + public function exportSites( $sites ) { + $attributes = [ + 'version' => '1.0', + 'xmlns' => 'http://www.mediawiki.org/xml/sitelist-1.0/', + ]; + + fwrite( $this->sink, Xml::openElement( 'sites', $attributes ) . "\n" ); + + foreach ( $sites as $site ) { + $this->exportSite( $site ); + } + + fwrite( $this->sink, Xml::closeElement( 'sites' ) . "\n" ); + fflush( $this->sink ); + } + + /** + * Writes a tag representing the given Site object. + * + * @param Site $site + */ + private function exportSite( Site $site ) { + if ( $site->getType() !== Site::TYPE_UNKNOWN ) { + $siteAttr = [ 'type' => $site->getType() ]; + } else { + $siteAttr = null; + } + + fwrite( $this->sink, "\t" . Xml::openElement( 'site', $siteAttr ) . "\n" ); + + fwrite( $this->sink, "\t\t" . Xml::element( 'globalid', null, $site->getGlobalId() ) . "\n" ); + + if ( $site->getGroup() !== Site::GROUP_NONE ) { + fwrite( $this->sink, "\t\t" . Xml::element( 'group', null, $site->getGroup() ) . "\n" ); + } + + if ( $site->getSource() !== Site::SOURCE_LOCAL ) { + fwrite( $this->sink, "\t\t" . Xml::element( 'source', null, $site->getSource() ) . "\n" ); + } + + if ( $site->shouldForward() ) { + fwrite( $this->sink, "\t\t" . Xml::element( 'forward', null, '' ) . "\n" ); + } + + foreach ( $site->getAllPaths() as $type => $path ) { + fwrite( $this->sink, "\t\t" . Xml::element( 'path', [ 'type' => $type ], $path ) . "\n" ); + } + + foreach ( $site->getLocalIds() as $type => $ids ) { + foreach ( $ids as $id ) { + fwrite( $this->sink, "\t\t" . Xml::element( 'localid', [ 'type' => $type ], $id ) . "\n" ); + } + } + + // @todo: export + // @todo: export + + fwrite( $this->sink, "\t" . Xml::closeElement( 'site' ) . "\n" ); + } + +}