Merge "Added a separate error message for mkdir failures"
[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 /**
23 * Generates a colourful notification intended for humans on IRC.
24 *
25 * @since 1.22
26 */
27
28 class IRCColourfulRCFeedFormatter implements RCFeedFormatter {
29 /**
30 * @see RCFeedFormatter::getLine
31 */
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;
39 }
40
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();
47 }
48 $title = $titleObj->getPrefixedText();
49 $title = self::cleanupForIRC( $title );
50
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'];
59 }
60 if ( $wgUseRCPatrol || ( $attribs['rc_type'] == RC_NEW && $wgUseNPPatrol ) ) {
61 $query .= '&rcid=' . $attribs['rc_id'];
62 }
63 // HACK: We need this hook for WMF's secure server setup
64 Hooks::run( 'IRCLineURL', [ &$url, &$query, $rc ] );
65 $url .= $query;
66 }
67
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;
74 }
75 // @todo i18n with parentheses in content language?
76 $szdiff = '(' . $szdiff . ')';
77 } else {
78 $szdiff = '';
79 }
80
81 $user = self::cleanupForIRC( $attribs['rc_user_text'] );
82
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 .= '!';
98 }
99 $flag .= ( $attribs['rc_type'] == RC_NEW ? "N" : "" )
100 . ( $attribs['rc_minor'] ? "M" : "" ) . ( $attribs['rc_bot'] ? "B" : "" );
101 }
102
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;
110 }
111 if ( $prefix !== false ) {
112 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
113 } else {
114 $titleString = "\00314[[\00307$title\00314]]";
115 }
116
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";
121
122 return $fullString;
123 }
124
125 /**
126 * Remove newlines, carriage returns and decode html entites
127 * @param string $text
128 * @return string
129 */
130 public static function cleanupForIRC( $text ) {
131 return str_replace(
132 [ "\n", "\r" ],
133 [ " ", "" ],
134 Sanitizer::decodeCharReferences( $text )
135 );
136 }
137 }