MediaWiki  master
SiteExporter.php
Go to the documentation of this file.
1 <?php
2 
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 }
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:33
shouldForward()
Gets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition: Site.php:240
const GROUP_NONE
Definition: Site.php:37
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition: Site.php:211
getType()
Returns the type of the site (ie mediawiki).
Definition: Site.php:172
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition: Site.php:144
const TYPE_UNKNOWN
Definition: Site.php:34
getAllPaths()
Returns the paths as associative array.
Definition: Site.php:633
getLocalIds()
Returns all local ids.
Definition: Site.php:584
getGroup()
Gets the group of the site (ie wikipedia).
Definition: Site.php:183
const SOURCE_LOCAL
Definition: Site.php:42
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:121
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:112
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:43