MediaWiki master
ChangesFeed.php
Go to the documentation of this file.
1<?php
8
17
26 private $format;
27
31 public function __construct( $format ) {
32 $this->format = $format;
33 }
34
43 public function getFeedObject( $title, $description, $url ) {
44 $mainConfig = MediaWikiServices::getInstance()->getMainConfig();
45 $sitename = $mainConfig->get( MainConfigNames::Sitename );
46 $languageCode = $mainConfig->get( MainConfigNames::LanguageCode );
47 $feedClasses = $mainConfig->get( MainConfigNames::FeedClasses );
48 '@phan-var array<string,class-string<ChannelFeed>> $feedClasses';
49 if ( !isset( $feedClasses[$this->format] ) ) {
50 return false;
51 }
52
53 if ( !array_key_exists( $this->format, $feedClasses ) ) {
54 // falling back to atom
55 $this->format = 'atom';
56 }
57
58 $feedTitle = "{$sitename} - {$title} [{$languageCode}]";
59 return new $feedClasses[$this->format](
60 $feedTitle, htmlspecialchars( $description ), $url );
61 }
62
69 public static function buildItems( $rows ) {
70 $items = [];
71
72 # Merge adjacent edits by one user
73 $sorted = [];
74 $n = 0;
75 foreach ( $rows as $obj ) {
76 if ( !in_array( $obj->rc_source, RecentChange::INTERNAL_SOURCES ) ) {
77 continue;
78 }
79
80 if ( $n > 0 &&
81 $obj->rc_source == RecentChange::SRC_EDIT &&
82 $obj->rc_namespace >= 0 &&
83 $obj->rc_cur_id == $sorted[$n - 1]->rc_cur_id &&
84 $obj->rc_user_text == $sorted[$n - 1]->rc_user_text ) {
85 $sorted[$n - 1]->rc_last_oldid = $obj->rc_last_oldid;
86 } else {
87 $sorted[$n] = $obj;
88 $n++;
89 }
90 }
91
93 $commentFormatter = $services->getRowCommentFormatter();
94 $formattedComments = $commentFormatter->formatItems(
95 $commentFormatter->rows( $rows )
96 ->commentKey( 'rc_comment' )
97 ->indexField( 'rc_id' )
98 );
99
100 $nsInfo = $services->getNamespaceInfo();
101 foreach ( $sorted as $obj ) {
102 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
103 $talkpage = $nsInfo->hasTalkNamespace( $obj->rc_namespace ) && $title->canExist()
104 ? $title->getTalkPage()->getFullURL()
105 : '';
106
107 // Skip items with deleted content (avoids partially complete/inconsistent output)
108 if ( $obj->rc_deleted ) {
109 continue;
110 }
111
112 if ( $obj->rc_this_oldid ) {
113 $url = $title->getFullURL( [
114 'diff' => $obj->rc_this_oldid,
115 'oldid' => $obj->rc_last_oldid,
116 ] );
117 } else {
118 // log entry or something like that.
119 $url = $title->getFullURL();
120 }
121
122 $items[] = new FeedItem(
123 $title->getPrefixedText(),
124 FeedUtils::formatDiff( $obj, $formattedComments[$obj->rc_id] ),
125 $url,
126 $obj->rc_timestamp,
127 ( $obj->rc_deleted & RevisionRecord::DELETED_USER )
128 ? wfMessage( 'rev-deleted-user' )->escaped() : $obj->rc_user_text,
129 $talkpage
130 );
131 }
132
133 return $items;
134 }
135}
136
138class_alias( ChangesFeed::class, 'ChangesFeed' );
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
makeTitle( $linkId)
Convert a link ID to a Title.to override Title
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:26
Helper functions for feeds.
Definition FeedUtils.php:30
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:69
Result wrapper for grabbing data queried from an IDatabase object.