Jump to content

Extension:DiscordRCFeed

From mediawiki.org
MediaWiki extensions manual
DiscordRCFeed
Release status: stable
Implementation Notify
Description Provides FormattedRCFeed and RCFeedFormatter subclasses for Discord
Author(s) Aleksi Postari, Lens0021
Latest version 1.0.3
Compatibility policy Snapshots releases along with MediaWiki. Master is not backward compatible.
MediaWiki >= 1.35.0
PHP >= 7.3
Database changes No
License MIT License
Download
Quarterly downloads 16 (Ranked 119th)
Translate the DiscordRCFeed extension if it is available at translatewiki.net

DiscordRCFeed extension provides the Discord versions of FormattedRCFeed and RCFeedFormatter subclasses which can be used in $wgRCFeeds .

Requirements

[edit]
  • Setting a feed requires the sockets PHP extension. If the extension is not enabled, actions like edits, moves, etc may work, but the action may not get logged in recent changes at all. See Manual:$wgRCFeeds for details.
  • Apache should have NE (NoEscape) flag on to prevent issues in URLs. By default you should have this enabled.

Installation

[edit]
  • Create a new Discord Webhook for your channel. You can create and manage webhooks for your channel by clicking the settings icon next to channel name in the Discord app. Read more from here: https://support.discord.com/hc/articles/228383668
  • After setting up the Webhook you will get a Webhook URL. Copy that URL as you will need it in the following steps.
  • Download and move the extracted DiscordRCFeed folder to your extensions/ directory.
    Developers and code contributors should install the extension from Git instead, using:cd extensions/
    git clone https://gerrit.wikimedia.org/r/mediawiki/extensions/DiscordRCFeed
  • Add the following code at the bottom of your LocalSettings.php file:
    wfLoadExtension( 'DiscordRCFeed' );
    $wgRCFeeds['discord'] = [
    	// Your Discord webhook URL here.
    	'url' => 'https://discord.com/api/webhooks/xx/xxxx',
    ];
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Additional options

[edit]

You can set the element that has the following key for the $wgRCFeeds['discord']:

Name Possible value Description Default
Display options
'style' 'structure', 'embed' or 'inline' Determine how the feed is shown. 'structure'
'user_tools' See below Controlling which tools to be included displaying the feed. See #Controlling Page Tools and User Tools below for details. defined in code
'page_tools'
Ignoring changes
'omit_bots' true or false Whether to skip bot edits. Same as described on Manual:$wgRCFeeds. false
'omit_anon' Whether to skip anon edits. Same as described on Manual:$wgRCFeeds.
'omit_user' Whether to skip registered users. Same as described on Manual:$wgRCFeeds.
'omit_minor' Whether to skip minor edits. Same as described on Manual:$wgRCFeeds.
'omit_patrolled' Whether to skip patrolled edits. Same as described on Manual:$wgRCFeeds.
'omit_talk' Whether to skip changes on talk pages.
'omit_namespaces' See below Lists for changes to be ignored by the feed. See #Filtering Notifications below for details. []
'omit_types'
'omit_log_types' ['patrol']
'omit_log_actions' []
'omit_content_models'
Scoping changes
'only_talk' true or false If this is true, only changes on talk pages would be displayed as all other changes are ignored. false
'only_namespaces' See below If not empty, only changes that match the given condition(s) are sent. See #Filtering Notifications below for details. []
'only_types'
'only_log_types'
'only_log_actions'
'only_content_models'
Other
'request_replace' See below An array which used to override the post data of the webhook request. See #Webhook Request Overriding below for details. []

Filtering Notifications

[edit]

$wgRCFeeds['discord']['omit_namespaces'] is a list that contains namespaces should be omitted.

// Disabling notifications from user talk page
$wgRCFeeds['discord']['omit_namespaces'] = [ NS_USER_TALK ];

// Disabling notifications from talk pages.
$wgRCFeeds['discord']['omit_namespaces'] = [
	NS_TALK,
	NS_USER_TALK,
	NS_PROJECT_TALK,
	NS_FILE_TALK,
	NS_MEDIAWIKI_TALK,
	NS_TEMPLATE_TALK,
	NS_HELP_TALK,
	NS_CATEGORY_TALK,
	NS_MODULE_TALK,
];

The others are similar.

  • 'omit_types' can contain RC_EDIT, RC_NEW, RC_LOG and RC_EXTERNAL. (see more) Note that RC_CATEGORIZE is always omitted by the same cause of phab:T127360.
  • 'omit_log_types' can contain'move','protect', 'delete', 'block', 'upload'...
  • 'omit_log_actions' can contain'move/move-noredirect', 'block/block', 'block/unblock'... (see more)
  • 'omit_content_models' can containCONTENT_MODEL_WIKITEXT, CONTENT_MODEL_JAVASCRIPT, CONTENT_MODEL_CSS... (see more)

The next example shows how to disable the new user notification.

$wgRCFeeds['discord']['omit_log_types'] = [
	'newusers',
];

Controlling Page Tools and User Tools

[edit]

Page tools and user tools are tools shown after page or user link.

Option Description Default
'user_tools' If this is false, user links of the feed would not get additional tool links. (talk | contribs)
'page_tools' If this is false, page links of the feed would not get additional tool links. (hist | diff)
// Remove page tools
$wgRCFeeds['discord']['page_tools'] = false;

// Redefine user tools
$wgRCFeeds['discord']['user_tools'] = [
	[
		'target' => 'special',
		'special' => 'Block',
		'text' => 'IP Block'
	],
	[
		'target' => 'talk',
		'text' => 'Discussion'
	],
	[
		'target' => 'special',
		'special' => 'Contributions',
		// message would be shown if 'msg' is given.
		'msg' => 'contribslink'
	],
];

The full default values can be found in includes/Constants.php.

Webhook Request Overriding

[edit]

$wgRCFeeds['discord']['request_replace'] is an associative array which used to override the post data of the webhook request. You can set username or avatar using this instead of setting in Discord. Visit https://discord.com/developers/docs/resources/webhook#execute-webhook-jsonform-params for all available parameters.

$wgRCFeeds['discord']['request_replace'] = [
	'username' => 'Captain Hook',
	'avatar_url' => 'https://upload.wikimedia.org/wikipedia/commons/7/79/MediaWiki-48px.png'
];

Next example shows how to reuse your icon set in $wgLogos.

$wgRCFeeds['discord']['request_replace'] = [
	'avatar_url' => "$wgCanonicalServer/${wgLogos['icon']}",
];

Registering Multiple Webhooks

[edit]

You can register multiple webhooks with separate settings. Only the important part is that all keys of the feeds you want to must start with 'discord', for example, 'discord', 'discord-second', 'discord2' and 'discord_copy' are valid keys.

wfLoadExtension( 'DiscordRCFeed' );
// Divide anonymous and registered users.
$wgRCFeeds = [
	'discord' => [
		'url' => 'https://discord.com/api/webhooks/aaa/xxxx',
		'omit_user' = true,
	],
	'discord_registered' => [
		'url' => 'https://discord.com/api/webhooks/bbb/xxxx',
		'omit_anon' = true,
	],
];

Configuration Examples

[edit]

Splitting changes on talk pages from others

[edit]
$wgRCFeeds = [
	'discord_content' => [
		'url' => 'https://discord.com/api/webhooks/aaa/xxxx',
		'omit_talk' => true,
	],
	'discord_talk' => [
		'url' => 'https://discord.com/api/webhooks/bbb/xxxx',
		'only_talk' => true,
	],
];

Note that this is not suitable for wiki where Structured Discussions is enabled because a page on NS_TOPIC is not considered as a talk page by Title::isTalkPage(). In that case, filtering by namespace is only the solution:

$wgRCFeeds['discord'] = [
	'url' => 'https://discord.com/api/webhooks/aaa/xxxx',
	'omit_namespaces' => [
		NS_TALK,
		NS_USER_TALK,
		NS_PROJECT_TALK,
		NS_FILE_TALK,
		NS_MEDIAWIKI_TALK,
		NS_TEMPLATE_TALK,
		NS_HELP_TALK,
		NS_CATEGORY_TALK,
		829, // NS_MODULE_TALK, if Scribunto is installed
		2600, // NS_TOPIC
		// could be longer
	],
];
$wgRCFeeds['discord_talk'] = [
	'url' => 'https://discord.com/api/webhooks/bbb/xxxx',
	'only_namespaces' => $wgRCFeeds['discord']['omit_namespaces'],
];

Reusing a feed setting for multiple servers

[edit]
// Define the source.
$wgRCFeeds['discord1'] = [
	'url' => 'https://discord.com/api/webhooks/aaa/xxxx',
	'omit_bots' => true,
	'omit_minor' => true,
	'omit_nemespaces' => [
	    NS_PROJECT,
	    NS_TEMPLATE,
	    NS_MODULE,
	    NS_USER,
    ],
    'only_types' => [ RC_EDIT, RC_NEW ],
];

// Reuse the same configuration.
$wgRCFeeds['discord2'] = $wgRCFeeds['discord1'];
$wgRCFeeds['discord2']['url'] = 'https://discord.com/api/webhooks/bbb/xxxx',

// Reuse but with some tweak.
$wgRCFeeds['discord3'] = $wgRCFeeds['discord1'];
$wgRCFeeds['discord3']['url'] = 'https://discord.com/api/webhooks/ccc/xxxx',
$wgRCFeeds['discord3']['omit_minor'] = false;
unset( $wgRCFeeds['discord3']['only_types'] );

JavaScript Changes Only

[edit]
$wgRCFeeds = [
	'discord' => [
		'url' => 'https://discord.com/api/webhooks/xxx/xxxx',
		'only_content_models' => CONTENT_MODEL_JAVASCRIPT,
	],
];

Notes

[edit]
  • Basically, DiscordRCFeed is a fork of DiscordNotifications, but heavily modified.
  • The basic usage of $wgRCFeeds is like below:
    $wgRCFeeds['discord'] = [
        'formatter' => 'MediaWiki\Extension\DiscordRCFeed\DiscordRCFeedFormatter',
    	'url' => 'https://discord.com/api/webhooks/xxx/xxxx',
    ];
    
    But because the 'formatter' value is too long for the end-users, DiscordRCFeed automatically uses 'MediaWiki\Extension\DiscordRCFeed\DiscordRCFeedFormatter' as its default formatter, if the value is omitted and the key of RCFeed is starts with 'discord'.

See also

[edit]

This extension was originally made for Femiwiki.