Apply $wgMaxArticleSize more exactly
[mediawiki.git] / includes / rcfeed / IRCColourfulRCFeedFormatter.php
blobddea69511ac8c4bfe94849e154d9cdff329194ae
1 <?php
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.
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.
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
19 * @file
22 /**
23 * Generates a colourful notification intended for humans on IRC.
25 * @since 1.22
28 class IRCColourfulRCFeedFormatter implements RCFeedFormatter {
29 /**
30 * @see RCFeedFormatter::getLine
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_CATEGORIZE ) {
37 // Don't send RC_CATEGORIZE events to IRC feed (T127360)
38 return null;
41 if ( $attribs['rc_type'] == RC_LOG ) {
42 // Don't use SpecialPage::getTitleFor, backwards compatibility with
43 // IRC API which expects "Log".
44 $titleObj = Title::newFromText( 'Log/' . $attribs['rc_log_type'], NS_SPECIAL );
45 } else {
46 $titleObj =& $rc->getTitle();
48 $title = $titleObj->getPrefixedText();
49 $title = self::cleanupForIRC( $title );
51 if ( $attribs['rc_type'] == RC_LOG ) {
52 $url = '';
53 } else {
54 $url = $wgCanonicalServer . $wgScript;
55 if ( $attribs['rc_type'] == RC_NEW ) {
56 $query = '?oldid=' . $attribs['rc_this_oldid'];
57 } else {
58 $query = '?diff=' . $attribs['rc_this_oldid'] . '&oldid=' . $attribs['rc_last_oldid'];
60 if ( $wgUseRCPatrol || ( $attribs['rc_type'] == RC_NEW && $wgUseNPPatrol ) ) {
61 $query .= '&rcid=' . $attribs['rc_id'];
63 // HACK: We need this hook for WMF's secure server setup
64 Hooks::run( 'IRCLineURL', [ &$url, &$query, $rc ] );
65 $url .= $query;
68 if ( $attribs['rc_old_len'] !== null && $attribs['rc_new_len'] !== null ) {
69 $szdiff = $attribs['rc_new_len'] - $attribs['rc_old_len'];
70 if ( $szdiff < -500 ) {
71 $szdiff = "\002$szdiff\002";
72 } elseif ( $szdiff >= 0 ) {
73 $szdiff = '+' . $szdiff;
75 // @todo i18n with parentheses in content language?
76 $szdiff = '(' . $szdiff . ')';
77 } else {
78 $szdiff = '';
81 $user = self::cleanupForIRC( $attribs['rc_user_text'] );
83 if ( $attribs['rc_type'] == RC_LOG ) {
84 $targetText = $rc->getTitle()->getPrefixedText();
85 $comment = self::cleanupForIRC( str_replace(
86 "[[$targetText]]",
87 "[[\00302$targetText\00310]]",
88 $actionComment
89 ) );
90 $flag = $attribs['rc_log_action'];
91 } else {
92 $comment = self::cleanupForIRC( $attribs['rc_comment'] );
93 $flag = '';
94 if ( !$attribs['rc_patrolled']
95 && ( $wgUseRCPatrol || $attribs['rc_type'] == RC_NEW && $wgUseNPPatrol )
96 ) {
97 $flag .= '!';
99 $flag .= ( $attribs['rc_type'] == RC_NEW ? "N" : "" )
100 . ( $attribs['rc_minor'] ? "M" : "" ) . ( $attribs['rc_bot'] ? "B" : "" );
103 if ( $feed['add_interwiki_prefix'] === true && $wgLocalInterwikis ) {
104 // we use the first entry in $wgLocalInterwikis in recent changes feeds
105 $prefix = $wgLocalInterwikis[0];
106 } elseif ( $feed['add_interwiki_prefix'] ) {
107 $prefix = $feed['add_interwiki_prefix'];
108 } else {
109 $prefix = false;
111 if ( $prefix !== false ) {
112 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
113 } else {
114 $titleString = "\00314[[\00307$title\00314]]";
117 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
118 # no colour (\003) switches back to the term default
119 $fullString = "$titleString\0034 $flag\00310 " .
120 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
122 return $fullString;
126 * Remove newlines, carriage returns and decode html entites
127 * @param string $text
128 * @return string
130 public static function cleanupForIRC( $text ) {
131 return str_replace(
132 [ "\n", "\r" ],
133 [ " ", "" ],
134 Sanitizer::decodeCharReferences( $text )