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