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