Update IRCLineURL hook to include RecentChange object
[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 wfRunHooks( '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( "[[$targetText]]", "[[\00302$targetText\00310]]", $actionComment ) );
81 $flag = $attribs['rc_log_action'];
82 } else {
83 $comment = self::cleanupForIRC( $attribs['rc_comment'] );
84 $flag = '';
85 if ( !$attribs['rc_patrolled'] && ( $wgUseRCPatrol || $attribs['rc_type'] == RC_NEW && $wgUseNPPatrol ) ) {
86 $flag .= '!';
87 }
88 $flag .= ( $attribs['rc_type'] == RC_NEW ? "N" : "" ) . ( $attribs['rc_minor'] ? "M" : "" ) . ( $attribs['rc_bot'] ? "B" : "" );
89 }
90
91 if ( $feed['add_interwiki_prefix'] === true && $wgLocalInterwikis ) {
92 // we use the first entry in $wgLocalInterwikis in recent changes feeds
93 $prefix = $wgLocalInterwikis[0];
94 } elseif ( $feed['add_interwiki_prefix'] ) {
95 $prefix = $feed['add_interwiki_prefix'];
96 } else {
97 $prefix = false;
98 }
99 if ( $prefix !== false ) {
100 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
101 } else {
102 $titleString = "\00314[[\00307$title\00314]]";
103 }
104
105 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
106 # no colour (\003) switches back to the term default
107 $fullString = "$titleString\0034 $flag\00310 " .
108 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
109
110 return $fullString;
111 }
112
113 /**
114 * Remove newlines, carriage returns and decode html entites
115 * @param string $text
116 * @return string
117 */
118 public static function cleanupForIRC( $text ) {
119 return Sanitizer::decodeCharReferences( str_replace(
120 array( "\n", "\r" ),
121 array( " ", "" ),
122 $text
123 ) );
124 }
125 }