MediaWiki 1.44.0
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 if ( !isset( $feedClasses[$this->format] ) ) {
63 return false;
64 }
65
66 if ( !array_key_exists( $this->format, $feedClasses ) ) {
67 // falling back to atom
68 $this->format = 'atom';
69 }
70
71 $feedTitle = "{$sitename} - {$title} [{$languageCode}]";
72 return new $feedClasses[$this->format](
73 $feedTitle, htmlspecialchars( $description ), $url );
74 }
75
82 public static function buildItems( $rows ) {
83 $items = [];
84
85 # Merge adjacent edits by one user
86 $sorted = [];
87 $n = 0;
88 foreach ( $rows as $obj ) {
89 if ( $obj->rc_type == RC_EXTERNAL ) {
90 continue;
91 }
92
93 if ( $n > 0 &&
94 $obj->rc_type == RC_EDIT &&
95 $obj->rc_namespace >= 0 &&
96 $obj->rc_cur_id == $sorted[$n - 1]->rc_cur_id &&
97 $obj->rc_user_text == $sorted[$n - 1]->rc_user_text ) {
98 $sorted[$n - 1]->rc_last_oldid = $obj->rc_last_oldid;
99 } else {
100 $sorted[$n] = $obj;
101 $n++;
102 }
103 }
104
105 $services = MediaWikiServices::getInstance();
106 $commentFormatter = $services->getRowCommentFormatter();
107 $formattedComments = $commentFormatter->formatItems(
108 $commentFormatter->rows( $rows )
109 ->commentKey( 'rc_comment' )
110 ->indexField( 'rc_id' )
111 );
112
113 $nsInfo = $services->getNamespaceInfo();
114 foreach ( $sorted as $obj ) {
115 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
116 $talkpage = $nsInfo->hasTalkNamespace( $obj->rc_namespace ) && $title->canExist()
117 ? $title->getTalkPage()->getFullURL()
118 : '';
119
120 // Skip items with deleted content (avoids partially complete/inconsistent output)
121 if ( $obj->rc_deleted ) {
122 continue;
123 }
124
125 if ( $obj->rc_this_oldid ) {
126 $url = $title->getFullURL( [
127 'diff' => $obj->rc_this_oldid,
128 'oldid' => $obj->rc_last_oldid,
129 ] );
130 } else {
131 // log entry or something like that.
132 $url = $title->getFullURL();
133 }
134
135 $items[] = new FeedItem(
136 $title->getPrefixedText(),
137 FeedUtils::formatDiff( $obj, $formattedComments[$obj->rc_id] ),
138 $url,
139 $obj->rc_timestamp,
140 ( $obj->rc_deleted & RevisionRecord::DELETED_USER )
141 ? wfMessage( 'rev-deleted-user' )->escaped() : $obj->rc_user_text,
142 $talkpage
143 );
144 }
145
146 return $items;
147 }
148}
149
151class_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.