Skip to content

Commit

Permalink
Avoid deprecated hooks and functions on newer versions (#77)
Browse files Browse the repository at this point in the history
* cleanup

`composer run fix`

* Avoid deprecated hooks and functions on newer versions
  • Loading branch information
lens0021 authored Aug 25, 2021
1 parent 98b689a commit e87223e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 50 deletions.
135 changes: 95 additions & 40 deletions DiscordNotificationsCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ private static function getDiscordArticleText( WikiPage $article, $diff = false
"protect",
"watch"*/ );
if ( $diff ) {
$out .= " | " . self::parseurl( $prefix . "&" . $wgDiscordNotificationWikiUrlEndingDiff . $article->getRevision()->getID() ) . "|" . self::msg( 'discordnotifications-diff' ) . ">)";
if ( defined( 'MW_VERSION' ) && version_compare( MW_VERSION, '1.31', '>=' ) ) { // Revision::getId was deprecated in MediaWiki 1.31
$revisionId = $article->getRevisionRecord()->getId();
} else {
$revisionId = $article->getRevision()->getID();
}
$out .= " | " . self::parseurl( $prefix . "&" . $wgDiscordNotificationWikiUrlEndingDiff . $revisionId ) . "|" . self::msg( 'discordnotifications-diff' ) . ">)";
} else {
$out .= ")";
}
Expand Down Expand Up @@ -93,6 +98,74 @@ private static function getDiscordTitleText( Title $title ) {
}
}

/**
* Returns whether the given title should be excluded
*/
private static function titleIsExcluded( $title ) {
global $wgDiscordExcludeNotificationsFrom;
if ( is_array( $wgDiscordExcludeNotificationsFrom ) && count( $wgDiscordExcludeNotificationsFrom ) > 0 ) {
foreach ( $wgDiscordExcludeNotificationsFrom as &$currentExclude ) {
if ( 0 === strpos( $title, $currentExclude ) ) return true;
}
}
return false;
}

/**
* Register different hooks depending on MediaWiki version
*/
public static function registerExtraHooks() {
global $wgHooks;
if ( defined( 'MW_VERSION' ) && version_compare( MW_VERSION, '1.35', '>=' ) ) {
$wgHooks['PageSaveComplete'][] = 'DiscordNotificationsCore::onDiscordPageSaveComplete';
} else {
$wgHooks['PageContentSaveComplete'][] = 'DiscordNotificationsCore::onDiscordArticleSaved';
$wgHooks['PageContentInsertComplete'][] = 'DiscordNotificationsCore::onDiscordArticleInserted';
}
}

/**
* Occurs after an article has been updated.
* @see https://www.mediawiki.org/wiki/Manual:Hooks/PageSaveComplete
*/
public static function onDiscordPageSaveComplete( $wikiPage, $user, $summary, $flags, $revisionRecord, $editResult ) {
global $wgDiscordNotificationEditedArticle, $wgDiscordIgnoreMinorEdits,
$wgDiscordNotificationAddedArticle, $wgDiscordIncludeDiffSize;
$isNew = (bool)( $flags & EDIT_NEW );

if ( !$wgDiscordNotificationEditedArticle && !$isNew ) return true;
if ( !$wgDiscordNotificationAddedArticle && $isNew ) return true;
if ( self::titleIsExcluded( $wikiPage->getTitle() ) ) return true;

if ( $isNew ) {
$message = self::msg( 'discordnotifications-article-created',
self::getDiscordUserText( $user ),
self::getDiscordArticleText( $wikiPage ),
$summary == "" ? "" : self::msg( 'discordnotifications-summary', $summary ) );
if ( $wgDiscordIncludeDiffSize ) {
$message .= " (" . self::msg( 'discordnotifications-bytes', $revisionRecord->getSize() ) . ")";
}
self::pushDiscordNotify( $message, $user, 'article_inserted' );
} else {
$isMinor = (bool)( $flags & EDIT_MINOR );
// Skip minor edits if user wanted to ignore them
if ( $isMinor && $wgDiscordIgnoreMinorEdits ) return true;

$message = self::msg(
'discordnotifications-article-saved',
self::getDiscordUserText( $user ),
$isMinor == true ? self::msg( 'discordnotifications-article-saved-minor-edits' ) : self::msg( 'discordnotifications-article-saved-edit' ),
self::getDiscordArticleText( $wikiPage, true ),
$summary == "" ? "" : self::msg( 'discordnotifications-summary', $summary ) );
if ( $wgDiscordIncludeDiffSize ) {
$message .= ' (' . self::msg( 'discordnotifications-bytes',
$revisionRecord->getSize() - MediaWiki\MediaWikiServices::getInstance()->getRevisionLookup()->getPreviousRevision( $revisionRecord )->getSize() ) . ')';
}
self::pushDiscordNotify( $message, $user, 'article_saved' );
}
return true;
}

/**
* Occurs after the save page request has been processed.
* @see https://www.mediawiki.org/wiki/Manual:Hooks/PageContentSaveComplete
Expand All @@ -102,13 +175,7 @@ public static function onDiscordArticleSaved( WikiPage $article, $user, $content
global $wgDiscordIgnoreMinorEdits, $wgDiscordIncludeDiffSize;
if ( !$wgDiscordNotificationEditedArticle ) return;

// Discard notifications from excluded pages
global $wgDiscordExcludeNotificationsFrom;
if ( is_array( $wgDiscordExcludeNotificationsFrom ) && count( $wgDiscordExcludeNotificationsFrom ) > 0 ) {
foreach ( $wgDiscordExcludeNotificationsFrom as &$currentExclude ) {
if ( 0 === strpos( $article->getTitle(), $currentExclude ) ) return;
}
}
if ( self::titleIsExcluded( $article->getTitle() ) ) return;

// Skip new articles that have view count below 1. Adding new articles is already handled in article_added function and
// calling it also here would trigger two notifications!
Expand Down Expand Up @@ -147,13 +214,7 @@ public static function onDiscordArticleInserted( WikiPage $article, $user, $text
global $wgDiscordNotificationAddedArticle, $wgDiscordIncludeDiffSize;
if ( !$wgDiscordNotificationAddedArticle ) return;

// Discard notifications from excluded pages
global $wgDiscordExcludeNotificationsFrom;
if ( is_array( $wgDiscordExcludeNotificationsFrom ) && count( $wgDiscordExcludeNotificationsFrom ) > 0 ) {
foreach ( $wgDiscordExcludeNotificationsFrom as &$currentExclude ) {
if ( 0 === strpos( $article->getTitle(), $currentExclude ) ) return;
}
}
if ( self::titleIsExcluded( $article->getTitle() ) ) return;

// Do not announce newly added file uploads as articles...
if ( $article->getTitle()->getNsText() == self::msg( 'discordnotifications-file-namespace' ) ) return true;
Expand All @@ -163,7 +224,14 @@ public static function onDiscordArticleInserted( WikiPage $article, $user, $text
self::getDiscordArticleText( $article ),
$summary == "" ? "" : self::msg( 'discordnotifications-summary', $summary ) );
if ( $wgDiscordIncludeDiffSize ) {
$message .= " (" . self::msg( 'discordnotifications-bytes', $article->getRevision()->getSize() ) . ")";
if ( defined( 'MW_VERSION' ) && version_compare( MW_VERSION, '1.31', '>=' ) ) {
// WikiPage::getRevision was deprecated in MediaWiki 1.35
// Revision::getSize was deprecated in MediaWiki 1.31
$size = $article->getRevisionRecord()->getSize();
} else {
$size = $article->getRevision()->getSize();
}
$message .= " (" . self::msg( 'discordnotifications-bytes', $size ) . ")";
}
self::pushDiscordNotify( $message, $user, 'article_inserted' );
return true;
Expand All @@ -180,13 +248,7 @@ public static function onDiscordArticleDeleted( WikiPage $article, User $user, $
global $wgDiscordNotificationShowSuppressed;
if ( !$wgDiscordNotificationShowSuppressed && $logEntry->getType() != 'delete' ) return;

// Discard notifications from excluded pages
global $wgDiscordExcludeNotificationsFrom;
if ( is_array( $wgDiscordExcludeNotificationsFrom ) && count( $wgDiscordExcludeNotificationsFrom ) > 0 ) {
foreach ( $wgDiscordExcludeNotificationsFrom as &$currentExclude ) {
if ( 0 === strpos( $article->getTitle(), $currentExclude ) ) return;
}
}
if ( self::titleIsExcluded( $article->getTitle() ) ) return;

$message = self::msg( 'discordnotifications-article-deleted',
self::getDiscordUserText( $user ),
Expand Down Expand Up @@ -234,12 +296,12 @@ public static function onDiscordArticleProtected( $article = null, $user = null,
* Occurs after page has been imported into wiki.
* @see https://www.mediawiki.org/wiki/Manual:Hooks/AfterImportPage
*/
public static function onDiscordAfterImportPage( $title = null, $origTitle = Null, $revCount = null, $sRevCount = null, $pageInfo = null ) {
public static function onDiscordAfterImportPage( $title = null, $origTitle = null, $revCount = null, $sRevCount = null, $pageInfo = null ) {
global $wgDiscordNotificationAfterImportPage;
if ( !$wgDiscordNotificationAfterImportPage ) return;

$message = self::msg( 'discordnotifications-import-complete',
self::getDiscordTitleText( $title ));
self::getDiscordTitleText( $title ) );
self::pushDiscordNotify( $message, null, 'import_complete' );
return true;
}
Expand All @@ -250,7 +312,7 @@ public static function onDiscordAfterImportPage( $title = null, $origTitle = Nul
*/
public static function onDiscordNewUserAccount( $user, $byEmail ) {
global $wgDiscordNotificationNewUser, $wgDiscordShowNewUserFullName;

// Disable reporting of new user email and IP address
//global $wgDiscordShowNewUserEmail, $wgDiscordShowNewUserIP;
$wgDiscordShowNewUserEmail = false;
Expand Down Expand Up @@ -335,7 +397,7 @@ public static function onDiscordUserBlocked( Block $block, $user ) {

global $wgDiscordNotificationWikiUrl, $wgDiscordNotificationWikiUrlEnding, $wgDiscordNotificationWikiUrlEndingBlockList;
$mReason = "";
if (defined('MW_VERSION') && version_compare(MW_VERSION, '1.35', '>=')) { //DatabaseBlock::$mReason was made protected in MW 1.35
if ( defined( 'MW_VERSION' ) && version_compare( MW_VERSION, '1.35', '>=' ) ) { // DatabaseBlock::$mReason was made protected in MW 1.35
$mReason = $block->getReasonComment()->text;
} else {
$mReason = $block->mReason;
Expand Down Expand Up @@ -383,13 +445,7 @@ public static function onDiscordApiFlowAfterExecute( APIBase $module ) {
$result = $module->getResult()->getResultData()['flow'][$action];
if ( $result['status'] != 'ok' ) return;

// Discard notifications from excluded pages
global $wgDiscordExcludeNotificationsFrom;
if ( count( $wgDiscordExcludeNotificationsFrom ) > 0 ) {
foreach ( $wgDiscordExcludeNotificationsFrom as &$currentExclude ) {
if ( 0 === strpos( $request['page'], $currentExclude ) ) return;
}
}
if ( self::titleIsExcluded( $request['page'] ) ) return;

global $wgDiscordNotificationWikiUrl, $wgDiscordNotificationWikiUrlEnding, $wgUser;
switch ( $action ) {
Expand Down Expand Up @@ -475,7 +531,7 @@ private static function pushDiscordNotify( $message, $user, $action ) {
global $wgDiscordIncomingWebhookUrl, $wgDiscordFromName, $wgDiscordAvatarUrl, $wgDiscordSendMethod, $wgDiscordExcludedPermission, $wgSitename, $wgDiscordAdditionalIncomingWebhookUrls;

if ( isset( $wgDiscordExcludedPermission ) && $wgDiscordExcludedPermission != "" ) {
if ($user && $user->isAllowed( $wgDiscordExcludedPermission ) ) {
if ( $user && $user->isAllowed( $wgDiscordExcludedPermission ) ) {
return; // Users with the permission suppress notifications
}
}
Expand Down Expand Up @@ -566,11 +622,10 @@ private static function sendCurlRequest( $url, $postData ) {
curl_setopt( $h, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $h, CURLOPT_CONNECTTIMEOUT, 10 ); // Set 10 second timeout to connection
curl_setopt( $h, CURLOPT_TIMEOUT, 10 ); // Set global 10 second timeout to handle all data
curl_setopt( $h, CURLOPT_HTTPHEADER, array(
curl_setopt( $h, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($postData)
)
); // Set Content-Type to application/json
'Content-Length: ' . strlen( $postData )
] ); // Set Content-Type to application/json
// Commented out lines below. Using default curl settings for host and peer verification.
//curl_setopt ($h, CURLOPT_SSL_VERIFYHOST, 0);
//curl_setopt ($h, CURLOPT_SSL_VERIFYPEER, 0);
Expand Down
13 changes: 3 additions & 10 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,10 @@
"i18n"
]
},
"ExtensionFunctions": [
"DiscordNotificationsCore::registerExtraHooks"
],
"Hooks": {
"PageContentSaveComplete": [
[
"DiscordNotificationsCore::onDiscordArticleSaved"
]
],
"PageContentInsertComplete": [
[
"DiscordNotificationsCore::onDiscordArticleInserted"
]
],
"ArticleDeleteComplete": [
[
"DiscordNotificationsCore::onDiscordArticleDeleted"
Expand Down

0 comments on commit e87223e

Please sign in to comment.