Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 18
CRAP
0.00% covered (danger)
0.00%
0 / 1
FeedItem
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 18
462
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 xmlEncode
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 xmlEncodeNullable
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 getUniqueID
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getUniqueIdUnescaped
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 setUniqueId
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUrl
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUrlUnescaped
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDescriptionUnescaped
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLanguage
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getDate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAuthor
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAuthorUnescaped
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getComments
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCommentsUnescaped
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 stripComment
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2004 Brooke Vibber <bvibber@wikimedia.org>
4 * https://www.mediawiki.org/
5 *
6 * @license GPL-2.0-or-later
7 * @file
8 */
9
10namespace MediaWiki\Feed;
11
12use MediaWiki\Language\LanguageCode;
13use MediaWiki\MainConfigNames;
14use MediaWiki\MediaWikiServices;
15use MediaWiki\Utils\UrlUtils;
16
17/**
18 * @defgroup Feed Feed
19 */
20
21/**
22 * A base class for outputting syndication feeds (e.g. RSS and other formats).
23 *
24 * @ingroup Feed
25 */
26class FeedItem {
27    /** @var string */
28    public $title;
29
30    /** @var string */
31    public $description;
32
33    /** @var string */
34    public $url;
35
36    /** @var string */
37    public $date;
38
39    /** @var string */
40    public $author;
41
42    /** @var string */
43    public $uniqueId;
44
45    /** @var string */
46    public $comments;
47
48    /** @var bool */
49    public $rssIsPermalink = false;
50
51    protected UrlUtils $urlUtils;
52
53    /**
54     * @param string $title Item's title
55     * @param string $description
56     * @param string $url URL uniquely designating the item.
57     * @param string $date Item's date
58     * @param string $author Author's user name
59     * @param string $comments
60     */
61    public function __construct(
62        $title, $description, $url, $date = '', $author = '', $comments = ''
63    ) {
64        $this->title = $title;
65        $this->description = $description;
66        $this->url = $url;
67        $this->uniqueId = $url;
68        $this->date = $date;
69        $this->author = $author;
70        $this->comments = $comments;
71        $this->urlUtils = MediaWikiServices::getInstance()->getUrlUtils();
72    }
73
74    /**
75     * Encode $string so that it can be safely embedded in a XML document
76     *
77     * @param string $string String to encode
78     *
79     * @return string
80     */
81    public function xmlEncode( $string ) {
82        $string = str_replace( "\r\n", "\n", $string );
83        $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
84
85        return htmlspecialchars( $string );
86    }
87
88    /**
89     * Encode $string so that it can be safely embedded in a XML document,
90     * returning `null` if $string was `null`.
91     * @since 1.44 (also backported to 1.39.12, 1.42.6 and 1.43.1)
92     */
93    public function xmlEncodeNullable( ?string $string ): ?string {
94        return $string !== null ? $this->xmlEncode( $string ) : null;
95    }
96
97    /**
98     * Get the unique id of this item; already xml-encoded
99     *
100     * @return string
101     */
102    public function getUniqueID() {
103        $id = $this->getUniqueIdUnescaped();
104        if ( $id ) {
105            return $this->xmlEncode( $id );
106        }
107    }
108
109    /**
110     * Get the unique id of this item, without any escaping
111     *
112     * @return string|null
113     */
114    public function getUniqueIdUnescaped(): ?string {
115        if ( $this->uniqueId ) {
116            return $this->urlUtils->expand( $this->uniqueId, PROTO_CURRENT );
117        }
118
119        return null;
120    }
121
122    /**
123     * Set the unique id of an item
124     *
125     * @param string $uniqueId Unique id for the item
126     * @param bool $rssIsPermalink Set to true if the guid (unique id) is a permalink (RSS feeds only)
127     */
128    public function setUniqueId( $uniqueId, $rssIsPermalink = false ) {
129        $this->uniqueId = $uniqueId;
130        $this->rssIsPermalink = $rssIsPermalink;
131    }
132
133    /**
134     * Get the title of this item; already xml-encoded
135     *
136     * @return string
137     */
138    public function getTitle() {
139        return $this->xmlEncode( $this->title );
140    }
141
142    /**
143     * Get the URL of this item; already xml-encoded
144     *
145     * @return string
146     */
147    public function getUrl() {
148        return $this->xmlEncode( $this->url );
149    }
150
151    /** Get the URL of this item without any escaping
152     *
153     * @return string
154     */
155    public function getUrlUnescaped() {
156        return $this->url;
157    }
158
159    /**
160     * Get the description of this item; already xml-encoded
161     *
162     * @return string
163     */
164    public function getDescription() {
165        return $this->xmlEncode( $this->description );
166    }
167
168    /**
169     * Get the description of this item without any escaping
170     *
171     * @return string
172     */
173    public function getDescriptionUnescaped() {
174        return $this->description;
175    }
176
177    /**
178     * Get the language of this item
179     *
180     * @return string
181     */
182    public function getLanguage() {
183        $languageCode = MediaWikiServices::getInstance()->getMainConfig()
184            ->get( MainConfigNames::LanguageCode );
185        return LanguageCode::bcp47( $languageCode );
186    }
187
188    /**
189     * Get the date of this item
190     *
191     * @return string
192     */
193    public function getDate() {
194        return $this->date;
195    }
196
197    /**
198     * Get the author of this item; already xml-encoded
199     *
200     * @return string
201     */
202    public function getAuthor() {
203        return $this->xmlEncode( $this->author );
204    }
205
206    /**
207     * Get the author of this item without any escaping
208     *
209     * @return string
210     */
211    public function getAuthorUnescaped() {
212        return $this->author;
213    }
214
215    /**
216     * Get the comment of this item; already xml-encoded
217     *
218     * @return string
219     */
220    public function getComments() {
221        return $this->xmlEncode( $this->comments );
222    }
223
224    /**
225     * Get the comment of this item without any escaping
226     *
227     * @return string
228     */
229    public function getCommentsUnescaped() {
230        return $this->comments;
231    }
232
233    /**
234     * Quickie hack... strip out wikilinks to more legible form from the comment.
235     *
236     * @param string $text Wikitext
237     *
238     * @return string
239     */
240    public static function stripComment( $text ) {
241        return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
242    }
243
244}