MediaWiki master
SiteExporter.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Site;
8
9use InvalidArgumentException;
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.0',
49 'xmlns' => 'http://www.mediawiki.org/xml/sitelist-1.0/',
50 ];
51
52 fwrite( $this->sink, Xml::openElement( 'sites', $attributes ) . "\n" );
53
54 foreach ( $sites as $site ) {
55 $this->exportSite( $site );
56 }
57
58 fwrite( $this->sink, Xml::closeElement( 'sites' ) . "\n" );
59 fflush( $this->sink );
60 }
61
65 private function exportSite( Site $site ) {
66 if ( $site->getType() !== Site::TYPE_UNKNOWN ) {
67 $siteAttr = [ 'type' => $site->getType() ];
68 } else {
69 $siteAttr = null;
70 }
71
72 fwrite( $this->sink, "\t" . Xml::openElement( 'site', $siteAttr ) . "\n" );
73
74 fwrite( $this->sink, "\t\t" . Xml::element( 'globalid', null, $site->getGlobalId() ) . "\n" );
75
76 if ( $site->getGroup() !== Site::GROUP_NONE ) {
77 fwrite( $this->sink, "\t\t" . Xml::element( 'group', null, $site->getGroup() ) . "\n" );
78 }
79
80 if ( $site->getSource() !== Site::SOURCE_LOCAL ) {
81 fwrite( $this->sink, "\t\t" . Xml::element( 'source', null, $site->getSource() ) . "\n" );
82 }
83
84 if ( $site->shouldForward() ) {
85 fwrite( $this->sink, "\t\t" . Xml::element( 'forward', null, '' ) . "\n" );
86 }
87
88 foreach ( $site->getAllPaths() as $type => $path ) {
89 fwrite( $this->sink, "\t\t" . Xml::element( 'path', [ 'type' => $type ], $path ) . "\n" );
90 }
91
92 foreach ( $site->getLocalIds() as $type => $ids ) {
93 foreach ( $ids as $id ) {
94 fwrite( $this->sink, "\t\t" . Xml::element( 'localid', [ 'type' => $type ], $id ) . "\n" );
95 }
96 }
97
98 // @todo: export <data>
99 // @todo: export <config>
100
101 fwrite( $this->sink, "\t" . Xml::closeElement( 'site' ) . "\n" );
102 }
103
104}
105
107class_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:255
static closeElement( $element)
Returns "</$element>".
Definition Html.php:319
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition Html.php:231
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:22
getGroup()
Gets the group of the site (ie wikipedia).
Definition Site.php:165
getAllPaths()
Returns the paths as associative array.
Definition Site.php:571
shouldForward()
Gets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition Site.php:208
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition Site.php:186
const SOURCE_LOCAL
Definition Site.php:31
getType()
Returns the type of the site (ie mediawiki).
Definition Site.php:154
getLocalIds()
Returns all local ids.
Definition Site.php:532
const TYPE_UNKNOWN
Definition Site.php:23
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition Site.php:133
Module of static functions for generating XML.
Definition Xml.php:19