MediaWiki  master
ChangesFeed.php
Go to the documentation of this file.
1 <?php
27 
33 class ChangesFeed {
34  private $format;
35 
39  public function __construct( $format ) {
40  $this->format = $format;
41  }
42 
51  public function getFeedObject( $title, $description, $url ) {
52  $mainConfig = MediaWikiServices::getInstance()->getMainConfig();
53  $sitename = $mainConfig->get( MainConfigNames::Sitename );
54  $languageCode = $mainConfig->get( MainConfigNames::LanguageCode );
55  $feedClasses = $mainConfig->get( MainConfigNames::FeedClasses );
56  if ( !isset( $feedClasses[$this->format] ) ) {
57  return false;
58  }
59 
60  if ( !array_key_exists( $this->format, $feedClasses ) ) {
61  // falling back to atom
62  $this->format = 'atom';
63  }
64 
65  $feedTitle = "{$sitename} - {$title} [{$languageCode}]";
66  return new $feedClasses[$this->format](
67  $feedTitle, htmlspecialchars( $description ), $url );
68  }
69 
76  public static function buildItems( $rows ) {
77  $items = [];
78 
79  # Merge adjacent edits by one user
80  $sorted = [];
81  $n = 0;
82  foreach ( $rows as $obj ) {
83  if ( $obj->rc_type == RC_EXTERNAL ) {
84  continue;
85  }
86 
87  if ( $n > 0 &&
88  $obj->rc_type == RC_EDIT &&
89  $obj->rc_namespace >= 0 &&
90  $obj->rc_cur_id == $sorted[$n - 1]->rc_cur_id &&
91  $obj->rc_user_text == $sorted[$n - 1]->rc_user_text ) {
92  $sorted[$n - 1]->rc_last_oldid = $obj->rc_last_oldid;
93  } else {
94  $sorted[$n] = $obj;
95  $n++;
96  }
97  }
98 
99  $services = MediaWikiServices::getInstance();
100  $commentFormatter = $services->getRowCommentFormatter();
101  $formattedComments = $commentFormatter->formatItems(
102  $commentFormatter->rows( $rows )
103  ->commentKey( 'rc_comment' )
104  ->indexField( 'rc_id' )
105  );
106 
107  $nsInfo = $services->getNamespaceInfo();
108  foreach ( $sorted as $obj ) {
109  $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
110  $talkpage = $nsInfo->hasTalkNamespace( $obj->rc_namespace ) && $title->canExist()
111  ? $title->getTalkPage()->getFullURL()
112  : '';
113 
114  // Skip items with deleted content (avoids partially complete/inconsistent output)
115  if ( $obj->rc_deleted ) {
116  continue;
117  }
118 
119  if ( $obj->rc_this_oldid ) {
120  $url = $title->getFullURL( [
121  'diff' => $obj->rc_this_oldid,
122  'oldid' => $obj->rc_last_oldid,
123  ] );
124  } else {
125  // log entry or something like that.
126  $url = $title->getFullURL();
127  }
128 
129  $items[] = new FeedItem(
130  $title->getPrefixedText(),
131  FeedUtils::formatDiff( $obj, $formattedComments[$obj->rc_id] ),
132  $url,
133  $obj->rc_timestamp,
134  ( $obj->rc_deleted & RevisionRecord::DELETED_USER )
135  ? wfMessage( 'rev-deleted-user' )->escaped() : $obj->rc_user_text,
136  $talkpage
137  );
138  }
139 
140  return $items;
141  }
142 }
const RC_EXTERNAL
Definition: Defines.php:118
const RC_EDIT
Definition: Defines.php:115
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Feed to Special:RecentChanges and Special:RecentChangesLinked.
Definition: ChangesFeed.php:33
getFeedObject( $title, $description, $url)
Get a ChannelFeed subclass object to use.
Definition: ChangesFeed.php:51
__construct( $format)
Definition: ChangesFeed.php:39
static buildItems( $rows)
Generate the feed items given a row from the database.
Definition: ChangesFeed.php:76
A base class for outputting syndication feeds (e.g.
Definition: FeedItem.php:36
static formatDiff( $row, $formattedComment=null)
Format a diff for the newsfeed.
Definition: FeedUtils.php:76
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Page revision base class.
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:638
Result wrapper for grabbing data queried from an IDatabase object.