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
81 private function exportSite( Site $site ) {
82 if ( $site->getType() !== Site::TYPE_UNKNOWN ) {
83 $siteAttr = [ 'type' => $site->getType() ];
84 } else {
85 $siteAttr = null;
86 }
87
88 fwrite( $this->sink, "\t" . Xml::openElement( 'site', $siteAttr ) . "\n" );
89
90 fwrite( $this->sink, "\t\t" . Xml::element( 'globalid', null, $site->getGlobalId() ) . "\n" );
91
92 if ( $site->getGroup() !== Site::GROUP_NONE ) {
93 fwrite( $this->sink, "\t\t" . Xml::element( 'group', null, $site->getGroup() ) . "\n" );
94 }
95
96 if ( $site->getSource() !== Site::SOURCE_LOCAL ) {
97 fwrite( $this->sink, "\t\t" . Xml::element( 'source', null, $site->getSource() ) . "\n" );
98 }
99
100 if ( $site->shouldForward() ) {
101 fwrite( $this->sink, "\t\t" . Xml::element( 'forward', null, '' ) . "\n" );
102 }
103
104 foreach ( $site->getAllPaths() as $type => $path ) {
105 fwrite( $this->sink, "\t\t" . Xml::element( 'path', [ 'type' => $type ], $path ) . "\n" );
106 }
107
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" );
111 }
112 }
113
114 // @todo: export <data>
115 // @todo: export <config>
116
117 fwrite( $this->sink, "\t" . Xml::closeElement( 'site' ) . "\n" );
118 }
119
120}
121
123class_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