Debian Codegraph @

includes/Feed/FeedItem.php

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