Compare commits

...

18 Commits

Author SHA1 Message Date
Jayden Bailey e000672074 [backport] Do not parse any Discord mentions in webhook messages
Resolves #42, closes #41
2022-04-18 21:14:56 +01:00
Jayden Bailey 6b9222a6df Merge branch 'master' into REL1_35 2021-10-19 14:21:00 +01:00
Jayden Bailey 0a6992b13b Update README 2021-10-19 14:20:41 +01:00
Jayden Bailey 925ca2c17c Remove composer.json. I don't use unit tests here. 2021-10-19 14:12:19 +01:00
Jayden Bailey 95567934b0 Remove composer.json. I don't use unit tests here. 2021-10-19 14:06:10 +01:00
Jayden 5e04b3afda Merge pull request #36 from dodocodes/patch-2
Update README.md
2021-09-14 02:55:52 +01:00
Jayden 91610c72fd Update README.md 2021-09-14 02:55:40 +01:00
Jayden f1cae6b097 Merge pull request #35 from DesignerThan95/patch-1
Update extension.json - added missing colon
2021-09-14 02:55:08 +01:00
DodoCodes.com 1a9cbbf48f Update README.md
Added instructions for installing missing dependencies.
2021-09-12 18:16:38 -07:00
DesignerThan df0b14017b Update extension.json - added missing colon 2021-08-17 23:38:05 +02:00
Jayden Bailey ffc78324ff Fix bug with preg_replace for username truncating and change truncation to be configurable 2021-05-04 17:27:34 +01:00
Jayden Bailey 5a303252e3 Merge branch 'master' of github.com:jaydenkieran/mw-discord 2021-05-04 17:02:24 +01:00
Jayden Bailey 4ec63619f5 Change emojis to be configurable (resolves #25) 2021-05-04 17:00:55 +01:00
Jayden Bailey 572d9e3d87 Sanitise summary/reason text input (closes #13) 2021-05-04 17:00:47 +01:00
Jayden Bailey 3eb34310ef Merge pull request #33 from Joeytje50/patch-1
Abbreviate anyone with a name longer than 25 chars
2021-05-04 16:08:34 +01:00
Joeytje50 7c20f06902 Abbreviate anyone with a name longer than 25 chars
Any username of 25 or more characters will be abbreviated to the first 20 characters, then `...`, and then the last three. This is especially helpful for RC stalkers with discord taking up only half their screen.

Different ways to abbreviate names would be possible by changing the regex pattern's numbers, or the resulting string.
2021-04-23 17:13:11 +02:00
Jayden Bailey 144499a248 Update README with branch/install info 2020-09-18 14:58:28 +01:00
Jayden Bailey 079e688ee9 Fix fatal error when blocking users
Fixes #27, closes #29
2020-09-18 14:51:07 +01:00
6 changed files with 145 additions and 78 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
MIT License
Copyright (c) 2018 Jayden Bailey
Copyright (c) 2021 Jayden Bailey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
+5 -3
View File
@@ -11,14 +11,14 @@ Multiple webhook URLs are supported and messages will be sent to all of them.
## Requirements
- **Discord webhook URL**: This can be obtained by editing a channel on a server with the correct permissions.
- **MediaWiki 1.31+**
- **MediaWiki 1.35+**. This extension aims to support the latest LTS release.
### Recommended
- **cURL**: By default, this extension sends requests using cURL. If you don't have cURL, you could try setting `$wgDiscordUseFileGetContents` to `true` instead, but this is not recommended.
## Installation
1. Clone this repository to your MediaWiki installation's `extensions` folder using `git clone https://github.com/jaydenkieran/mw-discord.git Discord`
1. Clone this repository to your MediaWiki installation's `extensions` folder using `git clone https://github.com/jaydenkieran/mw-discord.git -b REL1_35 Discord`
2. Modify your `LocalSettings.php` file and add:
```php
@@ -48,12 +48,14 @@ This extension can be configured using the `LocalSettings.php` file in your Medi
| `$wgDiscordNoNull` | bool | Do not send notifications for [null edits](https://www.mediawiki.org/wiki/Manual:Purge#Null_edits) | `true`
| `$wgDiscordSuppressPreviews` | bool | Force previews for links in Discord messages to be suppressed | `true`
| `$wgDiscordMaxChars` | int | Maximum amount of characters for user-generated text (e.g summaries, reasons). Set to `null` to disable truncation | `null`
| `$wgDiscordMaxCharsUsernames` | int | Maximum amount of characters for usernames. Set to `null` to disable truncation | `25`
| `$wgDiscordDisabledHooks` | array | List of hooks to disable sending webhooks for (see [below](#hooks-used)) | `[]`
| `$wgDiscordDisabledNS` | array | List of namespaces to disable sending webhooks for | `[]`
| `$wgDiscordDisabledUsers` | array | List of users whose performed actions shouldn't send webhooks | `[]`
| `$wgDiscordPrependTimestamp` | bool | Prepend a timestamp (in UTC) to all sent messages. The format can be changed by editing the MediaWiki message `discord-timestampformat` | `false`
| `$wgDiscordUseFileGetContents` | bool | Use `file_get_contents` instead of cURL. Requires `allow_url_fopen` to be set to true in `php.ini`. Not recommended as cURL makes simultaneous calls instead. | `false`
| `$wgDiscordUseEmojis` | bool | Prepend emojis to different types of messages to help distinguish them | `false`
| `$wgDiscordEmojis` | array | Map of hook names and their associated emojis to prepend to messages if `$wgDiscordUseEmojis` is enabled | See [extension.json](/extension.json)
## Hooks used
- `PageContentSaveComplete` - New edits to pages and page creations
@@ -84,4 +86,4 @@ This extension can be translated through the messages in the `ì18n` folder if y
Any excess whitespace in text that is translated will be stripped (e.g double spaces, etc).
## License
This extension is licensed under the MIT License, [see here](LICENSE) for more information. This project is originally inspired by Szmyk's [mediawiki-discord](https://github.com/Szmyk/mediawiki-discord) project, but has been rewritten completely to be more suitable for my needs.
This extension is licensed under the MIT License, [see here](LICENSE) for more information. This project is originally inspired by Szmyk's [mediawiki-discord](https://github.com/Szmyk/mediawiki-discord) project, but has been rewritten completely to be more suitable for my needs.
-5
View File
@@ -1,5 +0,0 @@
{
"require": {
"phpunit/phpunit": "4.8.*"
}
}
+23 -1
View File
@@ -15,12 +15,34 @@
"DiscordNoNull": true,
"DiscordSuppressPreviews": true,
"DiscordMaxChars": null,
"DiscordMaxCharsUsernames": 25,
"DiscordDisabledHooks": [],
"DiscordDisabledNS": [],
"DiscordDisabledUsers": [],
"DiscordPrependTimestamp": false,
"DiscordUseFileGetContents": false,
"DiscordUseEmojis": false
"DiscordUseEmojis": false,
"DiscordEmojis": {
"PageContentSaveComplete": ":pencil2:",
"ArticleDeleteComplete": ":wastebasket:",
"ArticleUndelete": ":wastebasket:",
"ArticleRevisionVisibilitySet": ":spy:",
"ArticleProtectComplete": ":lock:",
"TitleMoveComplete": ":truck:",
"LocalUserCreated": ":wave:",
"BlockIpComplete": ":no_entry_sign:",
"UnblockUserComplete": ":no_entry_sign:",
"UserGroupsChanged": ":people_holding_hands:",
"UploadComplete": ":inbox_tray:",
"FileDeleteComplete": ":wastebasket:",
"FileUndeleteComplete": ":wastebasket:",
"AfterImportPage": ":books:",
"ArticleMergeComplete": ":card_box:",
"ApprovedRevsRevisionApproved": ":white_check_mark:",
"ApprovedRevsRevisionUnapproved": ":white_check_mark:",
"ApprovedRevsFileRevisionApproved": ":white_check_mark:",
"ApprovedRevsFileRevisionUnapproved": ":white_check_mark:"
}
},
"AutoloadClasses": {
"DiscordHooks": "src/DiscordHooks.php",
+71 -48
View File
@@ -12,8 +12,9 @@ class DiscordHooks {
*/
public static function onPageContentSaveComplete( &$wikiPage, &$user, $content, $summary, $isMinor, $isWatch, $section, &$flags, $revision, &$status, $baseRevId, $undidRevId ) {
global $wgDiscordNoBots, $wgDiscordNoMinor, $wgDiscordNoNull;
$hookName = 'PageContentSaveComplete';
if ( DiscordUtils::isDisabled( 'PageContentSaveComplete', $wikiPage->getTitle()->getNamespace(), $user ) ) {
if ( DiscordUtils::isDisabled( $hookName, $wikiPage->getTitle()->getNamespace(), $user ) ) {
return true;
}
@@ -47,8 +48,8 @@ class DiscordHooks {
$msg = wfMessage( $msgKey, DiscordUtils::createUserLinks( $user ),
DiscordUtils::createMarkdownLink( $wikiPage->getTitle(), $wikiPage->getTitle()->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
DiscordUtils::createRevisionText( $revision ),
( $summary ? ('`' . DiscordUtils::truncateText( $summary ) . '`' ) : '' ) )->plain();
DiscordUtils::handleDiscord(':pencil2:', $msg);
( $summary ? ('`' . DiscordUtils::sanitiseText( DiscordUtils::truncateText( $summary ) ) . '`' ) : '' ) )->plain();
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
@@ -58,8 +59,9 @@ class DiscordHooks {
*/
public static function onArticleDeleteComplete( &$article, User &$user, $reason, $id, $content, LogEntry $logEntry, $archivedRevisionCount ) {
global $wgDiscordNoBots;
$hookName = 'ArticleDeleteComplete';
if ( DiscordUtils::isDisabled( 'ArticleDeleteComplete', $article->getTitle()->getNamespace(), $user ) ) {
if ( DiscordUtils::isDisabled( $hookName, $article->getTitle()->getNamespace(), $user ) ) {
return true;
}
@@ -70,9 +72,9 @@ class DiscordHooks {
$msg = wfMessage( 'discord-articledelete', DiscordUtils::createUserLinks( $user ),
DiscordUtils::createMarkdownLink( $article->getTitle(), $article->getTitle()->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
( $reason ? ('`' . DiscordUtils::truncateText( $reason ) . '`' ) : '' ),
( $reason ? ('`' . DiscordUtils::sanitiseText( DiscordUtils::truncateText( $reason ) ) . '`' ) : '' ),
$archivedRevisionCount)->plain();
DiscordUtils::handleDiscord(':wastebasket:', $msg);
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
@@ -82,10 +84,11 @@ class DiscordHooks {
*/
public static function onArticleUndelete( Title $title, $create, $comment, $oldPageId, $restoredPages ) {
global $wgDiscordNoBots;
$hookName = 'ArticleUndelete';
$user = RequestContext::getMain()->getUser();
if ( DiscordUtils::isDisabled( 'ArticleUndelete', $title->getNamespace(), $user ) ) {
if ( DiscordUtils::isDisabled( $hookName, $title->getNamespace(), $user ) ) {
return true;
}
@@ -97,8 +100,8 @@ class DiscordHooks {
$msg = wfMessage( 'discord-articleundelete', DiscordUtils::createUserLinks( $user ),
($create ? '' : wfMessage( 'discord-undeleterev' )->text() ),
DiscordUtils::createMarkdownLink( $title, $title->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
( $comment ? ('`' . DiscordUtils::truncateText( $comment ) . '`' ) : '' ))->plain();
DiscordUtils::handleDiscord(':wastebasket:', $msg);
( $comment ? ('`' . DiscordUtils::sanitiseText( DiscordUtils::truncateText( $comment ) ) . '`' ) : '' ))->plain();
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
@@ -108,10 +111,11 @@ class DiscordHooks {
*/
public static function onArticleRevisionVisibilitySet( &$title, $ids, $visibilityChangeMap ) {
global $wgDiscordNoBots;
$hookName = 'ArticleRevisionVisibilitySet';
$user = RequestContext::getMain()->getUser();
if ( DiscordUtils::isDisabled( 'ArticleRevisionVisibilitySet', $title->getNamespace(), $user ) ) {
if ( DiscordUtils::isDisabled( $hookName, $title->getNamespace(), $user ) ) {
return true;
}
@@ -123,7 +127,7 @@ class DiscordHooks {
$msg = wfMessage( 'discord-revvisibility', DiscordUtils::createUserLinks( $user ),
count($visibilityChangeMap),
DiscordUtils::createMarkdownLink( $title, $title->getFullUrl( '', '', $proto = PROTO_HTTP ) ) )->plain();
DiscordUtils::handleDiscord(':spy:', $msg);
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
@@ -133,8 +137,9 @@ class DiscordHooks {
*/
public static function onArticleProtectComplete( &$article, &$user, $protect, $reason ) {
global $wgDiscordNoBots;
$hookName = 'ArticleProtectComplete';
if ( DiscordUtils::isDisabled( 'ArticleProtectComplete', $article->getTitle()->getNamespace(), $user ) ) {
if ( DiscordUtils::isDisabled( $hookName, $article->getTitle()->getNamespace(), $user ) ) {
return true;
}
@@ -145,9 +150,9 @@ class DiscordHooks {
$msg = wfMessage( 'discord-articleprotect', DiscordUtils::createUserLinks( $user ),
DiscordUtils::createMarkdownLink( $article->getTitle(), $article->getTitle()->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
( $reason ? ('`' . DiscordUtils::truncateText( $reason ) . '`' ) : '' ),
( $reason ? ('`' . DiscordUtils::sanitiseText( DiscordUtils::truncateText( $reason ) ) . '`' ) : '' ),
implode(", ", $protect) )->plain();
DiscordUtils::handleDiscord(':lock:', $msg);
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
@@ -157,8 +162,9 @@ class DiscordHooks {
*/
public static function onTitleMoveComplete( Title &$title, Title &$newTitle, User $user, $oldid, $newid, $reason, Revision $revision ) {
global $wgDiscordNoBots;
$hookName = 'TitleMoveComplete';
if ( DiscordUtils::isDisabled( 'TitleMoveComplete', $title->getNamespace(), $user ) ) {
if ( DiscordUtils::isDisabled( $hookName, $title->getNamespace(), $user ) ) {
return true;
}
@@ -170,9 +176,9 @@ class DiscordHooks {
$msg = wfMessage( 'discord-titlemove', DiscordUtils::createUserLinks( $user ),
DiscordUtils::createMarkdownLink( $title, $title->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
DiscordUtils::createMarkdownLink( $newTitle, $newTitle->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
( $reason ? ('`' . DiscordUtils::truncateText( $reason ) . '`' ) : '' ),
( $reason ? ('`' . DiscordUtils::sanitiseText( DiscordUtils::truncateText( $reason ) ) . '`' ) : '' ),
DiscordUtils::createRevisionText( $revision ) )->plain();
DiscordUtils::handleDiscord(':truck:', $msg);
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
@@ -181,12 +187,14 @@ class DiscordHooks {
* @see https://www.mediawiki.org/wiki/Manual:Hooks/LocalUserCreated
*/
public static function onLocalUserCreated( $user, $autocreated ) {
if ( DiscordUtils::isDisabled( 'LocalUserCreated', NULL, $user ) ) {
$hookName = 'LocalUserCreated';
if ( DiscordUtils::isDisabled( $hookName, NULL, $user ) ) {
return true;
}
$msg = wfMessage( 'discord-localusercreated', DiscordUtils::createUserLinks( $user ) )->plain();
DiscordUtils::handleDiscord(':wave:', $msg);
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
@@ -195,7 +203,9 @@ class DiscordHooks {
* @see https://www.mediawiki.org/wiki/Manual:Hooks/BlockIpComplete
*/
public static function onBlockIpComplete( Block $block, User $user ) {
if ( DiscordUtils::isDisabled( 'BlockIpComplete', NULL, $user ) ) {
$hookName = 'BlockIpComplete';
if ( DiscordUtils::isDisabled( $hookName, NULL, $user ) ) {
return true;
}
@@ -207,9 +217,9 @@ class DiscordHooks {
}
$msg = wfMessage( 'discord-blockipcomplete', DiscordUtils::createUserLinks( $user ), DiscordUtils::createUserLinks( $block->getTarget() ),
( $block->mReason ? ('`' . DiscordUtils::truncateText( $block->mReason ) . '`' ) : '' ),
( $block->getReasonComment()->text ? ('`' . DiscordUtils::sanitiseText( DiscordUtils::truncateText( $block->getReasonComment()->text ) ) . '`' ) : '' ),
$expiryMsg )->plain();
DiscordUtils::handleDiscord(':no_entry_sign:', $msg);
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
@@ -218,12 +228,14 @@ class DiscordHooks {
* @see https://www.mediawiki.org/wiki/Manual:Hooks/UnblockUserComplete
*/
public static function onUnblockUserComplete( Block $block, User $user ) {
if ( DiscordUtils::isDisabled( 'UnblockUserComplete', NULL, $user ) ) {
$hookName = 'UnblockUserComplete';
if ( DiscordUtils::isDisabled( $hookName, NULL, $user ) ) {
return true;
}
$msg = wfMessage( 'discord-unblockusercomplete', DiscordUtils::createUserLinks( $user ), DiscordUtils::createUserLinks( $block->getTarget() ) )->text();
DiscordUtils::handleDiscord(':no_entry_sign:', $msg);
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
@@ -232,7 +244,9 @@ class DiscordHooks {
* @see https://www.mediawiki.org/wiki/Manual:Hooks/UserGroupsChanged
*/
public static function onUserGroupsChanged( User $user, array $added, array $removed, $performer, $reason ) {
if ( DiscordUtils::isDisabled( 'UserGroupsChanged', NULL, $performer ) ) {
$hookName = 'UserGroupsChanged';
if ( DiscordUtils::isDisabled( $hookName, NULL, $performer ) ) {
return true;
}
@@ -243,10 +257,10 @@ class DiscordHooks {
$msg = wfMessage( 'discord-usergroupschanged', DiscordUtils::createUserLinks( $performer ),
DiscordUtils::createUserLinks( $user ),
( $reason ? ('`' . DiscordUtils::truncateText( $reason ) . '`' ) : '' ),
( $reason ? ('`' . DiscordUtils::sanitiseText( DiscordUtils::truncateText( $reason ) ) . '`' ) : '' ),
( ( count($added) > 0 ) ? ( '+ ' . join(', ', $added) ) : ''),
( ( count($removed) > 0 ) ? ( '- ' . join(', ', $removed) ) : '' ) )->plain();
DiscordUtils::handleDiscord(':people_holding_hands:', $msg);
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
@@ -256,11 +270,12 @@ class DiscordHooks {
*/
public static function onUploadComplete( &$image ) {
global $wgDiscordNoBots;
$hookName = 'UploadComplete';
$lf = $image->getLocalFile();
$user = $lf->getUser( $type = 'object' ); // only supported in MW 1.31+
if ( DiscordUtils::isDisabled( 'UploadComplete', NS_FILE, $user ) ) {
if ( DiscordUtils::isDisabled( $hookName, NS_FILE, $user ) ) {
return true;
}
@@ -275,12 +290,12 @@ class DiscordHooks {
$msg = wfMessage( 'discord-uploadcomplete', DiscordUtils::createUserLinks( $user ),
( $isNewRevision ? wfMessage( 'discord-uploadnewver' )->text() : '' ),
DiscordUtils::createMarkdownLink( $lf->getName(), $lf->getTitle()->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
( $comment ? ('`' . DiscordUtils::truncateText( $comment ) . '`' ) : '' ),
( $comment ? ('`' . DiscordUtils::sanitiseText( DiscordUtils::truncateText( $comment ) ) . '`' ) : '' ),
DiscordUtils::formatBytes($lf->getSize()),
$lf->getWidth(),
$lf->getHeight(),
$lf->getMimeType() )->plain();
DiscordUtils::handleDiscord(':inbox_tray:', $msg);
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
@@ -290,8 +305,9 @@ class DiscordHooks {
*/
public static function onFileDeleteComplete( $file, $oldimage, $article, $user, $reason ) {
global $wgDiscordNoBots;
$hookName = 'FileDeleteComplete';
if ( DiscordUtils::isDisabled( 'FileDeleteComplete', NS_FILE, $user ) ) {
if ( DiscordUtils::isDisabled( $hookName, NS_FILE, $user ) ) {
return true;
}
@@ -307,8 +323,8 @@ class DiscordHooks {
$msg = wfMessage( 'discord-filedeletecomplete', DiscordUtils::createUserLinks( $user ),
DiscordUtils::createMarkdownLink( $file->getName(), $file->getTitle()->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
( $reason ? ('`' . DiscordUtils::truncateText( $reason ) . '`' ) : '' ) )->plain();
DiscordUtils::handleDiscord(':wastebasket:', $msg);
( $reason ? ('`' . DiscordUtils::sanitiseText( DiscordUtils::truncateText( $reason ) ) . '`' ) : '' ) )->plain();
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
@@ -318,8 +334,9 @@ class DiscordHooks {
*/
public static function onFileUndeleteComplete( $title, $fileVersions, $user, $reason ) {
global $wgDiscordNoBots;
$hookName = 'FileUndeleteComplete';
if ( DiscordUtils::isDisabled( 'FileUndeleteComplete', NS_FILE, $user ) ) {
if ( DiscordUtils::isDisabled( $hookName, NS_FILE, $user ) ) {
return true;
}
@@ -330,8 +347,8 @@ class DiscordHooks {
$msg = wfMessage( 'discord-fileundeletecomplete', DiscordUtils::createUserLinks( $user ),
DiscordUtils::createMarkdownLink( $title, $title->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
( $reason ? ('`' . DiscordUtils::truncateText( $reason ) . '`' ) : '' ) )->plain();
DiscordUtils::handleDiscord(':wastebasket:', $msg);
( $reason ? ('`' . DiscordUtils::sanitiseText( DiscordUtils::truncateText( $reason ) ) . '`' ) : '' ) )->plain();
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
@@ -341,10 +358,11 @@ class DiscordHooks {
*/
public static function onAfterImportPage( $title, $origTitle, $revCount, $sRevCount, $pageInfo ) {
global $wgDiscordNoBots;
$hookName = 'AfterImportPage';
$user = RequestContext::getMain()->getUser();
if ( DiscordUtils::isDisabled( 'AfterImportPage', $title->getNamespace(), $user ) ) {
if ( DiscordUtils::isDisabled( $hookName, $title->getNamespace(), $user ) ) {
return true;
}
@@ -356,16 +374,17 @@ class DiscordHooks {
$msg = wfMessage( 'discord-afterimportpage', DiscordUtils::createUserLinks( $user ),
DiscordUtils::createMarkdownLink( $title, $title->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
$revCount, $sRevCount)->plain();
DiscordUtils::handleDiscord(':books:', $msg);
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
public static function onArticleMergeComplete( $targetTitle, $destTitle ) {
global $wgDiscordNoBots;
$hookName = 'ArticleMergeComplete';
$user = RequestContext::getMain()->getUser();
if ( DiscordUtils::isDisabled( 'ArticleMergeComplete', $destTitle->getNamespace(), $user ) ) {
if ( DiscordUtils::isDisabled( $hookName, $destTitle->getNamespace(), $user ) ) {
return true;
}
@@ -377,7 +396,7 @@ class DiscordHooks {
$msg = wfMessage( 'discord-articlemergecomplete', DiscordUtils::createUserLinks( $user ),
DiscordUtils::createMarkdownLink( $targetTitle, $targetTitle->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
DiscordUtils::createMarkdownLink( $destTitle, $destTitle->getFullUrl( '', '', $proto = PROTO_HTTP ) ))->plain();
DiscordUtils::handleDiscord(':card_box:', $msg);
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
@@ -387,10 +406,11 @@ class DiscordHooks {
*/
public static function externalonApprovedRevsRevisionApproved ( $output, $title, $rev_id, $content ) {
global $wgDiscordNoBots;
$hookName = 'ApprovedRevsRevisionApproved';
$user = RequestContext::getMain()->getUser();
if ( DiscordUtils::isDisabled( 'ApprovedRevsRevisionApproved', $title->getNamespace(), $user ) ) {
if ( DiscordUtils::isDisabled( $hookName, $title->getNamespace(), $user ) ) {
return true;
}
@@ -414,7 +434,7 @@ class DiscordHooks {
DiscordUtils::createMarkdownLink( $title, $title->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
DiscordUtils::createMarkdownLink( $rev_id, $revLink ),
$revAuthor)->plain();
DiscordUtils::handleDiscord(':white_check_mark:', $msg);
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
@@ -424,10 +444,11 @@ class DiscordHooks {
*/
public static function externalonApprovedRevsRevisionUnapproved ( $output, $title, $content ) {
global $wgDiscordNoBots;
$hookName = 'ApprovedRevsRevisionUnapproved';
$user = RequestContext::getMain()->getUser();
if ( DiscordUtils::isDisabled( 'ApprovedRevsRevisionUnapproved', $title->getNamespace(), $user ) ) {
if ( DiscordUtils::isDisabled( $hookName, $title->getNamespace(), $user ) ) {
return true;
}
@@ -438,7 +459,7 @@ class DiscordHooks {
$msg = wfMessage( 'discord-approvedrevsrevisionunapproved', DiscordUtils::createUserLinks( $user ),
DiscordUtils::createMarkdownLink( $title, $title->getFullUrl( '', '', $proto = PROTO_HTTP ) ))->plain();
DiscordUtils::handleDiscord(':white_check_mark:', $msg);
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
@@ -448,10 +469,11 @@ class DiscordHooks {
*/
public static function externalonApprovedRevsFileRevisionApproved ( $parser, $title, $timestamp, $sha1 ) {
global $wgDiscordNoBots;
$hookName = 'ApprovedRevsFileRevisionApproved';
$user = RequestContext::getMain()->getUser();
if ( DiscordUtils::isDisabled( 'ApprovedRevsFileRevisionApproved', $title->getNamespace(), $user ) ) {
if ( DiscordUtils::isDisabled( $hookName, $title->getNamespace(), $user ) ) {
return true;
}
@@ -475,7 +497,7 @@ class DiscordHooks {
DiscordUtils::createMarkdownLink( $title, $title->getFullURL('', '', $proto = PROTO_HTTP) ),
DiscordUtils::createMarkdownLink( 'direct', $displayedFileUrl ),
DiscordUtils::createUserLinks( $uploader ) )->plain();
DiscordUtils::handleDiscord(':white_check_mark:', $msg);
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
@@ -485,10 +507,11 @@ class DiscordHooks {
*/
public static function externalonApprovedRevsFileRevisionUnapproved ( $parser, $title ) {
global $wgDiscordNoBots;
$hookName = 'ApprovedRevsFileRevisionUnapproved';
$user = RequestContext::getMain()->getUser();
if ( DiscordUtils::isDisabled( 'ApprovedRevsFileRevisionUnapproved', $title->getNamespace(), $user ) ) {
if ( DiscordUtils::isDisabled( $hookName, $title->getNamespace(), $user ) ) {
return true;
}
@@ -499,7 +522,7 @@ class DiscordHooks {
$msg = wfMessage( 'discord-approvedrevsfilerevisionunapproved', DiscordUtils::createUserLinks( $user ),
DiscordUtils::createMarkdownLink( $title, $title->getFullUrl( '', '', $proto = PROTO_HTTP ) ))->plain();
DiscordUtils::handleDiscord(':white_check_mark:', $msg);
DiscordUtils::handleDiscord($hookName, $msg);
return true;
}
}
+45 -20
View File
@@ -45,8 +45,8 @@ class DiscordUtils {
/**
* Handles sending a webhook to Discord using cURL
*/
public static function handleDiscord ($emoji, $msg) {
global $wgDiscordWebhookURL, $wgDiscordUseEmojis, $wgDiscordPrependTimestamp, $wgDiscordUseFileGetContents;
public static function handleDiscord ($hookName, $msg) {
global $wgDiscordWebhookURL, $wgDiscordEmojis, $wgDiscordUseEmojis, $wgDiscordPrependTimestamp, $wgDiscordUseFileGetContents;
if ( !$wgDiscordWebhookURL ) {
// There's nothing in here, so we won't do anything
@@ -75,13 +75,19 @@ class DiscordUtils {
if ( $wgDiscordUseEmojis ) {
// Add emoji
$emoji = $wgDiscordEmojis[$hookName];
$stripped = $emoji . ' ' . $stripped;
}
DeferredUpdates::addCallableUpdate( function() use ( $stripped, $urls, $wgDiscordUseFileGetContents ) {
$user_agent = 'mw-discord/1.0 (github.com/jaydenkieran)';
$json_data = [ 'content' => "$stripped" ];
$json = json_encode($json_data);
$json_data = [
'content' => "$stripped",
'allowed_mentions' => [
'parse' => []
]
];
$json = json_encode($json_data);
if ( $wgDiscordUseFileGetContents ) {
// They want to use file_get_contents
@@ -100,12 +106,12 @@ class DiscordUtils {
$result = file_get_contents( $value, false, $context );
}
} else {
// By default, we use cURL
// By default, we use cURL
// Set up cURL multi handlers
$c_handlers = [];
$result = [];
$mh = curl_multi_init();
foreach ($urls as &$value) {
$c_handlers[$value] = curl_init( $value );
curl_setopt( $c_handlers[$value], CURLOPT_POST, 1 ); // Send as a POST request
@@ -121,19 +127,19 @@ class DiscordUtils {
));
curl_multi_add_handle( $mh, $c_handlers[$value] );
}
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running);
// Remove all handlers and then close the multi handler
foreach($c_handlers as $k => $ch) {
$result[$k] = curl_multi_getcontent($ch);
wfDebugLog( 'discord', 'Result of cURL was: ' . $result[$k] );
curl_multi_remove_handle($mh, $ch);
}
curl_multi_close($mh);
}
} );
@@ -154,14 +160,24 @@ class DiscordUtils {
* Creates links for a specific MediaWiki User object
*/
public static function createUserLinks ($user) {
global $wgDiscordMaxCharsUsernames;
if ( $user instanceof User ) {
$isAnon = $user->isAnon();
$contribs = Title::newFromText("Special:Contributions/" . $user);
$userPage = DiscordUtils::createMarkdownLink( $user, ( $isAnon ? $contribs : $user->getUserPage() )->getFullUrl( '', '', $proto = PROTO_HTTP ) );
if ($wgDiscordMaxCharsUsernames) {
$user_abbr = strval($user);
if (strlen($user_abbr) > $wgDiscordMaxCharsUsernames) {
$user_abbr = substr($user_abbr, 0, $wgDiscordMaxCharsUsernames);
$user_abbr = $user_abbr.'...';
}
}
$userPage = DiscordUtils::createMarkdownLink( $user_abbr, ( $isAnon ? $contribs : $user->getUserPage() )->getFullUrl( '', '', $proto = PROTO_HTTP ) );
$userTalk = DiscordUtils::createMarkdownLink( wfMessage( 'discord-talk' )->text(), $user->getTalkPage()->getFullUrl( '', '', $proto = PROTO_HTTP ) );
$userContribs = DiscordUtils::createMarkdownLink( wfMessage( 'discord-contribs' )->text(), $contribs->getFullURL( '', '', $proto = PROTO_HTTP ) );
$text = wfMessage( 'discord-userlinks', $userPage, $userTalk, $userContribs )->text();
$text = wfMessage( 'discord-userlinks', $userPage, $userTalk, $userContribs )->text();
} else {
// If it's a string, which can be likely (for example when range blocking a user)
// We need to handle this differently.
@@ -196,7 +212,7 @@ class DiscordUtils {
$text = wfMessage( 'discord-revisionlinks', $diff, $minor, $size )->text();
return $text;
}
/**
* Strip bad characters from a URL
*/
@@ -206,20 +222,20 @@ class DiscordUtils {
$url = str_replace(")", "%29", $url);
return $url;
}
/**
* Formats bytes to a string representing B, KB, MB, GB, TB
*/
public static function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
public static function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= (1 << (10 * $pow));
$bytes /= (1 << (10 * $pow));
return round($bytes, $precision) . ' ' . $units[$pow];
return round($bytes, $precision) . ' ' . $units[$pow];
}
/**
@@ -235,6 +251,15 @@ class DiscordUtils {
}
return $text;
}
/**
* Sanitise text input, including removing the potential for abuse
* of Discord's @everyone and @here pings
*/
public static function sanitiseText($text) {
$text = preg_replace('/(`|@)/', '', $text);
return $text;
}
}
?>