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