HTML escape parameter 'text' of hook 'SkinEditSectionLinks'
[lhc/web/wiklou.git] / includes / changes / ChangesFeed.php
1 <?php
2 /**
3 * Feed for list of changes.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Feed to Special:RecentChanges and Special:RecentChangesLinked.
25 *
26 * @ingroup Feed
27 */
28 class ChangesFeed {
29 private $format;
30
31 /**
32 * @param string $format Feed's format (either 'rss' or 'atom')
33 */
34 public function __construct( $format ) {
35 $this->format = $format;
36 }
37
38 /**
39 * Get a ChannelFeed subclass object to use
40 *
41 * @param string $title Feed's title
42 * @param string $description Feed's description
43 * @param string $url Url of origin page
44 * @return ChannelFeed|bool ChannelFeed subclass or false on failure
45 */
46 public function getFeedObject( $title, $description, $url ) {
47 global $wgSitename, $wgLanguageCode, $wgFeedClasses;
48
49 if ( !isset( $wgFeedClasses[$this->format] ) ) {
50 return false;
51 }
52
53 if ( !array_key_exists( $this->format, $wgFeedClasses ) ) {
54 // falling back to atom
55 $this->format = 'atom';
56 }
57
58 $feedTitle = "$wgSitename - {$title} [$wgLanguageCode]";
59 return new $wgFeedClasses[$this->format](
60 $feedTitle, htmlspecialchars( $description ), $url );
61 }
62
63 /**
64 * Generate the feed items given a row from the database.
65 * @param object $rows IDatabase resource with recentchanges rows
66 * @return array
67 */
68 public static function buildItems( $rows ) {
69 $items = [];
70
71 # Merge adjacent edits by one user
72 $sorted = [];
73 $n = 0;
74 foreach ( $rows as $obj ) {
75 if ( $obj->rc_type == RC_EXTERNAL ) {
76 continue;
77 }
78
79 if ( $n > 0 &&
80 $obj->rc_type == RC_EDIT &&
81 $obj->rc_namespace >= 0 &&
82 $obj->rc_cur_id == $sorted[$n - 1]->rc_cur_id &&
83 $obj->rc_user_text == $sorted[$n - 1]->rc_user_text ) {
84 $sorted[$n - 1]->rc_last_oldid = $obj->rc_last_oldid;
85 } else {
86 $sorted[$n] = $obj;
87 $n++;
88 }
89 }
90
91 foreach ( $sorted as $obj ) {
92 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
93 $talkpage = MWNamespace::hasTalkNamespace( $obj->rc_namespace )
94 ? $title->getTalkPage()->getFullURL()
95 : '';
96
97 // Skip items with deleted content (avoids partially complete/inconsistent output)
98 if ( $obj->rc_deleted ) {
99 continue;
100 }
101
102 if ( $obj->rc_this_oldid ) {
103 $url = $title->getFullURL( [
104 'diff' => $obj->rc_this_oldid,
105 'oldid' => $obj->rc_last_oldid,
106 ] );
107 } else {
108 // log entry or something like that.
109 $url = $title->getFullURL();
110 }
111
112 $items[] = new FeedItem(
113 $title->getPrefixedText(),
114 FeedUtils::formatDiff( $obj ),
115 $url,
116 $obj->rc_timestamp,
117 ( $obj->rc_deleted & Revision::DELETED_USER )
118 ? wfMessage( 'rev-deleted-user' )->escaped() : $obj->rc_user_text,
119 $talkpage
120 );
121 }
122
123 return $items;
124 }
125 }