Followup to r64962: Fixed watchlist parameter in API. User options watchdeletions...
[lhc/web/wiklou.git] / includes / api / ApiBase.php
index 2c3164d..1d31f6f 100644 (file)
@@ -475,8 +475,10 @@ abstract class ApiBase {
                $params = $this->getFinalParams();
                $results = array();
 
-               foreach ( $params as $paramName => $paramSettings ) {
-                       $results[$paramName] = $this->getParameterFromSettings( $paramName, $paramSettings, $parseLimit );
+               if( $params ) { // getFinalParams() can return false
+                       foreach ( $params as $paramName => $paramSettings ) {
+                               $results[$paramName] = $this->getParameterFromSettings( $paramName, $paramSettings, $parseLimit );
+                       }
                }
 
                return $results;
@@ -532,30 +534,63 @@ abstract class ApiBase {
 
                return $mValidNamespaces;
        }
+
        /**
-       * Handle watchlist settings
-       */
-       protected function getWatchlistValue ( $watchlist, $titleObj ) {
+        * 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 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, $userOption = null ) {
                switch ( $watchlist ) {
                        case 'watch':
-                               $watch = true;
-                               break;
+                               return true;
+
                        case 'unwatch':
-                               $watch = false;
-                               break;
+                               return false;
+
                        case 'preferences':
                                global $wgUser;
-                       
-                               if ( $titleObj->exists() ) {
-                                       $watch = $wgUser->getOption( 'watchdefault' ) || $titleObj->userIsWatching();
+                               # If the user is already watching, don't bother checking
+                               if ( $titleObj->userIsWatching() ) {
+                                       return null;
+                               }
+                               # If no user option was passed, use watchdefault or watchcreation
+                               if ( is_null( $userOption ) ) {
+                                       $userOption = $titleObj->exists() 
+                                               ? 'watchdefault' : 'watchcreations';
                                }
-                               break;
+                               # If the corresponding user option is true, watch, else no change
+                               return $wgUser->getOption( $userOption ) ? true : null;
+
                        case 'nochange':
+                               return null;
+
                        default:
-                               $watch = $titleObj->userIsWatching();
+                               return null;
+               }
+       }
+
+       /**
+        * 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, $userOption = null ) {
+               $value = $this->getWatchlistValue( $watch, $titleObj, $userOption );
+               if( $value === null ) { 
+                       return;
+               }
+
+               $articleObj = new Article( $titleObj );
+               if ( $value ) {
+                       $articleObj->doWatch();
+               } else {
+                       $articleObj->doUnwatch();
                }
-               
-               return $watch;
        }
 
        /**
@@ -873,6 +908,8 @@ abstract class ApiBase {
                'ipb_blocked_as_range' => array( 'code' => 'blockedasrange', 'info' => "IP address ``\$1'' was blocked as part of range ``\$2''. You can't unblock the IP invidually, but you can unblock the range as a whole." ),
                'ipb_cant_unblock' => array( 'code' => 'cantunblock', 'info' => "The block you specified was not found. It may have been unblocked already" ),
                'mailnologin' => array( 'code' => 'cantsend', 'info' => "You are not logged in, you do not have a confirmed e-mail address, or you are not allowed to send e-mail to other users, so you cannot send e-mail" ),
+               'ipbblocked' => array( 'code' => 'ipbblocked', 'info' => 'You cannot block or unblock users while you are yourself blocked' ),
+               'ipbnounblockself' => array( 'code' => 'ipbnounblockself', 'info' => 'You are not allowed to unblock yourself' ),
                'usermaildisabled' => array( 'code' => 'usermaildisabled', 'info' => "User email has been disabled" ),
                'blockedemailuser' => array( 'code' => 'blockedfrommail', 'info' => "You have been blocked from sending e-mail" ),
                'notarget' => array( 'code' => 'notarget', 'info' => "You have not specified a valid target for this action" ),