MediaWiki master
IRCColourfulRCFeedFormatter.php
Go to the documentation of this file.
1<?php
2
22namespace MediaWiki\RCFeed;
23
28use RecentChange;
29
51 public function getLine( array $feed, RecentChange $rc, $actionComment ) {
53 $mainConfig = $services->getMainConfig();
54 $localInterwikis = $mainConfig->get( MainConfigNames::LocalInterwikis );
55 $useRCPatrol = $mainConfig->get( MainConfigNames::UseRCPatrol );
56 $useNPPatrol = $mainConfig->get( MainConfigNames::UseNPPatrol );
57 $attribs = $rc->getAttributes();
58 if ( $attribs['rc_type'] == RC_CATEGORIZE ) {
59 // Don't send RC_CATEGORIZE events to IRC feed (T127360)
60 return null;
61 }
62
63 if ( $attribs['rc_type'] == RC_LOG ) {
64 // Don't use SpecialPage::getTitleFor, backwards compatibility with
65 // IRC API which expects "Log".
66 $titleObj = Title::newFromText( 'Log/' . $attribs['rc_log_type'], NS_SPECIAL );
67 } else {
68 $titleObj = $rc->getTitle();
69 }
70 $title = $titleObj->getPrefixedText();
71 $title = self::cleanupForIRC( $title );
72
73 $notifyUrl = $rc->getNotifyUrl() ?? '';
74
75 if ( $attribs['rc_old_len'] !== null && $attribs['rc_new_len'] !== null ) {
76 $szdiff = $attribs['rc_new_len'] - $attribs['rc_old_len'];
77 if ( $szdiff < -500 ) {
78 $szdiff = "\002$szdiff\002";
79 } elseif ( $szdiff >= 0 ) {
80 $szdiff = '+' . $szdiff;
81 }
82 // @todo i18n with parentheses in content language?
83 $szdiff = '(' . $szdiff . ')';
84 } else {
85 $szdiff = '';
86 }
87
88 $user = self::cleanupForIRC( $attribs['rc_user_text'] );
89
90 if ( $attribs['rc_type'] == RC_LOG ) {
91 $targetText = $rc->getTitle()->getPrefixedText();
92 $comment = self::cleanupForIRC( str_replace(
93 "[[$targetText]]",
94 "[[\00302$targetText\00310]]",
95 $actionComment
96 ) );
97 $flag = $attribs['rc_log_action'];
98 } else {
99 $store = $services->getCommentStore();
100 $comment = self::cleanupForIRC( $store->getComment( 'rc_comment', $attribs )->text );
101 $flag = '';
102 if ( !$attribs['rc_patrolled']
103 && ( $useRCPatrol || ( $attribs['rc_type'] == RC_NEW && $useNPPatrol ) )
104 ) {
105 $flag .= '!';
106 }
107 $flag .= ( $attribs['rc_type'] == RC_NEW ? "N" : "" )
108 . ( $attribs['rc_minor'] ? "M" : "" ) . ( $attribs['rc_bot'] ? "B" : "" );
109 }
110
111 if ( $feed['add_interwiki_prefix'] === true && $localInterwikis ) {
112 // we use the first entry in $wgLocalInterwikis in recent changes feeds
113 $prefix = $localInterwikis[0];
114 } elseif ( $feed['add_interwiki_prefix'] ) {
115 $prefix = $feed['add_interwiki_prefix'];
116 } else {
117 $prefix = false;
118 }
119 if ( $prefix !== false ) {
120 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
121 } else {
122 $titleString = "\00314[[\00307$title\00314]]";
123 }
124
125 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
126 # no colour (\003) switches back to the term default
127 $fullString = "$titleString\0034 $flag\00310 " .
128 "\00302$notifyUrl\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
129
130 return $fullString;
131 }
132
138 public static function cleanupForIRC( $text ) {
139 return str_replace(
140 [ "\n", "\r" ],
141 [ " ", "" ],
142 Sanitizer::decodeCharReferences( $text )
143 );
144 }
145}
147class_alias( IRCColourfulRCFeedFormatter::class, 'IRCColourfulRCFeedFormatter' );
const RC_NEW
Definition Defines.php:118
const NS_SPECIAL
Definition Defines.php:54
const RC_LOG
Definition Defines.php:119
const RC_CATEGORIZE
Definition Defines.php:121
A class containing constants representing the names of configuration variables.
const UseRCPatrol
Name constant for the UseRCPatrol setting, for use with Config::get()
const UseNPPatrol
Name constant for the UseNPPatrol setting, for use with Config::get()
const LocalInterwikis
Name constant for the LocalInterwikis 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.
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
Format a notification as a human-readable string using IRC colour codes.
getLine(array $feed, RecentChange $rc, $actionComment)
static cleanupForIRC( $text)
Remove newlines, carriage returns and decode html entities.
Represents a title within MediaWiki.
Definition Title.php:79
Utility class for creating new RC entries.
getNotifyUrl()
Get the extra URL that is given as part of the notification to RCFeed consumers.
Interface for RC feed formatters.