MediaWiki  master
IRCColourfulRCFeedFormatter.php
Go to the documentation of this file.
1 <?php
2 
25 
47  public function getLine( array $feed, RecentChange $rc, $actionComment ) {
48  $mainConfig = MediaWikiServices::getInstance()->getMainConfig();
49  $useRCPatrol = $mainConfig->get( MainConfigNames::UseRCPatrol );
50  $useNPPatrol = $mainConfig->get( MainConfigNames::UseNPPatrol );
51  $localInterwikis = $mainConfig->get( MainConfigNames::LocalInterwikis );
52  $canonicalServer = $mainConfig->get( MainConfigNames::CanonicalServer );
53  $script = $mainConfig->get( MainConfigNames::Script );
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();
69 
70  if ( $attribs['rc_type'] == RC_LOG ) {
71  $url = '';
72  } else {
73  $url = $canonicalServer . $script;
74  if ( $attribs['rc_type'] == RC_NEW ) {
75  $query = '?oldid=' . $attribs['rc_this_oldid'];
76  } else {
77  $query = '?diff=' . $attribs['rc_this_oldid'] . '&oldid=' . $attribs['rc_last_oldid'];
78  }
79  if ( $useRCPatrol || ( $attribs['rc_type'] == RC_NEW && $useNPPatrol ) ) {
80  $query .= '&rcid=' . $attribs['rc_id'];
81  }
82 
83  Hooks::runner()->onIRCLineURL( $url, $query, $rc );
84  $url .= $query;
85  }
86 
87  if ( $attribs['rc_old_len'] !== null && $attribs['rc_new_len'] !== null ) {
88  $szdiff = $attribs['rc_new_len'] - $attribs['rc_old_len'];
89  if ( $szdiff < -500 ) {
90  $szdiff = "\002$szdiff\002";
91  } elseif ( $szdiff >= 0 ) {
92  $szdiff = '+' . $szdiff;
93  }
94  // @todo i18n with parentheses in content language?
95  $szdiff = '(' . $szdiff . ')';
96  } else {
97  $szdiff = '';
98  }
99 
100  $user = self::cleanupForIRC( $attribs['rc_user_text'] );
101 
102  if ( $attribs['rc_type'] == RC_LOG ) {
103  $targetText = $rc->getTitle()->getPrefixedText();
104  $comment = self::cleanupForIRC( str_replace(
105  "[[$targetText]]",
106  "[[\00302$targetText\00310]]",
107  $actionComment
108  ) );
109  $flag = $attribs['rc_log_action'];
110  } else {
111  $store = MediaWikiServices::getInstance()->getCommentStore();
112  $comment = self::cleanupForIRC( $store->getComment( 'rc_comment', $attribs )->text );
113  $flag = '';
114  if ( !$attribs['rc_patrolled']
115  && ( $useRCPatrol || $attribs['rc_type'] == RC_NEW && $useNPPatrol )
116  ) {
117  $flag .= '!';
118  }
119  $flag .= ( $attribs['rc_type'] == RC_NEW ? "N" : "" )
120  . ( $attribs['rc_minor'] ? "M" : "" ) . ( $attribs['rc_bot'] ? "B" : "" );
121  }
122 
123  if ( $feed['add_interwiki_prefix'] === true && $localInterwikis ) {
124  // we use the first entry in $wgLocalInterwikis in recent changes feeds
125  $prefix = $localInterwikis[0];
126  } elseif ( $feed['add_interwiki_prefix'] ) {
127  $prefix = $feed['add_interwiki_prefix'];
128  } else {
129  $prefix = false;
130  }
131  if ( $prefix !== false ) {
132  $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
133  } else {
134  $titleString = "\00314[[\00307$title\00314]]";
135  }
136 
137  # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
138  # no colour (\003) switches back to the term default
139  $fullString = "$titleString\0034 $flag\00310 " .
140  "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
141 
142  return $fullString;
143  }
144 
150  public static function cleanupForIRC( $text ) {
151  return str_replace(
152  [ "\n", "\r" ],
153  [ " ", "" ],
155  );
156  }
157 }
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
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:172
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.
Represents a title within MediaWiki.
Definition: Title.php:82
Utility class for creating new RC entries.
static decodeCharReferences( $text)
Decode any character references, numeric or named entities, in the text and return a UTF-8 string.
Definition: Sanitizer.php:1371
Interface for RC feed formatters.