MediaWiki REL1_35
SiteExporter.php
Go to the documentation of this file.
1<?php
2
31
35 private $sink;
36
40 public function __construct( $sink ) {
41 // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.is_resource
42 if ( !is_resource( $sink ) || get_resource_type( $sink ) !== 'stream' ) {
43 throw new InvalidArgumentException( '$sink must be a file handle' );
44 }
45
46 $this->sink = $sink;
47 }
48
55 public function exportSites( $sites ) {
56 $attributes = [
57 'version' => '1.0',
58 'xmlns' => 'http://www.mediawiki.org/xml/sitelist-1.0/',
59 ];
60
61 fwrite( $this->sink, Xml::openElement( 'sites', $attributes ) . "\n" );
62
63 foreach ( $sites as $site ) {
64 $this->exportSite( $site );
65 }
66
67 fwrite( $this->sink, Xml::closeElement( 'sites' ) . "\n" );
68 fflush( $this->sink );
69 }
70
76 private function exportSite( Site $site ) {
77 if ( $site->getType() !== Site::TYPE_UNKNOWN ) {
78 $siteAttr = [ 'type' => $site->getType() ];
79 } else {
80 $siteAttr = null;
81 }
82
83 fwrite( $this->sink, "\t" . Xml::openElement( 'site', $siteAttr ) . "\n" );
84
85 fwrite( $this->sink, "\t\t" . Xml::element( 'globalid', null, $site->getGlobalId() ) . "\n" );
86
87 if ( $site->getGroup() !== Site::GROUP_NONE ) {
88 fwrite( $this->sink, "\t\t" . Xml::element( 'group', null, $site->getGroup() ) . "\n" );
89 }
90
91 if ( $site->getSource() !== Site::SOURCE_LOCAL ) {
92 fwrite( $this->sink, "\t\t" . Xml::element( 'source', null, $site->getSource() ) . "\n" );
93 }
94
95 if ( $site->shouldForward() ) {
96 fwrite( $this->sink, "\t\t" . Xml::element( 'forward', null, '' ) . "\n" );
97 }
98
99 foreach ( $site->getAllPaths() as $type => $path ) {
100 fwrite( $this->sink, "\t\t" . Xml::element( 'path', [ 'type' => $type ], $path ) . "\n" );
101 }
102
103 foreach ( $site->getLocalIds() as $type => $ids ) {
104 foreach ( $ids as $id ) {
105 fwrite( $this->sink, "\t\t" . Xml::element( 'localid', [ 'type' => $type ], $id ) . "\n" );
106 }
107 }
108
109 // @todo: export <data>
110 // @todo: export <config>
111
112 fwrite( $this->sink, "\t" . Xml::closeElement( 'site' ) . "\n" );
113 }
114
115}
exportSites( $sites)
Writes a <site> tag for each Site object in $sites, and encloses the entire list between <sites> tags...
resource $sink
__construct( $sink)
exportSite(Site $site)
Writes a <site> tag representing the given Site object.
Definition Site.php:31
shouldForward()
Gets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition Site.php:238
const GROUP_NONE
Definition Site.php:35
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition Site.php:209
getType()
Returns the type of the site (ie mediawiki).
Definition Site.php:170
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition Site.php:142
const TYPE_UNKNOWN
Definition Site.php:32
getAllPaths()
Returns the paths as associative array.
Definition Site.php:634
getLocalIds()
Returns all local ids.
Definition Site.php:585
getGroup()
Gets the group of the site (ie wikipedia).
Definition Site.php:181
const SOURCE_LOCAL
Definition Site.php:40