Merge "rcfeed: Replace usage of deprecated CommentStore::getStore()"
[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 use MediaWiki\MediaWikiServices;
23
24 /**
25 * Generates a colourful notification intended for humans on IRC.
26 *
27 * @since 1.22
28 */
29
30 class IRCColourfulRCFeedFormatter implements RCFeedFormatter {
31 /**
32 * @see RCFeedFormatter::getLine
33 * @param array $feed
34 * @param RecentChange $rc
35 * @param string|null $actionComment
36 * @return string|null
37 */
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)
44 return null;
45 }
46
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 );
51 } else {
52 $titleObj =& $rc->getTitle();
53 }
54 $title = $titleObj->getPrefixedText();
55 $title = self::cleanupForIRC( $title );
56
57 if ( $attribs['rc_type'] == RC_LOG ) {
58 $url = '';
59 } else {
60 $url = $wgCanonicalServer . $wgScript;
61 if ( $attribs['rc_type'] == RC_NEW ) {
62 $query = '?oldid=' . $attribs['rc_this_oldid'];
63 } else {
64 $query = '?diff=' . $attribs['rc_this_oldid'] . '&oldid=' . $attribs['rc_last_oldid'];
65 }
66 if ( $wgUseRCPatrol || ( $attribs['rc_type'] == RC_NEW && $wgUseNPPatrol ) ) {
67 $query .= '&rcid=' . $attribs['rc_id'];
68 }
69 // HACK: We need this hook for WMF's secure server setup
70 Hooks::run( 'IRCLineURL', [ &$url, &$query, $rc ] );
71 $url .= $query;
72 }
73
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;
80 }
81 // @todo i18n with parentheses in content language?
82 $szdiff = '(' . $szdiff . ')';
83 } else {
84 $szdiff = '';
85 }
86
87 $user = self::cleanupForIRC( $attribs['rc_user_text'] );
88
89 if ( $attribs['rc_type'] == RC_LOG ) {
90 $targetText = $rc->getTitle()->getPrefixedText();
91 $comment = self::cleanupForIRC( str_replace(
92 "[[$targetText]]",
93 "[[\00302$targetText\00310]]",
94 $actionComment
95 ) );
96 $flag = $attribs['rc_log_action'];
97 } else {
98 $store = MediaWikiServices::getInstance()->getCommentStore();
99 $comment = self::cleanupForIRC(
100 $store->getComment( 'rc_comment', $attribs )->text
101 );
102 $flag = '';
103 if ( !$attribs['rc_patrolled']
104 && ( $wgUseRCPatrol || $attribs['rc_type'] == RC_NEW && $wgUseNPPatrol )
105 ) {
106 $flag .= '!';
107 }
108 $flag .= ( $attribs['rc_type'] == RC_NEW ? "N" : "" )
109 . ( $attribs['rc_minor'] ? "M" : "" ) . ( $attribs['rc_bot'] ? "B" : "" );
110 }
111
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'];
117 } else {
118 $prefix = false;
119 }
120 if ( $prefix !== false ) {
121 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
122 } else {
123 $titleString = "\00314[[\00307$title\00314]]";
124 }
125
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";
130
131 return $fullString;
132 }
133
134 /**
135 * Remove newlines, carriage returns and decode html entites
136 * @param string $text
137 * @return string
138 */
139 public static function cleanupForIRC( $text ) {
140 return str_replace(
141 [ "\n", "\r" ],
142 [ " ", "" ],
143 Sanitizer::decodeCharReferences( $text )
144 );
145 }
146 }