MediaWiki master
SiteExporter.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Site;
22
23use InvalidArgumentException;
25
36
40 private $sink;
41
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
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
79 private function exportSite( Site $site ) {
80 if ( $site->getType() !== Site::TYPE_UNKNOWN ) {
81 $siteAttr = [ 'type' => $site->getType() ];
82 } else {
83 $siteAttr = null;
84 }
85
86 fwrite( $this->sink, "\t" . Xml::openElement( 'site', $siteAttr ) . "\n" );
87
88 fwrite( $this->sink, "\t\t" . Xml::element( 'globalid', null, $site->getGlobalId() ) . "\n" );
89
90 if ( $site->getGroup() !== Site::GROUP_NONE ) {
91 fwrite( $this->sink, "\t\t" . Xml::element( 'group', null, $site->getGroup() ) . "\n" );
92 }
93
94 if ( $site->getSource() !== Site::SOURCE_LOCAL ) {
95 fwrite( $this->sink, "\t\t" . Xml::element( 'source', null, $site->getSource() ) . "\n" );
96 }
97
98 if ( $site->shouldForward() ) {
99 fwrite( $this->sink, "\t\t" . Xml::element( 'forward', null, '' ) . "\n" );
100 }
101
102 foreach ( $site->getAllPaths() as $type => $path ) {
103 fwrite( $this->sink, "\t\t" . Xml::element( 'path', [ 'type' => $type ], $path ) . "\n" );
104 }
105
106 foreach ( $site->getLocalIds() as $type => $ids ) {
107 foreach ( $ids as $id ) {
108 fwrite( $this->sink, "\t\t" . Xml::element( 'localid', [ 'type' => $type ], $id ) . "\n" );
109 }
110 }
111
112 // @todo: export <data>
113 // @todo: export <config>
114
115 fwrite( $this->sink, "\t" . Xml::closeElement( 'site' ) . "\n" );
116 }
117
118}
119
121class_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:240
static closeElement( $element)
Returns "</$element>".
Definition Html.php:304
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition Html.php:216
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:36
getGroup()
Gets the group of the site (ie wikipedia).
Definition Site.php:179
getAllPaths()
Returns the paths as associative array.
Definition Site.php:585
shouldForward()
Gets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition Site.php:222
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition Site.php:200
const SOURCE_LOCAL
Definition Site.php:45
getType()
Returns the type of the site (ie mediawiki).
Definition Site.php:168
getLocalIds()
Returns all local ids.
Definition Site.php:546
const TYPE_UNKNOWN
Definition Site.php:37
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition Site.php:147
Module of static functions for generating XML.
Definition Xml.php:37