changes: Split Feed.php into a class per file
[lhc/web/wiklou.git] / includes / changes / FeedItem.php
1 <?php
2 /**
3 * Copyright © 2004 Brion Vibber <brion@pobox.com>
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 /**
25 * @defgroup Feed Feed
26 */
27
28 /**
29 * A base class for outputting syndication feeds (e.g. RSS and other formats).
30 *
31 * @ingroup Feed
32 */
33 class FeedItem {
34 /** @var Title */
35 public $title;
36
37 public $description;
38
39 public $url;
40
41 public $date;
42
43 public $author;
44
45 public $uniqueId;
46
47 public $comments;
48
49 public $rssIsPermalink = false;
50
51 /**
52 * @param string|Title $title Item's title
53 * @param string $description
54 * @param string $url URL uniquely designating the item.
55 * @param string $date Item's date
56 * @param string $author Author's user name
57 * @param string $comments
58 */
59 function __construct( $title, $description, $url, $date = '', $author = '', $comments = '' ) {
60 $this->title = $title;
61 $this->description = $description;
62 $this->url = $url;
63 $this->uniqueId = $url;
64 $this->date = $date;
65 $this->author = $author;
66 $this->comments = $comments;
67 }
68
69 /**
70 * Encode $string so that it can be safely embedded in a XML document
71 *
72 * @param string $string String to encode
73 * @return string
74 */
75 public function xmlEncode( $string ) {
76 $string = str_replace( "\r\n", "\n", $string );
77 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
78 return htmlspecialchars( $string );
79 }
80
81 /**
82 * Get the unique id of this item; already xml-encoded
83 * @return string
84 */
85 public function getUniqueID() {
86 $id = $this->getUniqueIdUnescaped();
87 if ( $id ) {
88 return $this->xmlEncode( $id );
89 }
90 }
91
92 /**
93 * Get the unique id of this item, without any escaping
94 * @return string
95 */
96 public function getUniqueIdUnescaped() {
97 if ( $this->uniqueId ) {
98 return wfExpandUrl( $this->uniqueId, PROTO_CURRENT );
99 }
100 }
101
102 /**
103 * Set the unique id of an item
104 *
105 * @param string $uniqueId Unique id for the item
106 * @param bool $rssIsPermalink Set to true if the guid (unique id) is a permalink (RSS feeds only)
107 */
108 public function setUniqueId( $uniqueId, $rssIsPermalink = false ) {
109 $this->uniqueId = $uniqueId;
110 $this->rssIsPermalink = $rssIsPermalink;
111 }
112
113 /**
114 * Get the title of this item; already xml-encoded
115 *
116 * @return string
117 */
118 public function getTitle() {
119 return $this->xmlEncode( $this->title );
120 }
121
122 /**
123 * Get the URL of this item; already xml-encoded
124 *
125 * @return string
126 */
127 public function getUrl() {
128 return $this->xmlEncode( $this->url );
129 }
130
131 /** Get the URL of this item without any escaping
132 *
133 * @return string
134 */
135 public function getUrlUnescaped() {
136 return $this->url;
137 }
138
139 /**
140 * Get the description of this item; already xml-encoded
141 *
142 * @return string
143 */
144 public function getDescription() {
145 return $this->xmlEncode( $this->description );
146 }
147
148 /**
149 * Get the description of this item without any escaping
150 *
151 * @return string
152 */
153 public function getDescriptionUnescaped() {
154 return $this->description;
155 }
156
157 /**
158 * Get the language of this item
159 *
160 * @return string
161 */
162 public function getLanguage() {
163 global $wgLanguageCode;
164 return LanguageCode::bcp47( $wgLanguageCode );
165 }
166
167 /**
168 * Get the date of this item
169 *
170 * @return string
171 */
172 public function getDate() {
173 return $this->date;
174 }
175
176 /**
177 * Get the author of this item; already xml-encoded
178 *
179 * @return string
180 */
181 public function getAuthor() {
182 return $this->xmlEncode( $this->author );
183 }
184
185 /**
186 * Get the author of this item without any escaping
187 *
188 * @return string
189 */
190 public function getAuthorUnescaped() {
191 return $this->author;
192 }
193
194 /**
195 * Get the comment of this item; already xml-encoded
196 *
197 * @return string
198 */
199 public function getComments() {
200 return $this->xmlEncode( $this->comments );
201 }
202
203 /**
204 * Get the comment of this item without any escaping
205 *
206 * @return string
207 */
208 public function getCommentsUnescaped() {
209 return $this->comments;
210 }
211
212 /**
213 * Quickie hack... strip out wikilinks to more legible form from the comment.
214 *
215 * @param string $text Wikitext
216 * @return string
217 */
218 public static function stripComment( $text ) {
219 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
220 }
221 /**#@-*/
222 }