MediaWiki
master
Toggle main menu visibility
SiteExporter.php
Go to the documentation of this file.
1
<?php
6
7
namespace
MediaWiki\Site;
8
9
use InvalidArgumentException;
10
use MediaWiki\Xml\Xml;
11
21
class
SiteExporter
{
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.1'
,
49
'xmlns'
=>
'http://www.mediawiki.org/xml/sitelist-1.1/'
,
50
];
51
52
fwrite( $this->sink,
Xml::openElement
(
'sites'
, $attributes ) .
"\n"
);
53
54
$orderedSites = [];
55
foreach
( $sites as $site ) {
56
$orderedSites[$site->getGlobalId()] = $site;
57
}
58
ksort( $orderedSites );
59
60
foreach
( $orderedSites as $site ) {
61
$this->exportSite( $site );
62
}
63
64
fwrite( $this->sink,
Xml::closeElement
(
'sites'
) .
"\n"
);
65
fflush( $this->sink );
66
}
67
71
private
function
exportSite(
Site
$site ) {
72
if
( $site->
getType
() !==
Site::TYPE_UNKNOWN
) {
73
$siteAttr = [
'type'
=> $site->
getType
() ];
74
}
else
{
75
$siteAttr =
null
;
76
}
77
78
fwrite( $this->sink,
"\t"
.
Xml::openElement
(
'site'
, $siteAttr ) .
"\n"
);
79
80
fwrite( $this->sink,
"\t\t"
.
Xml::element
(
'globalid'
,
null
, $site->
getGlobalId
() ) .
"\n"
);
81
82
if
( $site->
getGroup
() !==
Site::GROUP_NONE
) {
83
fwrite( $this->sink,
"\t\t"
.
Xml::element
(
'group'
,
null
, $site->
getGroup
() ) .
"\n"
);
84
}
85
86
if
( $site->
getLanguageCode
() !==
null
) {
87
fwrite( $this->sink,
"\t\t"
.
Xml::element
(
'language'
,
null
, $site->
getLanguageCode
() ) .
"\n"
);
88
}
89
90
foreach
( $site->
getAllPaths
() as $type =>
$path
) {
91
fwrite( $this->sink,
"\t\t"
.
Xml::element
(
'path'
, [
'type'
=> $type ],
$path
) .
"\n"
);
92
}
93
94
foreach
( $site->
getLocalIds
() as $type => $ids ) {
95
foreach
( $ids as $id ) {
96
fwrite( $this->sink,
"\t\t"
.
Xml::element
(
'localid'
, [
'type'
=> $type ], $id ) .
"\n"
);
97
}
98
}
99
100
// @todo: export <data>
101
// @todo: export <config>
102
103
fwrite( $this->sink,
"\t"
.
Xml::closeElement
(
'site'
) .
"\n"
);
104
}
105
106
}
107
109
class_alias( SiteExporter::class,
'SiteExporter'
);
$path
$path
Definition
NoLocalSettings.php:14
MediaWiki\Html\Html\openElement
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition
Html.php:308
MediaWiki\Html\Html\closeElement
static closeElement( $element)
Returns "</$element>".
Definition
Html.php:345
MediaWiki\Html\Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml\element()).
Definition
Html.php:284
MediaWiki\Site\SiteExporter
Utility for exporting site entries to XML.
Definition
SiteExporter.php:21
MediaWiki\Site\SiteExporter\__construct
__construct( $sink)
Definition
SiteExporter.php:31
MediaWiki\Site\SiteExporter\exportSites
exportSites( $sites)
Writes a <site> tag for each Site object in $sites, and encloses the entire list between <sites> tags...
Definition
SiteExporter.php:46
MediaWiki\Site\Site
Represents a single site.
Definition
Site.php:20
MediaWiki\Site\Site\GROUP_NONE
const GROUP_NONE
Definition
Site.php:24
MediaWiki\Site\Site\getGroup
getGroup()
Gets the group of the site (ie wikipedia).
Definition
Site.php:163
MediaWiki\Site\Site\getAllPaths
getAllPaths()
Returns the paths as associative array.
Definition
Site.php:572
MediaWiki\Site\Site\getType
getType()
Returns the type of the site (ie mediawiki).
Definition
Site.php:152
MediaWiki\Site\Site\getLocalIds
getLocalIds()
Returns all local ids.
Definition
Site.php:533
MediaWiki\Site\Site\getLanguageCode
getLanguageCode()
Returns language code of the sites primary language.
Definition
Site.php:421
MediaWiki\Site\Site\TYPE_UNKNOWN
const TYPE_UNKNOWN
Definition
Site.php:21
MediaWiki\Site\Site\getGlobalId
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition
Site.php:131
includes
Site
SiteExporter.php
Generated on
for MediaWiki by
1.18.0