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
22 use MediaWiki\MediaWikiServices
;
25 * Generates a colourful notification intended for humans on IRC.
30 class IRCColourfulRCFeedFormatter
implements RCFeedFormatter
{
32 * @see RCFeedFormatter::getLine
34 * @param RecentChange $rc
35 * @param string|null $actionComment
38 public function getLine( array $feed, RecentChange
$rc, $actionComment ) {
39 global $wgUseRCPatrol, $wgUseNPPatrol, $wgLocalInterwikis,
40 $wgCanonicalServer, $wgScript;
41 $attribs = $rc->getAttributes();
42 if ( $attribs['rc_type'] == RC_CATEGORIZE
) {
43 // Don't send RC_CATEGORIZE events to IRC feed (T127360)
47 if ( $attribs['rc_type'] == RC_LOG
) {
48 // Don't use SpecialPage::getTitleFor, backwards compatibility with
49 // IRC API which expects "Log".
50 $titleObj = Title
::newFromText( 'Log/' . $attribs['rc_log_type'], NS_SPECIAL
);
52 $titleObj =& $rc->getTitle();
54 $title = $titleObj->getPrefixedText();
55 $title = self
::cleanupForIRC( $title );
57 if ( $attribs['rc_type'] == RC_LOG
) {
60 $url = $wgCanonicalServer . $wgScript;
61 if ( $attribs['rc_type'] == RC_NEW
) {
62 $query = '?oldid=' . $attribs['rc_this_oldid'];
64 $query = '?diff=' . $attribs['rc_this_oldid'] . '&oldid=' . $attribs['rc_last_oldid'];
66 if ( $wgUseRCPatrol ||
( $attribs['rc_type'] == RC_NEW
&& $wgUseNPPatrol ) ) {
67 $query .= '&rcid=' . $attribs['rc_id'];
69 // HACK: We need this hook for WMF's secure server setup
70 Hooks
::run( 'IRCLineURL', [ &$url, &$query, $rc ] );
74 if ( $attribs['rc_old_len'] !== null && $attribs['rc_new_len'] !== null ) {
75 $szdiff = $attribs['rc_new_len'] - $attribs['rc_old_len'];
76 if ( $szdiff < -500 ) {
77 $szdiff = "\002$szdiff\002";
78 } elseif ( $szdiff >= 0 ) {
79 $szdiff = '+' . $szdiff;
81 // @todo i18n with parentheses in content language?
82 $szdiff = '(' . $szdiff . ')';
87 $user = self
::cleanupForIRC( $attribs['rc_user_text'] );
89 if ( $attribs['rc_type'] == RC_LOG
) {
90 $targetText = $rc->getTitle()->getPrefixedText();
91 $comment = self
::cleanupForIRC( str_replace(
93 "[[\00302$targetText\00310]]",
96 $flag = $attribs['rc_log_action'];
98 $store = MediaWikiServices
::getInstance()->getCommentStore();
99 $comment = self
::cleanupForIRC(
100 $store->getComment( 'rc_comment', $attribs )->text
103 if ( !$attribs['rc_patrolled']
104 && ( $wgUseRCPatrol ||
$attribs['rc_type'] == RC_NEW
&& $wgUseNPPatrol )
108 $flag .= ( $attribs['rc_type'] == RC_NEW ?
"N" : "" )
109 . ( $attribs['rc_minor'] ?
"M" : "" ) . ( $attribs['rc_bot'] ?
"B" : "" );
112 if ( $feed['add_interwiki_prefix'] === true && $wgLocalInterwikis ) {
113 // we use the first entry in $wgLocalInterwikis in recent changes feeds
114 $prefix = $wgLocalInterwikis[0];
115 } elseif ( $feed['add_interwiki_prefix'] ) {
116 $prefix = $feed['add_interwiki_prefix'];
120 if ( $prefix !== false ) {
121 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
123 $titleString = "\00314[[\00307$title\00314]]";
126 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
127 # no colour (\003) switches back to the term default
128 $fullString = "$titleString\0034 $flag\00310 " .
129 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
135 * Remove newlines, carriage returns and decode html entities
136 * @param string $text
139 public static function cleanupForIRC( $text ) {
143 Sanitizer
::decodeCharReferences( $text )