Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
IRCColourfulRCFeedFormatter
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 2
420
0.00% covered (danger)
0.00%
0 / 1
 getLine
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 1
380
 cleanupForIRC
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
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
22use MediaWiki\MainConfigNames;
23use MediaWiki\MediaWikiServices;
24use MediaWiki\Parser\Sanitizer;
25use MediaWiki\Title\Title;
26
27/**
28 * Format a notification as a human-readable string using IRC colour codes.
29 *
30 * Parameters:
31 * - `add_interwiki_prefix`: Whether the titles should be prefixed with
32 *   the first entry in the $wgLocalInterwikis array (or the value of
33 *   $wgLocalInterwiki, if set).
34 *   Default: false.
35 *
36 * @see $wgRCFeeds
37 * @since 1.22
38 */
39
40class IRCColourfulRCFeedFormatter implements RCFeedFormatter {
41    /**
42     * @see RCFeedFormatter::getLine
43     * @param array $feed
44     * @param RecentChange $rc
45     * @param string|null $actionComment
46     * @return string|null
47     */
48    public function getLine( array $feed, RecentChange $rc, $actionComment ) {
49        $services = MediaWikiServices::getInstance();
50        $mainConfig = $services->getMainConfig();
51        $localInterwikis = $mainConfig->get( MainConfigNames::LocalInterwikis );
52        $useRCPatrol = $mainConfig->get( MainConfigNames::UseRCPatrol );
53        $useNPPatrol = $mainConfig->get( MainConfigNames::UseNPPatrol );
54        $attribs = $rc->getAttributes();
55        if ( $attribs['rc_type'] == RC_CATEGORIZE ) {
56            // Don't send RC_CATEGORIZE events to IRC feed (T127360)
57            return null;
58        }
59
60        if ( $attribs['rc_type'] == RC_LOG ) {
61            // Don't use SpecialPage::getTitleFor, backwards compatibility with
62            // IRC API which expects "Log".
63            $titleObj = Title::newFromText( 'Log/' . $attribs['rc_log_type'], NS_SPECIAL );
64        } else {
65            $titleObj = $rc->getTitle();
66        }
67        $title = $titleObj->getPrefixedText();
68        $title = self::cleanupForIRC( $title );
69
70        $notifyUrl = $rc->getNotifyUrl() ?? '';
71
72        if ( $attribs['rc_old_len'] !== null && $attribs['rc_new_len'] !== null ) {
73            $szdiff = $attribs['rc_new_len'] - $attribs['rc_old_len'];
74            if ( $szdiff < -500 ) {
75                $szdiff = "\002$szdiff\002";
76            } elseif ( $szdiff >= 0 ) {
77                $szdiff = '+' . $szdiff;
78            }
79            // @todo i18n with parentheses in content language?
80            $szdiff = '(' . $szdiff . ')';
81        } else {
82            $szdiff = '';
83        }
84
85        $user = self::cleanupForIRC( $attribs['rc_user_text'] );
86
87        if ( $attribs['rc_type'] == RC_LOG ) {
88            $targetText = $rc->getTitle()->getPrefixedText();
89            $comment = self::cleanupForIRC( str_replace(
90                "[[$targetText]]",
91                "[[\00302$targetText\00310]]",
92                $actionComment
93            ) );
94            $flag = $attribs['rc_log_action'];
95        } else {
96            $store = $services->getCommentStore();
97            $comment = self::cleanupForIRC( $store->getComment( 'rc_comment', $attribs )->text );
98            $flag = '';
99            if ( !$attribs['rc_patrolled']
100                && ( $useRCPatrol || ( $attribs['rc_type'] == RC_NEW && $useNPPatrol ) )
101            ) {
102                $flag .= '!';
103            }
104            $flag .= ( $attribs['rc_type'] == RC_NEW ? "N" : "" )
105                . ( $attribs['rc_minor'] ? "M" : "" ) . ( $attribs['rc_bot'] ? "B" : "" );
106        }
107
108        if ( $feed['add_interwiki_prefix'] === true && $localInterwikis ) {
109            // we use the first entry in $wgLocalInterwikis in recent changes feeds
110            $prefix = $localInterwikis[0];
111        } elseif ( $feed['add_interwiki_prefix'] ) {
112            $prefix = $feed['add_interwiki_prefix'];
113        } else {
114            $prefix = false;
115        }
116        if ( $prefix !== false ) {
117            $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
118        } else {
119            $titleString = "\00314[[\00307$title\00314]]";
120        }
121
122        # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
123        # no colour (\003) switches back to the term default
124        $fullString = "$titleString\0034 $flag\00310 " .
125            "\00302$notifyUrl\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
126
127        return $fullString;
128    }
129
130    /**
131     * Remove newlines, carriage returns and decode html entities
132     * @param string $text
133     * @return string
134     */
135    public static function cleanupForIRC( $text ) {
136        return str_replace(
137            [ "\n", "\r" ],
138            [ " ", "" ],
139            Sanitizer::decodeCharReferences( $text )
140        );
141    }
142}