Compare commits

...

3 Commits

Author SHA1 Message Date
Jayden Bailey d6d0f22c01 Add wgDiscordDisabledUsers, closes #1 2018-12-30 21:14:07 +00:00
Jayden Bailey 0cf579aa6a Add DiscordMaxChars 2018-12-16 21:12:13 +00:00
Jayden Bailey ac38da8529 remove defunct tests 2018-12-16 20:49:24 +00:00
7 changed files with 64 additions and 59 deletions
+2 -1
View File
@@ -1,2 +1,3 @@
/vendor/
debug.log
debug.log
.DS_Store
+3 -1
View File
@@ -45,8 +45,10 @@ This extension can be configured using the `LocalSettings.php` file in your Medi
| `$wgDiscordNoMinor` | bool | Do not send notifications that are for [minor edits](https://meta.wikimedia.org/wiki/Help:Minor_edit) | `false`
| `$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`
| `$wgDiscordDisabledHooks` | array | List of hooks to disable sending webhooks for (see [below](#hooks-used)) | `[]`
| `$wgDiscordDisabledNS` | array | List of namespaces 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 | `[]`
## Hooks used
- `PageContentSaveComplete` - New edits to pages and page creations
+4 -2
View File
@@ -4,7 +4,7 @@
"[https://github.com/jaydenkieran Jayden Bailey]"
],
"url": "https://github.com/jaydenkieran/mw-discord",
"version": "1.0",
"version": "1.0.2",
"descriptionmsg": "discord-desc",
"license-name": "MIT",
"manifest_version": 1,
@@ -14,8 +14,10 @@
"DiscordNoMinor": false,
"DiscordNoNull": true,
"DiscordSuppressPreviews": true,
"DiscordMaxChars": null,
"DiscordDisabledHooks": [],
"DiscordDisabledNS": []
"DiscordDisabledNS": [],
"DiscordDisabledUsers": []
},
"AutoloadClasses": {
"DiscordHooks": "src/DiscordHooks.php",
-7
View File
@@ -1,7 +0,0 @@
<phpunit colors="true">
<testsuites>
<testsuite name="Utils tests">
<file>tests/UtilsTests.php</file>
</testsuite>
</testsuites>
</phpunit>
+26 -25
View File
@@ -13,7 +13,7 @@ class DiscordHooks {
public static function onPageContentSaveComplete( &$wikiPage, &$user, $content, $summary, $isMinor, $isWatch, $section, &$flags, $revision, &$status, $baseRevId, $undidRevId ) {
global $wgDiscordNoBots, $wgDiscordNoMinor, $wgDiscordNoNull;
if ( DiscordUtils::isDisabled( 'PageContentSaveComplete', $wikiPage->getTitle()->getNamespace() ) ) {
if ( DiscordUtils::isDisabled( 'PageContentSaveComplete', $wikiPage->getTitle()->getNamespace(), $user ) ) {
return true;
}
@@ -40,7 +40,7 @@ class DiscordHooks {
$msg = wfMessage( 'discord-edit', DiscordUtils::createUserLinks( $user ),
DiscordUtils::createMarkdownLink( $wikiPage->getTitle(), $wikiPage->getTitle()->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
DiscordUtils::createRevisionText( $revision ),
( $summary ? ('`' . $summary . '`' ) : '' ) )->text();
( $summary ? ('`' . DiscordUtils::truncateText( $summary ) . '`' ) : '' ) )->text();
DiscordUtils::handleDiscord($msg);
return true;
}
@@ -52,7 +52,7 @@ class DiscordHooks {
public static function onArticleDeleteComplete( &$article, User &$user, $reason, $id, $content, LogEntry $logEntry, $archivedRevisionCount ) {
global $wgDiscordNoBots, $wgDiscordNoMinor, $wgDiscordNoNull;
if ( DiscordUtils::isDisabled( 'ArticleDeleteComplete', $article->getTitle()->getNamespace() ) ) {
if ( DiscordUtils::isDisabled( 'ArticleDeleteComplete', $article->getTitle()->getNamespace(), $user ) ) {
return true;
}
@@ -63,7 +63,7 @@ class DiscordHooks {
$msg = wfMessage( 'discord-articledelete', DiscordUtils::createUserLinks( $user ),
DiscordUtils::createMarkdownLink( $article->getTitle(), $article->getTitle()->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
( $reason ? ('`' . $reason . '`' ) : '' ),
( $reason ? ('`' . DiscordUtils::truncateText( $reason ) . '`' ) : '' ),
$archivedRevisionCount)->text();
DiscordUtils::handleDiscord($msg);
return true;
@@ -76,14 +76,14 @@ class DiscordHooks {
public static function onArticleUndelete( Title $title, $create, $comment, $oldPageId, $restoredPages ) {
global $wgUser;
if ( DiscordUtils::isDisabled( 'ArticleUndelete', $title->getNamespace() ) ) {
if ( DiscordUtils::isDisabled( 'ArticleUndelete', $title->getNamespace(), $wgUser ) ) {
return true;
}
$msg = wfMessage( 'discord-articleundelete', DiscordUtils::createUserLinks( $wgUser ),
($create ? '' : wfMessage( 'discord-undeleterev' )->text() ),
DiscordUtils::createMarkdownLink( $title, $title->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
( $comment ? ('`' . $comment . '`' ) : '' ))->text();
( $comment ? ('`' . DiscordUtils::truncateText( $comment ) . '`' ) : '' ))->text();
DiscordUtils::handleDiscord($msg);
return true;
}
@@ -95,7 +95,7 @@ class DiscordHooks {
public static function onArticleRevisionVisibilitySet( &$title, $ids, $visibilityChangeMap ) {
global $wgUser;
if ( DiscordUtils::isDisabled( 'ArticleRevisionVisibilitySet', $title->getNamespace() ) ) {
if ( DiscordUtils::isDisabled( 'ArticleRevisionVisibilitySet', $title->getNamespace(), $wgUser ) ) {
return true;
}
@@ -113,7 +113,7 @@ class DiscordHooks {
public static function onArticleProtectComplete( &$article, &$user, $protect, $reason ) {
global $wgDiscordNoBots;
if ( DiscordUtils::isDisabled( 'ArticleProtectComplete', $article->getTitle()->getNamespace() ) ) {
if ( DiscordUtils::isDisabled( 'ArticleProtectComplete', $article->getTitle()->getNamespace(), $user ) ) {
return true;
}
@@ -124,7 +124,7 @@ class DiscordHooks {
$msg = wfMessage( 'discord-articleprotect', DiscordUtils::createUserLinks( $user ),
DiscordUtils::createMarkdownLink( $article->getTitle(), $article->getTitle()->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
( $reason ? ('`' . $reason . '`' ) : '' ),
( $reason ? ('`' . DiscordUtils::truncateText( $reason ) . '`' ) : '' ),
implode(", ", $protect) )->text();
DiscordUtils::handleDiscord($msg);
return true;
@@ -137,7 +137,7 @@ class DiscordHooks {
public static function onTitleMoveComplete( Title &$title, Title &$newTitle, User $user, $oldid, $newid, $reason, Revision $revision ) {
global $wgDiscordNoBots;
if ( DiscordUtils::isDisabled( 'TitleMoveComplete', $title->getNamespace() ) ) {
if ( DiscordUtils::isDisabled( 'TitleMoveComplete', $title->getNamespace(), $user ) ) {
return true;
}
@@ -149,7 +149,7 @@ 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 ? ('`' . $reason . '`' ) : '' ),
( $reason ? ('`' . DiscordUtils::truncateText( $reason ) . '`' ) : '' ),
DiscordUtils::createRevisionText( $revision ) )->text();
DiscordUtils::handleDiscord($msg);
return true;
@@ -160,7 +160,7 @@ class DiscordHooks {
* @see https://www.mediawiki.org/wiki/Manual:Hooks/LocalUserCreated
*/
public static function onLocalUserCreated( $user, $autocreated ) {
if ( DiscordUtils::isDisabled( 'LocalUserCreated', NULL ) ) {
if ( DiscordUtils::isDisabled( 'LocalUserCreated', NULL, $user ) ) {
return true;
}
@@ -174,7 +174,7 @@ class DiscordHooks {
* @see https://www.mediawiki.org/wiki/Manual:Hooks/BlockIpComplete
*/
public static function onBlockIpComplete( Block $block, User $user ) {
if ( DiscordUtils::isDisabled( 'BlockIpComplete', NULL ) ) {
if ( DiscordUtils::isDisabled( 'BlockIpComplete', NULL, $user ) ) {
return true;
}
@@ -186,7 +186,7 @@ class DiscordHooks {
}
$msg = wfMessage( 'discord-blockipcomplete', DiscordUtils::createUserLinks( $user ), DiscordUtils::createUserLinks( $block->getTarget() ),
( $block->mReason ? ('`' . $block->mReason . '`' ) : '' ),
( $block->mReason ? ('`' . DiscordUtils::truncateText( $block->mReason ) . '`' ) : '' ),
$expiryMsg )->text();
DiscordUtils::handleDiscord($msg);
return true;
@@ -197,7 +197,7 @@ class DiscordHooks {
* @see https://www.mediawiki.org/wiki/Manual:Hooks/UnblockUserComplete
*/
public static function onUnblockUserComplete( Block $block, User $user ) {
if ( DiscordUtils::isDisabled( 'UnblockUserComplete', NULL ) ) {
if ( DiscordUtils::isDisabled( 'UnblockUserComplete', NULL, $user ) ) {
return true;
}
@@ -211,7 +211,7 @@ 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 ) ) {
if ( DiscordUtils::isDisabled( 'UserGroupsChanged', NULL, $performer ) ) {
return true;
}
@@ -222,7 +222,7 @@ class DiscordHooks {
$msg = wfMessage( 'discord-usergroupschanged', DiscordUtils::createUserLinks( $performer ),
DiscordUtils::createUserLinks( $user ),
( $reason ? ('`' . $reason . '`' ) : '' ),
( $reason ? ('`' . DiscordUtils::truncateText( $reason ) . '`' ) : '' ),
( ( count($added) > 0 ) ? ( '+ ' . join(', ', $added) ) : ''),
( ( count($removed) > 0 ) ? ( '- ' . join(', ', $removed) ) : '' ) )->text();
DiscordUtils::handleDiscord($msg);
@@ -234,19 +234,20 @@ class DiscordHooks {
* @see https://www.mediawiki.org/wiki/Manual:Hooks/UploadComplete
*/
public static function onUploadComplete( &$image ) {
if ( DiscordUtils::isDisabled( 'UploadComplete', NS_FILE ) ) {
$lf = $image->getLocalFile();
$user = $lf->getUser( $type = 'object' ); // only supported in MW 1.31+
if ( DiscordUtils::isDisabled( 'UploadComplete', NS_FILE, $user ) ) {
return true;
}
$lf = $image->getLocalFile();
$user = $lf->getUser( $type = 'object' ); // only supported in MW 1.31+
$comment = $lf->getDescription();
$isNewRevision = count($lf->getHistory()) > 0;
$msg = wfMessage( 'discord-uploadcomplete', DiscordUtils::createUserLinks( $user ),
( $isNewRevision ? wfMessage( 'discord-uploadnewver' )->text() : '' ),
DiscordUtils::createMarkdownLink( $lf->getName(), $lf->getTitle()->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
( $comment ? ('`' . $comment . '`' ) : '' ),
( $comment ? ('`' . DiscordUtils::truncateText( $comment ) . '`' ) : '' ),
DiscordUtils::formatBytes($lf->getSize()),
$lf->getWidth(),
$lf->getHeight(),
@@ -260,7 +261,7 @@ class DiscordHooks {
* @see https://www.mediawiki.org/wiki/Manual:Hooks/FileDeleteComplete
*/
public static function onFileDeleteComplete( $file, $oldimage, $article, $user, $reason ) {
if ( DiscordUtils::isDisabled( 'FileDeleteComplete', NS_FILE ) ) {
if ( DiscordUtils::isDisabled( 'FileDeleteComplete', NS_FILE, $user ) ) {
return true;
}
@@ -271,7 +272,7 @@ class DiscordHooks {
$msg = wfMessage( 'discord-filedeletecomplete', DiscordUtils::createUserLinks( $user ),
DiscordUtils::createMarkdownLink( $file->getName(), $file->getTitle()->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
( $reason ? ('`' . $reason . '`' ) : '' ) )->text();
( $reason ? ('`' . DiscordUtils::truncateText( $reason ) . '`' ) : '' ) )->text();
DiscordUtils::handleDiscord($msg);
return true;
}
@@ -281,13 +282,13 @@ class DiscordHooks {
* @see https://www.mediawiki.org/wiki/Manual:Hooks/FileUndeleteComplete
*/
public static function onFileUndeleteComplete( $title, $fileVersions, $user, $reason ) {
if ( DiscordUtils::isDisabled( 'FileUndeleteComplete', NS_FILE ) ) {
if ( DiscordUtils::isDisabled( 'FileUndeleteComplete', NS_FILE, $user ) ) {
return true;
}
$msg = wfMessage( 'discord-fileundeletecomplete', DiscordUtils::createUserLinks( $user ),
DiscordUtils::createMarkdownLink( $title, $title->getFullUrl( '', '', $proto = PROTO_HTTP ) ),
( $reason ? ('`' . $reason . '`' ) : '' ) )->text();
( $reason ? ('`' . DiscordUtils::truncateText( $reason ) . '`' ) : '' ) )->text();
DiscordUtils::handleDiscord($msg);
return true;
}
+29 -3
View File
@@ -4,8 +4,8 @@ class DiscordUtils {
/**
* Checks if criteria is met for this action to be cancelled
*/
public static function isDisabled ( $hook, $ns ) {
global $wgDiscordDisabledHooks, $wgDiscordDisabledNS;
public static function isDisabled ( $hook, $ns, $user ) {
global $wgDiscordDisabledHooks, $wgDiscordDisabledNS, $wgDiscordDisabledUsers;
if ( is_array( $wgDiscordDisabledHooks ) ) {
if ( in_array( strtolower( $hook ), array_map( 'strtolower', $wgDiscordDisabledHooks ) ) ) {
@@ -26,6 +26,18 @@ class DiscordUtils {
} else {
wfDebugLog( 'discord', 'The value of $wgDiscordDisabledNS is not valid and therefore all namespaces are enabled.' );
}
if ( is_array( $wgDiscordDisabledUsers ) ) {
if ( !is_null( $user ) ) {
if ( $user instanceof User ) {
if ( in_array( $user->getName(), $wgDiscordDisabledUsers ) ) {
// User shouldn't trigger a message, return true
return true;
}
}
}
} else {
wfDebugLog( 'discord', 'The value of $wgDiscordDisabledUsers is not valid and therefore all users can trigger messages.' );
}
return false;
}
@@ -162,7 +174,21 @@ class DiscordUtils {
$bytes /= (1 << (10 * $pow));
return round($bytes, $precision) . ' ' . $units[$pow];
}
}
/**
* Truncate text to maximum allowed characters
*/
public static function truncateText($text) {
global $wgDiscordMaxChars;
if ($wgDiscordMaxChars) {
if (strlen($text) > $wgDiscordMaxChars) {
$text = substr($text, 0, $wgDiscordMaxChars);
$text = $text.'...';
}
}
return $text;
}
}
?>
-20
View File
@@ -1,20 +0,0 @@
<?php
require "src/Utils.php";
class UtilsTests extends PHPUnit_Framework_TestCase {
public function testCreateMarkdownLink()
{
$link = DiscordUtils::CreateMarkdownLink("Link", "https://example.com");
$this->assertEquals("[Link](https://example.com)", $link);
}
public function testRemoveMultipleSlashes()
{
$url = DiscordUtils::RemoveMultipleSlashes("https://example.com/page/page2//page3/page4//page5//");
$this->assertEquals("https://example.com/page/page2/page3/page4/page5/", $url);
}
}
?>