Merge "mw.loader: Remove Deferred overhead from execute() hot code path"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 14 Sep 2017 17:03:01 +0000 (17:03 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 14 Sep 2017 17:03:01 +0000 (17:03 +0000)
17 files changed:
RELEASE-NOTES-1.30
includes/DefaultSettings.php
includes/MediaWiki.php
includes/Preferences.php
includes/api/ApiEmailUser.php
includes/changes/ChangesList.php
includes/jobqueue/JobQueueMemory.php
includes/jobqueue/jobs/HTMLCacheUpdateJob.php
includes/skins/Skin.php
includes/specials/SpecialEmailuser.php
includes/user/CentralIdLookup.php
includes/user/User.php
languages/i18n/en.json
languages/i18n/qqq.json
maintenance/populateIpChanges.php
resources/src/mediawiki.rcfilters/styles/mw.rcfilters.ui.FilterTagMultiselectWidget.less
tests/phpunit/includes/SiteStatsTest.php

index 67a449a..8517a8f 100644 (file)
@@ -70,6 +70,9 @@ section).
 ** This is currently gated by $wgCommentTableSchemaMigrationStage. Most wikis
    can set this to MIGRATION_NEW and run maintenance/migrateComments.php as
    soon as any necessary extensions are updated.
+* (T138166) Added ability for users to prohibit other users from sending them
+  emails with Special:Emailuser. Can be enabled by setting
+  $wgEnableUserEmailBlacklist to true.
 
 === External library changes in 1.30 ===
 
@@ -216,6 +219,8 @@ changes to languages because of Phabricator reports.
 * wfUsePHP() is deprecated.
 * wfFixSessionID() was removed.
 * wfShellExec() and related functions are deprecated, use Shell::command().
+* (T138166) SpecialEmailUser::getTarget() now requires a second argument, the sending
+  user object. Using the method without the second argument is deprecated.
 
 == Compatibility ==
 MediaWiki 1.30 requires PHP 5.5.9 or later. There is experimental support for
index f9d2879..852cd08 100644 (file)
@@ -1603,6 +1603,13 @@ $wgEnableEmail = true;
  */
 $wgEnableUserEmail = true;
 
+/**
+ * Set to true to enable user-to-user e-mail blacklist.
+ *
+ * @since 1.30
+ */
+$wgEnableUserEmailBlacklist = false;
+
 /**
  * If true put the sending user's email in a Reply-To header
  * instead of From (false). ($wgPasswordSender will be used as From.)
index 7b59ee9..0f40c19 100644 (file)
@@ -712,10 +712,11 @@ class MediaWiki {
                        MWExceptionHandler::rollbackMasterChangesAndLog( $e );
                }
 
+               $blocksHttpClient = true;
                // Defer everything else if possible...
-               $callback = function () use ( $mode ) {
+               $callback = function () use ( $mode, &$blocksHttpClient ) {
                        try {
-                               $this->restInPeace( $mode );
+                               $this->restInPeace( $mode, $blocksHttpClient );
                        } catch ( Exception $e ) {
                                // If this is post-send, then displaying errors can cause broken HTML
                                MWExceptionHandler::rollbackMasterChangesAndLog( $e );
@@ -725,9 +726,11 @@ class MediaWiki {
                if ( function_exists( 'register_postsend_function' ) ) {
                        // https://github.com/facebook/hhvm/issues/1230
                        register_postsend_function( $callback );
+                       $blocksHttpClient = false;
                } else {
                        if ( function_exists( 'fastcgi_finish_request' ) ) {
                                fastcgi_finish_request();
+                               $blocksHttpClient = false;
                        } else {
                                // Either all DB and deferred updates should happen or none.
                                // The latter should not be cancelled due to client disconnect.
@@ -870,8 +873,9 @@ class MediaWiki {
        /**
         * Ends this task peacefully
         * @param string $mode Use 'fast' to always skip job running
+        * @param bool $blocksHttpClient Whether this blocks an HTTP response to a client
         */
-       public function restInPeace( $mode = 'fast' ) {
+       public function restInPeace( $mode = 'fast', $blocksHttpClient = true ) {
                $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
                // Assure deferred updates are not in the main transaction
                $lbFactory->commitMasterChanges( __METHOD__ );
@@ -887,8 +891,8 @@ class MediaWiki {
                // Important: this must be the last deferred update added (T100085, T154425)
                DeferredUpdates::addCallableUpdate( [ JobQueueGroup::class, 'pushLazyJobs' ] );
 
-               // Do any deferred jobs
-               DeferredUpdates::doUpdates( 'enqueue' );
+               // Do any deferred jobs; preferring to run them now if a client will not wait on them
+               DeferredUpdates::doUpdates( $blocksHttpClient ? 'enqueue' : 'run' );
 
                // Now that everything specific to this request is done,
                // try to occasionally run jobs (if enabled) from the queues
index 9349527..0a9d701 100644 (file)
@@ -554,6 +554,22 @@ class Preferences {
                                        'label-message' => 'tog-ccmeonemails',
                                        'disabled' => $disableEmailPrefs,
                                ];
+
+                               if ( $config->get( 'EnableUserEmailBlacklist' )
+                                        && !$disableEmailPrefs
+                                        && !(bool)$user->getOption( 'disablemail' )
+                               ) {
+                                       $lookup = CentralIdLookup::factory();
+                                       $ids = $user->getOption( 'email-blacklist', [] );
+                                       $names = $ids ? $lookup->namesFromCentralIds( $ids, $user ) : [];
+
+                                       $defaultPreferences['email-blacklist'] = [
+                                               'type' => 'usersmultiselect',
+                                               'label-message' => 'email-blacklist-label',
+                                               'section' => 'personal/email',
+                                               'default' => implode( "\n", $names ),
+                                       ];
+                               }
                        }
 
                        if ( $config->get( 'EnotifWatchlist' ) ) {
index 4b4b76b..edea266 100644 (file)
@@ -34,7 +34,7 @@ class ApiEmailUser extends ApiBase {
                $params = $this->extractRequestParams();
 
                // Validate target
-               $targetUser = SpecialEmailUser::getTarget( $params['target'] );
+               $targetUser = SpecialEmailUser::getTarget( $params['target'], $this->getUser() );
                if ( !( $targetUser instanceof User ) ) {
                        switch ( $targetUser ) {
                                case 'notarget':
index cac4769..bc50096 100644 (file)
@@ -182,9 +182,9 @@ class ChangesList extends ContextSource {
                        $classes[] = self::CSS_CLASS_PREFIX . 'edit';
                        $classes[] = Sanitizer::escapeClass( self::CSS_CLASS_PREFIX . 'ns' .
                                $rc->mAttribs['rc_namespace'] . '-' . $rc->mAttribs['rc_title'] );
-                       $classes[] = Sanitizer::escapeClass( self::CSS_CLASS_PREFIX . 'ns-' .
-                               $rc->mAttribs['rc_namespace'] );
                }
+               $classes[] = Sanitizer::escapeClass( self::CSS_CLASS_PREFIX . 'ns-' .
+                       $rc->mAttribs['rc_namespace'] );
 
                // Indicate watched status on the line to allow for more
                // comprehensive styling.
index 649e2af..f9e2c3d 100644 (file)
@@ -177,7 +177,7 @@ class JobQueueMemory extends JobQueue {
                return new MappedIterator(
                        $unclaimed,
                        function ( $value ) {
-                               $this->jobFromSpecInternal( $value );
+                               return $this->jobFromSpecInternal( $value );
                        }
                );
        }
@@ -196,7 +196,7 @@ class JobQueueMemory extends JobQueue {
                return new MappedIterator(
                        $claimed,
                        function ( $value ) {
-                               $this->jobFromSpecInternal( $value );
+                               return $this->jobFromSpecInternal( $value );
                        }
                );
        }
index 4c16d7f..0aa33ca 100644 (file)
@@ -38,8 +38,15 @@ use MediaWiki\MediaWikiServices;
 class HTMLCacheUpdateJob extends Job {
        function __construct( Title $title, array $params ) {
                parent::__construct( 'htmlCacheUpdate', $title, $params );
-               // Base backlink purge jobs can be de-duplicated
-               $this->removeDuplicates = ( !isset( $params['range'] ) && !isset( $params['pages'] ) );
+               // Avoid the overhead of de-duplication when it would be pointless.
+               // Note that these jobs always set page_touched to the current time,
+               // so letting the older existing job "win" is still correct.
+               $this->removeDuplicates = (
+                       // Ranges rarely will line up
+                       !isset( $params['range'] ) &&
+                       // Multiple pages per job make matches unlikely
+                       !( isset( $params['pages'] ) && count( $params['pages'] ) != 1 )
+               );
        }
 
        /**
index 54bba30..8fb0d1c 100644 (file)
@@ -1057,10 +1057,10 @@ abstract class Skin extends ContextSource {
                        $targetUser = User::newFromId( $id );
                }
 
-               # The sending user must have a confirmed email address and the target
-               # user must have a confirmed email address and allow emails from users.
-               return $this->getUser()->canSendEmail() &&
-                       $targetUser->canReceiveEmail();
+               # The sending user must have a confirmed email address and the receiving
+               # user must accept emails from the sender.
+               return $this->getUser()->canSendEmail()
+                       && SpecialEmailUser::validateTarget( $targetUser, $this->getUser() ) === '';
        }
 
        /**
index 830b438..249be7f 100644 (file)
@@ -44,7 +44,7 @@ class SpecialEmailUser extends UnlistedSpecialPage {
        }
 
        public function getDescription() {
-               $target = self::getTarget( $this->mTarget );
+               $target = self::getTarget( $this->mTarget, $this->getUser() );
                if ( !$target instanceof User ) {
                        return $this->msg( 'emailuser-title-notarget' )->text();
                }
@@ -142,7 +142,7 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                                throw new ErrorPageError( $title, $msg, $params );
                }
                // Got a valid target user name? Else ask for one.
-               $ret = self::getTarget( $this->mTarget );
+               $ret = self::getTarget( $this->mTarget, $this->getUser() );
                if ( !$ret instanceof User ) {
                        if ( $this->mTarget != '' ) {
                                // Messages used here: notargettext, noemailtext, nowikiemailtext
@@ -187,9 +187,14 @@ class SpecialEmailUser extends UnlistedSpecialPage {
         * Validate target User
         *
         * @param string $target Target user name
-        * @return User User object on success or a string on error
+        * @param User|null $sender User sending the email
+        * @return User|string User object on success or a string on error
         */
-       public static function getTarget( $target ) {
+       public static function getTarget( $target, User $sender = null ) {
+               if ( $sender === null ) {
+                       wfDeprecated( __METHOD__ . ' without specifying the sending user', '1.30' );
+               }
+
                if ( $target == '' ) {
                        wfDebug( "Target is empty.\n" );
 
@@ -197,21 +202,50 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                }
 
                $nu = User::newFromName( $target );
-               if ( !$nu instanceof User || !$nu->getId() ) {
+               $error = self::validateTarget( $nu, $sender );
+
+               return $error ? $error : $nu;
+       }
+
+       /**
+        * Validate target User
+        *
+        * @param User $target Target user
+        * @param User|null $sender User sending the email
+        * @return string Error message or empty string if valid.
+        * @since 1.30
+        */
+       public static function validateTarget( $target, User $sender = null ) {
+               if ( $sender === null ) {
+                       wfDeprecated( __METHOD__ . ' without specifying the sending user', '1.30' );
+               }
+
+               if ( !$target instanceof User || !$target->getId() ) {
                        wfDebug( "Target is invalid user.\n" );
 
                        return 'notarget';
-               } elseif ( !$nu->isEmailConfirmed() ) {
+               } elseif ( !$target->isEmailConfirmed() ) {
                        wfDebug( "User has no valid email.\n" );
 
                        return 'noemail';
-               } elseif ( !$nu->canReceiveEmail() ) {
+               } elseif ( !$target->canReceiveEmail() ) {
                        wfDebug( "User does not allow user emails.\n" );
 
                        return 'nowikiemail';
+               } elseif ( $sender !== null ) {
+                       $blacklist = $target->getOption( 'email-blacklist', [] );
+                       if ( $blacklist ) {
+                               $lookup = CentralIdLookup::factory();
+                               $senderId = $lookup->centralIdFromLocalUser( $sender );
+                               if ( $senderId !== 0 && in_array( $senderId, $blacklist ) ) {
+                                       wfDebug( "User does not allow user emails from this user.\n" );
+
+                                       return 'nowikiemail';
+                               }
+                       }
                }
 
-               return $nu;
+               return '';
        }
 
        /**
@@ -326,7 +360,7 @@ class SpecialEmailUser extends UnlistedSpecialPage {
        public static function submit( array $data, IContextSource $context ) {
                $config = $context->getConfig();
 
-               $target = self::getTarget( $data['Target'] );
+               $target = self::getTarget( $data['Target'], $context->getUser() );
                if ( !$target instanceof User ) {
                        // Messages used here: notargettext, noemailtext, nowikiemailtext
                        return Status::newFatal( $target . 'text' );
index 2ced6e2..618b7f0 100644 (file)
@@ -157,6 +157,27 @@ abstract class CentralIdLookup implements IDBAccessObject {
                return $idToName[$id];
        }
 
+       /**
+        * Given a an array of central user IDs, return the (local) user names.
+        * @param int[] $ids Central user IDs
+        * @param int|User $audience One of the audience constants, or a specific user
+        * @param int $flags IDBAccessObject read flags
+        * @return string[] User names
+        * @since 1.30
+        */
+       public function namesFromCentralIds(
+               array $ids, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
+       ) {
+               $idToName = array_fill_keys( $ids, false );
+               $names = $this->lookupCentralIds( $idToName, $audience, $flags );
+               $names = array_unique( $names );
+               $names = array_filter( $names, function ( $name ) {
+                       return $name !== false && $name !== '';
+               } );
+
+               return array_values( $names );
+       }
+
        /**
         * Given a (local) user name, return the central ID
         * @note There's no requirement that the user name actually exists locally,
@@ -174,6 +195,27 @@ abstract class CentralIdLookup implements IDBAccessObject {
                return $nameToId[$name];
        }
 
+       /**
+        * Given an array of (local) user names, return the central IDs.
+        * @param string[] $names Canonicalized user names
+        * @param int|User $audience One of the audience constants, or a specific user
+        * @param int $flags IDBAccessObject read flags
+        * @return int[] User IDs
+        * @since 1.30
+        */
+       public function centralIdsFromNames(
+               array $names, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
+       ) {
+               $nameToId = array_fill_keys( $names, false );
+               $ids = $this->lookupUserNames( $nameToId, $audience, $flags );
+               $ids = array_unique( $ids );
+               $ids = array_filter( $ids, function ( $id ) {
+                       return $id !== false;
+               } );
+
+               return array_values( $ids );
+       }
+
        /**
         * Given a central user ID, return a local User object
         * @note Unlike nameFromCentralId(), this does guarantee that the local
index 0c39610..6115144 100644 (file)
@@ -5317,6 +5317,13 @@ class User implements IDBAccessObject {
                                        $data[$row->up_property] = $row->up_value;
                                }
                        }
+
+                       // Convert the email blacklist from a new line delimited string
+                       // to an array of ids.
+                       if ( isset( $data['email-blacklist'] ) && $data['email-blacklist'] ) {
+                               $data['email-blacklist'] = array_map( 'intval', explode( "\n", $data['email-blacklist'] ) );
+                       }
+
                        foreach ( $data as $property => $value ) {
                                $this->mOptionOverrides[$property] = $value;
                                $this->mOptions[$property] = $value;
@@ -5339,6 +5346,26 @@ class User implements IDBAccessObject {
                // Not using getOptions(), to keep hidden preferences in database
                $saveOptions = $this->mOptions;
 
+               // Convert usernames to ids.
+               if ( isset( $this->mOptions['email-blacklist'] ) ) {
+                       if ( $this->mOptions['email-blacklist'] ) {
+                               $value = $this->mOptions['email-blacklist'];
+                               // Email Blacklist may be an array of ids or a string of new line
+                               // delimnated user names.
+                               if ( is_array( $value ) ) {
+                                       $ids = array_filter( $value, 'is_numeric' );
+                               } else {
+                                       $lookup = CentralIdLookup::factory();
+                                       $ids = $lookup->centralIdsFromNames( explode( "\n", $value ), $this );
+                               }
+                               $this->mOptions['email-blacklist'] = $ids;
+                               $saveOptions['email-blacklist'] = implode( "\n", $this->mOptions['email-blacklist'] );
+                       } else {
+                               // If the blacklist is empty, set it to null rather than an empty string.
+                               $this->mOptions['email-blacklist'] = null;
+                       }
+               }
+
                // Allow hooks to abort, for instance to save to a global profile.
                // Reset options to default state before saving.
                if ( !Hooks::run( 'UserSaveOptions', [ $this, &$saveOptions ] ) ) {
index 5dd8345..24e1818 100644 (file)
        "timezoneregion-indian": "Indian Ocean",
        "timezoneregion-pacific": "Pacific Ocean",
        "allowemail": "Enable email from other users",
+       "email-blacklist-label": "Prohibit these users from sending emails to me:",
        "prefs-searchoptions": "Search",
        "prefs-namespaces": "Namespaces",
        "default": "default",
index 9d2e77d..208c7c3 100644 (file)
        "timezoneregion-indian": "Used in \"Time zone\" listbox in [[Special:Preferences#mw-prefsection-datetime|preferences]], \"date and time\" tab.\n{{Related|Timezoneregion}}",
        "timezoneregion-pacific": "Used in \"Time zone\" listbox in [[Special:Preferences#mw-prefsection-datetime|preferences]], \"date and time\" tab.\n{{Related|Timezoneregion}}",
        "allowemail": "Used in [[Special:Preferences]] > {{int:prefs-personal}} > {{int:email}}.",
+       "email-blacklist-label": "Used in [[Special:Preferences]] > {{int:prefs-prohibit}} > {{int:email}}.",
        "prefs-searchoptions": "{{Identical|Search}}",
        "prefs-namespaces": "Shown as legend of the second fieldset of the tab 'Search' in [[Special:Preferences]]\n{{Identical|Namespace}}",
        "default": "{{Identical|Default}}",
index e086c5e..ffb8c43 100644 (file)
@@ -82,19 +82,15 @@ TEXT
                        $this->output( "...checking $this->mBatchSize revisions for IP edits that need copying, " .
                                "starting with rev_id $blockStart\n" );
 
+                       $insertRows = [];
                        foreach ( $rows as $row ) {
                                // Double-check to make sure this is an IP, e.g. not maintenance user or imported revision.
                                if ( IP::isValid( $row->rev_user_text ) ) {
-                                       $dbw->insert(
-                                               'ip_changes',
-                                               [
-                                                       'ipc_rev_id' => $row->rev_id,
-                                                       'ipc_rev_timestamp' => $row->rev_timestamp,
-                                                       'ipc_hex' => IP::toHex( $row->rev_user_text ),
-                                               ],
-                                               __METHOD__,
-                                               'IGNORE'
-                                       );
+                                       $insertRows[] = [
+                                               'ipc_rev_id' => $row->rev_id,
+                                               'ipc_rev_timestamp' => $row->rev_timestamp,
+                                               'ipc_hex' => IP::toHex( $row->rev_user_text ),
+                                       ];
 
                                        $revCount++;
                                }
@@ -104,6 +100,13 @@ TEXT
 
                        $blockStart++;
 
+                       $dbw->insert(
+                               'ip_changes',
+                               $insertRows,
+                               __METHOD__,
+                               'IGNORE'
+                       );
+
                        $lbFactory->waitForReplication();
                        usleep( $throttle * 1000 );
                }
index f3e1351..e7233a8 100644 (file)
@@ -17,6 +17,7 @@
                border-radius: 2px 2px 0 0;
                padding: 0.6em;
                margin-top: 1em;
+               line-height: normal;
        }
 
        .oo-ui-tagMultiselectWidget.oo-ui-widget-enabled &-animate.oo-ui-tagMultiselectWidget-handle {
@@ -41,6 +42,7 @@
                        &-savedQueryTitle {
                                color: #222; // Base10
                                font-weight: bold;
+                               vertical-align: top;
                                margin-left: 1em;
                                width: ~'calc( 100% - 10em )';
                                overflow: hidden;
index ea476a7..cdbf9fd 100644 (file)
@@ -11,6 +11,11 @@ class SiteStatsTest extends MediaWikiTestCase {
                $cache = \MediaWiki\MediaWikiServices::getInstance()->getMainWANObjectCache();
                $jobq = JobQueueGroup::singleton();
 
+               // Delete EditPage jobs that might have been left behind by other tests
+               $jobq->get( 'htmlCacheUpdate' )->delete();
+               $jobq->get( 'recentChangesUpdate' )->delete();
+               $cache->delete( $cache->makeKey( 'SiteStats', 'jobscount' ) );
+
                $jobq->push( new NullJob( Title::newMainPage(), [] ) );
                $this->assertEquals( 1, SiteStats::jobs(),
                         'A single job enqueued bumps jobscount stat to 1' );