Merge "Allow easy suppression of multiple deleted revs"
[lhc/web/wiklou.git] / includes / rcfeed / IRCColourfulRCFeedFormatter.php
1 <?php
2
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 */
21
22 /**
23 * Generates a colourful notification intended for humans on IRC.
24 *
25 * @since 1.22
26 */
27
28 class IRCColourfulRCFeedFormatter implements RCFeedFormatter {
29 /**
30 * @see RCFeedFormatter::getLine
31 */
32 public function getLine( array $feed, RecentChange $rc, $actionComment ) {
33 global $wgUseRCPatrol, $wgUseNPPatrol, $wgLocalInterwikis,
34 $wgCanonicalServer, $wgScript;
35 $attribs = $rc->getAttributes();
36 if ( $attribs['rc_type'] == RC_LOG ) {
37 // Don't use SpecialPage::getTitleFor, backwards compatibility with
38 // IRC API which expects "Log".
39 $titleObj = Title::newFromText( 'Log/' . $attribs['rc_log_type'], NS_SPECIAL );
40 } else {
41 $titleObj =& $rc->getTitle();
42 }
43 $title = $titleObj->getPrefixedText();
44 $title = self::cleanupForIRC( $title );
45
46 if ( $attribs['rc_type'] == RC_LOG ) {
47 $url = '';
48 } else {
49 $url = $wgCanonicalServer . $wgScript;
50 if ( $attribs['rc_type'] == RC_NEW ) {
51 $query = '?oldid=' . $attribs['rc_this_oldid'];
52 } else {
53 $query = '?diff=' . $attribs['rc_this_oldid'] . '&oldid=' . $attribs['rc_last_oldid'];
54 }
55 if ( $wgUseRCPatrol || ( $attribs['rc_type'] == RC_NEW && $wgUseNPPatrol ) ) {
56 $query .= '&rcid=' . $attribs['rc_id'];
57 }
58 // HACK: We need this hook for WMF's secure server setup
59 Hooks::run( 'IRCLineURL', array( &$url, &$query, $rc ) );
60 $url .= $query;
61 }
62
63 if ( $attribs['rc_old_len'] !== null && $attribs['rc_new_len'] !== null ) {
64 $szdiff = $attribs['rc_new_len'] - $attribs['rc_old_len'];
65 if ( $szdiff < -500 ) {
66 $szdiff = "\002$szdiff\002";
67 } elseif ( $szdiff >= 0 ) {
68 $szdiff = '+' . $szdiff;
69 }
70 // @todo i18n with parentheses in content language?
71 $szdiff = '(' . $szdiff . ')';
72 } else {
73 $szdiff = '';
74 }
75
76 $user = self::cleanupForIRC( $attribs['rc_user_text'] );
77
78 if ( $attribs['rc_type'] == RC_LOG ) {
79 $targetText = $rc->getTitle()->getPrefixedText();
80 $comment = self::cleanupForIRC( str_replace(
81 "[[$targetText]]",
82 "[[\00302$targetText\00310]]",
83 $actionComment
84 ) );
85 $flag = $attribs['rc_log_action'];
86 } else {
87 $comment = self::cleanupForIRC( $attribs['rc_comment'] );
88 $flag = '';
89 if ( !$attribs['rc_patrolled']
90 && ( $wgUseRCPatrol || $attribs['rc_type'] == RC_NEW && $wgUseNPPatrol )
91 ) {
92 $flag .= '!';
93 }
94 $flag .= ( $attribs['rc_type'] == RC_NEW ? "N" : "" )
95 . ( $attribs['rc_minor'] ? "M" : "" ) . ( $attribs['rc_bot'] ? "B" : "" );
96 }
97
98 if ( $feed['add_interwiki_prefix'] === true && $wgLocalInterwikis ) {
99 // we use the first entry in $wgLocalInterwikis in recent changes feeds
100 $prefix = $wgLocalInterwikis[0];
101 } elseif ( $feed['add_interwiki_prefix'] ) {
102 $prefix = $feed['add_interwiki_prefix'];
103 } else {
104 $prefix = false;
105 }
106 if ( $prefix !== false ) {
107 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
108 } else {
109 $titleString = "\00314[[\00307$title\00314]]";
110 }
111
112 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
113 # no colour (\003) switches back to the term default
114 $fullString = "$titleString\0034 $flag\00310 " .
115 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
116
117 return $fullString;
118 }
119
120 /**
121 * Remove newlines, carriage returns and decode html entites
122 * @param string $text
123 * @return string
124 */
125 public static function cleanupForIRC( $text ) {
126 return str_replace(
127 array( "\n", "\r" ),
128 array( " ", "" ),
129 Sanitizer::decodeCharReferences( $text )
130 );
131 }
132 }