Merge "Fix form submission on Special:RandomInCategory"
[lhc/web/wiklou.git] / includes / User.php
index b09d79c..25b35b3 100644 (file)
@@ -1240,7 +1240,10 @@ class User {
 
                $defOpt = $wgDefaultUserOptions;
                // Default language setting
-               $defOpt['language'] = $defOpt['variant'] = $wgContLang->getCode();
+               $defOpt['language'] = $wgContLang->getCode();
+               foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
+                       $defOpt[$langCode == $wgContLang->getCode() ? 'variant' : "variant-$langCode"] = $langCode;
+               }
                foreach ( SearchEngine::searchableNamespaces() as $nsnum => $nsname ) {
                        $defOpt['searchNs' . $nsnum] = !empty( $wgNamespacesToBeSearchedDefault[$nsnum] );
                }
@@ -1828,8 +1831,14 @@ class User {
        }
 
        /**
-        * Return the revision and link for the oldest new talk page message for
-        * this user.
+        * Return the data needed to construct links for new talk page message
+        * alerts. If there are new messages, this will return an associative array
+        * with the following data:
+        *     wiki: The database name of the wiki
+        *     link: Root-relative link to the user's talk page
+        *     rev: The last talk page revision that the user has seen or null. This
+        *         is useful for building diff links.
+        * If there are no new messages, it returns an empty array.
         * @note This function was designed to accomodate multiple talk pages, but
         * currently only returns a single link and revision.
         * @return Array
@@ -1853,8 +1862,9 @@ class User {
        }
 
        /**
-        * Get the revision ID for the oldest new talk page message for this user
-        * @return int|null Revision id or null if there are no new messages
+        * Get the revision ID for the last talk page revision viewed by the talk
+        * page owner.
+        * @return int|null Revision ID or null
         */
        public function getNewMessageRevisionId() {
                $newMessageRevisionId = null;
@@ -2340,7 +2350,7 @@ class User {
        }
 
        /**
-        * Get the user's current setting for a given option, as a boolean value.
+        * Get the user's current setting for a given option, as an integer value.
         *
         * @param string $oname The option to check
         * @param int $defaultOverride A default value returned if the option does not exist
@@ -2372,6 +2382,49 @@ class User {
                $this->mOptions[$oname] = $val;
        }
 
+       /**
+        * Get a token stored in the preferences (like the watchlist one),
+        * resetting it if it's empty (and saving changes).
+        *
+        * @param string $oname The option name to retrieve the token from
+        * @return string|bool User's current value for the option, or false if this option is disabled.
+        * @see resetTokenFromOption()
+        * @see getOption()
+        */
+       public function getTokenFromOption( $oname ) {
+               global $wgHiddenPrefs;
+               if ( in_array( $oname, $wgHiddenPrefs ) ) {
+                       return false;
+               }
+
+               $token = $this->getOption( $oname );
+               if ( !$token ) {
+                       $token = $this->resetTokenFromOption( $oname );
+                       $this->saveSettings();
+               }
+               return $token;
+       }
+
+       /**
+        * Reset a token stored in the preferences (like the watchlist one).
+        * *Does not* save user's preferences (similarly to setOption()).
+        *
+        * @param string $oname The option name to reset the token in
+        * @return string|bool New token value, or false if this option is disabled.
+        * @see getTokenFromOption()
+        * @see setOption()
+        */
+       public function resetTokenFromOption( $oname ) {
+               global $wgHiddenPrefs;
+               if ( in_array( $oname, $wgHiddenPrefs ) ) {
+                       return false;
+               }
+
+               $token = MWCryptRand::generateHex( 40 );
+               $this->setOption( $oname, $token );
+               return $token;
+       }
+
        /**
         * Return a list of the types of user options currently returned by
         * User::getOptionKinds().
@@ -2546,6 +2599,26 @@ class User {
                return $this->mDatePreference;
        }
 
+       /**
+        * Determine based on the wiki configuration and the user's options,
+        * whether this user must be over HTTPS no matter what.
+        *
+        * @return bool
+        */
+       public function requiresHTTPS() {
+               global $wgSecureLogin;
+               if ( !$wgSecureLogin ) {
+                       return false;
+               } else {
+                       $https = $this->getBoolOption( 'prefershttps' );
+                       wfRunHooks( 'UserRequiresHTTPS', array( $this, &$https ) );
+                       if ( $https ) {
+                               $https = wfCanIPUseHTTPS( $this->getRequest()->getIP() );
+                       }
+                       return $https;
+               }
+       }
+
        /**
         * Get the user preferred stub threshold
         *
@@ -2665,7 +2738,7 @@ class User {
 
        /**
         * Get the user's edit count.
-        * @return int
+        * @return int, null for anonymous users
         */
        public function getEditCount() {
                if ( !$this->getId() ) {
@@ -2687,10 +2760,10 @@ class User {
                                // it has not been initialized. do so.
                                $count = $this->initEditCount();
                        }
-                       $this->mEditCount = intval( $count );
+                       $this->mEditCount = $count;
                        wfProfileOut( __METHOD__ );
                }
-               return $this->mEditCount;
+               return (int) $this->mEditCount;
        }
 
        /**
@@ -3063,7 +3136,9 @@ class User {
         *  null (default): Use the default ($wgCookieSecure) to set the secure attribute
         */
        protected function setCookie( $name, $value, $exp = 0, $secure = null ) {
-               $this->getRequest()->response()->setcookie( $name, $value, $exp, null, null, $secure );
+               $this->getRequest()->response()->setcookie( $name, $value, $exp, array(
+                       'secure' => $secure,
+               ) );
        }
 
        /**
@@ -3131,7 +3206,7 @@ class User {
                 * will cause the site to redirect the user to HTTPS, if they access
                 * it over HTTP. Bug 29898.
                 */
-               if ( $request->getCheck( 'wpStickHTTPS' ) ) {
+               if ( $request->getCheck( 'wpStickHTTPS' ) || $this->requiresHTTPS() ) {
                        $this->setCookie( 'forceHTTPS', 'true', time() + 2592000, false ); //30 days
                }
        }
@@ -3684,6 +3759,8 @@ class User {
                } elseif ( $type === true ) {
                        $message = 'confirmemail_body_changed';
                } else {
+                       // Give grep a chance to find the usages:
+                       // confirmemail_body_changed, confirmemail_body_set
                        $message = 'confirmemail_body_' . $type;
                }