MediaWiki master
ChangesFeed.php
Go to the documentation of this file.
1<?php
22
31
40 private $format;
41
45 public function __construct( $format ) {
46 $this->format = $format;
47 }
48
57 public function getFeedObject( $title, $description, $url ) {
58 $mainConfig = MediaWikiServices::getInstance()->getMainConfig();
59 $sitename = $mainConfig->get( MainConfigNames::Sitename );
60 $languageCode = $mainConfig->get( MainConfigNames::LanguageCode );
61 $feedClasses = $mainConfig->get( MainConfigNames::FeedClasses );
62 '@phan-var array<string,class-string<ChannelFeed>> $feedClasses';
63 if ( !isset( $feedClasses[$this->format] ) ) {
64 return false;
65 }
66
67 if ( !array_key_exists( $this->format, $feedClasses ) ) {
68 // falling back to atom
69 $this->format = 'atom';
70 }
71
72 $feedTitle = "{$sitename} - {$title} [{$languageCode}]";
73 return new $feedClasses[$this->format](
74 $feedTitle, htmlspecialchars( $description ), $url );
75 }
76
83 public static function buildItems( $rows ) {
84 $items = [];
85
86 # Merge adjacent edits by one user
87 $sorted = [];
88 $n = 0;
89 foreach ( $rows as $obj ) {
90 if ( $obj->rc_type == RC_EXTERNAL ) {
91 continue;
92 }
93
94 if ( $n > 0 &&
95 $obj->rc_type == RC_EDIT &&
96 $obj->rc_namespace >= 0 &&
97 $obj->rc_cur_id == $sorted[$n - 1]->rc_cur_id &&
98 $obj->rc_user_text == $sorted[$n - 1]->rc_user_text ) {
99 $sorted[$n - 1]->rc_last_oldid = $obj->rc_last_oldid;
100 } else {
101 $sorted[$n] = $obj;
102 $n++;
103 }
104 }
105
106 $services = MediaWikiServices::getInstance();
107 $commentFormatter = $services->getRowCommentFormatter();
108 $formattedComments = $commentFormatter->formatItems(
109 $commentFormatter->rows( $rows )
110 ->commentKey( 'rc_comment' )
111 ->indexField( 'rc_id' )
112 );
113
114 $nsInfo = $services->getNamespaceInfo();
115 foreach ( $sorted as $obj ) {
116 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
117 $talkpage = $nsInfo->hasTalkNamespace( $obj->rc_namespace ) && $title->canExist()
118 ? $title->getTalkPage()->getFullURL()
119 : '';
120
121 // Skip items with deleted content (avoids partially complete/inconsistent output)
122 if ( $obj->rc_deleted ) {
123 continue;
124 }
125
126 if ( $obj->rc_this_oldid ) {
127 $url = $title->getFullURL( [
128 'diff' => $obj->rc_this_oldid,
129 'oldid' => $obj->rc_last_oldid,
130 ] );
131 } else {
132 // log entry or something like that.
133 $url = $title->getFullURL();
134 }
135
136 $items[] = new FeedItem(
137 $title->getPrefixedText(),
138 FeedUtils::formatDiff( $obj, $formattedComments[$obj->rc_id] ),
139 $url,
140 $obj->rc_timestamp,
141 ( $obj->rc_deleted & RevisionRecord::DELETED_USER )
142 ? wfMessage( 'rev-deleted-user' )->escaped() : $obj->rc_user_text,
143 $talkpage
144 );
145 }
146
147 return $items;
148 }
149}
150
152class_alias( ChangesFeed::class, 'ChangesFeed' );
const RC_EXTERNAL
Definition Defines.php:120
const RC_EDIT
Definition Defines.php:117
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Class to support the outputting of syndication feeds in Atom and RSS format.
A base class for outputting syndication feeds (e.g.
Definition FeedItem.php:40
Helper functions for feeds.
Definition FeedUtils.php:43
A class containing constants representing the names of configuration variables.
const FeedClasses
Name constant for the FeedClasses setting, for use with Config::get()
const Sitename
Name constant for the Sitename setting, for use with Config::get()
const LanguageCode
Name constant for the LanguageCode setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
XML feed for Special:RecentChanges and Special:RecentChangesLinked.
getFeedObject( $title, $description, $url)
Get a MediaWiki\Feed\ChannelFeed subclass object to use.
static buildItems( $rows)
Generate the feed items given a row from the database.
Page revision base class.
Represents a title within MediaWiki.
Definition Title.php:78
Result wrapper for grabbing data queried from an IDatabase object.