MediaWiki  master
ChangesFeed.php
Go to the documentation of this file.
1 <?php
31 
37 class ChangesFeed {
38  private $format;
39 
43  public function __construct( $format ) {
44  $this->format = $format;
45  }
46 
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 
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 }
const RC_EXTERNAL
Definition: Defines.php:119
const RC_EDIT
Definition: Defines.php:116
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Feed to Special:RecentChanges and Special:RecentChangesLinked.
Definition: ChangesFeed.php:37
getFeedObject( $title, $description, $url)
Get a MediaWiki\Feed\ChannelFeed subclass object to use.
Definition: ChangesFeed.php:55
__construct( $format)
Definition: ChangesFeed.php:43
static buildItems( $rows)
Generate the feed items given a row from the database.
Definition: ChangesFeed.php:80
Class to support the outputting of syndication feeds in Atom and RSS format.
Definition: ChannelFeed.php:38
A base class for outputting syndication feeds (e.g.
Definition: FeedItem.php:40
Helper functions for feeds.
Definition: FeedUtils.php:45
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Page revision base class.
Represents a title within MediaWiki.
Definition: Title.php:76
Result wrapper for grabbing data queried from an IDatabase object.