From 2103572a1bfa75aca8fcf263477c410f5504e1e8 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 30 Nov 2004 05:45:56 +0000 Subject: [PATCH] Added hooks for watching and unwatching articles. Documented in hooks.doc, and an example in Syslog extension. --- docs/hooks.doc | 16 +++++++++++ extensions/Syslog.php | 62 ++++++++++++++++++++++++++--------------- includes/Article.php | 65 ++++++++++++++++++++++++++++++------------- 3 files changed, 101 insertions(+), 42 deletions(-) diff --git a/docs/hooks.doc b/docs/hooks.doc index 2032aacbcc..6f5120e7ee 100644 --- a/docs/hooks.doc +++ b/docs/hooks.doc @@ -293,6 +293,14 @@ $text: text of the mail $action: action name $article: article "acted on" +'UnwatchArticle': before a watch is removed from an article +$user: user watching +$article: article object to be removed + +'UnwatchArticle': after a watch is removed from an article +$user: user that was watching +$article: article object removed + 'UserLoginComplete': after a user has logged in $user: the user object that was created on login @@ -302,3 +310,11 @@ $user: the user object that is about to be logged out 'UserLogoutComplete': after a user has logged out $user: the user object _after_ logout (won't have name, ID, etc.) +'WatchArticle': before a watch is added to an article +$user: user that will watch +$article: article object to be watched + +'WatchArticleComplete': after a watch is added to an article +$user: user that watched +$article: article object watched + diff --git a/extensions/Syslog.php b/extensions/Syslog.php index 3f69bf9246..d499bb9550 100644 --- a/extensions/Syslog.php +++ b/extensions/Syslog.php @@ -32,29 +32,6 @@ if (defined('MEDIAWIKI')) { $wgSyslogFacility = LOG_USER; } - # Hook for login - - function syslogUserLogin(&$user) { - syslog(LOG_INFO, "User '" . $user->getName() . "' logged in."); - return true; - } - - # Hook for logout - - function syslogUserLogout(&$user) { - syslog(LOG_INFO, "User '" . $user->getName() . "' logged out."); - return true; - } - - # Hook for IP & user blocks - - function syslogBlockIp(&$block, &$user) { - syslog(LOG_NOTICE, "User '" . $user->getName() . - "' blocked '" . (($block->mUser) ? $block->mUser : $block->mAddress) . - "' for '" . $block->mReason . "' until '" . $block->mExpiry . "'"); - return true; - } - # Hook for article protection function syslogArticleProtect(&$article, &$user, $protect, &$reason, &$moveonly) { @@ -88,10 +65,47 @@ if (defined('MEDIAWIKI')) { return true; } + # Hook for IP & user blocks + + function syslogBlockIp(&$block, &$user) { + syslog(LOG_NOTICE, "User '" . $user->getName() . + "' blocked '" . (($block->mUser) ? $block->mUser : $block->mAddress) . + "' for '" . $block->mReason . "' until '" . $block->mExpiry . "'"); + return true; + } + function syslogEmailUser(&$to, &$from, &$subject, &$text) { syslog(LOG_INFO, "Email sent from '$from' to '$to' with subject '$subject'"); } + + # Hook for unwatch + + function syslogUnwatch(&$user, &$article) { + syslog(LOG_INFO, "User '" . $user->getName() . "' stopped watching '" . + $article->mTitle->getPrefixedText() . "'"); + } + + # Hook for login + + function syslogUserLogin(&$user) { + syslog(LOG_INFO, "User '" . $user->getName() . "' logged in"); + return true; + } + + # Hook for logout + + function syslogUserLogout(&$user) { + syslog(LOG_INFO, "User '" . $user->getName() . "' logged out"); + return true; + } + + # Hook for watch + function syslogWatch(&$user, &$article) { + syslog(LOG_INFO, "User '" . $user->getName() . "' started watching '" . + $article->mTitle->getPrefixedText() . "'"); + } + # Setup -- called once environment is configured function setupSyslog() { @@ -108,6 +122,8 @@ if (defined('MEDIAWIKI')) { $wgHooks['ArticleDeleteComplete'][] = 'syslogArticleDelete'; $wgHooks['ArticleSaveComplete'][] = 'syslogArticleSave'; $wgHooks['EmailUserComplete'][] = 'syslogEmailUser'; + $wgHooks['WatchArticleComplete'][] = 'syslogWatch'; + $wgHooks['UnwatchArticleComplete'][] = 'syslogUnwatch'; return true; } diff --git a/includes/Article.php b/includes/Article.php index 2a4f5e0e93..0ad959cac9 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -1210,9 +1210,11 @@ class Article { /** - * Add or remove this page to my watchlist based on value of $add + * Add this page to $wgUser's watchlist */ - function watch( $add = true ) { + + function watch() { + global $wgUser, $wgOut; if ( 0 == $wgUser->getID() ) { @@ -1223,33 +1225,58 @@ class Article { $wgOut->readOnlyPage(); return; } - if( $add ) - $wgUser->addWatch( $this->mTitle ); - else - $wgUser->removeWatch( $this->mTitle ); - $wgOut->setPagetitle( wfMsg( $add ? 'addedwatch' : 'removedwatch' ) ); - $wgOut->setRobotpolicy( 'noindex,follow' ); - - $sk = $wgUser->getSkin() ; - $link = $this->mTitle->getPrefixedText(); + if (wfRunHooks('WatchArticle', $wgUser, $this)) { + + $wgUser->addWatch( $this->mTitle ); + $wgUser->saveSettings(); - if($add) + wfRunHooks('WatchArticleComplete', $wgUser, $this); + + $wgOut->setPagetitle( wfMsg( 'addedwatch' ) ); + $wgOut->setRobotpolicy( 'noindex,follow' ); + + $link = $this->mTitle->getPrefixedText(); $text = wfMsg( 'addedwatchtext', $link ); - else - $text = wfMsg( 'removedwatchtext', $link ); - $wgOut->addWikiText( $text ); - - $wgUser->saveSettings(); + $wgOut->addWikiText( $text ); + } $wgOut->returnToMain( true, $this->mTitle->getPrefixedText() ); } /** - * Stop watching a page, it act just like a call to watch(false) + * Stop watching a page */ + function unwatch() { - $this->watch( false ); + + global $wgUser, $wgOut; + + if ( 0 == $wgUser->getID() ) { + $wgOut->errorpage( 'watchnologin', 'watchnologintext' ); + return; + } + if ( wfReadOnly() ) { + $wgOut->readOnlyPage(); + return; + } + + if (wfRunHooks('UnwatchArticle', $wgUser, $this)) { + + $wgUser->removeWatch( $this->mTitle ); + $wgUser->saveSettings(); + + wfRunHooks('UnwatchArticleComplete', $wgUser, $this); + + $wgOut->setPagetitle( wfMsg( 'removedwatch' ) ); + $wgOut->setRobotpolicy( 'noindex,follow' ); + + $link = $this->mTitle->getPrefixedText(); + $text = wfMsg( 'removedwatchtext', $link ); + $wgOut->addWikiText( $text ); + } + + $wgOut->returnToMain( true, $this->mTitle->getPrefixedText() ); } /** -- 2.20.1