MediaWiki  master
SiteExporter.php
Go to the documentation of this file.
1 <?php
30 class SiteExporter {
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 }
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...
__construct( $sink)
Represents a single site.
Definition: Site.php:32
shouldForward()
Gets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition: Site.php:218
const GROUP_NONE
Definition: Site.php:36
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition: Site.php:196
getType()
Returns the type of the site (ie mediawiki).
Definition: Site.php:164
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition: Site.php:143
const TYPE_UNKNOWN
Definition: Site.php:33
getAllPaths()
Returns the paths as associative array.
Definition: Site.php:599
getLocalIds()
Returns all local ids.
Definition: Site.php:556
getGroup()
Gets the group of the site (ie wikipedia).
Definition: Site.php:175
const SOURCE_LOCAL
Definition: Site.php:41
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:120
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:111
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:46