Debian Codegraph @

includes/recentchanges/ChangesFeed.php

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