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