Cleanup watchlist preference usage
authorBrad Jorsch <bjorsch@wikimedia.org>
Fri, 8 Nov 2013 18:01:28 +0000 (13:01 -0500)
committerAaron Schulz <aschulz@wikimedia.org>
Fri, 8 Nov 2013 20:12:55 +0000 (20:12 +0000)
In general, the web UI does a check of the watchlist preferences along
the lines of "watch if watchdefault, or if watchcreations and the title
doesn't exist". So there's no way to have it watch edits but not
creations. Make the API behavior match this.

For API action=protect&watchlist=preferences, we want to use
'watchdefault' always to match the behavior of the web UI.

For Special:Upload, the code is all there to do a "watch if
watchdefault, or if watchcreations and the file doesn't exist". But for
some reason that code wasn't being used in favor of just using
watchcreations all the time. Fix that, too. And have the API use that
instead of checking if the file page exists.

Bug: 56766
Change-Id: I57fc46d9a97b3ea2169173727db842d0d7ecf81d

includes/api/ApiBase.php
includes/api/ApiProtect.php
includes/api/ApiUpload.php
includes/specials/SpecialUpload.php

index ce6ecda..6be044c 100644 (file)
@@ -818,7 +818,7 @@ abstract class ApiBase extends ContextSource {
         * @param string $watchlist Valid values: 'watch', 'unwatch', 'preferences', 'nochange'
         * @param $titleObj Title the page under consideration
         * @param string $userOption The user option to consider when $watchlist=preferences.
-        *      If not set will magically default to either watchdefault or watchcreations
+        *      If not set will use watchdefault always and watchcreations if $titleObj doesn't exist.
         * @return bool
         */
        protected function getWatchlistValue( $watchlist, $titleObj, $userOption = null ) {
@@ -837,10 +837,10 @@ abstract class ApiBase extends ContextSource {
                                if ( $userWatching ) {
                                        return true;
                                }
-                               # If no user option was passed, use watchdefault or watchcreations
+                               # If no user option was passed, use watchdefault and watchcreations
                                if ( is_null( $userOption ) ) {
-                                       $userOption = $titleObj->exists()
-                                                       ? 'watchdefault' : 'watchcreations';
+                                       return $this->getUser()->getBoolOption( 'watchdefault' ) ||
+                                               $this->getUser()->getBoolOption( 'watchcreations' ) && !$titleObj->exists();
                                }
                                # Watch the article based on the user preference
                                return $this->getUser()->getBoolOption( $userOption );
index 7830c8b..428eef3 100644 (file)
@@ -98,7 +98,7 @@ class ApiProtect extends ApiBase {
                $cascade = $params['cascade'];
 
                $watch = $params['watch'] ? 'watch' : $params['watchlist'];
-               $this->setWatch( $watch, $titleObj );
+               $this->setWatch( $watch, $titleObj, 'watchdefault' );
 
                $status = $pageObj->doUpdateRestrictions( $protections, $expiryarray, $cascade, $params['reason'], $this->getUser() );
 
index 467eccf..5839edc 100644 (file)
@@ -587,7 +587,19 @@ class ApiUpload extends ApiBase {
 
                /** @var $file File */
                $file = $this->mUpload->getLocalFile();
-               $watch = $this->getWatchlistValue( $this->mParams['watchlist'], $file->getTitle() );
+
+               // For preferences mode, we want to watch if 'watchdefault' is set or
+               // if the *file* doesn't exist and 'watchcreations' is set. But
+               // getWatchlistValue()'s automatic handling checks if the *title*
+               // exists or not, so we need to check both prefs manually.
+               $watch = $this->getWatchlistValue(
+                       $this->mParams['watchlist'], $file->getTitle(), 'watchdefault'
+               );
+               if ( !$watch && $this->mParams['watchlist'] == 'preferences' && !$file->exists() ) {
+                       $watch = $this->getWatchlistValue(
+                               $this->mParams['watchlist'], $file->getTitle(), 'watchcreations'
+                       );
+               }
 
                // Deprecated parameters
                if ( $this->mParams['watch'] ) {
index b79aaa9..0700c49 100644 (file)
@@ -60,7 +60,7 @@ class SpecialUpload extends SpecialPage {
 
        /** User input variables from the root section **/
        public $mIgnoreWarning;
-       public $mWatchThis;
+       public $mWatchthis;
        public $mCopyrightStatus;
        public $mCopyrightSource;
 
@@ -75,8 +75,6 @@ class SpecialUpload extends SpecialPage {
        public $uploadFormTextTop;
        public $uploadFormTextAfterSummary;
 
-       public $mWatchthis;
-
        /**
         * Initialize instance variables from request and create an Upload handler
         */
@@ -517,11 +515,17 @@ class SpecialUpload extends SpecialPage {
                        return true;
                }
 
+               $desiredTitleObj = Title::makeTitleSafe( NS_FILE, $this->mDesiredDestName );
+               if ( $desiredTitleObj instanceof Title && $this->getUser()->isWatched( $desiredTitleObj ) ) {
+                       // Already watched, don't change that
+                       return true;
+               }
+
                $local = wfLocalFile( $this->mDesiredDestName );
                if ( $local && $local->exists() ) {
                        // We're uploading a new version of an existing file.
                        // No creation, so don't watch it if we're not already.
-                       return $this->getUser()->isWatched( $local->getTitle() );
+                       return false;
                } else {
                        // New page should get watched if that's our option.
                        return $this->getUser()->getOption( 'watchcreations' );
@@ -1011,7 +1015,7 @@ class UploadForm extends HTMLForm {
                                        'id' => 'wpWatchthis',
                                        'label-message' => 'watchthisupload',
                                        'section' => 'options',
-                                       'default' => $user->getOption( 'watchcreations' ),
+                                       'default' => $this->mWatch,
                                )
                        );
                }