Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 49 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
SitemapFeed | |
0.00% |
0 / 49 |
|
0.00% |
0 / 7 |
132 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
setPublicationLang | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setPublicationName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
contentType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
outHeader | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
outItem | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
30 | |||
outFooter | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\GoogleNewsSitemap; |
4 | |
5 | use MediaWiki\Feed\ChannelFeed; |
6 | use MediaWiki\Feed\FeedItem; |
7 | use UnexpectedValueException; |
8 | use XMLWriter; |
9 | |
10 | class SitemapFeed extends ChannelFeed { |
11 | private $writer; |
12 | private $publicationName; |
13 | private $publicationLang; |
14 | |
15 | public function __construct() { |
16 | global $wgSitename, $wgLanguageCode; |
17 | |
18 | $this->writer = new XMLWriter(); |
19 | $this->publicationName = $wgSitename; |
20 | $this->publicationLang = $wgLanguageCode; |
21 | } |
22 | |
23 | /** |
24 | * Set the publication language code. Only used if different from |
25 | * $wgLanguageCode, which could happen if Google disagrees with us |
26 | * on say what code zh gets. |
27 | * @param string $lang Language code (like en) |
28 | */ |
29 | public function setPublicationLang( $lang ) { |
30 | $this->publicationLang = $lang; |
31 | } |
32 | |
33 | /** |
34 | * Set the publication name. Normally $wgSitename, but could |
35 | * need to be changed, if Google gives the publication a different |
36 | * name then $wgSitename. |
37 | * @param string $name The name of the publication |
38 | */ |
39 | public function setPublicationName( $name ) { |
40 | $this->publicationName = $name; |
41 | } |
42 | |
43 | public function contentType() { |
44 | return 'application/xml'; |
45 | } |
46 | |
47 | /** |
48 | * Output feed headers. |
49 | */ |
50 | public function outHeader() { |
51 | $this->httpHeaders(); |
52 | |
53 | $this->writer->openURI( 'php://output' ); |
54 | $this->writer->setIndent( true ); |
55 | $this->writer->startDocument( '1.0', 'UTF-8' ); |
56 | $this->writer->startElement( 'urlset' ); |
57 | $this->writer->writeAttribute( 'xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9' ); |
58 | $this->writer->writeAttribute( 'xmlns:news', 'http://www.google.com/schemas/sitemap-news/0.9' ); |
59 | } |
60 | |
61 | /** |
62 | * Output a SiteMap 0.9 item |
63 | * @param FeedItem $item to be output |
64 | */ |
65 | public function outItem( $item ) { |
66 | if ( !( $item instanceof FeedItem ) ) { |
67 | throw new UnexpectedValueException( 'Requires a FeedItem or subclass.' ); |
68 | } |
69 | |
70 | if ( !( $item instanceof FeedSMItem ) ) { |
71 | $item = FeedSMItem::newFromFeedItem( $item ); |
72 | } |
73 | |
74 | $this->writer->startElement( 'url' ); |
75 | |
76 | $this->writer->startElement( 'loc' ); |
77 | $this->writer->text( $item->getUrl() ); |
78 | $this->writer->endElement(); |
79 | |
80 | $this->writer->startElement( 'news:news' ); |
81 | |
82 | $this->writer->startElement( 'news:publication_date' ); |
83 | $this->writer->text( wfTimestamp( TS_ISO_8601, $item->getDate() ) ); |
84 | $this->writer->endElement(); |
85 | |
86 | $this->writer->startElement( 'news:title' ); |
87 | $this->writer->text( $item->getTitle() ); |
88 | $this->writer->endElement(); |
89 | |
90 | $this->writer->startElement( 'news:publication' ); |
91 | $this->writer->startElement( 'news:name' ); |
92 | $this->writer->text( $this->publicationName ); |
93 | $this->writer->endElement(); |
94 | $this->writer->startElement( 'news:language' ); |
95 | $this->writer->text( $this->publicationLang ); |
96 | $this->writer->endElement(); |
97 | // @phan-suppress-next-line PhanPluginDuplicateAdjacentStatement |
98 | $this->writer->endElement(); |
99 | |
100 | if ( $item->getKeywords() ) { |
101 | $this->writer->startElement( 'news:keywords' ); |
102 | $this->writer->text( $item->getKeywords() ); |
103 | $this->writer->endElement(); |
104 | } |
105 | |
106 | // end news:news |
107 | $this->writer->endElement(); |
108 | if ( $item->getLastMod() ) { |
109 | $this->writer->startElement( 'lastmod' ); |
110 | $this->writer->text( wfTimestamp( TS_ISO_8601, $item->getLastMod() ) ); |
111 | $this->writer->endElement(); |
112 | } |
113 | // end url |
114 | $this->writer->endElement(); |
115 | } |
116 | |
117 | /** |
118 | * Output SiteMap 0.9 footer |
119 | */ |
120 | public function outFooter() { |
121 | $this->writer->endDocument(); |
122 | $this->writer->flush(); |
123 | } |
124 | } |