Debian Codegraph @

includes/recentchanges/RCFeed/IRCColourfulRCFeedFormatter.php

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