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 * 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
24namespace MediaWiki\Feed;
25
26use MediaWiki\Language\LanguageCode;
27use MediaWiki\MainConfigNames;
28use MediaWiki\MediaWikiServices;
29use 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 */
40class 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     * Encode $string so that it can be safely embedded in a XML document,
104     * returning `null` if $string was `null`.
105     * @since 1.44 (also backported to 1.39.12, 1.42.6 and 1.43.1)
106     */
107    public function xmlEncodeNullable( ?string $string ): ?string {
108        return $string !== null ? $this->xmlEncode( $string ) : null;
109    }
110
111    /**
112     * Get the unique id of this item; already xml-encoded
113     *
114     * @return string
115     */
116    public function getUniqueID() {
117        $id = $this->getUniqueIdUnescaped();
118        if ( $id ) {
119            return $this->xmlEncode( $id );
120        }
121    }
122
123    /**
124     * Get the unique id of this item, without any escaping
125     *
126     * @return string|null
127     */
128    public function getUniqueIdUnescaped(): ?string {
129        if ( $this->uniqueId ) {
130            return $this->urlUtils->expand( $this->uniqueId, PROTO_CURRENT );
131        }
132
133        return null;
134    }
135
136    /**
137     * Set the unique id of an item
138     *
139     * @param string $uniqueId Unique id for the item
140     * @param bool $rssIsPermalink Set to true if the guid (unique id) is a permalink (RSS feeds only)
141     */
142    public function setUniqueId( $uniqueId, $rssIsPermalink = false ) {
143        $this->uniqueId = $uniqueId;
144        $this->rssIsPermalink = $rssIsPermalink;
145    }
146
147    /**
148     * Get the title of this item; already xml-encoded
149     *
150     * @return string
151     */
152    public function getTitle() {
153        return $this->xmlEncode( $this->title );
154    }
155
156    /**
157     * Get the URL of this item; already xml-encoded
158     *
159     * @return string
160     */
161    public function getUrl() {
162        return $this->xmlEncode( $this->url );
163    }
164
165    /** Get the URL of this item without any escaping
166     *
167     * @return string
168     */
169    public function getUrlUnescaped() {
170        return $this->url;
171    }
172
173    /**
174     * Get the description of this item; already xml-encoded
175     *
176     * @return string
177     */
178    public function getDescription() {
179        return $this->xmlEncode( $this->description );
180    }
181
182    /**
183     * Get the description of this item without any escaping
184     *
185     * @return string
186     */
187    public function getDescriptionUnescaped() {
188        return $this->description;
189    }
190
191    /**
192     * Get the language of this item
193     *
194     * @return string
195     */
196    public function getLanguage() {
197        $languageCode = MediaWikiServices::getInstance()->getMainConfig()
198            ->get( MainConfigNames::LanguageCode );
199        return LanguageCode::bcp47( $languageCode );
200    }
201
202    /**
203     * Get the date of this item
204     *
205     * @return string
206     */
207    public function getDate() {
208        return $this->date;
209    }
210
211    /**
212     * Get the author of this item; already xml-encoded
213     *
214     * @return string
215     */
216    public function getAuthor() {
217        return $this->xmlEncode( $this->author );
218    }
219
220    /**
221     * Get the author of this item without any escaping
222     *
223     * @return string
224     */
225    public function getAuthorUnescaped() {
226        return $this->author;
227    }
228
229    /**
230     * Get the comment of this item; already xml-encoded
231     *
232     * @return string
233     */
234    public function getComments() {
235        return $this->xmlEncode( $this->comments );
236    }
237
238    /**
239     * Get the comment of this item without any escaping
240     *
241     * @return string
242     */
243    public function getCommentsUnescaped() {
244        return $this->comments;
245    }
246
247    /**
248     * Quickie hack... strip out wikilinks to more legible form from the comment.
249     *
250     * @param string $text Wikitext
251     *
252     * @return string
253     */
254    public static function stripComment( $text ) {
255        return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
256    }
257
258}