From 637a7435cebb7e25693387469c03fbe2221c4692 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Tue, 27 Sep 2016 13:35:58 -0700 Subject: [PATCH] Trigger Schema:ExternalLinksChange logging on page deletion If a page is being deleted, use the ArticleDelete hook to queue a list of URLs that are being "removed" from the page. The ArticleDeleteComplete hook will trigger actually sending the logs - so if something prevents the deletion, nothing will be logged. Bug: T115119 Change-Id: I32e357bb88305a46251b05714a4ff75b75ae37aa --- SpamBlacklistHooks.php | 37 ++++++++++++++++++++++++++++++++++++- SpamBlacklist_body.php | 10 +++++----- extension.json | 4 +++- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/SpamBlacklistHooks.php b/SpamBlacklistHooks.php index e4dee92d..eb0d5dd9 100644 --- a/SpamBlacklistHooks.php +++ b/SpamBlacklistHooks.php @@ -189,7 +189,7 @@ class SpamBlacklistHooks { ) { if ( $revision ) { BaseBlacklist::getInstance( 'spam' ) - ->doLogging( $user, $wikiPage->getTitle(), $revision ); + ->doLogging( $user, $wikiPage->getTitle(), $revision->getId() ); } if ( !BaseBlacklist::isLocalSource( $wikiPage->getTitle() ) ) { @@ -252,4 +252,39 @@ class SpamBlacklistHooks { return true; } + + /** + * @param WikiPage $article + * @param User $user + * @param $reason + * @param $error + */ + public static function onArticleDelete( WikiPage &$article, User &$user, &$reason, &$error ) { + /** @var SpamBlacklist $spam */ + $spam = BaseBlacklist::getInstance( 'spam' ); + if ( !$spam->isLoggingEnabled() ) { + return; + } + + // Log the changes, but we only commit them once the deletion has happened. + // We do that since the external links table could get cleared before the + // ArticleDeleteComplete hook runs + $spam->logUrlChanges( $spam->getCurrentLinks( $article->getTitle() ), [], [] ); + } + + /** + * @param WikiPage $page + * @param User $user + * @param $reason + * @param $id + * @param Content|null $content + * @param LogEntry $logEntry + */ + public static function onArticleDeleteComplete( &$page, User &$user, $reason, + $id, Content $content = null, LogEntry $logEntry + ) { + /** @var SpamBlacklist $spam */ + $spam = BaseBlacklist::getInstance( 'spam' ); + $spam->doLogging( $user, $page->getTitle(), $page->getLatest() ); + } } diff --git a/SpamBlacklist_body.php b/SpamBlacklist_body.php index a4d62d85..e14fa444 100644 --- a/SpamBlacklist_body.php +++ b/SpamBlacklist_body.php @@ -175,7 +175,7 @@ class SpamBlacklist extends BaseBlacklist { return $retVal; } - private function isLoggingEnabled() { + public function isLoggingEnabled() { global $wgSpamBlacklistEventLogging; return $wgSpamBlacklistEventLogging && class_exists( 'EventLogging' ); } @@ -187,7 +187,7 @@ class SpamBlacklist extends BaseBlacklist { * @param string[] $newLinks * @param string[] $addedLinks */ - private function logUrlChanges( $oldLinks, $newLinks, $addedLinks ) { + public function logUrlChanges( $oldLinks, $newLinks, $addedLinks ) { if ( !$this->isLoggingEnabled() ) { return; } @@ -207,15 +207,15 @@ class SpamBlacklist extends BaseBlacklist { * * @param User $user * @param Title $title - * @param Revision $rev + * @param int $revId */ - public function doLogging( User $user, Title $title, Revision $rev ) { + public function doLogging( User $user, Title $title, $revId ) { if ( !$this->isLoggingEnabled() ) { return; } $baseInfo = array( - 'revId' => $rev->getId(), + 'revId' => $revId, 'pageId' => $title->getArticleID(), 'pageNamespace' => $title->getNamespace(), 'userId' => $user->getId(), diff --git a/extension.json b/extension.json index 794fb0ac..52c40b91 100644 --- a/extension.json +++ b/extension.json @@ -65,7 +65,9 @@ ], "UploadVerifyUpload": [ "SpamBlacklistHooks::onUploadVerifyUpload" - ] + ], + "ArticleDelete": "SpamBlacklistHooks::onArticleDelete", + "ArticleDeleteComplete": "SpamBlacklistHooks::onArticleDeleteComplete" }, "config": { "BlacklistSettings": {