From 6c6c2f698c3d73c617764e385576d92c87aa1ed1 Mon Sep 17 00:00:00 2001 From: Bryan Tong Minh Date: Wed, 14 Apr 2010 12:17:39 +0000 Subject: [PATCH] Followup to r64962: Fixed watchlist parameter in API. User options watchdeletions and watchmoves can now by overridden. Unwatching while editting is now possible. --- includes/api/ApiBase.php | 34 ++++++++++++++++++++-------------- includes/api/ApiDelete.php | 4 +--- includes/api/ApiEditPage.php | 2 +- includes/api/ApiMove.php | 6 +++--- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index 4aeac9aa9d..1d31f6f14f 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -538,10 +538,12 @@ abstract class ApiBase { /** * Return true if we're to watch the page, false if not, null if no change. * @param $watchlist String Valid values: 'watch', 'unwatch', 'preferences', 'nochange' - * @param $titleObj Title (optional) the page under consideration + * @param $titleObj Title the page under consideration + * @param $userOption The user option to consider when $watchlist=preferences. + * If not set will magically default to either watchdefault or watchcreations * @returns mixed */ - protected function getWatchlistValue ( $watchlist, $titleObj = null ) { + protected function getWatchlistValue ( $watchlist, $titleObj, $userOption = null ) { switch ( $watchlist ) { case 'watch': return true; @@ -551,16 +553,17 @@ abstract class ApiBase { case 'preferences': global $wgUser; - if ( isset($titleObj) && !$titleObj->userIsWatching() ) { - if ( $titleObj->exists() ) { - if ( $wgUser->getOption( 'watchdefault' ) ) { - return true; - } - } elseif ( $wgUser->getOption( 'watchcreations' ) ) { - return true; - } + # If the user is already watching, don't bother checking + if ( $titleObj->userIsWatching() ) { + return null; } - return null; + # If no user option was passed, use watchdefault or watchcreation + if ( is_null( $userOption ) ) { + $userOption = $titleObj->exists() + ? 'watchdefault' : 'watchcreations'; + } + # If the corresponding user option is true, watch, else no change + return $wgUser->getOption( $userOption ) ? true : null; case 'nochange': return null; @@ -574,10 +577,13 @@ abstract class ApiBase { * Set a watch (or unwatch) based the based on a watchlist parameter. * @param $watch String Valid values: 'watch', 'unwatch', 'preferences', 'nochange' * @param $titleObj Title the article's title to change + * @param $userOption The user option to consider when $watch=preferences */ - protected function setWatch ( $watch, $titleObj ) { - $value = $this->getWatchlistValue( $watch, $titleObj ); - if( $value === null ) return; + protected function setWatch ( $watch, $titleObj, $userOption = null ) { + $value = $this->getWatchlistValue( $watch, $titleObj, $userOption ); + if( $value === null ) { + return; + } $articleObj = new Article( $titleObj ); if ( $value ) { diff --git a/includes/api/ApiDelete.php b/includes/api/ApiDelete.php index 8c6c10e87a..f5294dd6ca 100644 --- a/includes/api/ApiDelete.php +++ b/includes/api/ApiDelete.php @@ -88,12 +88,10 @@ class ApiDelete extends ApiBase { $watch = 'watch'; } elseif ( $params['unwatch'] ) { $watch = 'unwatch'; - } elseif ( $wgUser->getOption( 'watchdeletion' ) ) { - $watch = 'watch'; } else { $watch = $params['watchlist']; } - $this->setWatch( $watch, $titleObj ); + $this->setWatch( $watch, $titleObj, 'watchdeletion' ); } $r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason ); diff --git a/includes/api/ApiEditPage.php b/includes/api/ApiEditPage.php index 02eca54988..b07c7ee71a 100644 --- a/includes/api/ApiEditPage.php +++ b/includes/api/ApiEditPage.php @@ -211,7 +211,7 @@ class ApiEditPage extends ApiBase { $watch = false; } - if ( $watch || $titleObj->userIsWatching() ) { + if ( $watch ) { $reqArr['wpWatchthis'] = ''; } diff --git a/includes/api/ApiMove.php b/includes/api/ApiMove.php index e59855b368..8aed6710ea 100644 --- a/includes/api/ApiMove.php +++ b/includes/api/ApiMove.php @@ -125,15 +125,15 @@ class ApiMove extends ApiBase { $watch = "preferences"; if ( isset( $params['watchlist'] ) ) { $watch = $params['watchlist']; - } elseif ( $wgUser->getOption( 'watchmoves' ) || $params['watch'] ) { + } elseif ( $params['watch'] ) { $watch = 'watch'; } elseif ( $params['unwatch'] ) { $watch = 'unwatch'; } // Watch pages - $this->setWatch( $watch, $fromTitle ); - $this->setWatch( $watch, $toTitle ); + $this->setWatch( $watch, $fromTitle, 'watchmoves' ); + $this->setWatch( $watch, $toTitle, 'watchmoves' ); $this->getResult()->addValue( null, $this->getModuleName(), $r ); } -- 2.20.1