Skip to content

Commit

Permalink
MirahezeIRCRCFeedFormatter: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
Universal-Omega authored Nov 23, 2024
1 parent 0453f25 commit 6a147c4
Showing 1 changed file with 9 additions and 134 deletions.
143 changes: 9 additions & 134 deletions includes/MirahezeIRCRCFeedFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,24 @@

namespace Miraheze\MirahezeMagic;

/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/

use IRCColourfulRCFeedFormatter;
use MediaWiki\Extension\CentralAuth\User\CentralAuthUser;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Parser\Sanitizer;
use MediaWiki\Title\Title;
use RCFeedFormatter;
use RecentChange;

/**
* Format a notification as a human-readable string using IRC colour codes.
*
* Parameters:
* - `add_interwiki_prefix`: Whether the titles should be prefixed with
* the first entry in the $wgLocalInterwikis array (or the value of
* $wgLocalInterwiki, if set).
* Default: false.
*
* @see $wgRCFeeds
* @since 1.22
*/

class MirahezeIRCRCFeedFormatter implements RCFeedFormatter {
class MirahezeIRCRCFeedFormatter implements IRCColourfulRCFeedFormatter {

/**
* @see RCFeedFormatter::getLine
* @param array $feed
* @param RecentChange $rc
* @param string|null $actionComment
* @return string|null
*/
/** @inheritDoc */
public function getLine( array $feed, RecentChange $rc, $actionComment ) {
$services = MediaWikiServices::getInstance();
$mainConfig = $services->getMainConfig();
$localInterwikis = $mainConfig->get( MainConfigNames::LocalInterwikis );
$useRCPatrol = $mainConfig->get( MainConfigNames::UseRCPatrol );
$useNPPatrol = $mainConfig->get( MainConfigNames::UseNPPatrol );
$dbName = $mainConfig->get( MainConfigNames::DBname );
$mainConfig = MediaWikiServices::getInstance()->getMainConfig();
$dbname = $mainConfig->get( MainConfigNames::DBname );

$attribs = $rc->getAttributes();
if ( $attribs['rc_type'] == RC_CATEGORIZE ) {
// Don't send RC_CATEGORIZE events to IRC feed (T127360)
return null;
}

/**
* Don't send renameuser log events to IRC for the
* Miraheze Trust and Safety team.
* WikiTide Foundation Trust and Safety team.
*/
if (
$attribs['rc_type'] == RC_LOG &&
Expand All @@ -78,87 +31,9 @@ public function getLine( array $feed, RecentChange $rc, $actionComment ) {
}
}

if ( $attribs['rc_type'] == RC_LOG ) {
// Don't use SpecialPage::getTitleFor, backwards compatibility with
// IRC API which expects "Log".
$titleObj = Title::newFromText( 'Log/' . $attribs['rc_log_type'], NS_SPECIAL );
} else {
$titleObj = Title::castFromPageReference( $rc->getPage() ) ?: Title::makeTitle( NS_SPECIAL, 'BadTitle' );
}
$title = $titleObj->getPrefixedText();
$title = self::cleanupForIRC( $title );

$notifyUrl = $rc->getNotifyUrl() ?? '';

if ( $attribs['rc_old_len'] !== null && $attribs['rc_new_len'] !== null ) {
$szdiff = $attribs['rc_new_len'] - $attribs['rc_old_len'];
if ( $szdiff < -500 ) {
$szdiff = "\002$szdiff\002";
} elseif ( $szdiff >= 0 ) {
$szdiff = '+' . $szdiff;
}
// @todo i18n with parentheses in content language?
$szdiff = '(' . $szdiff . ')';
} else {
$szdiff = '';
}

$user = self::cleanupForIRC( $attribs['rc_user_text'] );

if ( $attribs['rc_type'] == RC_LOG ) {
$targetTitle = Title::castFromPageReference( $rc->getPage() ) ?: Title::makeTitle( NS_SPECIAL, 'BadTitle' );
$targetText = $targetTitle->getPrefixedText();
$comment = self::cleanupForIRC( str_replace(
"[[$targetText]]",
"[[\00302$targetText\00310]]",
$actionComment
) );
$flag = $attribs['rc_log_action'];
} else {
$store = $services->getCommentStore();
$comment = self::cleanupForIRC( $store->getComment( 'rc_comment', $attribs )->text );
$flag = '';
if ( !$attribs['rc_patrolled']
&& ( $useRCPatrol || ( $attribs['rc_type'] == RC_NEW && $useNPPatrol ) )
) {
$flag .= '!';
}
$flag .= ( $attribs['rc_type'] == RC_NEW ? "N" : "" )
. ( $attribs['rc_minor'] ? "M" : "" ) . ( $attribs['rc_bot'] ? "B" : "" );
}

if ( $feed['add_interwiki_prefix'] === true && $localInterwikis ) {
// we use the first entry in $wgLocalInterwikis in recent changes feeds
$prefix = $localInterwikis[0];
} elseif ( $feed['add_interwiki_prefix'] ) {
$prefix = $feed['add_interwiki_prefix'];
} else {
$prefix = false;
}
if ( $prefix !== false ) {
$titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
} else {
$titleString = "\00314[[\00307$title\00314]]";
}

# see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
# no colour (\003) switches back to the term default
$fullString = "$dbName \0035*\003 $titleString\0034 $flag\00310 " .
"\00302$notifyUrl\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";

return $fullString;
}

/**
* Remove newlines, carriage returns and decode html entities
* @param string $text
* @return string
*/
public static function cleanupForIRC( $text ) {
return str_replace(
[ "\n", "\r" ],
[ " ", "" ],
Sanitizer::decodeCharReferences( $text )
);
return "$dbname \0035*\003 " .
parent::getLine( $feed, $rc, $actionComment );
}
}

0 comments on commit 6a147c4

Please sign in to comment.