a71dcbb14ca2d52bf20acf9bc004ba6d794bac33
[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 public $format, $type, $titleMsg, $descMsg;
30
31 /**
32 * @param string $format Feed's format (either 'rss' or 'atom')
33 * @param string $type Type of feed (for cache keys)
34 */
35 public function __construct( $format, $type ) {
36 $this->format = $format;
37 $this->type = $type;
38 }
39
40 /**
41 * Get a ChannelFeed subclass object to use
42 *
43 * @param string $title Feed's title
44 * @param string $description Feed's description
45 * @param string $url Url of origin page
46 * @return ChannelFeed|bool ChannelFeed subclass or false on failure
47 */
48 public function getFeedObject( $title, $description, $url ) {
49 global $wgSitename, $wgLanguageCode, $wgFeedClasses;
50
51 if ( !isset( $wgFeedClasses[$this->format] ) ) {
52 return false;
53 }
54
55 if ( !array_key_exists( $this->format, $wgFeedClasses ) ) {
56 // falling back to atom
57 $this->format = 'atom';
58 }
59
60 $feedTitle = "$wgSitename - {$title} [$wgLanguageCode]";
61 return new $wgFeedClasses[$this->format](
62 $feedTitle, htmlspecialchars( $description ), $url );
63 }
64
65 /**
66 * Generate the feed items given a row from the database.
67 * @param object $rows IDatabase resource with recentchanges rows
68 * @return array
69 */
70 public static function buildItems( $rows ) {
71 $items = [];
72
73 # Merge adjacent edits by one user
74 $sorted = [];
75 $n = 0;
76 foreach ( $rows as $obj ) {
77 if ( $obj->rc_type == RC_EXTERNAL ) {
78 continue;
79 }
80
81 if ( $n > 0 &&
82 $obj->rc_type == RC_EDIT &&
83 $obj->rc_namespace >= 0 &&
84 $obj->rc_cur_id == $sorted[$n - 1]->rc_cur_id &&
85 $obj->rc_user_text == $sorted[$n - 1]->rc_user_text ) {
86 $sorted[$n - 1]->rc_last_oldid = $obj->rc_last_oldid;
87 } else {
88 $sorted[$n] = $obj;
89 $n++;
90 }
91 }
92
93 foreach ( $sorted as $obj ) {
94 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
95 $talkpage = MWNamespace::hasTalkNamespace( $obj->rc_namespace )
96 ? $title->getTalkPage()->getFullURL()
97 : '';
98
99 // Skip items with deleted content (avoids partially complete/inconsistent output)
100 if ( $obj->rc_deleted ) {
101 continue;
102 }
103
104 if ( $obj->rc_this_oldid ) {
105 $url = $title->getFullURL( [
106 'diff' => $obj->rc_this_oldid,
107 'oldid' => $obj->rc_last_oldid,
108 ] );
109 } else {
110 // log entry or something like that.
111 $url = $title->getFullURL();
112 }
113
114 $items[] = new FeedItem(
115 $title->getPrefixedText(),
116 FeedUtils::formatDiff( $obj ),
117 $url,
118 $obj->rc_timestamp,
119 ( $obj->rc_deleted & Revision::DELETED_USER )
120 ? wfMessage( 'rev-deleted-user' )->escaped() : $obj->rc_user_text,
121 $talkpage
122 );
123 }
124
125 return $items;
126 }
127 }