FeedItem.php 4.8 KB

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