Make Special:ConfirmEmail load the user from the master
authorAaron Schulz <aschulz@wikimedia.org>
Mon, 3 Aug 2015 23:20:39 +0000 (16:20 -0700)
committerLegoktm <legoktm.wikipedia@gmail.com>
Tue, 4 Aug 2015 18:40:38 +0000 (18:40 +0000)
* This can help guard against stale reads if the user was
  created or changed a second ago.

Bug: T105896
Change-Id: Ib2a59762cd8f4a4b7ad86d0700f186bee1d5b2d1

includes/User.php
includes/specials/SpecialConfirmemail.php

index cefbe62..665a689 100644 (file)
@@ -519,19 +519,24 @@ class User implements IDBAccessObject {
         * If the code is invalid or has expired, returns NULL.
         *
         * @param string $code Confirmation code
+        * @param int $flags User::READ_* bitfield
         * @return User|null
         */
-       public static function newFromConfirmationCode( $code ) {
-               $dbr = wfGetDB( DB_SLAVE );
-               $id = $dbr->selectField( 'user', 'user_id', array(
-                       'user_email_token' => md5( $code ),
-                       'user_email_token_expires > ' . $dbr->addQuotes( $dbr->timestamp() ),
-                       ) );
-               if ( $id !== false ) {
-                       return User::newFromId( $id );
-               } else {
-                       return null;
-               }
+       public static function newFromConfirmationCode( $code, $flags = 0 ) {
+               $db = ( $flags & self::READ_LATEST ) == self::READ_LATEST
+                       ? wfGetDB( DB_MASTER )
+                       : wfGetDB( DB_SLAVE );
+
+               $id = $db->selectField(
+                       'user',
+                       'user_id',
+                       array(
+                               'user_email_token' => md5( $code ),
+                               'user_email_token_expires > ' . $db->addQuotes( $db->timestamp() ),
+                       )
+               );
+
+               return $id ? User::newFromId( $id ) : null;
        }
 
        /**
index b6ab112..6356155 100644 (file)
@@ -120,7 +120,7 @@ class EmailConfirmation extends UnlistedSpecialPage {
         * @param string $code Confirmation code
         */
        function attemptConfirm( $code ) {
-               $user = User::newFromConfirmationCode( $code );
+               $user = User::newFromConfirmationCode( $code, User::READ_LATEST );
                if ( !is_object( $user ) ) {
                        $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
 
@@ -164,7 +164,7 @@ class EmailInvalidation extends UnlistedSpecialPage {
         * @param string $code Confirmation code
         */
        function attemptInvalidate( $code ) {
-               $user = User::newFromConfirmationCode( $code );
+               $user = User::newFromConfirmationCode( $code, User::READ_LATEST );
                if ( !is_object( $user ) ) {
                        $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );