Merge "Add parserTests for language converter markup."
[lhc/web/wiklou.git] / includes / rcfeed / IRCColourfulRCFeedFormatter.php
1 <?php
2 class IRCColourfulRCFeedFormatter implements RCFeedFormatter {
3 /**
4 * Generates a colourful notification intended for humans on IRC.
5 * @see RCFeedFormatter::getLine
6 */
7 public function getLine( array $feed, RecentChange $rc, $actionComment ) {
8 global $wgUseRCPatrol, $wgUseNPPatrol, $wgLocalInterwiki,
9 $wgCanonicalServer, $wgScript;
10 $attribs = $rc->getAttributes();
11 if ( $attribs['rc_type'] == RC_LOG ) {
12 // Don't use SpecialPage::getTitleFor, backwards compatibility with
13 // IRC API which expects "Log".
14 $titleObj = Title::newFromText( 'Log/' . $attribs['rc_log_type'], NS_SPECIAL );
15 } else {
16 $titleObj =& $rc->getTitle();
17 }
18 $title = $titleObj->getPrefixedText();
19 $title = self::cleanupForIRC( $title );
20
21 if ( $attribs['rc_type'] == RC_LOG ) {
22 $url = '';
23 } else {
24 $url = $wgCanonicalServer . $wgScript;
25 if ( $attribs['rc_type'] == RC_NEW ) {
26 $query = '?oldid=' . $attribs['rc_this_oldid'];
27 } else {
28 $query = '?diff=' . $attribs['rc_this_oldid'] . '&oldid=' . $attribs['rc_last_oldid'];
29 }
30 if ( $wgUseRCPatrol || ( $attribs['rc_type'] == RC_NEW && $wgUseNPPatrol ) ) {
31 $query .= '&rcid=' . $attribs['rc_id'];
32 }
33 // HACK: We need this hook for WMF's secure server setup
34 wfRunHooks( 'IRCLineURL', array( &$url, &$query ) );
35 $url .= $query;
36 }
37
38 if ( $attribs['rc_old_len'] !== null && $attribs['rc_new_len'] !== null ) {
39 $szdiff = $attribs['rc_new_len'] - $attribs['rc_old_len'];
40 if ( $szdiff < -500 ) {
41 $szdiff = "\002$szdiff\002";
42 } elseif ( $szdiff >= 0 ) {
43 $szdiff = '+' . $szdiff;
44 }
45 // @todo i18n with parentheses in content language?
46 $szdiff = '(' . $szdiff . ')';
47 } else {
48 $szdiff = '';
49 }
50
51 $user = self::cleanupForIRC( $attribs['rc_user_text'] );
52
53 if ( $attribs['rc_type'] == RC_LOG ) {
54 $targetText = $rc->getTitle()->getPrefixedText();
55 $comment = self::cleanupForIRC( str_replace( "[[$targetText]]", "[[\00302$targetText\00310]]", $actionComment ) );
56 $flag = $attribs['rc_log_action'];
57 } else {
58 $comment = self::cleanupForIRC( $attribs['rc_comment'] );
59 $flag = '';
60 if ( !$attribs['rc_patrolled'] && ( $wgUseRCPatrol || $attribs['rc_type'] == RC_NEW && $wgUseNPPatrol ) ) {
61 $flag .= '!';
62 }
63 $flag .= ( $attribs['rc_type'] == RC_NEW ? "N" : "" ) . ( $attribs['rc_minor'] ? "M" : "" ) . ( $attribs['rc_bot'] ? "B" : "" );
64 }
65
66 if ( $feed['add_interwiki_prefix'] === true && $wgLocalInterwiki !== false ) {
67 $prefix = $wgLocalInterwiki;
68 } elseif ( $feed['add_interwiki_prefix'] ) {
69 $prefix = $feed['add_interwiki_prefix'];
70 } else {
71 $prefix = false;
72 }
73 if ( $prefix !== false ) {
74 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
75 } else {
76 $titleString = "\00314[[\00307$title\00314]]";
77 }
78
79 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
80 # no colour (\003) switches back to the term default
81 $fullString = "$titleString\0034 $flag\00310 " .
82 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
83
84 return $fullString;
85 }
86
87 /**
88 * Remove newlines, carriage returns and decode html entites
89 * @param string $text
90 * @return string
91 */
92 public static function cleanupForIRC( $text ) {
93 return Sanitizer::decodeCharReferences( str_replace(
94 array( "\n", "\r" ),
95 array( " ", "" ),
96 $text
97 ) );
98 }
99 }