Change project structure
This commit is contained in:
@@ -0,0 +1,302 @@
|
||||
<?php
|
||||
|
||||
final class MediawikiDiscord
|
||||
{
|
||||
static function getUserText ($user)
|
||||
{
|
||||
return MediawikiDiscordUtils::CreateMarkdownLink ($user, $user->getUserPage()->getFullUrl());
|
||||
}
|
||||
|
||||
static function getPageText ($wikiPage)
|
||||
{
|
||||
return MediawikiDiscordUtils::CreateMarkdownLink ($wikiPage->getTitle()->getFullText(), $wikiPage->getTitle()->getFullURL());
|
||||
}
|
||||
|
||||
static function getTitleText ($title)
|
||||
{
|
||||
return MediawikiDiscordUtils::CreateMarkdownLink ($title, $title->getFullURL());
|
||||
}
|
||||
|
||||
static function getFileText ($file)
|
||||
{
|
||||
return MediawikiDiscordUtils::CreateMarkdownLink ($file->getName(), $file->getTitle()->getFullUrl());
|
||||
}
|
||||
}
|
||||
|
||||
final class MediawikiDiscordHooks
|
||||
{
|
||||
static function onPageContentSaveComplete ($wikiPage, $user, $content, $summary, $isMinor, $isWatch, $section, $flags, $revision, $status, $baseRevId)
|
||||
{
|
||||
if ($status->value['new'] == true) //page is just created, there is no need to trigger second notification
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$message = wfMessage('onPageContentSaveComplete', MediawikiDiscord::getUserText($user),
|
||||
MediawikiDiscord::getPageText($wikiPage))->inContentLanguage()->plain();
|
||||
|
||||
if (empty($summary) == false)
|
||||
{
|
||||
$message .= sprintf(" (%s `%s`)",
|
||||
wfMessage('summary')->inContentLanguage()->plain(),
|
||||
$summary);
|
||||
}
|
||||
|
||||
(new DiscordNotification($message))->Send();
|
||||
}
|
||||
|
||||
static function onPageContentInsertComplete ($wikiPage, $user)
|
||||
{
|
||||
if ($wikiPage->getTitle()->getNamespace() == NS_FILE) //the page is file, there is no need to trigger second notification of file's page creation
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$message = wfMessage('onPageContentInsertComplete', MediawikiDiscord::getUserText($user),
|
||||
MediawikiDiscord::getPageText($wikiPage))->inContentLanguage()->plain();
|
||||
|
||||
(new DiscordNotification($message))->Send();
|
||||
}
|
||||
|
||||
static function onTitleMoveComplete ($title, $newTitle, $user, $oldid, $newid, $reason, $revision)
|
||||
{
|
||||
$message = wfMessage('onTitleMoveComplete', MediawikiDiscord::getUserText($user),
|
||||
MediawikiDiscord::getTitleText($title),
|
||||
MediawikiDiscord::getTitleText($newTitle))->inContentLanguage()->plain();
|
||||
|
||||
if (empty($reason) == false)
|
||||
{
|
||||
$message .= sprintf(" (%s `%s`)",
|
||||
wfMessage('mergehistory-reason')->inContentLanguage()->plain(),
|
||||
$reason);
|
||||
}
|
||||
|
||||
(new DiscordNotification($message))->Send();
|
||||
}
|
||||
|
||||
static function onArticleDeleteComplete($wikiPage, $user, $reason)
|
||||
{
|
||||
if ($wikiPage->getTitle()->getNamespace() == NS_FILE) //the page is file, there is no need to trigger second notification of file's page deletion
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$message = wfMessage('onArticleDeleteComplete', MediawikiDiscord::getUserText($user),
|
||||
MediawikiDiscord::getPageText($wikiPage))->inContentLanguage()->plain();
|
||||
|
||||
if (empty($reason) == false)
|
||||
{
|
||||
$message .= sprintf(" (%s `%s`)",
|
||||
wfMessage('mergehistory-reason')->inContentLanguage()->plain(),
|
||||
$reason);
|
||||
}
|
||||
|
||||
(new DiscordNotification($message))->Send();
|
||||
}
|
||||
|
||||
static function onArticleUndelete($title, $create, $comment)
|
||||
{
|
||||
$message = wfMessage('onArticleUndelete', MediawikiDiscord::getTitleText($title))->inContentLanguage()->plain();
|
||||
|
||||
if (empty($comment) == false)
|
||||
{
|
||||
$message .= sprintf(" (%s `%s`)",
|
||||
wfMessage('import-comment')->inContentLanguage()->plain(),
|
||||
$comment);
|
||||
}
|
||||
|
||||
(new DiscordNotification($message))->Send();
|
||||
}
|
||||
|
||||
static function onArticleProtectComplete ($wikiPage, $user, $protect, $reason, $moveonly)
|
||||
{
|
||||
$message = wfMessage('onArticleProtectComplete', MediawikiDiscord::getUserText($user),
|
||||
MediawikiDiscord::getPageText($wikiPage))->inContentLanguage()->plain();
|
||||
|
||||
if (empty($reason) == false)
|
||||
{
|
||||
$message .= sprintf(" (%s `%s`)",
|
||||
wfMessage('mergehistory-reason')->inContentLanguage()->plain(),
|
||||
$reason);
|
||||
}
|
||||
|
||||
(new DiscordNotification($message))->Send();
|
||||
}
|
||||
|
||||
static function onUploadComplete($image)
|
||||
{
|
||||
global $wgUser;
|
||||
|
||||
$isNewRevision = count($image->getLocalFile()->getHistory()) > 0;
|
||||
|
||||
if ($isNewRevision == true)
|
||||
{
|
||||
$message = wfMessage('onUploadComplete_NewVersion', MediawikiDiscord::getUserText($wgUser),
|
||||
MediawikiDiscord::getFileText($image->getLocalFile()))->inContentLanguage()->plain();
|
||||
}
|
||||
else
|
||||
{
|
||||
$message = wfMessage('onUploadComplete', MediawikiDiscord::getUserText($wgUser),
|
||||
MediawikiDiscord::getFileText($image->getLocalFile()))->inContentLanguage()->plain();
|
||||
}
|
||||
|
||||
$discordNotification = new DiscordNotification($message);
|
||||
|
||||
$mimeType = $image->getLocalFile()->getMimeType();
|
||||
|
||||
if (($mimeType == "image/jpeg")
|
||||
|| ($mimeType == "image/png")
|
||||
|| ($mimeType == "image/gif")
|
||||
|| ($mimeType == "image/webp"))
|
||||
{
|
||||
$imageUrl = MediawikiDiscordUtils::RemoveMultipleSlashes($image->getLocalFile()->getFullUrl());
|
||||
|
||||
$discordNotification->SetEmbedImage($imageUrl);
|
||||
}
|
||||
|
||||
$discordNotification->Send();
|
||||
}
|
||||
|
||||
static function onFileDeleteComplete($file, $oldimage, $article, $user, $reason)
|
||||
{
|
||||
$message = wfMessage('onFileDeleteComplete', MediawikiDiscord::getUserText($user),
|
||||
MediawikiDiscord::getFileText($file))->inContentLanguage()->plain();
|
||||
|
||||
if (empty($reason) == false)
|
||||
{
|
||||
$message .= sprintf(" (%s `%s`)",
|
||||
wfMessage('mergehistory-reason')->inContentLanguage()->plain(),
|
||||
$reason);
|
||||
}
|
||||
|
||||
(new DiscordNotification($message))->Send();
|
||||
}
|
||||
|
||||
static function onLocalUserCreated($user, $autocreated)
|
||||
{
|
||||
$message = wfMessage('onLocalUserCreated', MediawikiDiscord::getUserText($user))->inContentLanguage()->plain();
|
||||
|
||||
(new DiscordNotification($message))->Send();
|
||||
}
|
||||
|
||||
static function onBlockIpComplete($block, $user)
|
||||
{
|
||||
$message = wfMessage('onBlockIpComplete', MediawikiDiscord::getUserText($user),
|
||||
MediawikiDiscord::getUserText($block->getTarget()))->inContentLanguage()->plain();
|
||||
|
||||
if (empty($block->mReason) == false)
|
||||
{
|
||||
$message .= sprintf(" (%s `%s`)",
|
||||
wfMessage('mergehistory-reason')->inContentLanguage()->plain(),
|
||||
$block->mReason);
|
||||
}
|
||||
|
||||
if (($expires = strtotime($block->mExpiry)))
|
||||
{
|
||||
$message .= sprintf(" (%s `%s`)",
|
||||
wfMessage('blocklist-expiry')->inContentLanguage()->plain(),
|
||||
date('Y-m-d H:i:s', $expires));
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($block->mExpiry == "infinity")
|
||||
{
|
||||
$message .= sprintf(" (`%s`)",
|
||||
wfMessage('infiniteblock')->inContentLanguage()->plain());
|
||||
}
|
||||
else
|
||||
{
|
||||
$message .= sprintf(" (%s `%s`)",
|
||||
wfMessage('blocklist-expiry')->inContentLanguage()->plain(),
|
||||
$block->mExpiry );
|
||||
}
|
||||
}
|
||||
|
||||
(new DiscordNotification($message))->Send();
|
||||
}
|
||||
|
||||
static function onUnblockUserComplete($block, $user)
|
||||
{
|
||||
$message = wfMessage('onUnblockUserComplete', MediawikiDiscord::getUserText($user),
|
||||
MediawikiDiscord::getUserText($block->getTarget()))->inContentLanguage()->plain();
|
||||
|
||||
(new DiscordNotification($message))->Send();
|
||||
}
|
||||
|
||||
static function onUserRights($user, array $addedGroups, array $removedGroups)
|
||||
{
|
||||
$message = wfMessage('onUserRights', MediawikiDiscord::getUserText($user))->inContentLanguage()->plain();
|
||||
|
||||
if (count($addedGroups) > 0)
|
||||
{
|
||||
$message .= sprintf(" %s: `%s`",
|
||||
wfMessage('added')->inContentLanguage()->plain(),
|
||||
join(', ', $addedGroups));
|
||||
}
|
||||
|
||||
if (count($removedGroups) > 0)
|
||||
{
|
||||
$message .= sprintf(" %s: `%s`",
|
||||
wfMessage('removed')->inContentLanguage()->plain(),
|
||||
join(', ', $removedGroups));
|
||||
}
|
||||
|
||||
(new DiscordNotification($message))->Send();
|
||||
}
|
||||
}
|
||||
|
||||
final class DiscordNotification
|
||||
{
|
||||
private $message;
|
||||
private $embedImageUrl;
|
||||
|
||||
public function __construct($message)
|
||||
{
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
public function SetMessage ($message)
|
||||
{
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
public function SetEmbedImage ($embedImageUrl)
|
||||
{
|
||||
$this->embedImageUrl = $embedImageUrl;
|
||||
}
|
||||
|
||||
public function Send ()
|
||||
{
|
||||
global $wgDiscordWebhookUrl;
|
||||
global $wgSitename;
|
||||
|
||||
$userName = $wgSitename;
|
||||
|
||||
if (strlen($userName) >= 32) //32 characters is a limit of Discord usernames
|
||||
{
|
||||
$userName = substr($userName, 0, -(strlen($userName) - 32)); //if the wiki's name is too long, just remove last characters
|
||||
}
|
||||
|
||||
$json = new stdClass();
|
||||
$json->content = $this->message;
|
||||
$json->username = $userName;
|
||||
|
||||
if ($this->embedImageUrl != null)
|
||||
{
|
||||
$json->embeds[0]->image->url = $this->embedImageUrl;
|
||||
}
|
||||
|
||||
$data = array
|
||||
(
|
||||
'http' => array
|
||||
(
|
||||
'method' => 'POST',
|
||||
'content' => json_encode($json)
|
||||
)
|
||||
);
|
||||
|
||||
file_get_contents($wgDiscordWebhookUrl, false, stream_context_create($data));
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user