Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
FeedItem
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 17
380
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
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
 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 / 2
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 LanguageCode;
27use MediaWiki\MainConfigNames;
28use MediaWiki\MediaWikiServices;
29use MediaWiki\Title\Title;
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 Title */
42    public $title;
43
44    public $description;
45
46    public $url;
47
48    public $date;
49
50    public $author;
51
52    public $uniqueId;
53
54    public $comments;
55
56    public $rssIsPermalink = false;
57
58    /**
59     * @param string|Title $title Item's title
60     * @param string $description
61     * @param string $url URL uniquely designating the item.
62     * @param string $date Item's date
63     * @param string $author Author's user name
64     * @param string $comments
65     */
66    public function __construct(
67        $title, $description, $url, $date = '', $author = '', $comments = ''
68    ) {
69        $this->title = $title;
70        $this->description = $description;
71        $this->url = $url;
72        $this->uniqueId = $url;
73        $this->date = $date;
74        $this->author = $author;
75        $this->comments = $comments;
76    }
77
78    /**
79     * Encode $string so that it can be safely embedded in a XML document
80     *
81     * @param string $string String to encode
82     * @return string
83     */
84    public function xmlEncode( $string ) {
85        $string = str_replace( "\r\n", "\n", $string );
86        $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
87        return htmlspecialchars( $string );
88    }
89
90    /**
91     * Get the unique id of this item; already xml-encoded
92     * @return string
93     */
94    public function getUniqueID() {
95        $id = $this->getUniqueIdUnescaped();
96        if ( $id ) {
97            return $this->xmlEncode( $id );
98        }
99    }
100
101    /**
102     * Get the unique id of this item, without any escaping
103     * @return string
104     */
105    public function getUniqueIdUnescaped() {
106        if ( $this->uniqueId ) {
107            return wfExpandUrl( $this->uniqueId, PROTO_CURRENT );
108        }
109    }
110
111    /**
112     * Set the unique id of an item
113     *
114     * @param string $uniqueId Unique id for the item
115     * @param bool $rssIsPermalink Set to true if the guid (unique id) is a permalink (RSS feeds only)
116     */
117    public function setUniqueId( $uniqueId, $rssIsPermalink = false ) {
118        $this->uniqueId = $uniqueId;
119        $this->rssIsPermalink = $rssIsPermalink;
120    }
121
122    /**
123     * Get the title of this item; already xml-encoded
124     *
125     * @return string
126     */
127    public function getTitle() {
128        return $this->xmlEncode( $this->title );
129    }
130
131    /**
132     * Get the URL of this item; already xml-encoded
133     *
134     * @return string
135     */
136    public function getUrl() {
137        return $this->xmlEncode( $this->url );
138    }
139
140    /** Get the URL of this item without any escaping
141     *
142     * @return string
143     */
144    public function getUrlUnescaped() {
145        return $this->url;
146    }
147
148    /**
149     * Get the description of this item; already xml-encoded
150     *
151     * @return string
152     */
153    public function getDescription() {
154        return $this->xmlEncode( $this->description );
155    }
156
157    /**
158     * Get the description of this item without any escaping
159     *
160     * @return string
161     */
162    public function getDescriptionUnescaped() {
163        return $this->description;
164    }
165
166    /**
167     * Get the language of this item
168     *
169     * @return string
170     */
171    public function getLanguage() {
172        $languageCode = MediaWikiServices::getInstance()->getMainConfig()
173            ->get( MainConfigNames::LanguageCode );
174        return LanguageCode::bcp47( $languageCode );
175    }
176
177    /**
178     * Get the date of this item
179     *
180     * @return string
181     */
182    public function getDate() {
183        return $this->date;
184    }
185
186    /**
187     * Get the author of this item; already xml-encoded
188     *
189     * @return string
190     */
191    public function getAuthor() {
192        return $this->xmlEncode( $this->author );
193    }
194
195    /**
196     * Get the author of this item without any escaping
197     *
198     * @return string
199     */
200    public function getAuthorUnescaped() {
201        return $this->author;
202    }
203
204    /**
205     * Get the comment of this item; already xml-encoded
206     *
207     * @return string
208     */
209    public function getComments() {
210        return $this->xmlEncode( $this->comments );
211    }
212
213    /**
214     * Get the comment of this item without any escaping
215     *
216     * @return string
217     */
218    public function getCommentsUnescaped() {
219        return $this->comments;
220    }
221
222    /**
223     * Quickie hack... strip out wikilinks to more legible form from the comment.
224     *
225     * @param string $text Wikitext
226     * @return string
227     */
228    public static function stripComment( $text ) {
229        return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
230    }
231
232}
233
234/** @deprecated class alias since 1.40 */
235class_alias( FeedItem::class, 'FeedItem' );