Merge "Replace rc_patrolled values with contants, part I"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 12 Apr 2018 20:56:54 +0000 (20:56 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 12 Apr 2018 20:56:54 +0000 (20:56 +0000)
52 files changed:
RELEASE-NOTES-1.31
includes/DefaultSettings.php
includes/OutputHandler.php
includes/Storage/NameTableStore.php
includes/api/ApiParse.php
includes/api/ApiUserrights.php
includes/filerepo/file/LocalFile.php
includes/jobqueue/JobQueueDB.php
includes/libs/rdbms/database/DatabaseMysqlBase.php
includes/libs/rdbms/database/IDatabase.php
includes/libs/rdbms/database/position/MySQLMasterPos.php
includes/libs/rdbms/lbfactory/LBFactory.php
includes/libs/rdbms/loadbalancer/ILoadBalancer.php
includes/libs/rdbms/loadbalancer/LoadBalancer.php
includes/objectcache/SqlBagOStuff.php
includes/page/WikiPage.php
includes/parser/MWTidy.php
includes/specialpage/ChangesListSpecialPage.php
includes/specials/SpecialRecentchanges.php
includes/specials/SpecialWatchlist.php
includes/tidy/TidyDriverBase.php
includes/user/User.php
languages/i18n/en.json
languages/i18n/qqq.json
maintenance/Maintenance.php
mw-config/config.css
resources/src/jquery/jquery.makeCollapsible.css
resources/src/jquery/jquery.makeCollapsible.js
resources/src/mediawiki.rcfilters/styles/mw.rcfilters.ui.FilterMenuHeaderWidget.less
resources/src/mediawiki.rcfilters/styles/mw.rcfilters.ui.FilterMenuOptionWidget.less
resources/src/mediawiki.rcfilters/styles/mw.rcfilters.ui.FilterMenuSectionOptionWidget.less
resources/src/mediawiki.rcfilters/styles/mw.rcfilters.ui.ItemMenuOptionWidget.less
resources/src/mediawiki.rcfilters/styles/mw.rcfilters.ui.MenuSelectWidget.less
resources/src/mediawiki.rcfilters/styles/mw.rcfilters.variables.less
resources/src/mediawiki.ui/components/icons.less
resources/src/mediawiki.widgets/mw.widgets.TitleOptionWidget.js
tests/parser/ParserTestRunner.php
tests/phpunit/MediaWikiTestCase.php
tests/phpunit/includes/TestUserRegistry.php
tests/phpunit/includes/api/ApiMainTest.php
tests/phpunit/includes/api/ApiParseTest.php
tests/phpunit/includes/api/ApiTestCase.php
tests/phpunit/includes/api/ApiUserrightsTest.php [new file with mode: 0644]
tests/phpunit/includes/auth/AuthManagerTest.php
tests/phpunit/includes/db/LoadBalancerTest.php
tests/phpunit/includes/debug/logger/monolog/KafkaHandlerTest.php
tests/phpunit/includes/jobqueue/JobQueueTest.php
tests/phpunit/includes/libs/rdbms/database/DBConnRefTest.php
tests/phpunit/includes/libs/rdbms/database/DatabaseMysqlBaseTest.php
tests/phpunit/includes/registration/VersionCheckerTest.php
tests/phpunit/includes/specialpage/ChangesListSpecialPageTest.php
tests/phpunit/includes/utils/BatchRowUpdateTest.php

index ebd9787..ea3aa8b 100644 (file)
@@ -32,6 +32,7 @@ production.
   performance reasons, and installations with this setting will now work as if it
   was configured with 'any'.
 * $wgLogAutopatrol now defaults to false instead of true.
+* $wgValidateAllHtml was removed and will be ignored.
 
 === New features in 1.31 ===
 * (T76554) User sub-pages named ….json are now protected in the same way that ….js
@@ -239,6 +240,11 @@ changes to languages because of Phabricator reports.
 * The ResourceLoaderGetLessVars hook, deprecated in 1.30, has been removed.
   Use ResourceLoaderModule::getLessVars() to expose local variables instead
   of global ones.
+* As part of work to modernise user-generated content clean-up, a config option and some
+  methods related to HTML validity were removed without deprecation. The public methods
+  MWTidy::checkErrors() and its callee TidyDriverBase::validate() are removed, as are
+  MediaWikiTestCase::assertValidHtmlSnippet() and ::assertValidHtmlDocument(). The
+  $wgValidateAllHtml configuration option is removed and will be ignored.
 
 === Deprecations in 1.31 ===
 * The Revision class was deprecated in favor of RevisionStore, BlobStore, and
@@ -325,6 +331,8 @@ changes to languages because of Phabricator reports.
 * The type string for the parameter $lang of DateFormatter::getInstance is
   deprecated.
 * Wikimedia\Rdbms\SavepointPostgres is deprecated.
+* The DO_MAINTENANCE constant is deprecated. RUN_MAINTENANCE_IF_MAIN should be
+  used instead.
 
 === Other changes in 1.31 ===
 * Browser support for Internet Explorer 10 was lowered from Grade A to Grade C.
index c000098..22f587e 100644 (file)
@@ -3265,12 +3265,6 @@ $wgSiteNotice = '';
  */
 $wgSiteSupportPage = '';
 
-/**
- * Validate the overall output using tidy and refuse
- * to display the page if it's not valid.
- */
-$wgValidateAllHtml = false;
-
 /**
  * Default skin, for new users and anonymous visitors. Registered users may
  * change this to any one of the other available skins in their preferences.
index 842922d..16c3784 100644 (file)
@@ -22,9 +22,6 @@
 
 namespace MediaWiki;
 
-use MWTidy;
-use Html;
-
 /**
  * @since 1.31
  */
@@ -36,31 +33,10 @@ class OutputHandler {
         * @return string
         */
        public static function handle( $s ) {
-               global $wgDisableOutputCompression, $wgValidateAllHtml, $wgMangleFlashPolicy;
+               global $wgDisableOutputCompression, $wgMangleFlashPolicy;
                if ( $wgMangleFlashPolicy ) {
                        $s = self::mangleFlashPolicy( $s );
                }
-               if ( $wgValidateAllHtml ) {
-                       $headers = headers_list();
-                       $isHTML = false;
-                       foreach ( $headers as $header ) {
-                               $parts = explode( ':', $header, 2 );
-                               if ( count( $parts ) !== 2 ) {
-                                       continue;
-                               }
-                               $name = strtolower( trim( $parts[0] ) );
-                               $value = trim( $parts[1] );
-                               if ( $name == 'content-type' && ( strpos( $value, 'text/html' ) === 0
-                                       || strpos( $value, 'application/xhtml+xml' ) === 0 )
-                               ) {
-                                       $isHTML = true;
-                                       break;
-                               }
-                       }
-                       if ( $isHTML ) {
-                               $s = self::validateAllHtml( $s );
-                       }
-               }
                if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
                        if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
                                $s = self::handleGzip( $s );
@@ -183,65 +159,4 @@ class OutputHandler {
                        header( "Content-Length: $length" );
                }
        }
-
-       /**
-        * Replace the output with an error if the HTML is not valid.
-        *
-        * @param string $s
-        * @return string
-        */
-       private static function validateAllHtml( $s ) {
-               $errors = '';
-               if ( MWTidy::checkErrors( $s, $errors ) ) {
-                       return $s;
-               }
-
-               header( 'Cache-Control: no-cache' );
-
-               $out = Html::element( 'h1', null, 'HTML validation error' );
-               $out .= Html::openElement( 'ul' );
-
-               $error = strtok( $errors, "\n" );
-               $badLines = [];
-               while ( $error !== false ) {
-                       if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
-                               $lineNum = intval( $m[1] );
-                               $badLines[$lineNum] = true;
-                               $out .= Html::rawElement( 'li', null,
-                                       Html::element( 'a', [ 'href' => "#line-{$lineNum}" ], $error ) ) . "\n";
-                       }
-                       $error = strtok( "\n" );
-               }
-
-               $out .= Html::closeElement( 'ul' );
-               $out .= Html::element( 'pre', null, $errors );
-               $out .= Html::openElement( 'ol' ) . "\n";
-               $line = strtok( $s, "\n" );
-               $i = 1;
-               while ( $line !== false ) {
-                       $attrs = [];
-                       if ( isset( $badLines[$i] ) ) {
-                               $attrs['class'] = 'highlight';
-                               $attrs['id'] = "line-$i";
-                       }
-                       $out .= Html::element( 'li', $attrs, $line ) . "\n";
-                       $line = strtok( "\n" );
-                       $i++;
-               }
-               $out .= Html::closeElement( 'ol' );
-
-               $style = <<<CSS
-       .highlight { background-color: #ffc }
-       li { white-space: pre }
-CSS;
-
-               $out = Html::htmlHeader( [ 'lang' => 'en', 'dir' => 'ltr' ] ) .
-                       Html::rawElement( 'head', null,
-                               Html::element( 'title', null, 'HTML validation error' ) .
-                               Html::inlineStyle( $style ) ) .
-                       Html::rawElement( 'body', null, $out ) .
-                       Html::closeElement( 'html' );
-
-               return $out;
-       }
 }
index a1eba74..465f299 100644 (file)
@@ -138,7 +138,7 @@ class NameTableStore {
                                // RACE: $name was already in the db, probably just inserted, so load from master
                                // Use DBO_TRX to avoid missing inserts due to other threads or REPEATABLE-READs
                                $table = $this->loadTable(
-                                       $this->getDBConnection( DB_MASTER, LoadBalancer::CONN_TRX_AUTO )
+                                       $this->getDBConnection( DB_MASTER, LoadBalancer::CONN_TRX_AUTOCOMMIT )
                                );
                                $searchResult = array_search( $name, $table, true );
                                if ( $searchResult === false ) {
index cbd62a9..099d278 100644 (file)
@@ -243,12 +243,6 @@ class ApiParse extends ApiBase {
                        if ( $params['onlypst'] ) {
                                // Build a result and bail out
                                $result_array = [];
-                               if ( $this->contentIsDeleted ) {
-                                       $result_array['textdeleted'] = true;
-                               }
-                               if ( $this->contentIsSuppressed ) {
-                                       $result_array['textsuppressed'] = true;
-                               }
                                $result_array['text'] = $this->pstContent->serialize( $format );
                                $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'text';
                                if ( isset( $prop['wikitext'] ) ) {
@@ -400,8 +394,8 @@ class ApiParse extends ApiBase {
                }
 
                if ( isset( $prop['displaytitle'] ) ) {
-                       $result_array['displaytitle'] = $p_result->getDisplayTitle() ?:
-                               $titleObj->getPrefixedText();
+                       $result_array['displaytitle'] = $p_result->getDisplayTitle() !== false
+                               ? $p_result->getDisplayTitle() : $titleObj->getPrefixedText();
                }
 
                if ( isset( $prop['headitems'] ) ) {
@@ -490,12 +484,7 @@ class ApiParse extends ApiBase {
                        }
 
                        $wgParser->startExternalParse( $titleObj, $popts, Parser::OT_PREPROCESS );
-                       $dom = $wgParser->preprocessToDom( $this->content->getNativeData() );
-                       if ( is_callable( [ $dom, 'saveXML' ] ) ) {
-                               $xml = $dom->saveXML();
-                       } else {
-                               $xml = $dom->__toString();
-                       }
+                       $xml = $wgParser->preprocessToDom( $this->content->getNativeData() )->__toString();
                        $result_array['parsetree'] = $xml;
                        $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'parsetree';
                }
@@ -578,7 +567,7 @@ class ApiParse extends ApiBase {
                        } else {
                                $this->content = $page->getContent( Revision::FOR_THIS_USER, $this->getUser() );
                                if ( !$this->content ) {
-                                       $this->dieWithError( [ 'apierror-missingcontent-pageid', $pageId ] );
+                                       $this->dieWithError( [ 'apierror-missingcontent-pageid', $page->getId() ] );
                                }
                        }
                        $this->contentIsDeleted = $isDeleted;
@@ -602,7 +591,7 @@ class ApiParse extends ApiBase {
                        $pout = $page->getParserOutput( $popts, $revId, $suppressCache );
                }
                if ( !$pout ) {
-                       $this->dieWithError( [ 'apierror-nosuchrevid', $revId ?: $page->getLatest() ] );
+                       $this->dieWithError( [ 'apierror-nosuchrevid', $revId ?: $page->getLatest() ] ); // @codeCoverageIgnore
                }
 
                return $pout;
index 3813aba..56c2c84 100644 (file)
@@ -58,14 +58,16 @@ class ApiUserrights extends ApiBase {
                $params = $this->extractRequestParams();
 
                // Figure out expiry times from the input
-               // $params['expiry'] may not be set in subclasses
+               // $params['expiry'] is not set in CentralAuth's ApiGlobalUserRights subclass
                if ( isset( $params['expiry'] ) ) {
                        $expiry = (array)$params['expiry'];
                } else {
                        $expiry = [ 'infinity' ];
                }
                $add = (array)$params['add'];
-               if ( count( $expiry ) !== count( $add ) ) {
+               if ( !$add ) {
+                       $expiry = [];
+               } elseif ( count( $expiry ) !== count( $add ) ) {
                        if ( count( $expiry ) === 1 ) {
                                $expiry = array_fill( 0, count( $add ), $expiry[0] );
                        } else {
@@ -186,6 +188,7 @@ class ApiUserrights extends ApiBase {
                                ApiBase::PARAM_ISMULTI => true
                        ],
                ];
+               // CentralAuth's ApiGlobalUserRights subclass can't handle expiries
                if ( !$this->getUserRightsPage()->canProcessExpiries() ) {
                        unset( $a['expiry'] );
                }
index 7fc45eb..0464f07 100644 (file)
@@ -3344,9 +3344,9 @@ class LocalFileMoveBatch {
                        __METHOD__,
                        [ 'FOR UPDATE' ]
                );
-               $oldRowCount = $dbw->selectField(
+               $oldRowCount = $dbw->selectRowCount(
                        'oldimage',
-                       'COUNT(*)',
+                       '*',
                        [ 'oi_name' => $this->oldName ],
                        __METHOD__,
                        [ 'FOR UPDATE' ]
index b68fdae..f01ba63 100644 (file)
@@ -190,7 +190,7 @@ class JobQueueDB extends JobQueue {
                // If the connection is busy with a transaction, then defer the job writes
                // until right before the main round commit step. Any errors that bubble
                // up will rollback the main commit round.
-               // b) mysql/postgres; DB connection is generally a separate CONN_TRX_AUTO handle.
+               // b) mysql/postgres; DB connection is generally a separate CONN_TRX_AUTOCOMMIT handle.
                // No transaction is active nor will be started by writes, so enqueue the jobs
                // now so that any errors will show up immediately as the interface expects. Any
                // errors that bubble up will rollback the main commit round.
@@ -780,7 +780,7 @@ class JobQueueDB extends JobQueue {
                return ( $lb->getServerType( $lb->getWriterIndex() ) !== 'sqlite' )
                        // Keep a separate connection to avoid contention and deadlocks;
                        // However, SQLite has the opposite behavior due to DB-level locking.
-                       ? $lb->getConnectionRef( $index, [], $this->wiki, $lb::CONN_TRX_AUTO )
+                       ? $lb->getConnectionRef( $index, [], $this->wiki, $lb::CONN_TRX_AUTOCOMMIT )
                        // Jobs insertion will be defered until the PRESEND stage to reduce contention.
                        : $lb->getConnectionRef( $index, [], $this->wiki );
        }
index 1624122..3e6190c 100644 (file)
@@ -933,18 +933,23 @@ abstract class DatabaseMysqlBase extends Database {
                        }
                        // Wait on the GTID set (MariaDB only)
                        $gtidArg = $this->addQuotes( implode( ',', $gtidsWait ) );
-                       $res = $this->doQuery( "SELECT MASTER_GTID_WAIT($gtidArg, $timeout)" );
+                       if ( strpos( $gtidArg, ':' ) !== false ) {
+                               // MySQL GTIDs, e.g "source_id:transaction_id"
+                               $res = $this->doQuery( "SELECT WAIT_FOR_EXECUTED_GTID_SET($gtidArg, $timeout)" );
+                       } else {
+                               // MariaDB GTIDs, e.g."domain:server:sequence"
+                               $res = $this->doQuery( "SELECT MASTER_GTID_WAIT($gtidArg, $timeout)" );
+                       }
                } else {
                        // Wait on the binlog coordinates
                        $encFile = $this->addQuotes( $pos->getLogFile() );
-                       $encPos = intval( $pos->pos[1] );
+                       $encPos = intval( $pos->getLogPosition()[$pos::CORD_EVENT] );
                        $res = $this->doQuery( "SELECT MASTER_POS_WAIT($encFile, $encPos, $timeout)" );
                }
 
                $row = $res ? $this->fetchRow( $res ) : false;
                if ( !$row ) {
-                       throw new DBExpectedError( $this,
-                               "MASTER_POS_WAIT() or MASTER_GTID_WAIT() failed: {$this->lastError()}" );
+                       throw new DBExpectedError( $this, "Replication wait failed: {$this->lastError()}" );
                }
 
                // Result can be NULL (error), -1 (timeout), or 0+ per the MySQL manual
@@ -976,21 +981,23 @@ abstract class DatabaseMysqlBase extends Database {
         * @return MySQLMasterPos|bool
         */
        public function getReplicaPos() {
-               $now = microtime( true );
-
-               if ( $this->useGTIDs ) {
-                       $res = $this->query( "SELECT @@global.gtid_slave_pos AS Value", __METHOD__ );
-                       $gtidRow = $this->fetchObject( $res );
-                       if ( $gtidRow && strlen( $gtidRow->Value ) ) {
-                               return new MySQLMasterPos( $gtidRow->Value, $now );
+               $now = microtime( true ); // as-of-time *before* fetching GTID variables
+
+               if ( $this->useGTIDs() ) {
+                       // Try to use GTIDs, fallbacking to binlog positions if not possible
+                       $data = $this->getServerGTIDs( __METHOD__ );
+                       // Use gtid_slave_pos for MariaDB and gtid_executed for MySQL
+                       foreach ( [ 'gtid_slave_pos', 'gtid_executed' ] as $name ) {
+                               if ( isset( $data[$name] ) && strlen( $data[$name] ) ) {
+                                       return new MySQLMasterPos( $data[$name], $now );
+                               }
                        }
                }
 
-               $res = $this->query( 'SHOW SLAVE STATUS', __METHOD__ );
-               $row = $this->fetchObject( $res );
-               if ( $row && strlen( $row->Relay_Master_Log_File ) ) {
+               $data = $this->getServerRoleStatus( 'SLAVE', __METHOD__ );
+               if ( $data && strlen( $data['Relay_Master_Log_File'] ) ) {
                        return new MySQLMasterPos(
-                               "{$row->Relay_Master_Log_File}/{$row->Exec_Master_Log_Pos}",
+                               "{$data['Relay_Master_Log_File']}/{$data['Exec_Master_Log_Pos']}",
                                $now
                        );
                }
@@ -1004,23 +1011,97 @@ abstract class DatabaseMysqlBase extends Database {
         * @return MySQLMasterPos|bool
         */
        public function getMasterPos() {
-               $now = microtime( true );
+               $now = microtime( true ); // as-of-time *before* fetching GTID variables
+
+               $pos = false;
+               if ( $this->useGTIDs() ) {
+                       // Try to use GTIDs, fallbacking to binlog positions if not possible
+                       $data = $this->getServerGTIDs( __METHOD__ );
+                       // Use gtid_binlog_pos for MariaDB and gtid_executed for MySQL
+                       foreach ( [ 'gtid_binlog_pos', 'gtid_executed' ] as $name ) {
+                               if ( isset( $data[$name] ) && strlen( $data[$name] ) ) {
+                                       $pos = new MySQLMasterPos( $data[$name], $now );
+                                       break;
+                               }
+                       }
+                       // Filter domains that are inactive or not relevant to the session
+                       if ( $pos ) {
+                               $pos->setActiveOriginServerId( $this->getServerId() );
+                               $pos->setActiveOriginServerUUID( $this->getServerUUID() );
+                               if ( isset( $data['gtid_domain_id'] ) ) {
+                                       $pos->setActiveDomain( $data['gtid_domain_id'] );
+                               }
+                       }
+               }
 
-               if ( $this->useGTIDs ) {
-                       $res = $this->query( "SELECT @@global.gtid_binlog_pos AS Value", __METHOD__ );
-                       $gtidRow = $this->fetchObject( $res );
-                       if ( $gtidRow && strlen( $gtidRow->Value ) ) {
-                               return new MySQLMasterPos( $gtidRow->Value, $now );
+               if ( !$pos ) {
+                       $data = $this->getServerRoleStatus( 'MASTER', __METHOD__ );
+                       if ( $data && strlen( $data['File'] ) ) {
+                               $pos = new MySQLMasterPos( "{$data['File']}/{$data['Position']}", $now );
                        }
                }
 
-               $res = $this->query( 'SHOW MASTER STATUS', __METHOD__ );
-               $row = $this->fetchObject( $res );
-               if ( $row && strlen( $row->File ) ) {
-                       return new MySQLMasterPos( "{$row->File}/{$row->Position}", $now );
+               return $pos;
+       }
+
+       /**
+        * @return int
+        * @throws DBQueryError If the variable doesn't exist for some reason
+        */
+       protected function getServerId() {
+               return $this->srvCache->getWithSetCallback(
+                       $this->srvCache->makeGlobalKey( 'mysql-server-id', $this->getServer() ),
+                       self::SERVER_ID_CACHE_TTL,
+                       function () {
+                               $res = $this->query( "SELECT @@server_id AS id", __METHOD__ );
+                               return intval( $this->fetchObject( $res )->id );
+                       }
+               );
+       }
+
+       /**
+        * @return string|null
+        */
+       protected function getServerUUID() {
+               return $this->srvCache->getWithSetCallback(
+                       $this->srvCache->makeGlobalKey( 'mysql-server-uuid', $this->getServer() ),
+                       self::SERVER_ID_CACHE_TTL,
+                       function () {
+                               $res = $this->query( "SHOW GLOBAL VARIABLES LIKE 'server_uuid'" );
+                               $row = $this->fetchObject( $res );
+
+                               return $row ? $row->Value : null;
+                       }
+               );
+       }
+
+       /**
+        * @param string $fname
+        * @return string[]
+        */
+       protected function getServerGTIDs( $fname = __METHOD__ ) {
+               $map = [];
+               // Get global-only variables like gtid_executed
+               $res = $this->query( "SHOW GLOBAL VARIABLES LIKE 'gtid_%'", $fname );
+               foreach ( $res as $row ) {
+                       $map[$row->Variable_name] = $row->Value;
+               }
+               // Get session-specific (e.g. gtid_domain_id since that is were writes will log)
+               $res = $this->query( "SHOW SESSION VARIABLES LIKE 'gtid_%'", $fname );
+               foreach ( $res as $row ) {
+                       $map[$row->Variable_name] = $row->Value;
                }
 
-               return false;
+               return $map;
+       }
+
+       /**
+        * @param string $role One of "MASTER"/"SLAVE"
+        * @param string $fname
+        * @return string[] Latest available server status row
+        */
+       protected function getServerRoleStatus( $role, $fname = __METHOD__ ) {
+               return $this->query( "SHOW $role STATUS", $fname )->fetchRow() ?: [];
        }
 
        public function serverIsReadOnly() {
@@ -1485,6 +1566,12 @@ abstract class DatabaseMysqlBase extends Database {
                return 'CAST( ' . $field . ' AS SIGNED )';
        }
 
+       /*
+        * @return bool Whether GTID support is used (mockable for testing)
+        */
+       protected function useGTIDs() {
+               return $this->useGTIDs;
+       }
 }
 
 class_alias( DatabaseMysqlBase::class, 'DatabaseMysqlBase' );
index beaaa14..b50ff70 100644 (file)
@@ -22,7 +22,6 @@ namespace Wikimedia\Rdbms;
 use InvalidArgumentException;
 use Wikimedia\ScopedCallback;
 use RuntimeException;
-use UnexpectedValueException;
 use stdClass;
 
 /**
@@ -1474,11 +1473,13 @@ interface IDatabase {
        /**
         * Run a callback as soon as the current transaction commits or rolls back.
         * An error is thrown if no transaction is pending. Queries in the function will run in
-        * AUTO-COMMIT mode unless there are begin() calls. Callbacks must commit any transactions
+        * AUTOCOMMIT mode unless there are begin() calls. Callbacks must commit any transactions
         * that they begin.
         *
         * This is useful for combining cooperative locks and DB transactions.
         *
+        * @note: do not assume that *other* IDatabase instances will be AUTOCOMMIT mode
+        *
         * The callback takes one argument:
         *   - How the transaction ended (IDatabase::TRIGGER_COMMIT or IDatabase::TRIGGER_ROLLBACK)
         *
@@ -1497,7 +1498,7 @@ interface IDatabase {
         * of the round, just after all peer transactions COMMIT. If the transaction round
         * is rolled back, then the callback is cancelled.
         *
-        * Queries in the function will run in AUTO-COMMIT mode unless there are begin() calls.
+        * Queries in the function will run in AUTOCOMMIT mode unless there are begin() calls.
         * Callbacks must commit any transactions that they begin.
         *
         * This is useful for updates to different systems or when separate transactions are needed.
@@ -1508,6 +1509,8 @@ interface IDatabase {
         *
         * Updates will execute in the order they were enqueued.
         *
+        * @note: do not assume that *other* IDatabase instances will be AUTOCOMMIT mode
+        *
         * The callback takes one argument:
         *   - How the transaction ended (IDatabase::TRIGGER_COMMIT or IDatabase::TRIGGER_IDLE)
         *
@@ -1557,21 +1560,71 @@ interface IDatabase {
        public function setTransactionListener( $name, callable $callback = null );
 
        /**
-        * Begin an atomic section of statements
+        * Begin an atomic section of SQL statements
+        *
+        * Start an implicit transaction if no transaction is already active, set a savepoint
+        * (if $cancelable is ATOMIC_CANCELABLE), and track the given section name to enforce
+        * that the transaction is not committed prematurely. The end of the section must be
+        * signified exactly once, either by endAtomic() or cancelAtomic(). Sections can have
+        * have layers of inner sections (sub-sections), but all sections must be ended in order
+        * of innermost to outermost. Transactions cannot be started or committed until all
+        * atomic sections are closed.
+        *
+        * ATOMIC_CANCELABLE is useful when the caller needs to handle specific failure cases
+        * by discarding the section's writes.  This should not be used for failures when:
+        *   - upsert() could easily be used instead
+        *   - insert() with IGNORE could easily be used instead
+        *   - select() with FOR UPDATE could be checked before issuing writes instead
+        *   - The failure is from code that runs after the first write but doesn't need to
+        *   - The failures are from contention solvable via onTransactionPreCommitOrIdle()
+        *   - The failures are deadlocks; the RDBMs usually discard the whole transaction
         *
-        * If a transaction has been started already, (optionally) sets a savepoint
-        * and tracks the given section name to make sure the transaction is not
-        * committed pre-maturely. This function can be used in layers (with
-        * sub-sections), so use a stack to keep track of the different atomic
-        * sections. If there is no transaction, one is started implicitly.
+        * @note: callers must use additional measures for situations involving two or more
+        *   (peer) transactions (e.g. updating two database servers at once). The transaction
+        *   and savepoint logic of this method only applies to this specific IDatabase instance.
         *
-        * The goal of this function is to create an atomic section of SQL queries
-        * without having to start a new transaction if it already exists.
+        * Example usage:
+        * @code
+        *     // Start a transaction if there isn't one already
+        *     $dbw->startAtomic( __METHOD__ );
+        *     // Serialize these thread table updates
+        *     $dbw->select( 'thread', '1', [ 'td_id' => $tid ], __METHOD__, 'FOR UPDATE' );
+        *     // Add a new comment for the thread
+        *     $dbw->insert( 'comment', $row, __METHOD__ );
+        *     $cid = $db->insertId();
+        *     // Update thread reference to last comment
+        *     $dbw->update( 'thread', [ 'td_latest' => $cid ], [ 'td_id' => $tid ], __METHOD__ );
+        *     // Demark the end of this conceptual unit of updates
+        *     $dbw->endAtomic( __METHOD__ );
+        * @endcode
         *
-        * All atomic levels *must* be explicitly closed using IDatabase::endAtomic()
-        * or IDatabase::cancelAtomic(), and any database transactions cannot be
-        * began or committed until all atomic levels are closed. There is no such
-        * thing as implicitly opening or closing an atomic section.
+        * Example usage (atomic changes that might have to be discarded):
+        * @code
+        *     // Start a transaction if there isn't one already
+        *     $dbw->startAtomic( __METHOD__, $dbw::ATOMIC_CANCELABLE );
+        *     // Create new record metadata row
+        *     $dbw->insert( 'records', $row, __METHOD__ );
+        *     // Figure out where to store the data based on the new row's ID
+        *     $path = $recordDirectory . '/' . $dbw->insertId();
+        *     // Write the record data to the storage system
+        *     $status = $fileBackend->create( [ 'dst' => $path, 'content' => $data ] );
+        *     if ( $status->isOK() ) {
+        *         // Try to cleanup files orphaned by transaction rollback
+        *         $dbw->onTransactionResolution(
+        *             function ( $type ) use ( $fileBackend, $path ) {
+        *                 if ( $type === IDatabase::TRIGGER_ROLLBACK ) {
+        *                     $fileBackend->delete( [ 'src' => $path ] );
+        *                 }
+        *             },
+        *             __METHOD__
+        *         );
+        *         // Demark the end of this conceptual unit of updates
+        *         $dbw->endAtomic( __METHOD__ );
+        *     } else {
+        *         // Discard these writes from the transaction (preserving prior writes)
+        *         $dbw->cancelAtomic( __METHOD__ );
+        *     }
+        * @endcode
         *
         * @since 1.23
         * @param string $fname
@@ -1605,8 +1658,11 @@ interface IDatabase {
         * corresponding startAtomic() implicitly started a transaction, that
         * transaction is rolled back.
         *
-        * Note that a call to IDatabase::rollback() will also roll back any open
-        * atomic sections.
+        * @note: callers must use additional measures for situations involving two or more
+        *   (peer) transactions (e.g. updating two database servers at once). The transaction
+        *   and savepoint logic of startAtomic() are bound to specific IDatabase instances.
+        *
+        * Note that a call to IDatabase::rollback() will also roll back any open atomic sections.
         *
         * @note As a micro-optimization to save a few DB calls, this method may only
         *  be called when startAtomic() was called with the ATOMIC_CANCELABLE flag.
@@ -1620,20 +1676,62 @@ interface IDatabase {
        public function cancelAtomic( $fname = __METHOD__, AtomicSectionIdentifier $sectionId = null );
 
        /**
-        * Run a callback to do an atomic set of updates for this database
+        * Perform an atomic section of reversable SQL statements from a callback
         *
         * The $callback takes the following arguments:
         *   - This database object
         *   - The value of $fname
         *
-        * If any exception occurs in the callback, then cancelAtomic() will be
-        * called to back out any statements executed by the callback and the error
-        * will be re-thrown. It may also be that the cancel itself fails with an
-        * exception before then. In any case, such errors are expected to
-        * terminate the request, without any outside caller attempting to catch
-        * errors and commit anyway.
+        * This will execute the callback inside a pair of startAtomic()/endAtomic() calls.
+        * If any exception occurs during execution of the callback, it will be handled as follows:
+        *   - If $cancelable is ATOMIC_CANCELABLE, cancelAtomic() will be called to back out any
+        *     (and only) statements executed during the atomic section. If that succeeds, then the
+        *     exception will be re-thrown; if it fails, then a different exception will be thrown
+        *     and any further query attempts will fail until rollback() is called.
+        *   - If $cancelable is ATOMIC_NOT_CANCELABLE, cancelAtomic() will be called to mark the
+        *     end of the section and the error will be re-thrown. Any further query attempts will
+        *     fail until rollback() is called.
+        *
+        * This method is convenient for letting calls to the caller of this method be wrapped
+        * in a try/catch blocks for exception types that imply that the caller failed but was
+        * able to properly discard the changes it made in the transaction. This method can be
+        * an alternative to explicit calls to startAtomic()/endAtomic()/cancelAtomic().
+        *
+        * Example usage, "RecordStore::save" method:
+        * @code
+        *     $dbw->doAtomicSection( __METHOD__, function ( $dbw ) use ( $record ) {
+        *         // Create new record metadata row
+        *         $dbw->insert( 'records', $record->toArray(), __METHOD__ );
+        *         // Figure out where to store the data based on the new row's ID
+        *         $path = $this->recordDirectory . '/' . $dbw->insertId();
+        *         // Write the record data to the storage system;
+        *         // blob store throughs StoreFailureException on failure
+        *         $this->blobStore->create( $path, $record->getJSON() );
+        *         // Try to cleanup files orphaned by transaction rollback
+        *         $dbw->onTransactionResolution(
+        *             function ( $type ) use ( $path ) {
+        *                 if ( $type === IDatabase::TRIGGER_ROLLBACK ) {
+        *                     $this->blobStore->delete( $path );
+        *                 }
+        *             },
+        *         },
+        *         __METHOD__
+        *     );
+        * @endcode
         *
-        * This can be an alternative to explicit startAtomic()/endAtomic()/cancelAtomic() calls.
+        * Example usage, caller of the "RecordStore::save" method:
+        * @code
+        *     $dbw->startAtomic( __METHOD__ );
+        *     // ...various SQL writes happen...
+        *     try {
+        *         $recordStore->save( $record );
+        *     } catch ( StoreFailureException $e ) {
+        *         $dbw->cancelAtomic( __METHOD__ );
+        *         // ...various SQL writes happen...
+        *     }
+        *     // ...various SQL writes happen...
+        *     $dbw->endAtomic( __METHOD__ );
+        * @endcode
         *
         * @see Database::startAtomic
         * @see Database::endAtomic
@@ -1646,7 +1744,6 @@ interface IDatabase {
         * @return mixed $res Result of the callback (since 1.28)
         * @throws DBError
         * @throws RuntimeException
-        * @throws UnexpectedValueException
         * @since 1.27; prior to 1.31 this did a rollback() instead of
         *  cancelAtomic(), and assumed no callers up the stack would ever try to
         *  catch the exception.
@@ -1788,7 +1885,7 @@ interface IDatabase {
         * This is useful when transactions might use snapshot isolation
         * (e.g. REPEATABLE-READ in innodb), so the "real" lag of that data
         * is this lag plus transaction duration. If they don't, it is still
-        * safe to be pessimistic. In AUTO-COMMIT mode, this still gives an
+        * safe to be pessimistic. In AUTOCOMMIT mode, this still gives an
         * indication of the staleness of subsequent reads.
         *
         * @return array ('lag': seconds or false on error, 'since': UNIX timestamp of BEGIN)
index cdcb79c..54eca79 100644 (file)
@@ -12,16 +12,36 @@ use UnexpectedValueException;
  *  - Binlog-based usage assumes single-source replication and non-hierarchical replication.
  *  - GTID-based usage allows getting/syncing with multi-source replication. It is assumed
  *    that GTID sets are complete (e.g. include all domains on the server).
+ *
+ * @see https://mariadb.com/kb/en/library/gtid/
+ * @see https://dev.mysql.com/doc/refman/5.6/en/replication-gtids-concepts.html
  */
 class MySQLMasterPos implements DBMasterPos {
-       /** @var string|null Binlog file base name */
-       public $binlog;
-       /** @var int[]|null Binglog file position tuple */
-       public $pos;
-       /** @var string[] GTID list */
-       public $gtids = [];
+       /** @var int One of (BINARY_LOG, GTID_MYSQL, GTID_MARIA) */
+       private $style;
+       /** @var string|null Base name of all Binary Log files */
+       private $binLog;
+       /** @var int[]|null Binary Log position tuple (index number, event number) */
+       private $logPos;
+       /** @var string[] Map of (server_uuid/gtid_domain_id => GTID) */
+       private $gtids = [];
+       /** @var int|null Active GTID domain ID */
+       private $activeDomain;
+       /** @var int|null ID of the server were DB writes originate */
+       private $activeServerId;
+       /** @var string|null UUID of the server were DB writes originate */
+       private $activeServerUUID;
        /** @var float UNIX timestamp */
-       public $asOfTime = 0.0;
+       private $asOfTime = 0.0;
+
+       const BINARY_LOG = 'binary-log';
+       const GTID_MARIA = 'gtid-maria';
+       const GTID_MYSQL = 'gtid-mysql';
+
+       /** @var int Key name of the binary log index number of a position tuple */
+       const CORD_INDEX = 0;
+       /** @var int Key name of the binary log event number of a position tuple */
+       const CORD_EVENT = 1;
 
        /**
         * @param string $position One of (comma separated GTID list, <binlog file>/<integer>)
@@ -38,18 +58,38 @@ class MySQLMasterPos implements DBMasterPos {
        protected function init( $position, $asOfTime ) {
                $m = [];
                if ( preg_match( '!^(.+)\.(\d+)/(\d+)$!', $position, $m ) ) {
-                       $this->binlog = $m[1]; // ideally something like host name
-                       $this->pos = [ (int)$m[2], (int)$m[3] ];
+                       $this->binLog = $m[1]; // ideally something like host name
+                       $this->logPos = [ self::CORD_INDEX => (int)$m[2], self::CORD_EVENT => (int)$m[3] ];
+                       $this->style = self::BINARY_LOG;
                } else {
                        $gtids = array_filter( array_map( 'trim', explode( ',', $position ) ) );
                        foreach ( $gtids as $gtid ) {
-                               if ( !self::parseGTID( $gtid ) ) {
+                               $components = self::parseGTID( $gtid );
+                               if ( !$components ) {
                                        throw new InvalidArgumentException( "Invalid GTID '$gtid'." );
                                }
-                               $this->gtids[] = $gtid;
+
+                               list( $domain, $pos ) = $components;
+                               if ( isset( $this->gtids[$domain] ) ) {
+                                       // For MySQL, handle the case where some past issue caused a gap in the
+                                       // executed GTID set, e.g. [last_purged+1,N-1] and [N+1,N+2+K]. Ignore the
+                                       // gap by using the GTID with the highest ending sequence number.
+                                       list( , $otherPos ) = self::parseGTID( $this->gtids[$domain] );
+                                       if ( $pos > $otherPos ) {
+                                               $this->gtids[$domain] = $gtid;
+                                       }
+                               } else {
+                                       $this->gtids[$domain] = $gtid;
+                               }
+
+                               if ( is_int( $domain ) ) {
+                                       $this->style = self::GTID_MARIA; // gtid_domain_id
+                               } else {
+                                       $this->style = self::GTID_MYSQL; // server_uuid
+                               }
                        }
                        if ( !$this->gtids ) {
-                               throw new InvalidArgumentException( "Got empty GTID set." );
+                               throw new InvalidArgumentException( "GTID set cannot be empty." );
                        }
                }
 
@@ -66,8 +106,8 @@ class MySQLMasterPos implements DBMasterPos {
                }
 
                // Prefer GTID comparisons, which work with multi-tier replication
-               $thisPosByDomain = $this->getGtidCoordinates();
-               $thatPosByDomain = $pos->getGtidCoordinates();
+               $thisPosByDomain = $this->getActiveGtidCoordinates();
+               $thatPosByDomain = $pos->getActiveGtidCoordinates();
                if ( $thisPosByDomain && $thatPosByDomain ) {
                        $comparisons = [];
                        // Check that this has positions reaching those in $pos for all domains in common
@@ -100,8 +140,8 @@ class MySQLMasterPos implements DBMasterPos {
                }
 
                // Prefer GTID comparisons, which work with multi-tier replication
-               $thisPosDomains = array_keys( $this->getGtidCoordinates() );
-               $thatPosDomains = array_keys( $pos->getGtidCoordinates() );
+               $thisPosDomains = array_keys( $this->getActiveGtidCoordinates() );
+               $thatPosDomains = array_keys( $pos->getActiveGtidCoordinates() );
                if ( $thisPosDomains && $thatPosDomains ) {
                        // Check that $this has a GTID for at least one domain also in $pos; due to MariaDB
                        // quirks, prior master switch-overs may result in inactive garbage GTIDs that cannot
@@ -118,74 +158,119 @@ class MySQLMasterPos implements DBMasterPos {
        }
 
        /**
-        * @return string|null
+        * @return string|null Base name of binary log files
+        * @since 1.31
+        */
+       public function getLogName() {
+               return $this->gtids ? null : $this->binLog;
+       }
+
+       /**
+        * @return int[]|null Tuple of (binary log file number, event number)
+        * @since 1.31
+        */
+       public function getLogPosition() {
+               return $this->gtids ? null : $this->logPos;
+       }
+
+       /**
+        * @return string|null Name of the binary log file for this position
+        * @since 1.31
         */
        public function getLogFile() {
-               return $this->gtids ? null : "{$this->binlog}.{$this->pos[0]}";
+               return $this->gtids ? null : "{$this->binLog}.{$this->logPos[self::CORD_INDEX]}";
        }
 
        /**
-        * @return string[]
+        * @return string[] Map of (server_uuid/gtid_domain_id => GTID)
+        * @since 1.31
         */
        public function getGTIDs() {
                return $this->gtids;
        }
 
        /**
-        * @return string GTID set or <binlog file>/<position> (e.g db1034-bin.000976/843431247)
+        * @param int|null $id @@gtid_domain_id of the active replication stream
+        * @since 1.31
         */
-       public function __toString() {
-               return $this->gtids
-                       ? implode( ',', $this->gtids )
-                       : $this->getLogFile() . "/{$this->pos[1]}";
+       public function setActiveDomain( $id ) {
+               $this->activeDomain = (int)$id;
+       }
+
+       /**
+        * @param int|null $id @@server_id of the server were writes originate
+        * @since 1.31
+        */
+       public function setActiveOriginServerId( $id ) {
+               $this->activeServerId = (int)$id;
+       }
+
+       /**
+        * @param string|null $id @@server_uuid of the server were writes originate
+        * @since 1.31
+        */
+       public function setActiveOriginServerUUID( $id ) {
+               $this->activeServerUUID = $id;
        }
 
        /**
         * @param MySQLMasterPos $pos
         * @param MySQLMasterPos $refPos
         * @return string[] List of GTIDs from $pos that have domains in $refPos
+        * @since 1.31
         */
        public static function getCommonDomainGTIDs( MySQLMasterPos $pos, MySQLMasterPos $refPos ) {
-               $gtidsCommon = [];
-
-               $relevantDomains = $refPos->getGtidCoordinates(); // (domain => unused)
-               foreach ( $pos->gtids as $gtid ) {
-                       list( $domain ) = self::parseGTID( $gtid );
-                       if ( isset( $relevantDomains[$domain] ) ) {
-                               $gtidsCommon[] = $gtid;
-                       }
-               }
-
-               return $gtidsCommon;
+               return array_values(
+                       array_intersect_key( $pos->gtids, $refPos->getActiveGtidCoordinates() )
+               );
        }
 
        /**
         * @see https://mariadb.com/kb/en/mariadb/gtid
         * @see https://dev.mysql.com/doc/refman/5.6/en/replication-gtids-concepts.html
-        * @return array Map of (domain => integer position); possibly empty
+        * @return array Map of (server_uuid/gtid_domain_id => integer position); possibly empty
         */
-       protected function getGtidCoordinates() {
+       protected function getActiveGtidCoordinates() {
                $gtidInfos = [];
-               foreach ( $this->gtids as $gtid ) {
-                       list( $domain, $pos ) = self::parseGTID( $gtid );
-                       $gtidInfos[$domain] = $pos;
+
+               foreach ( $this->gtids as $domain => $gtid ) {
+                       list( $domain, $pos, $server ) = self::parseGTID( $gtid );
+
+                       $ignore = false;
+                       // Filter out GTIDs from non-active replication domains
+                       if ( $this->style === self::GTID_MARIA && $this->activeDomain !== null ) {
+                               $ignore |= ( $domain !== $this->activeDomain );
+                       }
+                       // Likewise for GTIDs from non-active replication origin servers
+                       if ( $this->style === self::GTID_MARIA && $this->activeServerId !== null ) {
+                               $ignore |= ( $server !== $this->activeServerId );
+                       } elseif ( $this->style === self::GTID_MYSQL && $this->activeServerUUID !== null ) {
+                               $ignore |= ( $server !== $this->activeServerUUID );
+                       }
+
+                       if ( !$ignore ) {
+                               $gtidInfos[$domain] = $pos;
+                       }
                }
 
                return $gtidInfos;
        }
 
        /**
-        * @param string $gtid
-        * @return array|null [domain, integer position] or null
+        * @param string $id GTID
+        * @return array|null [domain ID or server UUID, sequence number, server ID/UUID] or null
         */
-       protected static function parseGTID( $gtid ) {
+       protected static function parseGTID( $id ) {
                $m = [];
-               if ( preg_match( '!^(\d+)-\d+-(\d+)$!', $gtid, $m ) ) {
+               if ( preg_match( '!^(\d+)-(\d+)-(\d+)$!', $id, $m ) ) {
                        // MariaDB style: <domain>-<server id>-<sequence number>
-                       return [ (int)$m[1], (int)$m[2] ];
-               } elseif ( preg_match( '!^(\w{8}-\w{4}-\w{4}-\w{4}-\w{12}):(\d+)$!', $gtid, $m ) ) {
-                       // MySQL style: <UUID domain>:<sequence number>
-                       return [ $m[1], (int)$m[2] ];
+                       return [ (int)$m[1], (int)$m[3], (int)$m[2] ];
+               } elseif ( preg_match( '!^(\w{8}-\w{4}-\w{4}-\w{4}-\w{12}):(?:\d+-|)(\d+)$!', $id, $m ) ) {
+                       // MySQL style: <server UUID>:<sequence number>-<sequence number>
+                       // Normally, the first number should reflect the point (gtid_purged) where older
+                       // binary logs where purged to save space. When doing comparisons, it may as well
+                       // be 1 in that case. Assume that this is generally the situation.
+                       return [ $m[1], (int)$m[2], $m[1] ];
                }
 
                return null;
@@ -194,16 +279,22 @@ class MySQLMasterPos implements DBMasterPos {
        /**
         * @see https://dev.mysql.com/doc/refman/5.7/en/show-master-status.html
         * @see https://dev.mysql.com/doc/refman/5.7/en/show-slave-status.html
-        * @return array|bool (binlog, (integer file number, integer position)) or false
+        * @return array|bool Map of (binlog:<string>, pos:(<integer>, <integer>)) or false
         */
        protected function getBinlogCoordinates() {
-               return ( $this->binlog !== null && $this->pos !== null )
-                       ? [ 'binlog' => $this->binlog, 'pos' => $this->pos ]
+               return ( $this->binLog !== null && $this->logPos !== null )
+                       ? [ 'binlog' => $this->binLog, 'pos' => $this->logPos ]
                        : false;
        }
 
        public function serialize() {
-               return serialize( [ 'position' => $this->__toString(), 'asOfTime' => $this->asOfTime ] );
+               return serialize( [
+                       'position' => $this->__toString(),
+                       'activeDomain' => $this->activeDomain,
+                       'activeServerId' => $this->activeServerId,
+                       'activeServerUUID' => $this->activeServerUUID,
+                       'asOfTime' => $this->asOfTime
+               ] );
        }
 
        public function unserialize( $serialized ) {
@@ -213,5 +304,23 @@ class MySQLMasterPos implements DBMasterPos {
                }
 
                $this->init( $data['position'], $data['asOfTime'] );
+               if ( isset( $data['activeDomain'] ) ) {
+                       $this->setActiveDomain( $data['activeDomain'] );
+               }
+               if ( isset( $data['activeServerId'] ) ) {
+                       $this->setActiveOriginServerId( $data['activeServerId'] );
+               }
+               if ( isset( $data['activeServerUUID'] ) ) {
+                       $this->setActiveOriginServerUUID( $data['activeServerUUID'] );
+               }
+       }
+
+       /**
+        * @return string GTID set or <binary log file>/<position> (e.g db1034-bin.000976/843431247)
+        */
+       public function __toString() {
+               return $this->gtids
+                       ? implode( ',', $this->gtids )
+                       : $this->getLogFile() . "/{$this->logPos[self::CORD_EVENT]}";
        }
 }
index b1ea810..19b8fdc 100644 (file)
@@ -131,7 +131,9 @@ abstract class LBFactory implements ILBFactory {
                        'ChronologyPositionIndex' => isset( $_GET['cpPosIndex'] ) ? $_GET['cpPosIndex'] : null
                ];
 
-               $this->cliMode = isset( $conf['cliMode'] ) ? $conf['cliMode'] : PHP_SAPI === 'cli';
+               $this->cliMode = isset( $conf['cliMode'] )
+                       ? $conf['cliMode']
+                       : ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' );
                $this->hostname = isset( $conf['hostname'] ) ? $conf['hostname'] : gethostname();
                $this->agent = isset( $conf['agent'] ) ? $conf['agent'] : '';
 
index 715f4e4..ae4362d 100644 (file)
@@ -85,6 +85,8 @@ interface ILoadBalancer {
        const DOMAIN_ANY = '';
 
        /** @var int DB handle should have DBO_TRX disabled and the caller will leave it as such */
+       const CONN_TRX_AUTOCOMMIT = 1;
+       /** @var int Alias for CONN_TRX_AUTOCOMMIT for b/c; deprecated since 1.31 */
        const CONN_TRX_AUTO = 1;
 
        /**
@@ -173,11 +175,11 @@ interface ILoadBalancer {
        /**
         * Get a connection handle by server index
         *
-        * The CONN_TRX_AUTO flag is ignored for databases with ATTR_DB_LEVEL_LOCKING
+        * The CONN_TRX_AUTOCOMMIT flag is ignored for databases with ATTR_DB_LEVEL_LOCKING
         * (e.g. sqlite) in order to avoid deadlocks. ILoadBalancer::getServerAttributes()
         * can be used to check such flags beforehand.
         *
-        * If the caller uses $domain or sets CONN_TRX_AUTO in $flags, then it must also
+        * If the caller uses $domain or sets CONN_TRX_AUTOCOMMIT in $flags, then it must also
         * call ILoadBalancer::reuseConnection() on the handle when finished using it.
         * In all other cases, this is not necessary, though not harmful either.
         *
@@ -209,7 +211,7 @@ interface ILoadBalancer {
         *
         * The handle's methods simply wrap those of a Database handle
         *
-        * The CONN_TRX_AUTO flag is ignored for databases with ATTR_DB_LEVEL_LOCKING
+        * The CONN_TRX_AUTOCOMMIT flag is ignored for databases with ATTR_DB_LEVEL_LOCKING
         * (e.g. sqlite) in order to avoid deadlocks. ILoadBalancer::getServerAttributes()
         * can be used to check such flags beforehand.
         *
@@ -218,7 +220,7 @@ interface ILoadBalancer {
         * @param int $i Server index or DB_MASTER/DB_REPLICA
         * @param array|string|bool $groups Query group(s), or false for the generic reader
         * @param string|bool $domain Domain ID, or false for the current domain
-        * @param int $flags Bitfield of CONN_* class constants (e.g. CONN_TRX_AUTO)
+        * @param int $flags Bitfield of CONN_* class constants (e.g. CONN_TRX_AUTOCOMMIT)
         * @return DBConnRef
         */
        public function getConnectionRef( $i, $groups = [], $domain = false, $flags = 0 );
@@ -228,7 +230,7 @@ interface ILoadBalancer {
         *
         * The handle's methods simply wrap those of a Database handle
         *
-        * The CONN_TRX_AUTO flag is ignored for databases with ATTR_DB_LEVEL_LOCKING
+        * The CONN_TRX_AUTOCOMMIT flag is ignored for databases with ATTR_DB_LEVEL_LOCKING
         * (e.g. sqlite) in order to avoid deadlocks. ILoadBalancer::getServerAttributes()
         * can be used to check such flags beforehand.
         *
@@ -237,7 +239,7 @@ interface ILoadBalancer {
         * @param int $i Server index or DB_MASTER/DB_REPLICA
         * @param array|string|bool $groups Query group(s), or false for the generic reader
         * @param string|bool $domain Domain ID, or false for the current domain
-        * @param int $flags Bitfield of CONN_* class constants (e.g. CONN_TRX_AUTO)
+        * @param int $flags Bitfield of CONN_* class constants (e.g. CONN_TRX_AUTOCOMMIT)
         * @return DBConnRef
         */
        public function getLazyConnectionRef( $i, $groups = [], $domain = false, $flags = 0 );
@@ -247,7 +249,7 @@ interface ILoadBalancer {
         *
         * The handle's methods simply wrap those of a Database handle
         *
-        * The CONN_TRX_AUTO flag is ignored for databases with ATTR_DB_LEVEL_LOCKING
+        * The CONN_TRX_AUTOCOMMIT flag is ignored for databases with ATTR_DB_LEVEL_LOCKING
         * (e.g. sqlite) in order to avoid deadlocks. ILoadBalancer::getServerAttributes()
         * can be used to check such flags beforehand.
         *
@@ -256,7 +258,7 @@ interface ILoadBalancer {
         * @param int $db Server index or DB_MASTER/DB_REPLICA
         * @param array|string|bool $groups Query group(s), or false for the generic reader
         * @param string|bool $domain Domain ID, or false for the current domain
-        * @param int $flags Bitfield of CONN_* class constants (e.g. CONN_TRX_AUTO)
+        * @param int $flags Bitfield of CONN_* class constants (e.g. CONN_TRX_AUTOCOMMIT)
         * @return MaintainableDBConnRef
         */
        public function getMaintenanceConnectionRef( $db, $groups = [], $domain = false, $flags = 0 );
@@ -267,11 +269,11 @@ interface ILoadBalancer {
         * The index must be an actual index into the array. If a connection to the server is
         * already open and not considered an "in use" foreign connection, this simply returns it.
         *
-        * Avoid using CONN_TRX_AUTO for databases with ATTR_DB_LEVEL_LOCKING (e.g. sqlite) in
+        * Avoid using CONN_TRX_AUTOCOMMIT for databases with ATTR_DB_LEVEL_LOCKING (e.g. sqlite) in
         * order to avoid deadlocks. ILoadBalancer::getServerAttributes() can be used to check
         * such flags beforehand.
         *
-        * If the caller uses $domain or sets CONN_TRX_AUTO in $flags, then it must also
+        * If the caller uses $domain or sets CONN_TRX_AUTOCOMMIT in $flags, then it must also
         * call ILoadBalancer::reuseConnection() on the handle when finished using it.
         * In all other cases, this is not necessary, though not harmful either.
         *
@@ -279,7 +281,7 @@ interface ILoadBalancer {
         *
         * @param int $i Server index (does not support DB_MASTER/DB_REPLICA)
         * @param string|bool $domain Domain ID, or false for the current domain
-        * @param int $flags Bitfield of CONN_* class constants (e.g. CONN_TRX_AUTO)
+        * @param int $flags Bitfield of CONN_* class constants (e.g. CONN_TRX_AUTOCOMMIT)
         * @return Database|bool Returns false on errors
         * @throws DBAccessError
         */
index db2ab1f..c587b42 100644 (file)
@@ -539,7 +539,7 @@ class LoadBalancer implements ILoadBalancer {
                                if ( $this->loads[$i] > 0 ) {
                                        $start = microtime( true );
                                        $ok = $this->doWait( $i, true, $timeout ) && $ok;
-                                       $timeout -= ( microtime( true ) - $start );
+                                       $timeout -= intval( microtime( true ) - $start );
                                        if ( $timeout <= 0 ) {
                                                break; // timeout reached
                                        }
@@ -575,7 +575,6 @@ class LoadBalancer implements ILoadBalancer {
                        if ( !empty( $connsByServer[$i] ) ) {
                                /** @var IDatabase[] $serverConns */
                                $serverConns = $connsByServer[$i];
-
                                return reset( $serverConns );
                        }
                }
@@ -689,7 +688,7 @@ class LoadBalancer implements ILoadBalancer {
                        $domain = false; // local connection requested
                }
 
-               if ( ( $flags & self::CONN_TRX_AUTO ) === self::CONN_TRX_AUTO ) {
+               if ( ( $flags & self::CONN_TRX_AUTOCOMMIT ) === self::CONN_TRX_AUTOCOMMIT ) {
                        // Assuming all servers are of the same type (or similar), which is overwhelmingly
                        // the case, use the master server information to get the attributes. The information
                        // for $i cannot be used since it might be DB_REPLICA, which might require connection
@@ -700,8 +699,9 @@ class LoadBalancer implements ILoadBalancer {
                                // rows (e.g. FOR UPDATE) or (b) make small commits during a larger transactions
                                // to reduce lock contention. None of these apply for sqlite and using separate
                                // connections just causes self-deadlocks.
-                               $flags &= ~self::CONN_TRX_AUTO;
-                               $this->connLogger->info( __METHOD__ . ': ignoring CONN_TRX_AUTO to avoid deadlocks.' );
+                               $flags &= ~self::CONN_TRX_AUTOCOMMIT;
+                               $this->connLogger->info( __METHOD__ .
+                                       ': ignoring CONN_TRX_AUTOCOMMIT to avoid deadlocks.' );
                        }
                }
 
@@ -859,7 +859,7 @@ class LoadBalancer implements ILoadBalancer {
                // main set of DB connections but rather its own pool since:
                // a) those are usually set to implicitly use transaction rounds via DBO_TRX
                // b) those must support the use of explicit transaction rounds via beginMasterChanges()
-               $autoCommit = ( ( $flags & self::CONN_TRX_AUTO ) == self::CONN_TRX_AUTO );
+               $autoCommit = ( ( $flags & self::CONN_TRX_AUTOCOMMIT ) == self::CONN_TRX_AUTOCOMMIT );
 
                if ( $domain !== false ) {
                        // Connection is to a foreign domain
@@ -937,7 +937,7 @@ class LoadBalancer implements ILoadBalancer {
                $domainInstance = DatabaseDomain::newFromId( $domain );
                $dbName = $domainInstance->getDatabase();
                $prefix = $domainInstance->getTablePrefix();
-               $autoCommit = ( ( $flags & self::CONN_TRX_AUTO ) == self::CONN_TRX_AUTO );
+               $autoCommit = ( ( $flags & self::CONN_TRX_AUTOCOMMIT ) == self::CONN_TRX_AUTOCOMMIT );
 
                if ( $autoCommit ) {
                        $connFreeKey = self::KEY_FOREIGN_FREE_NOROUND;
@@ -1220,7 +1220,7 @@ class LoadBalancer implements ILoadBalancer {
        }
 
        public function closeConnection( IDatabase $conn ) {
-               $serverIndex = $conn->getLBInfo( 'serverIndex' ); // second index level of mConns
+               $serverIndex = $conn->getLBInfo( 'serverIndex' );
                foreach ( $this->conns as $type => $connsByServer ) {
                        if ( !isset( $connsByServer[$serverIndex] ) ) {
                                continue;
index 6d35658..8ff14ed 100644 (file)
@@ -181,7 +181,7 @@ class SqlBagOStuff extends BagOStuff {
                                $index = $this->replicaOnly ? DB_REPLICA : DB_MASTER;
                                if ( $lb->getServerType( $lb->getWriterIndex() ) !== 'sqlite' ) {
                                        // Keep a separate connection to avoid contention and deadlocks
-                                       $db = $lb->getConnection( $index, [], false, $lb::CONN_TRX_AUTO );
+                                       $db = $lb->getConnection( $index, [], false, $lb::CONN_TRX_AUTOCOMMIT );
                                        // @TODO: Use a blank trx profiler to ignore expections as this is a cache
                                } else {
                                        // However, SQLite has the opposite behavior due to DB-level locking.
index e5232d3..afe266b 100644 (file)
@@ -2871,13 +2871,32 @@ class WikiPage implements Page, IDBAccessObject {
                // In the future, we may keep revisions and mark them with
                // the rev_deleted field, which is reserved for this purpose.
 
+               // Lock rows in `revision` and its temp tables, but not any others.
+               // Note array_intersect() preserves keys from the first arg, and we're
+               // assuming $revQuery has `revision` primary and isn't using subtables
+               // for anything we care about.
+               $res = $dbw->select(
+                       array_intersect(
+                               $revQuery['tables'],
+                               [ 'revision', 'revision_comment_temp', 'revision_actor_temp' ]
+                       ),
+                       '1',
+                       [ 'rev_page' => $id ],
+                       __METHOD__,
+                       'FOR UPDATE',
+                       $revQuery['joins']
+               );
+               foreach ( $res as $row ) {
+                       // Fetch all rows in case the DB needs that to properly lock them.
+               }
+
                // Get all of the page revisions
                $res = $dbw->select(
                        $revQuery['tables'],
                        $revQuery['fields'],
                        [ 'rev_page' => $id ],
                        __METHOD__,
-                       'FOR UPDATE',
+                       [],
                        $revQuery['joins']
                );
 
index 330859d..19cf573 100644 (file)
@@ -52,27 +52,6 @@ class MWTidy {
                return $driver->tidy( $text );
        }
 
-       /**
-        * Check HTML for errors, used if $wgValidateAllHtml = true.
-        *
-        * @param string $text
-        * @param string &$errorStr Return the error string
-        * @return bool Whether the HTML is valid
-        * @throws MWException
-        */
-       public static function checkErrors( $text, &$errorStr = null ) {
-               $driver = self::singleton();
-               if ( !$driver ) {
-                       throw new MWException( __METHOD__ .
-                               ': tidy is disabled, caller should have checked MWTidy::isEnabled()' );
-               }
-               if ( $driver->supportsValidate() ) {
-                       return $driver->validate( $text, $errorStr );
-               } else {
-                       throw new MWException( __METHOD__ . ": tidy driver does not support validate()" );
-               }
-       }
-
        /**
         * @return bool
         */
index eb2cada..2a4acc8 100644 (file)
@@ -87,9 +87,12 @@ abstract class ChangesListSpecialPage extends SpecialPage {
 
        // Same format as filterGroupDefinitions, but for a single group (reviewStatus)
        // that is registered conditionally.
+       private $legacyReviewStatusFilterGroupDefinition;
+
+       // Single filter group registered conditionally
        private $reviewStatusFilterGroupDefinition;
 
-       // Single filter registered conditionally
+       // Single filter group registered conditionally
        private $hideCategorizationFilterDefinition;
 
        /**
@@ -301,7 +304,7 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                ]
                        ],
 
-                       // reviewStatus (conditional)
+                       // significance (conditional)
 
                        [
                                'name' => 'significance',
@@ -457,17 +460,14 @@ abstract class ChangesListSpecialPage extends SpecialPage {
 
                ];
 
-               $this->reviewStatusFilterGroupDefinition = [
+               $this->legacyReviewStatusFilterGroupDefinition = [
                        [
-                               'name' => 'reviewStatus',
+                               'name' => 'legacyReviewStatus',
                                'title' => 'rcfilters-filtergroup-reviewstatus',
                                'class' => ChangesListBooleanFilterGroup::class,
-                               'priority' => -5,
                                'filters' => [
                                        [
                                                'name' => 'hidepatrolled',
-                                               'label' => 'rcfilters-filter-patrolled-label',
-                                               'description' => 'rcfilters-filter-patrolled-description',
                                                // rcshowhidepatr-show, rcshowhidepatr-hide
                                                // wlshowhidepatr
                                                'showHideSuffix' => 'showhidepatr',
@@ -477,27 +477,75 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                                ) {
                                                        $conds[] = 'rc_patrolled = 0';
                                                },
-                                               'cssClassSuffix' => 'patrolled',
-                                               'isRowApplicableCallable' => function ( $ctx, $rc ) {
-                                                       return $rc->getAttribute( 'rc_patrolled' );
-                                               },
+                                               'isReplacedInStructuredUi' => true,
                                        ],
                                        [
                                                'name' => 'hideunpatrolled',
-                                               'label' => 'rcfilters-filter-unpatrolled-label',
-                                               'description' => 'rcfilters-filter-unpatrolled-description',
                                                'default' => false,
                                                'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
                                                        &$query_options, &$join_conds
                                                ) {
                                                        $conds[] = 'rc_patrolled != 0';
                                                },
-                                               'cssClassSuffix' => 'unpatrolled',
+                                               'isReplacedInStructuredUi' => true,
+                                       ],
+                               ],
+                       ]
+               ];
+
+               $this->reviewStatusFilterGroupDefinition = [
+                       [
+                               'name' => 'reviewStatus',
+                               'title' => 'rcfilters-filtergroup-reviewstatus',
+                               'class' => ChangesListStringOptionsFilterGroup::class,
+                               'isFullCoverage' => true,
+                               'priority' => -5,
+                               'filters' => [
+                                       [
+                                               'name' => 'unpatrolled',
+                                               'label' => 'rcfilters-filter-reviewstatus-unpatrolled-label',
+                                               'description' => 'rcfilters-filter-reviewstatus-unpatrolled-description',
+                                               'cssClassSuffix' => 'reviewstatus-unpatrolled',
+                                               'isRowApplicableCallable' => function ( $ctx, $rc ) {
+                                                       return $rc->getAttribute( 'rc_patrolled' ) == RecentChange::PRC_UNPATROLLED;
+                                               },
+                                       ],
+                                       [
+                                               'name' => 'manual',
+                                               'label' => 'rcfilters-filter-reviewstatus-manual-label',
+                                               'description' => 'rcfilters-filter-reviewstatus-manual-description',
+                                               'cssClassSuffix' => 'reviewstatus-manual',
                                                'isRowApplicableCallable' => function ( $ctx, $rc ) {
-                                                       return !$rc->getAttribute( 'rc_patrolled' );
+                                                       return $rc->getAttribute( 'rc_patrolled' ) == RecentChange::PRC_PATROLLED;
+                                               },
+                                       ],
+                                       [
+                                               'name' => 'auto',
+                                               'label' => 'rcfilters-filter-reviewstatus-auto-label',
+                                               'description' => 'rcfilters-filter-reviewstatus-auto-description',
+                                               'cssClassSuffix' => 'reviewstatus-auto',
+                                               'isRowApplicableCallable' => function ( $ctx, $rc ) {
+                                                       return $rc->getAttribute( 'rc_patrolled' ) == RecentChange::PRC_AUTOPATROLLED;
                                                },
                                        ],
                                ],
+                               'default' => ChangesListStringOptionsFilterGroup::NONE,
+                               'queryCallable' => function ( $specialPageClassName, $ctx, $dbr,
+                                       &$tables, &$fields, &$conds, &$query_options, &$join_conds, $selected
+                               ) {
+                                       if ( $selected === [] ) {
+                                               return;
+                                       }
+                                       $rcPatrolledValues = [
+                                               'unpatrolled' => RecentChange::PRC_UNPATROLLED,
+                                               'manual' => RecentChange::PRC_PATROLLED,
+                                               'auto' => RecentChange::PRC_AUTOPATROLLED,
+                                       ];
+                                       // e.g. rc_patrolled IN (0, 2)
+                                       $conds['rc_patrolled'] = array_map( function ( $s ) use ( $rcPatrolledValues ) {
+                                               return $rcPatrolledValues[ $s ];
+                                       }, $selected );
+                               }
                        ]
                ];
 
@@ -910,6 +958,7 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                // information to all users just because the user that saves the edit can
                // patrol or is logged in)
                if ( !$this->including() && $this->getUser()->useRCPatrol() ) {
+                       $this->registerFiltersFromDefinitions( $this->legacyReviewStatusFilterGroupDefinition );
                        $this->registerFiltersFromDefinitions( $this->reviewStatusFilterGroupDefinition );
                }
 
@@ -1339,7 +1388,7 @@ abstract class ChangesListSpecialPage extends SpecialPage {
        }
 
        /**
-        * Replace old options 'hideanons' or 'hideliu' with structured UI equivalent
+        * Replace old options with their structured UI equivalents
         *
         * @param FormOptions $opts
         * @return bool True if the change was made
@@ -1349,21 +1398,40 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                        return false;
                }
 
+               $changed = false;
+
                // At this point 'hideanons' and 'hideliu' cannot be both true,
                // because fixBackwardsCompatibilityOptions resets (at least) 'hideanons' in such case
                if ( $opts[ 'hideanons' ] ) {
                        $opts->reset( 'hideanons' );
                        $opts[ 'userExpLevel' ] = 'registered';
-                       return true;
+                       $changed = true;
                }
 
                if ( $opts[ 'hideliu' ] ) {
                        $opts->reset( 'hideliu' );
                        $opts[ 'userExpLevel' ] = 'unregistered';
-                       return true;
+                       $changed = true;
                }
 
-               return false;
+               if ( $this->getFilterGroup( 'legacyReviewStatus' ) ) {
+                       if ( $opts[ 'hidepatrolled' ] ) {
+                               $opts->reset( 'hidepatrolled' );
+                               $opts[ 'reviewStatus' ] = 'unpatrolled';
+                               $changed = true;
+                       }
+
+                       if ( $opts[ 'hideunpatrolled' ] ) {
+                               $opts->reset( 'hideunpatrolled' );
+                               $opts[ 'reviewStatus' ] = implode(
+                                       ChangesListStringOptionsFilterGroup::SEPARATOR,
+                                       [ 'manual', 'auto' ]
+                               );
+                               $changed = true;
+                       }
+               }
+
+               return $changed;
        }
 
        /**
index cb2f420..bfef5e0 100644 (file)
@@ -208,8 +208,12 @@ class SpecialRecentChanges extends ChangesListSpecialPage {
                $reviewStatus = $this->getFilterGroup( 'reviewStatus' );
                if ( $reviewStatus !== null ) {
                        // Conditional on feature being available and rights
-                       $hidePatrolled = $reviewStatus->getFilter( 'hidepatrolled' );
-                       $hidePatrolled->setDefault( $user->getBoolOption( 'hidepatrolled' ) );
+                       if ( $user->getBoolOption( 'hidepatrolled' ) ) {
+                               $reviewStatus->setDefault( 'unpatrolled' );
+                               $legacyReviewStatus = $this->getFilterGroup( 'legacyReviewStatus' );
+                               $legacyHidePatrolled = $legacyReviewStatus->getFilter( 'hidepatrolled' );
+                               $legacyHidePatrolled->setDefault( true );
+                       }
                }
 
                $changeType = $this->getFilterGroup( 'changeType' );
index 3fe6c1e..dda1dac 100644 (file)
@@ -264,8 +264,12 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                $reviewStatus = $this->getFilterGroup( 'reviewStatus' );
                if ( $reviewStatus !== null ) {
                        // Conditional on feature being available and rights
-                       $hidePatrolled = $reviewStatus->getFilter( 'hidepatrolled' );
-                       $hidePatrolled->setDefault( $user->getBoolOption( 'watchlisthidepatrolled' ) );
+                       if ( $user->getBoolOption( 'watchlisthidepatrolled' ) ) {
+                               $reviewStatus->setDefault( 'unpatrolled' );
+                               $legacyReviewStatus = $this->getFilterGroup( 'legacyReviewStatus' );
+                               $legacyHidePatrolled = $legacyReviewStatus->getFilter( 'hidepatrolled' );
+                               $legacyHidePatrolled->setDefault( true );
+                       }
                }
 
                $authorship = $this->getFilterGroup( 'authorship' );
index f88b673..a53360c 100644 (file)
@@ -20,18 +20,6 @@ abstract class TidyDriverBase {
                return false;
        }
 
-       /**
-        * Check HTML for errors, used if $wgValidateAllHtml = true.
-        *
-        * @param string $text
-        * @param string &$errorStr Return the error string
-        * @throws \MWException
-        * @return bool Whether the HTML is valid
-        */
-       public function validate( $text, &$errorStr ) {
-               throw new \MWException( static::class . ' does not support validate()' );
-       }
-
        /**
         * Clean up HTML
         *
index 3e6b212..ea395f4 100644 (file)
@@ -3084,7 +3084,7 @@ class User implements IDBAccessObject, UserIdentity {
         * Get the user's current setting for a given option.
         *
         * @param string $oname The option to check
-        * @param string $defaultOverride A default value returned if the option does not exist
+        * @param string|array $defaultOverride A default value returned if the option does not exist
         * @param bool $ignoreHidden Whether to ignore the effects of $wgHiddenPrefs
         * @return string|array|int|null User's current value for the option
         * @see getBoolOption()
index 4bdf97e..5cfad4b 100644 (file)
        "rcfilters-filter-humans-label": "Human (not bot)",
        "rcfilters-filter-humans-description": "Edits made by human editors.",
        "rcfilters-filtergroup-reviewstatus": "Review status",
-       "rcfilters-filter-patrolled-label": "Patrolled",
-       "rcfilters-filter-patrolled-description": "Edits marked as patrolled.",
-       "rcfilters-filter-unpatrolled-label": "Unpatrolled",
-       "rcfilters-filter-unpatrolled-description": "Edits not marked as patrolled.",
+       "rcfilters-filter-reviewstatus-unpatrolled-description": "Edits not manually or automatically marked as patrolled.",
+       "rcfilters-filter-reviewstatus-unpatrolled-label": "Unpatrolled",
+       "rcfilters-filter-reviewstatus-manual-description": "Edits manually marked as patrolled.",
+       "rcfilters-filter-reviewstatus-manual-label": "Manually patrolled",
+       "rcfilters-filter-reviewstatus-auto-description": "Edits by advanced users whose work is automatically marked as patrolled.",
+       "rcfilters-filter-reviewstatus-auto-label": "Autopatrolled",
        "rcfilters-filtergroup-significance": "Significance",
        "rcfilters-filter-minor-label": "Minor edits",
        "rcfilters-filter-minor-description": "Edits the author labeled as minor.",
index 6e54765..374dd90 100644 (file)
        "rcfilters-filter-humans-label": "Label for the filter for showing edits made by human editors.",
        "rcfilters-filter-humans-description": "Description for the filter for showing edits made by human editors.",
        "rcfilters-filtergroup-reviewstatus": "Title for the filter group about review status (in core this is whether it's been patrolled)",
-       "rcfilters-filter-patrolled-label": "Label for the filter for showing patrolled edits",
-       "rcfilters-filter-patrolled-description": "Label for the filter showing patrolled edits",
-       "rcfilters-filter-unpatrolled-label": "Label for the filter for showing unpatrolled edits",
-       "rcfilters-filter-unpatrolled-description": "Description for the filter for showing unpatrolled edits",
+       "rcfilters-filter-reviewstatus-manual-description": "Description for the filter showing manually patrolled edits",
+       "rcfilters-filter-reviewstatus-manual-label": "Label for the filter showing manually patrolled edits",
+       "rcfilters-filter-reviewstatus-auto-description": "Description for the filter showing automatically patrolled edits",
+       "rcfilters-filter-reviewstatus-auto-label": "Label for the filter showing automatically patrolled edits",
+       "rcfilters-filter-reviewstatus-unpatrolled-description": "Description for the filter for showing unpatrolled edits",
+       "rcfilters-filter-reviewstatus-unpatrolled-label": "Label for the filter for showing unpatrolled edits",
        "rcfilters-filtergroup-significance": "Title for the filter group for edit significance.\n{{Identical|Significance}}",
        "rcfilters-filter-minor-label": "Label for the filter for showing edits marked as minor.",
        "rcfilters-filter-minor-description": "Description for the filter for showing edits marked as minor.",
index 9685177..13fee9c 100644 (file)
@@ -35,6 +35,10 @@ use Wikimedia\Rdbms\DBReplicationWaitError;
 
 // Define this so scripts can easily find doMaintenance.php
 define( 'RUN_MAINTENANCE_IF_MAIN', __DIR__ . '/doMaintenance.php' );
+
+/**
+ * @deprecated since 1.31
+ */
 define( 'DO_MAINTENANCE', RUN_MAINTENANCE_IF_MAIN ); // original name, harmless
 
 $maintClass = false;
@@ -1211,6 +1215,12 @@ abstract class Maintenance {
                        }
                        define( 'MW_DB', $bits[0] );
                        define( 'MW_PREFIX', $bits[1] );
+               } elseif ( isset( $this->mOptions['server'] ) ) {
+                       // Provide the option for site admins to detect and configure
+                       // multiple wikis based on server names. This offers --server
+                       // as alternative to --wiki.
+                       // See https://www.mediawiki.org/wiki/Manual:Wiki_family
+                       $_SERVER['SERVER_NAME'] = $this->mOptions['server'];
                }
 
                if ( !is_readable( $settingsFile ) ) {
@@ -1218,9 +1228,6 @@ abstract class Maintenance {
                                "must exist and be readable in the source directory.\n" .
                                "Use --conf to specify it." );
                }
-               if ( isset( $this->mOptions['server'] ) ) {
-                       $_SERVER['SERVER_NAME'] = $this->mOptions['server'];
-               }
                $wgCommandLineMode = true;
 
                return $settingsFile;
index ea5835d..2468c71 100644 (file)
        min-width: 20em;
 }
 
+/* Hide empty live-log textarea */
+#config-live-log textarea:empty {
+       display: none;
+}
+
 /* tooltip styles */
 .config-help-field-hint {
        display: none;
index 2e5efba..693cd7f 100644 (file)
@@ -6,6 +6,12 @@
        -ms-user-select: none;
        user-select: none;
 }
+.mw-collapsible-toggle:before {
+       content: '[';
+}
+.mw-collapsible-toggle:after {
+       content: ']';
+}
 /* Align the toggle based on the direction of the content language */
 /* @noflip */
 .mw-content-ltr .mw-collapsible-toggle,
index aa76d6d..7826bab 100644 (file)
@@ -8,7 +8,6 @@
  * @class jQuery.plugin.makeCollapsible
  */
 ( function ( $, mw ) {
-
        /**
         * Handler for a click on a collapsible toggler.
         *
                                                role: 'button',
                                                tabindex: 0
                                        } )
-                                       .prepend( '<span>[</span>' )
-                                       .append( '<span>]</span>' )
                                        .on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler );
                        };
 
index a9c2096..928b483 100644 (file)
@@ -1,5 +1,6 @@
 @import 'mediawiki.mixins';
 @import 'mediawiki.ui/variables';
+@import 'mw.rcfilters.variables';
 
 .mw-rcfilters-ui-filterMenuHeaderWidget {
        &-title {
 
                &-invert,
                &-highlight {
-                       width: 1em;
+                       min-width: 1em;
                        vertical-align: middle;
                        // Using the same padding that the filter item
                        // uses, so the button is aligned with the highlight
                        // buttons for the filters
-                       padding-right: 0.5em;
+                       padding-right: 12 / @font-size-system-ui / @font-size-vector;
                }
 
                &-back {
                        width: 1em;
                        vertical-align: middle;
-                       padding-left: 0.5em;
+
+                       .mw-rcfilters-ui-filterMenuHeaderWidget-backButton:first-child {
+                               // Overwrite `.oo-ui-buttonElement-frameless.oo-ui-iconElement:first-child`
+                               margin-left: 0;
+                       }
                }
 
                &-title {
index 0f858e6..07e43c0 100644 (file)
@@ -1,7 +1,12 @@
 @import 'mediawiki.mixins';
 @import 'mediawiki.ui/variables';
+@import 'mw.rcfilters.variables';
 
 .mw-rcfilters-ui-filterMenuOptionWidget {
+       .mw-rcfilters-ui-filterMenuSectionOptionWidget ~ & {
+               padding-left: 12 / @font-size-system-ui / @font-size-vector;
+       }
+
        &.oo-ui-flaggedElement-muted {
                &:not( .oo-ui-optionWidget-selected ) {
                        // Namespaces are muted 'the other way around' when they
                }
        }
 
+       // Override OOUI's pretty specific
+       // `.oo-ui-fieldLayout.oo-ui-labelElement.oo-ui-fieldLayout-align-inline > .oo-ui-fieldLayout-body > .oo-ui-fieldLayout-header`
+       // selector
+       .mw-rcfilters-ui-itemMenuOptionWidget-itemCheckbox > .oo-ui-fieldLayout > .oo-ui-fieldLayout-body > .oo-ui-fieldLayout-header {
+               padding-top: 0;
+               padding-bottom: 0;
+               padding-left: 12 / @font-size-system-ui / @font-size-vector;
+       }
 }
index 3e32c83..50073ff 100644 (file)
@@ -1,27 +1,25 @@
 @import 'mediawiki.mixins';
 @import 'mediawiki.ui/variables';
+@import 'mw.rcfilters.variables';
 
 .mw-rcfilters-ui-filterMenuSectionOptionWidget {
-       background: @colorGray14;
-       padding-bottom: 0.7em;
-
-       &-header {
-               padding: 0 0.75em;
-               // Use a high specificity to override OOUI
-               .oo-ui-optionWidget.oo-ui-labelElement &-title.oo-ui-labelElement-label {
-                       color: @colorGray5;
-                       .box-sizing( border-box );
-                       display: inline-block;
-               }
+       background-color: @colorGray14;
+       padding-bottom: 8 / @font-size-system-ui / @font-size-vector;
+       padding-left: 12 / @font-size-system-ui / @font-size-vector;
+       padding-right: 12 / @font-size-system-ui / @font-size-vector;
+
+       &-header-title.oo-ui-labelElement-label {
+               color: @colorGray5;
+               display: inline-block;
        }
 
        &-whatsThisButton {
                margin-left: 1.5em;
 
                &.oo-ui-buttonElement > .oo-ui-buttonElement-button {
-                       font-weight: normal;
                        border: 0; // Override OOUI `border` needed for frameless keyboard focus
                        padding: 0;
+                       font-weight: normal;
 
                        &:focus {
                                .box-shadow( none );
@@ -33,8 +31,8 @@
                        padding: 1em;
 
                        &-header {
-                               font-weight: bold;
                                margin-bottom: 1em;
+                               font-weight: bold;
                        }
 
                        &-link {
@@ -47,9 +45,7 @@
                }
        }
 
-       &-active {
-               .mw-rcfilters-ui-filterMenuSectionOptionWidget-header-title {
-                       font-weight: bold;
-               }
+       &-active .mw-rcfilters-ui-filterMenuSectionOptionWidget-header-title {
+               font-weight: bold;
        }
 }
index 21a169c..72b40fe 100644 (file)
@@ -12,7 +12,7 @@
        }
 
        &-view-namespaces {
-               border-top: 5px solid @colorGray12;
+               border-top: 4px solid @colorGray12;
 
                &:first-child,
                &.mw-rcfilters-ui-itemMenuOptionWidget-identifier-subject + &.mw-rcfilters-ui-itemMenuOptionWidget-identifier-talk {
@@ -25,7 +25,8 @@
        }
 
        .mw-rcfilters-ui-table {
-               padding-top: 0.5em;
+               padding-top: 6 / @font-size-system-ui / @font-size-vector;
+               padding-bottom: 6 / @font-size-system-ui / @font-size-vector;
        }
 
        &.oo-ui-optionWidget-selected {
        }
 
        &-itemCheckbox {
+               .oo-ui-fieldLayout-body > .oo-ui-fieldLayout-header {
+                       padding-left: 12 / @font-size-system-ui / @font-size-vector;
+               }
+
                .oo-ui-fieldLayout.oo-ui-fieldLayout-align-inline {
                        // Override margin-top and -bottom rules from FieldLayout
                        margin: 0 !important; /* stylelint-disable-line declaration-no-important */
index 0906d68..198c820 100644 (file)
@@ -1,5 +1,6 @@
 @import 'mediawiki.mixins';
 @import 'mediawiki.ui/variables';
+@import 'mw.rcfilters.variables';
 
 .mw-rcfilters-ui-menuSelectWidget {
        z-index: auto;
@@ -10,8 +11,8 @@
        }
 
        &-noresults {
-               padding: 0.5em;
                color: @colorGray5;
+               padding: 12 / @font-size-system-ui / @font-size-vector;
        }
 
        &-body {
@@ -19,9 +20,9 @@
        }
 
        &-footer {
-               padding: 0.5em;
                background-color: @colorGray15;
                border-top: 1px solid @colorGray12;
+               padding: 12 / @font-size-system-ui / @font-size-vector;
 
                & + & {
                        border-top: 0;
index 5f97e1e..987f525 100644 (file)
@@ -1,3 +1,8 @@
+// “External” variables
+@font-size-system-ui: 16; // Assumed browser default of `16px`
+@font-size-vector: 0.875em; // equals `14px` at browser default of `16px`
+
+// RCFilters variables
 @background-color-base: #fff;
 @background-color-primary: #eaf3ff;
 @color-base--inverted: #fff;
index d185b24..461de2f 100644 (file)
        min-height: @iconSize;
        min-width: @iconSize;
 
+       // If an inline element has been marked as a mw-ui-icon element it must be inline-block
+       span& {
+               display: inline-block;
+       }
+
        // Standalone icons
        //
        // Markup:
index 7d49a09..76d4bfb 100644 (file)
@@ -51,6 +51,9 @@
                // Parent constructor
                mw.widgets.TitleOptionWidget.parent.call( this, config );
 
+               // Remove check icon
+               this.checkIcon.$element.remove();
+
                // Initialization
                this.$label.attr( 'href', config.url );
                this.$element.addClass( 'mw-widget-titleOptionWidget' );
index 28335ec..844a43f 100644 (file)
@@ -615,9 +615,13 @@ class ParserTestRunner {
                        return false;
                } );// hooks::register
 
+               // Reset the service in case any other tests already cached some prefixes.
+               MediaWikiServices::getInstance()->resetServiceForTesting( 'InterwikiLookup' );
+
                return function () {
                        // Tear down
                        Hooks::clear( 'InterwikiLoadPrefix' );
+                       MediaWikiServices::getInstance()->resetServiceForTesting( 'InterwikiLookup' );
                };
        }
 
index 4d42498..8b2d099 100644 (file)
@@ -1995,61 +1995,6 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                return $loaded;
        }
 
-       /**
-        * Asserts that the given string is a valid HTML snippet.
-        * Wraps the given string in the required top level tags and
-        * then calls assertValidHtmlDocument().
-        * The snippet is expected to be HTML 5.
-        *
-        * @since 1.23
-        *
-        * @note Will mark the test as skipped if the "tidy" module is not installed.
-        * @note This ignores $wgUseTidy, so we can check for valid HTML even (and especially)
-        *        when automatic tidying is disabled.
-        *
-        * @param string $html An HTML snippet (treated as the contents of the body tag).
-        */
-       protected function assertValidHtmlSnippet( $html ) {
-               $html = '<!DOCTYPE html><html><head><title>test</title></head><body>' . $html . '</body></html>';
-               $this->assertValidHtmlDocument( $html );
-       }
-
-       /**
-        * Asserts that the given string is valid HTML document.
-        *
-        * @since 1.23
-        *
-        * @note Will mark the test as skipped if the "tidy" module is not installed.
-        * @note This ignores $wgUseTidy, so we can check for valid HTML even (and especially)
-        *        when automatic tidying is disabled.
-        *
-        * @param string $html A complete HTML document
-        */
-       protected function assertValidHtmlDocument( $html ) {
-               // Note: we only validate if the tidy PHP extension is available.
-               // In case wgTidyInternal is false, MWTidy would fall back to the command line version
-               // of tidy. In that case however, we can not reliably detect whether a failing validation
-               // is due to malformed HTML, or caused by tidy not being installed as a command line tool.
-               // That would cause all HTML assertions to fail on a system that has no tidy installed.
-               if ( !$GLOBALS['wgTidyInternal'] || !MWTidy::isEnabled() ) {
-                       $this->markTestSkipped( 'Tidy extension not installed' );
-               }
-
-               $errorBuffer = '';
-               MWTidy::checkErrors( $html, $errorBuffer );
-               $allErrors = preg_split( '/[\r\n]+/', $errorBuffer );
-
-               // Filter Tidy warnings which aren't useful for us.
-               // Tidy eg. often cries about parameters missing which have actually
-               // been deprecated since HTML4, thus we should not care about them.
-               $errors = preg_grep(
-                       '/^(.*Warning: (trimming empty|.* lacks ".*?" attribute).*|\s*)$/m',
-                       $allErrors, PREG_GREP_INVERT
-               );
-
-               $this->assertEmpty( $errors, implode( "\n", $errors ) );
-       }
-
        /**
         * Used as a marker to prevent wfResetOutputBuffers from breaking PHPUnit.
         * @param string $buffer
index 4818b49..0c178ca 100644 (file)
@@ -107,4 +107,19 @@ class TestUserRegistry {
        public static function clear() {
                self::$testUsers = [];
        }
+
+       /**
+        * @todo It would be nice if this were a non-static method of TestUser
+        * instead, but that doesn't seem possible without friends?
+        *
+        * @return bool True if it's safe to modify the user
+        */
+       public static function isMutable( User $user ) {
+               foreach ( self::$testUsers as $key => $testUser ) {
+                       if ( $user === $testUser->getUser() ) {
+                               return false;
+                       }
+               }
+               return true;
+       }
 }
index 8ffe4fc..d17334b 100644 (file)
@@ -46,7 +46,7 @@ class ApiMainTest extends ApiTestCase {
         */
        private function getNonInternalApiMain( array $requestData, array $headers = [] ) {
                $req = $this->getMockBuilder( WebRequest::class )
-                       ->setMethods( [ 'response', 'getIP' ] )
+                       ->setMethods( [ 'response', 'getRawIP' ] )
                        ->getMock();
                $response = new FauxResponse();
                $req->method( 'response' )->willReturn( $response );
index e236437..a04271f 100644 (file)
@@ -13,48 +13,136 @@ class ApiParseTest extends ApiTestCase {
        protected static $revIds = [];
 
        public function addDBDataOnce() {
-               $user = static::getTestSysop()->getUser();
                $title = Title::newFromText( __CLASS__ );
-               $page = WikiPage::factory( $title );
 
-               $status = $page->doEditContent(
-                       ContentHandler::makeContent( 'Test for revdel', $title, CONTENT_MODEL_WIKITEXT ),
-                       __METHOD__ . ' Test for revdel', 0, false, $user
-               );
-               if ( !$status->isOK() ) {
-                       $this->fail( "Failed to create $title: " . $status->getWikiText( false, false, 'en' ) );
-               }
+               $status = $this->editPage( __CLASS__, 'Test for revdel' );
                self::$pageId = $status->value['revision']->getPage();
                self::$revIds['revdel'] = $status->value['revision']->getId();
 
-               $status = $page->doEditContent(
-                       ContentHandler::makeContent( 'Test for oldid', $title, CONTENT_MODEL_WIKITEXT ),
-                       __METHOD__ . ' Test for oldid', 0, false, $user
-               );
-               if ( !$status->isOK() ) {
-                       $this->fail( "Failed to edit $title: " . $status->getWikiText( false, false, 'en' ) );
-               }
+               $status = $this->editPage( __CLASS__, 'Test for suppressed' );
+               self::$revIds['suppressed'] = $status->value['revision']->getId();
+
+               $status = $this->editPage( __CLASS__, 'Test for oldid' );
                self::$revIds['oldid'] = $status->value['revision']->getId();
 
-               $status = $page->doEditContent(
-                       ContentHandler::makeContent( 'Test for latest', $title, CONTENT_MODEL_WIKITEXT ),
-                       __METHOD__ . ' Test for latest', 0, false, $user
+               $status = $this->editPage( __CLASS__, 'Test for latest' );
+               self::$revIds['latest'] = $status->value['revision']->getId();
+
+               $this->revisionDelete( self::$revIds['revdel'] );
+               $this->revisionDelete(
+                       self::$revIds['suppressed'],
+                       [ Revision::DELETED_TEXT => 1, Revision::DELETED_RESTRICTED => 1 ]
                );
-               if ( !$status->isOK() ) {
-                       $this->fail( "Failed to edit $title: " . $status->getWikiText( false, false, 'en' ) );
+
+               Title::clearCaches(); // Otherwise it has the wrong latest revision for some reason
+       }
+
+       /**
+        * Assert that the given result of calling $this->doApiRequest() with
+        * action=parse resulted in $html, accounting for the boilerplate that the
+        * parser adds around the parsed page.  Also asserts that warnings match
+        * the provided $warning.
+        *
+        * @param string $html Expected HTML
+        * @param array $res Returned from doApiRequest()
+        * @param string|null $warnings Exact value of expected warnings, null for
+        *   no warnings
+        */
+       protected function assertParsedTo( $expected, array $res, $warnings = null ) {
+               $this->doAssertParsedTo( $expected, $res, $warnings, [ $this, 'assertSame' ] );
+       }
+
+       /**
+        * Same as above, but asserts that the HTML matches a regexp instead of a
+        * literal string match.
+        *
+        * @param string $html Expected HTML
+        * @param array $res Returned from doApiRequest()
+        * @param string|null $warnings Exact value of expected warnings, null for
+        *   no warnings
+        */
+       protected function assertParsedToRegExp( $expected, array $res, $warnings = null ) {
+               $this->doAssertParsedTo( $expected, $res, $warnings, [ $this, 'assertRegExp' ] );
+       }
+
+       private function doAssertParsedTo( $expected, array $res, $warnings, callable $callback ) {
+               $html = $res[0]['parse']['text'];
+
+               $expectedStart = '<div class="mw-parser-output">';
+               $this->assertSame( $expectedStart, substr( $html, 0, strlen( $expectedStart ) ) );
+
+               $html = substr( $html, strlen( $expectedStart ) );
+
+               if ( $res[1]->getBool( 'disablelimitreport' ) ) {
+                       $expectedEnd = "</div>";
+                       $this->assertSame( $expectedEnd, substr( $html, -strlen( $expectedEnd ) ) );
+
+                       $html = substr( $html, 0, strlen( $html ) - strlen( $expectedEnd ) );
+               } else {
+                       $expectedEnd = '#\n<!-- \nNewPP limit report\n(?>.+?\n-->)\n' .
+                               '<!--\nTransclusion expansion time report \(%,ms,calls,template\)\n(?>.*?\n-->)\n' .
+                               '</div>(\n<!-- Saved in parser cache (?>.*?\n -->)\n)?$#s';
+                       $this->assertRegExp( $expectedEnd, $html );
+
+                       $html = preg_replace( $expectedEnd, '', $html );
                }
-               self::$revIds['latest'] = $status->value['revision']->getId();
 
-               RevisionDeleter::createList(
-                       'revision', RequestContext::getMain(), $title, [ self::$revIds['revdel'] ]
-               )->setVisibility( [
-                       'value' => [
-                               Revision::DELETED_TEXT => 1,
+               call_user_func( $callback, $expected, $html );
+
+               if ( $warnings === null ) {
+                       $this->assertCount( 1, $res[0] );
+               } else {
+                       $this->assertCount( 2, $res[0] );
+                       // This deliberately fails if there are extra warnings
+                       $this->assertSame( [ 'parse' => [ 'warnings' => $warnings ] ], $res[0]['warnings'] );
+               }
+       }
+
+       /**
+        * Set up an interwiki entry for testing.
+        */
+       protected function setupInterwiki() {
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->insert(
+                       'interwiki',
+                       [
+                               'iw_prefix' => 'madeuplanguage',
+                               'iw_url' => "https://example.com/wiki/$1",
+                               'iw_api' => '',
+                               'iw_wikiid' => '',
+                               'iw_local' => false,
                        ],
-                       'comment' => 'Test for revdel',
-               ] );
+                       __METHOD__,
+                       'IGNORE'
+               );
 
-               Title::clearCaches(); // Otherwise it has the wrong latest revision for some reason
+               $this->setMwGlobals( 'wgExtraInterlanguageLinkPrefixes', [ 'madeuplanguage' ] );
+               $this->tablesUsed[] = 'interwiki';
+       }
+
+       /**
+        * Set up a skin for testing.
+        *
+        * @todo Should this code be in MediaWikiTestCase or something?
+        */
+       protected function setupSkin() {
+               $factory = new SkinFactory();
+               $factory->register( 'testing', 'Testing', function () {
+                       $skin = $this->getMockBuilder( SkinFallback::class )
+                               ->setMethods( [ 'getDefaultModules', 'setupSkinUserCss' ] )
+                               ->getMock();
+                       $skin->expects( $this->once() )->method( 'getDefaultModules' )
+                               ->willReturn( [
+                                       'core' => [ 'foo', 'bar' ],
+                                       'content' => [ 'baz' ]
+                               ] );
+                       $skin->expects( $this->once() )->method( 'setupSkinUserCss' )
+                               ->will( $this->returnCallback( function ( OutputPage $out ) {
+                                       $out->addModuleStyles( 'foo.styles' );
+                               } ) );
+                       return $skin;
+               } );
+               $this->setService( 'SkinFactory', $factory );
        }
 
        public function testParseByName() {
@@ -62,14 +150,14 @@ class ApiParseTest extends ApiTestCase {
                        'action' => 'parse',
                        'page' => __CLASS__,
                ] );
-               $this->assertContains( 'Test for latest', $res[0]['parse']['text'] );
+               $this->assertParsedTo( "<p>Test for latest\n</p>", $res );
 
                $res = $this->doApiRequest( [
                        'action' => 'parse',
                        'page' => __CLASS__,
                        'disablelimitreport' => 1,
                ] );
-               $this->assertContains( 'Test for latest', $res[0]['parse']['text'] );
+               $this->assertParsedTo( "<p>Test for latest\n</p>", $res );
        }
 
        public function testParseById() {
@@ -77,7 +165,7 @@ class ApiParseTest extends ApiTestCase {
                        'action' => 'parse',
                        'pageid' => self::$pageId,
                ] );
-               $this->assertContains( 'Test for latest', $res[0]['parse']['text'] );
+               $this->assertParsedTo( "<p>Test for latest\n</p>", $res );
        }
 
        public function testParseByOldId() {
@@ -85,36 +173,46 @@ class ApiParseTest extends ApiTestCase {
                        'action' => 'parse',
                        'oldid' => self::$revIds['oldid'],
                ] );
-               $this->assertContains( 'Test for oldid', $res[0]['parse']['text'] );
+               $this->assertParsedTo( "<p>Test for oldid\n</p>", $res );
                $this->assertArrayNotHasKey( 'textdeleted', $res[0]['parse'] );
                $this->assertArrayNotHasKey( 'textsuppressed', $res[0]['parse'] );
        }
 
-       public function testParseRevDel() {
-               $user = static::getTestUser()->getUser();
-               $sysop = static::getTestSysop()->getUser();
-
-               try {
-                       $this->doApiRequest( [
-                               'action' => 'parse',
-                               'oldid' => self::$revIds['revdel'],
-                       ], null, null, $user );
-                       $this->fail( "API did not return an error as expected" );
-               } catch ( ApiUsageException $ex ) {
-                       $this->assertTrue( ApiTestCase::apiExceptionHasCode( $ex, 'permissiondenied' ),
-                               "API failed with error 'permissiondenied'" );
-               }
-
+       public function testRevDel() {
                $res = $this->doApiRequest( [
                        'action' => 'parse',
                        'oldid' => self::$revIds['revdel'],
-               ], null, null, $sysop );
-               $this->assertContains( 'Test for revdel', $res[0]['parse']['text'] );
+               ] );
+
+               $this->assertParsedTo( "<p>Test for revdel\n</p>", $res );
                $this->assertArrayHasKey( 'textdeleted', $res[0]['parse'] );
                $this->assertArrayNotHasKey( 'textsuppressed', $res[0]['parse'] );
        }
 
-       public function testParseNonexistentPage() {
+       public function testRevDelNoPermission() {
+               $this->setExpectedException( ApiUsageException::class,
+                       "You don't have permission to view deleted revision text." );
+
+               $this->doApiRequest( [
+                       'action' => 'parse',
+                       'oldid' => self::$revIds['revdel'],
+               ], null, null, static::getTestUser()->getUser() );
+       }
+
+       public function testSuppressed() {
+               $this->setGroupPermissions( 'sysop', 'viewsuppressed', true );
+
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'oldid' => self::$revIds['suppressed']
+               ] );
+
+               $this->assertParsedTo( "<p>Test for suppressed\n</p>", $res );
+               $this->assertArrayHasKey( 'textsuppressed', $res[0]['parse'] );
+               $this->assertArrayHasKey( 'textdeleted', $res[0]['parse'] );
+       }
+
+       public function testNonexistentPage() {
                try {
                        $this->doApiRequest( [
                                'action' => 'parse',
@@ -130,24 +228,446 @@ class ApiParseTest extends ApiTestCase {
                }
        }
 
-       public function testSkinModules() {
-               $factory = new SkinFactory();
-               $factory->register( 'testing', 'Testing', function () {
-                       $skin = $this->getMockBuilder( SkinFallback::class )
-                               ->setMethods( [ 'getDefaultModules', 'setupSkinUserCss' ] )
-                               ->getMock();
-                       $skin->expects( $this->once() )->method( 'getDefaultModules' )
-                               ->willReturn( [
-                                       'core' => [ 'foo', 'bar' ],
-                                       'content' => [ 'baz' ]
-                               ] );
-                       $skin->expects( $this->once() )->method( 'setupSkinUserCss' )
-                               ->will( $this->returnCallback( function ( OutputPage $out ) {
-                                       $out->addModuleStyles( 'foo.styles' );
-                               } ) );
-                       return $skin;
-               } );
-               $this->setService( 'SkinFactory', $factory );
+       public function testTitleProvided() {
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'title' => 'Some interesting page',
+                       'text' => '{{PAGENAME}} has attracted my attention',
+               ] );
+
+               $this->assertParsedTo( "<p>Some interesting page has attracted my attention\n</p>", $res );
+       }
+
+       public function testSection() {
+               $name = ucfirst( __FUNCTION__ );
+
+               $this->editPage( $name,
+                       "Intro\n\n== Section 1 ==\n\nContent 1\n\n== Section 2 ==\n\nContent 2" );
+
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'page' => $name,
+                       'section' => 1,
+               ] );
+
+               $this->assertParsedToRegExp( '!<h2>.*Section 1.*</h2>\n<p>Content 1\n</p>!', $res );
+       }
+
+       public function testInvalidSection() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'The "section" parameter must be a valid section ID or "new".' );
+
+               $this->doApiRequest( [
+                       'action' => 'parse',
+                       'section' => 'T-new',
+               ] );
+       }
+
+       public function testSectionNoContent() {
+               $name = ucfirst( __FUNCTION__ );
+
+               $status = $this->editPage( $name,
+                       "Intro\n\n== Section 1 ==\n\nContent 1\n\n== Section 2 ==\n\nContent 2" );
+
+               $this->setExpectedException( ApiUsageException::class,
+                       "Missing content for page ID {$status->value['revision']->getPage()}." );
+
+               $this->db->delete( 'revision', [ 'rev_id' => $status->value['revision']->getId() ] );
+
+               // Suppress warning in WikiPage::getContentModel
+               Wikimedia\suppressWarnings();
+               try {
+                       $this->doApiRequest( [
+                               'action' => 'parse',
+                               'page' => $name,
+                               'section' => 1,
+                       ] );
+               } finally {
+                       Wikimedia\restoreWarnings();
+               }
+       }
+
+       public function testNewSectionWithPage() {
+               $this->setExpectedException( ApiUsageException::class,
+                       '"section=new" cannot be combined with the "oldid", "pageid" or "page" ' .
+                       'parameters. Please use "title" and "text".' );
+
+               $this->doApiRequest( [
+                       'action' => 'parse',
+                       'page' => __CLASS__,
+                       'section' => 'new',
+               ] );
+       }
+
+       public function testNonexistentOldId() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'There is no revision with ID 2147483647.' );
+
+               $this->doApiRequest( [
+                       'action' => 'parse',
+                       'oldid' => pow( 2, 31 ) - 1,
+               ] );
+       }
+
+       public function testUnfollowedRedirect() {
+               $name = ucfirst( __FUNCTION__ );
+
+               $this->editPage( $name, "#REDIRECT [[$name 2]]" );
+               $this->editPage( "$name 2", "Some ''text''" );
+
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'page' => $name,
+               ] );
+
+               // Can't use assertParsedTo because the parser output is different for
+               // redirects
+               $this->assertRegExp( "/Redirect to:.*$name 2/", $res[0]['parse']['text'] );
+               $this->assertArrayNotHasKey( 'warnings', $res[0] );
+       }
+
+       public function testFollowedRedirect() {
+               $name = ucfirst( __FUNCTION__ );
+
+               $this->editPage( $name, "#REDIRECT [[$name 2]]" );
+               $this->editPage( "$name 2", "Some ''text''" );
+
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'page' => $name,
+                       'redirects' => true,
+               ] );
+
+               $this->assertParsedTo( "<p>Some <i>text</i>\n</p>", $res );
+       }
+
+       public function testFollowedRedirectById() {
+               $name = ucfirst( __FUNCTION__ );
+
+               $id = $this->editPage( $name, "#REDIRECT [[$name 2]]" )->value['revision']->getPage();
+               $this->editPage( "$name 2", "Some ''text''" );
+
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'pageid' => $id,
+                       'redirects' => true,
+               ] );
+
+               $this->assertParsedTo( "<p>Some <i>text</i>\n</p>", $res );
+       }
+
+       public function testInvalidTitle() {
+               $this->setExpectedException( ApiUsageException::class, 'Bad title "|".' );
+
+               $this->doApiRequest( [
+                       'action' => 'parse',
+                       'title' => '|',
+               ] );
+       }
+
+       public function testTitleWithNonexistentRevId() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'There is no revision with ID 2147483647.' );
+
+               $this->doApiRequest( [
+                       'action' => 'parse',
+                       'title' => __CLASS__,
+                       'revid' => pow( 2, 31 ) - 1,
+               ] );
+       }
+
+       public function testTitleWithNonMatchingRevId() {
+               $name = ucfirst( __FUNCTION__ );
+
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'title' => $name,
+                       'revid' => self::$revIds['latest'],
+                       'text' => 'Some text',
+               ] );
+
+               $this->assertParsedTo( "<p>Some text\n</p>", $res,
+                       'r' . self::$revIds['latest'] . " is not a revision of $name." );
+       }
+
+       public function testRevId() {
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'revid' => self::$revIds['latest'],
+                       'text' => 'My revid is {{REVISIONID}}!',
+               ] );
+
+               $this->assertParsedTo( "<p>My revid is " . self::$revIds['latest'] . "!\n</p>", $res );
+       }
+
+       public function testTitleNoText() {
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'title' => 'Special:AllPages',
+               ] );
+
+               $this->assertParsedTo( '', $res,
+                       '"title" used without "text", and parsed page properties were requested. ' .
+                               'Did you mean to use "page" instead of "title"?' );
+       }
+
+       public function testRevidNoText() {
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'revid' => self::$revIds['latest'],
+               ] );
+
+               $this->assertParsedTo( '', $res,
+                       '"revid" used without "text", and parsed page properties were requested. ' .
+                               'Did you mean to use "oldid" instead of "revid"?' );
+       }
+
+       public function testTextNoContentModel() {
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'text' => "Some ''text''",
+               ] );
+
+               $this->assertParsedTo( "<p>Some <i>text</i>\n</p>", $res,
+                       'No "title" or "contentmodel" was given, assuming wikitext.' );
+       }
+
+       public function testSerializationError() {
+               $this->setExpectedException( APIUsageException::class,
+                       'Content serialization failed: Could not unserialize content' );
+
+               $this->mergeMwGlobalArrayValue( 'wgContentHandlers',
+                       [ 'testing-serialize-error' => 'DummySerializeErrorContentHandler' ] );
+
+               $this->doApiRequest( [
+                       'action' => 'parse',
+                       'text' => "Some ''text''",
+                       'contentmodel' => 'testing-serialize-error',
+               ] );
+       }
+
+       public function testNewSection() {
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'title' => __CLASS__,
+                       'section' => 'new',
+                       'sectiontitle' => 'Title',
+                       'text' => 'Content',
+               ] );
+
+               $this->assertParsedToRegExp( '!<h2>.*Title.*</h2>\n<p>Content\n</p>!', $res );
+       }
+
+       public function testExistingSection() {
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'title' => __CLASS__,
+                       'section' => 1,
+                       'text' => "Intro\n\n== Section 1 ==\n\nContent\n\n== Section 2 ==\n\nMore content",
+               ] );
+
+               $this->assertParsedToRegExp( '!<h2>.*Section 1.*</h2>\n<p>Content\n</p>!', $res );
+       }
+
+       public function testNoPst() {
+               $name = ucfirst( __FUNCTION__ );
+
+               $this->editPage( "Template:$name", "Template ''text''" );
+
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'text' => "{{subst:$name}}",
+                       'contentmodel' => 'wikitext',
+               ] );
+
+               $this->assertParsedTo( "<p>{{subst:$name}}\n</p>", $res );
+       }
+
+       public function testPst() {
+               $name = ucfirst( __FUNCTION__ );
+
+               $this->editPage( "Template:$name", "Template ''text''" );
+
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'pst' => '',
+                       'text' => "{{subst:$name}}",
+                       'contentmodel' => 'wikitext',
+                       'prop' => 'text|wikitext',
+               ] );
+
+               $this->assertParsedTo( "<p>Template <i>text</i>\n</p>", $res );
+               $this->assertSame( "{{subst:$name}}", $res[0]['parse']['wikitext'] );
+       }
+
+       public function testOnlyPst() {
+               $name = ucfirst( __FUNCTION__ );
+
+               $this->editPage( "Template:$name", "Template ''text''" );
+
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'onlypst' => '',
+                       'text' => "{{subst:$name}}",
+                       'contentmodel' => 'wikitext',
+                       'prop' => 'text|wikitext',
+                       'summary' => 'Summary',
+               ] );
+
+               $this->assertSame(
+                       [ 'parse' => [
+                               'text' => "Template ''text''",
+                               'wikitext' => "{{subst:$name}}",
+                               'parsedsummary' => 'Summary',
+                       ] ],
+                       $res[0]
+               );
+       }
+
+       public function testHeadHtml() {
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'page' => __CLASS__,
+                       'prop' => 'headhtml',
+               ] );
+
+               // Just do a rough sanity check
+               $this->assertRegExp( '#<!DOCTYPE.*<html.*<head.*</head>.*<body#s',
+                       $res[0]['parse']['headhtml'] );
+               $this->assertArrayNotHasKey( 'warnings', $res[0] );
+       }
+
+       public function testCategoriesHtml() {
+               $name = ucfirst( __FUNCTION__ );
+
+               $this->editPage( $name, "[[Category:$name]]" );
+
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'page' => $name,
+                       'prop' => 'categorieshtml',
+               ] );
+
+               $this->assertRegExp( "#Category.*Category:$name.*$name#",
+                       $res[0]['parse']['categorieshtml'] );
+               $this->assertArrayNotHasKey( 'warnings', $res[0] );
+       }
+
+       public function testEffectiveLangLinks() {
+               $hookRan = false;
+               $this->setTemporaryHook( 'LanguageLinks',
+                       function () use ( &$hookRan ) {
+                               $hookRan = true;
+                       }
+               );
+
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'title' => __CLASS__,
+                       'text' => '[[zh:' . __CLASS__ . ']]',
+                       'effectivelanglinks' => '',
+               ] );
+
+               $this->assertTrue( $hookRan );
+               $this->assertSame( 'The parameter "effectivelanglinks" has been deprecated.',
+                       $res[0]['warnings']['parse']['warnings'] );
+       }
+
+       /**
+        * @param array $arr Extra params to add to API request
+        */
+       private function doTestLangLinks( array $arr = [] ) {
+               $this->setupInterwiki();
+
+               $res = $this->doApiRequest( array_merge( [
+                       'action' => 'parse',
+                       'title' => 'Omelette',
+                       'text' => '[[madeuplanguage:Omelette]]',
+                       'prop' => 'langlinks',
+               ], $arr ) );
+
+               $langLinks = $res[0]['parse']['langlinks'];
+
+               $this->assertCount( 1, $langLinks );
+               $this->assertSame( 'madeuplanguage', $langLinks[0]['lang'] );
+               $this->assertSame( 'Omelette', $langLinks[0]['title'] );
+               $this->assertSame( 'https://example.com/wiki/Omelette', $langLinks[0]['url'] );
+               $this->assertArrayNotHasKey( 'warnings', $res[0] );
+       }
+
+       public function testLangLinks() {
+               $this->doTestLangLinks();
+       }
+
+       public function testLangLinksWithSkin() {
+               $this->setupSkin();
+               $this->doTestLangLinks( [ 'useskin' => 'testing' ] );
+       }
+
+       public function testHeadItems() {
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'title' => __CLASS__,
+                       'text' => '',
+                       'prop' => 'headitems',
+               ] );
+
+               $this->assertSame( [], $res[0]['parse']['headitems'] );
+               $this->assertSame(
+                       '"prop=headitems" is deprecated since MediaWiki 1.28. ' .
+                               'Use "prop=headhtml" when creating new HTML documents, ' .
+                               'or "prop=modules|jsconfigvars" when updating a document client-side.',
+                       $res[0]['warnings']['parse']['warnings']
+               );
+       }
+
+       public function testHeadItemsWithSkin() {
+               $this->setupSkin();
+
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'title' => __CLASS__,
+                       'text' => '',
+                       'prop' => 'headitems',
+                       'useskin' => 'testing',
+               ] );
+
+               $this->assertSame( [], $res[0]['parse']['headitems'] );
+               $this->assertSame(
+                       '"prop=headitems" is deprecated since MediaWiki 1.28. ' .
+                               'Use "prop=headhtml" when creating new HTML documents, ' .
+                               'or "prop=modules|jsconfigvars" when updating a document client-side.',
+                       $res[0]['warnings']['parse']['warnings']
+               );
+       }
+
+       public function testModules() {
+               $this->setTemporaryHook( 'ParserAfterParse',
+                       function ( $parser ) {
+                               $output = $parser->getOutput();
+                               $output->addModules( [ 'foo', 'bar' ] );
+                               $output->addModuleScripts( [ 'baz', 'quuz' ] );
+                               $output->addModuleStyles( [ 'aaa', 'zzz' ] );
+                               $output->addJsConfigVars( [ 'x' => 'y', 'z' => -3 ] );
+                       }
+               );
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'title' => __CLASS__,
+                       'text' => 'Content',
+                       'prop' => 'modules|jsconfigvars|encodedjsconfigvars',
+               ] );
+
+               $this->assertSame( [ 'foo', 'bar' ], $res[0]['parse']['modules'] );
+               $this->assertSame( [ 'baz', 'quuz' ], $res[0]['parse']['modulescripts'] );
+               $this->assertSame( [ 'aaa', 'zzz' ], $res[0]['parse']['modulestyles'] );
+               $this->assertSame( [ 'x' => 'y', 'z' => -3 ], $res[0]['parse']['jsconfigvars'] );
+               $this->assertSame( '{"x":"y","z":-3}', $res[0]['parse']['encodedjsconfigvars'] );
+               $this->assertArrayNotHasKey( 'warnings', $res[0] );
+       }
+
+       public function testModulesWithSkin() {
+               $this->setupSkin();
 
                $res = $this->doApiRequest( [
                        'action' => 'parse',
@@ -170,5 +690,160 @@ class ApiParseTest extends ApiTestCase {
                        $res[0]['parse']['modulestyles'],
                        'resp.parse.modulestyles'
                );
+               $this->assertSame(
+                       [ 'parse' =>
+                               [ 'warnings' =>
+                                       'Property "modules" was set but not "jsconfigvars" or ' .
+                                       '"encodedjsconfigvars". Configuration variables are necessary for ' .
+                                       'proper module usage.'
+                               ]
+                       ],
+                       $res[0]['warnings']
+               );
+       }
+
+       public function testIndicators() {
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'title' => __CLASS__,
+                       'text' =>
+                               '<indicator name="b">BBB!</indicator>Some text<indicator name="a">aaa</indicator>',
+                       'prop' => 'indicators',
+               ] );
+
+               $this->assertSame(
+                       // It seems we return in markup order and not display order
+                       [ 'b' => 'BBB!', 'a' => 'aaa' ],
+                       $res[0]['parse']['indicators']
+               );
+               $this->assertArrayNotHasKey( 'warnings', $res[0] );
+       }
+
+       public function testIndicatorsWithSkin() {
+               $this->setupSkin();
+
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'title' => __CLASS__,
+                       'text' =>
+                               '<indicator name="b">BBB!</indicator>Some text<indicator name="a">aaa</indicator>',
+                       'prop' => 'indicators',
+                       'useskin' => 'testing',
+               ] );
+
+               $this->assertSame(
+                       // Now we return in display order rather than markup order
+                       [ 'a' => 'aaa', 'b' => 'BBB!' ],
+                       $res[0]['parse']['indicators']
+               );
+               $this->assertArrayNotHasKey( 'warnings', $res[0] );
+       }
+
+       public function testIwlinks() {
+               $this->setupInterwiki();
+
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'title' => 'Omelette',
+                       'text' => '[[:madeuplanguage:Omelette]][[madeuplanguage:Spaghetti]]',
+                       'prop' => 'iwlinks',
+               ] );
+
+               $iwlinks = $res[0]['parse']['iwlinks'];
+
+               $this->assertCount( 1, $iwlinks );
+               $this->assertSame( 'madeuplanguage', $iwlinks[0]['prefix'] );
+               $this->assertSame( 'https://example.com/wiki/Omelette', $iwlinks[0]['url'] );
+               $this->assertSame( 'madeuplanguage:Omelette', $iwlinks[0]['title'] );
+               $this->assertArrayNotHasKey( 'warnings', $res[0] );
+       }
+
+       public function testLimitReports() {
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'pageid' => self::$pageId,
+                       'prop' => 'limitreportdata|limitreporthtml',
+               ] );
+
+               // We don't bother testing the actual values here
+               $this->assertInternalType( 'array', $res[0]['parse']['limitreportdata'] );
+               $this->assertInternalType( 'string', $res[0]['parse']['limitreporthtml'] );
+               $this->assertArrayNotHasKey( 'warnings', $res[0] );
+       }
+
+       public function testParseTreeNonWikitext() {
+               $this->setExpectedException( ApiUsageException::class,
+                       '"prop=parsetree" is only supported for wikitext content.' );
+
+               $this->doApiRequest( [
+                       'action' => 'parse',
+                       'text' => '',
+                       'contentmodel' => 'json',
+                       'prop' => 'parsetree',
+               ] );
+       }
+
+       public function testParseTree() {
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'text' => "Some ''text'' is {{nice|to have|i=think}}",
+                       'contentmodel' => 'wikitext',
+                       'prop' => 'parsetree',
+               ] );
+
+               // Preprocessor_DOM and Preprocessor_Hash give different results here,
+               // so we'll accept either
+               $this->assertRegExp(
+                       '#^<root>Some \'\'text\'\' is <template><title>nice</title>' .
+                               '<part><name index="1"/><value>to have</value></part>' .
+                               '<part><name>i</name>(?:<equals>)?=(?:</equals>)?<value>think</value></part>' .
+                               '</template></root>$#',
+                       $res[0]['parse']['parsetree']
+               );
+               $this->assertArrayNotHasKey( 'warnings', $res[0] );
+       }
+
+       public function testDisableTidy() {
+               $this->setMwGlobals( 'wgTidyConfig', [ 'driver' => 'RemexHtml' ] );
+
+               // Check that disabletidy doesn't have an effect just because tidying
+               // doesn't work for some other reason
+               $res1 = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'text' => "<b>Mixed <i>up</b></i>",
+                       'contentmodel' => 'wikitext',
+               ] );
+               $this->assertParsedTo( "<p><b>Mixed <i>up</i></b>\n</p>", $res1 );
+
+               $res2 = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'text' => "<b>Mixed <i>up</b></i>",
+                       'contentmodel' => 'wikitext',
+                       'disabletidy' => '',
+               ] );
+
+               $this->assertParsedTo( "<p><b>Mixed <i>up</b></i>\n</p>", $res2 );
+       }
+
+       public function testFormatCategories() {
+               $name = ucfirst( __FUNCTION__ );
+
+               $this->editPage( "Category:$name", 'Content' );
+               $this->editPage( 'Category:Hidden', '__HIDDENCAT__' );
+
+               $res = $this->doApiRequest( [
+                       'action' => 'parse',
+                       'title' => __CLASS__,
+                       'text' => "[[Category:$name]][[Category:Foo|Sort me]][[Category:Hidden]]",
+                       'prop' => 'categories',
+               ] );
+
+               $this->assertSame(
+                       [ [ 'sortkey' => '', 'category' => $name ],
+                               [ 'sortkey' => 'Sort me', 'category' => 'Foo', 'missing' => true ],
+                               [ 'sortkey' => '', 'category' => 'Hidden', 'hidden' => true ] ],
+                       $res[0]['parse']['categories']
+               );
+               $this->assertArrayNotHasKey( 'warnings', $res[0] );
        }
 }
index 31c8136..a5ee7dd 100644 (file)
@@ -56,6 +56,28 @@ abstract class ApiTestCase extends MediaWikiLangTestCase {
                return $page->doEditContent( ContentHandler::makeContent( $text, $title ), $summary );
        }
 
+       /**
+        * Revision-deletes a revision.
+        *
+        * @param Revision|int $rev Revision to delete
+        * @param array $value Keys are Revision::DELETED_* flags.  Values are 1 to set the bit, 0 to
+        *   clear, -1 to leave alone.  (All other values also clear the bit.)
+        * @param string $comment Deletion comment
+        */
+       protected function revisionDelete(
+               $rev, array $value = [ Revision::DELETED_TEXT => 1 ], $comment = ''
+       ) {
+               if ( is_int( $rev ) ) {
+                       $rev = Revision::newFromId( $rev );
+               }
+               RevisionDeleter::createList(
+                       'revision', RequestContext::getMain(), $rev->getTitle(), [ $rev->getId() ]
+               )->setVisibility( [
+                       'value' => $value,
+                       'comment' => $comment,
+               ] );
+       }
+
        /**
         * Does the API request and returns the result.
         *
@@ -99,6 +121,10 @@ abstract class ApiTestCase extends MediaWikiLangTestCase {
                }
 
                if ( $tokenType !== null ) {
+                       if ( $tokenType === 'auto' ) {
+                               $tokenType = ( new ApiMain() )->getModuleManager()
+                                       ->getModule( $params['action'], 'action' )->needsToken();
+                       }
                        $params['token'] = ApiQueryTokens::getToken(
                                $wgUser, $sessionObj, ApiQueryTokens::getTokenTypeSalts()[$tokenType]
                        )->toString();
@@ -142,7 +168,7 @@ abstract class ApiTestCase extends MediaWikiLangTestCase {
         * @return array Result of the API call
         */
        protected function doApiRequestWithToken( array $params, array $session = null,
-               User $user = null, $tokenType = 'csrf'
+               User $user = null, $tokenType = 'auto'
        ) {
                return $this->doApiRequest( $params, $session, false, $user, $tokenType );
        }
diff --git a/tests/phpunit/includes/api/ApiUserrightsTest.php b/tests/phpunit/includes/api/ApiUserrightsTest.php
new file mode 100644 (file)
index 0000000..0229e76
--- /dev/null
@@ -0,0 +1,358 @@
+<?php
+
+/**
+ * @group API
+ * @group Database
+ * @group medium
+ *
+ * @covers ApiUserrights
+ */
+class ApiUserrightsTest extends ApiTestCase {
+       /**
+        * Unsets $wgGroupPermissions['bureaucrat']['userrights'], and sets
+        * $wgAddGroups['bureaucrat'] and $wgRemoveGroups['bureaucrat'] to the
+        * specified values.
+        *
+        * @param array|bool $add Groups bureaucrats should be allowed to add, true for all
+        * @param array|bool $remove Groups bureaucrats should be allowed to remove, true for all
+        */
+       protected function setPermissions( $add = [], $remove = [] ) {
+               global $wgAddGroups, $wgRemoveGroups;
+
+               $this->setGroupPermissions( 'bureaucrat', 'userrights', false );
+
+               if ( $add ) {
+                       $this->stashMwGlobals( 'wgAddGroups' );
+                       $wgAddGroups['bureaucrat'] = $add;
+               }
+               if ( $remove ) {
+                       $this->stashMwGlobals( 'wgRemoveGroups' );
+                       $wgRemoveGroups['bureaucrat'] = $remove;
+               }
+       }
+
+       /**
+        * Perform an API userrights request that's expected to be successful.
+        *
+        * @param array|string $expectedGroups Group(s) that the user is expected
+        *   to have after the API request
+        * @param array $params Array to pass to doApiRequestWithToken().  'action'
+        *   => 'userrights' is implicit.  If no 'user' or 'userid' is specified,
+        *   we add a 'user' parameter.  If no 'add' or 'remove' is specified, we
+        *   add 'add' => 'sysop'.
+        * @param User|null $user The user that we're modifying.  The user must be
+        *   mutable, because we're going to change its groups!  null means that
+        *   we'll make up our own user to modify, and doesn't make sense if 'user'
+        *   or 'userid' is specified in $params.
+        */
+       protected function doSuccessfulRightsChange(
+               $expectedGroups = 'sysop', array $params = [], User $user = null
+       ) {
+               $expectedGroups = (array)$expectedGroups;
+               $params['action'] = 'userrights';
+
+               if ( !$user ) {
+                       $user = $this->getMutableTestUser()->getUser();
+               }
+
+               $this->assertTrue( TestUserRegistry::isMutable( $user ),
+                       'Immutable user passed to doSuccessfulRightsChange!' );
+
+               if ( !isset( $params['user'] ) && !isset( $params['userid'] ) ) {
+                       $params['user'] = $user->getName();
+               }
+               if ( !isset( $params['add'] ) && !isset( $params['remove'] ) ) {
+                       $params['add'] = 'sysop';
+               }
+
+               $res = $this->doApiRequestWithToken( $params );
+
+               $user->clearInstanceCache();
+               $this->assertSame( $expectedGroups, $user->getGroups() );
+
+               $this->assertArrayNotHasKey( 'warnings', $res[0] );
+       }
+
+       /**
+        * Perform an API userrights request that's expected to fail.
+        *
+        * @param string $expectedException Expected exception text
+        * @param array $params As for doSuccessfulRightsChange()
+        * @param User|null $user As for doSuccessfulRightsChange().  If there's no
+        *   user who will possibly be affected (such as if an invalid username is
+        *   provided in $params), pass null.
+        */
+       protected function doFailedRightsChange(
+               $expectedException, array $params = [], User $user = null
+       ) {
+               $params['action'] = 'userrights';
+
+               $this->setExpectedException( ApiUsageException::class, $expectedException );
+
+               if ( !$user ) {
+                       // If 'user' or 'userid' is specified and $user was not specified,
+                       // the user we're creating now will have nothing to do with the API
+                       // request, but that's okay, since we're just testing that it has
+                       // no groups.
+                       $user = $this->getMutableTestUser()->getUser();
+               }
+
+               $this->assertTrue( TestUserRegistry::isMutable( $user ),
+                       'Immutable user passed to doFailedRightsChange!' );
+
+               if ( !isset( $params['user'] ) && !isset( $params['userid'] ) ) {
+                       $params['user'] = $user->getName();
+               }
+               if ( !isset( $params['add'] ) && !isset( $params['remove'] ) ) {
+                       $params['add'] = 'sysop';
+               }
+               $expectedGroups = $user->getGroups();
+
+               try {
+                       $this->doApiRequestWithToken( $params );
+               } finally {
+                       $user->clearInstanceCache();
+                       $this->assertSame( $expectedGroups, $user->getGroups() );
+               }
+       }
+
+       public function testAdd() {
+               $this->doSuccessfulRightsChange();
+       }
+
+       public function testBlockedWithUserrights() {
+               global $wgUser;
+
+               $block = new Block( [ 'address' => $wgUser, 'by' => $wgUser->getId(), ] );
+               $block->insert();
+
+               try {
+                       $this->doSuccessfulRightsChange();
+               } finally {
+                       $block->delete();
+                       $wgUser->clearInstanceCache();
+               }
+       }
+
+       public function testBlockedWithoutUserrights() {
+               $user = $this->getTestSysop()->getUser();
+
+               $this->setPermissions( true, true );
+
+               $block = new Block( [ 'address' => $user, 'by' => $user->getId() ] );
+               $block->insert();
+
+               try {
+                       $this->doFailedRightsChange( 'You have been blocked from editing.' );
+               } finally {
+                       $block->delete();
+                       $user->clearInstanceCache();
+               }
+       }
+
+       public function testAddMultiple() {
+               $this->doSuccessfulRightsChange(
+                       [ 'bureaucrat', 'sysop' ],
+                       [ 'add' => 'bureaucrat|sysop' ]
+               );
+       }
+
+       public function testTooFewExpiries() {
+               $this->doFailedRightsChange(
+                       '2 expiry timestamps were provided where 3 were needed.',
+                       [ 'add' => 'sysop|bureaucrat|bot', 'expiry' => 'infinity|tomorrow' ]
+               );
+       }
+
+       public function testTooManyExpiries() {
+               $this->doFailedRightsChange(
+                       '3 expiry timestamps were provided where 2 were needed.',
+                       [ 'add' => 'sysop|bureaucrat', 'expiry' => 'infinity|tomorrow|never' ]
+               );
+       }
+
+       public function testInvalidExpiry() {
+               $this->doFailedRightsChange( 'Invalid expiry time', [ 'expiry' => 'yummy lollipops!' ] );
+       }
+
+       public function testMultipleInvalidExpiries() {
+               $this->doFailedRightsChange(
+                       'Invalid expiry time "foo".',
+                       [ 'add' => 'sysop|bureaucrat', 'expiry' => 'foo|bar' ]
+               );
+       }
+
+       public function testWithTag() {
+               ChangeTags::defineTag( 'custom tag' );
+
+               $user = $this->getMutableTestUser()->getUser();
+
+               $this->doSuccessfulRightsChange( 'sysop', [ 'tags' => 'custom tag' ], $user );
+
+               $dbr = wfGetDB( DB_REPLICA );
+               $this->assertSame(
+                       'custom tag',
+                       $dbr->selectField(
+                               [ 'change_tag', 'logging' ],
+                               'ct_tag',
+                               [
+                                       'ct_log_id = log_id',
+                                       'log_namespace' => NS_USER,
+                                       'log_title' => strtr( $user->getName(), ' ', '_' )
+                               ],
+                               __METHOD__
+                       )
+               );
+       }
+
+       public function testWithoutTagPermission() {
+               global $wgGroupPermissions;
+
+               ChangeTags::defineTag( 'custom tag' );
+
+               $this->stashMwGlobals( 'wgGroupPermissions' );
+               $wgGroupPermissions['user']['applychangetags'] = false;
+
+               $this->doFailedRightsChange(
+                       'You do not have permission to apply change tags along with your changes.',
+                       [ 'tags' => 'custom tag' ]
+               );
+       }
+
+       public function testNonexistentUser() {
+               $this->doFailedRightsChange(
+                       'There is no user by the name "Nonexistent user". Check your spelling.',
+                       [ 'user' => 'Nonexistent user' ]
+               );
+       }
+
+       public function testWebToken() {
+               $sysop = $this->getTestSysop()->getUser();
+               $user = $this->getMutableTestUser()->getUser();
+
+               $token = $sysop->getEditToken( $user->getName() );
+
+               $res = $this->doApiRequest( [
+                       'action' => 'userrights',
+                       'user' => $user->getName(),
+                       'add' => 'sysop',
+                       'token' => $token,
+               ] );
+
+               $user->clearInstanceCache();
+               $this->assertSame( [ 'sysop' ], $user->getGroups() );
+
+               $this->assertArrayNotHasKey( 'warnings', $res[0] );
+       }
+
+       /**
+        * Helper for testCanProcessExpiries that returns a mock ApiUserrights that either can or cannot
+        * process expiries.  Although the regular page can process expiries, we use a mock here to
+        * ensure that it's the result of canProcessExpiries() that makes a difference, and not some
+        * error in the way we construct the mock.
+        *
+        * @param bool $canProcessExpiries
+        */
+       private function getMockForProcessingExpiries( $canProcessExpiries ) {
+               $sysop = $this->getTestSysop()->getUser();
+               $user = $this->getMutableTestUser()->getUser();
+
+               $token = $sysop->getEditToken( 'userrights' );
+
+               $main = new ApiMain( new FauxRequest( [
+                       'action' => 'userrights',
+                       'user' => $user->getName(),
+                       'add' => 'sysop',
+                       'token' => $token,
+               ] ) );
+
+               $mockUserRightsPage = $this->getMockBuilder( UserrightsPage::class )
+                       ->setMethods( [ 'canProcessExpiries' ] )
+                       ->getMock();
+               $mockUserRightsPage->method( 'canProcessExpiries' )->willReturn( $canProcessExpiries );
+
+               $mockApi = $this->getMockBuilder( ApiUserrights::class )
+                       ->setConstructorArgs( [ $main, 'userrights' ] )
+                       ->setMethods( [ 'getUserRightsPage' ] )
+                       ->getMock();
+               $mockApi->method( 'getUserRightsPage' )->willReturn( $mockUserRightsPage );
+
+               return $mockApi;
+       }
+
+       public function testCanProcessExpiries() {
+               $mock1 = $this->getMockForProcessingExpiries( true );
+               $this->assertArrayHasKey( 'expiry', $mock1->getAllowedParams() );
+
+               $mock2 = $this->getMockForProcessingExpiries( false );
+               $this->assertArrayNotHasKey( 'expiry', $mock2->getAllowedParams() );
+       }
+
+       /**
+        * Tests adding and removing various groups with various permissions.
+        *
+        * @dataProvider addAndRemoveGroupsProvider
+        * @param array|null $permissions [ [ $wgAddGroups, $wgRemoveGroups ] ] or null for 'userrights'
+        *   to be set in $wgGroupPermissions
+        * @param array $groupsToChange [ [ groups to add ], [ groups to remove ] ]
+        * @param array $expectedGroups Array of expected groups
+        */
+       public function testAddAndRemoveGroups(
+               array $permissions = null, array $groupsToChange, array $expectedGroups
+       ) {
+               if ( $permissions !== null ) {
+                       $this->setPermissions( $permissions[0], $permissions[1] );
+               }
+
+               $params = [
+                       'add' => implode( '|', $groupsToChange[0] ),
+                       'remove' => implode( '|', $groupsToChange[1] ),
+               ];
+
+               // We'll take a bot so we have a group to remove
+               $user = $this->getMutableTestUser( [ 'bot' ] )->getUser();
+
+               $this->doSuccessfulRightsChange( $expectedGroups, $params, $user );
+       }
+
+       public function addAndRemoveGroupsProvider() {
+               return [
+                       'Simple add' => [
+                               [ [ 'sysop' ], [] ],
+                               [ [ 'sysop' ], [] ],
+                               [ 'bot', 'sysop' ]
+                       ], 'Add with only remove permission' => [
+                               [ [], [ 'sysop' ] ],
+                               [ [ 'sysop' ], [] ],
+                               [ 'bot' ],
+                       ], 'Add with global remove permission' => [
+                               [ [], true ],
+                               [ [ 'sysop' ], [] ],
+                               [ 'bot' ],
+                       ], 'Simple remove' => [
+                               [ [], [ 'bot' ] ],
+                               [ [], [ 'bot' ] ],
+                               [],
+                       ], 'Remove with only add permission' => [
+                               [ [ 'bot' ], [] ],
+                               [ [], [ 'bot' ] ],
+                               [ 'bot' ],
+                       ], 'Remove with global add permission' => [
+                               [ true, [] ],
+                               [ [], [ 'bot' ] ],
+                               [ 'bot' ],
+                       ], 'Add and remove same new group' => [
+                               null,
+                               [ [ 'sysop' ], [ 'sysop' ] ],
+                               // The userrights code does removals before adds, so it doesn't remove the sysop
+                               // group here and only adds it.
+                               [ 'bot', 'sysop' ],
+                       ], 'Add and remove same existing group' => [
+                               null,
+                               [ [ 'bot' ], [ 'bot' ] ],
+                               // But here it first removes the existing group and then re-adds it.
+                               [ 'bot' ],
+                       ],
+               ];
+       }
+}
index 211eba0..cc16248 100644 (file)
@@ -879,14 +879,10 @@ class AuthManagerTest extends \MediaWikiTestCase {
                        );
                        $mocks[$key]->expects( $this->any() )->method( 'getUniqueId' )
                                ->will( $this->returnValue( $key ) );
-                       $mocks[$key . '2'] = $this->getMockForAbstractClass(
-                               "MediaWiki\\Auth\\$class", [], "Mock$class"
-                       );
+                       $mocks[$key . '2'] = $this->getMockForAbstractClass( "MediaWiki\\Auth\\$class" );
                        $mocks[$key . '2']->expects( $this->any() )->method( 'getUniqueId' )
                                ->will( $this->returnValue( $key . '2' ) );
-                       $mocks[$key . '3'] = $this->getMockForAbstractClass(
-                               "MediaWiki\\Auth\\$class", [], "Mock$class"
-                       );
+                       $mocks[$key . '3'] = $this->getMockForAbstractClass( "MediaWiki\\Auth\\$class" );
                        $mocks[$key . '3']->expects( $this->any() )->method( 'getUniqueId' )
                                ->will( $this->returnValue( $key . '3' ) );
                }
@@ -1901,9 +1897,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
                                ) );
 
                        for ( $i = 2; $i <= 3; $i++ ) {
-                               $mocks[$key . $i] = $this->getMockForAbstractClass(
-                                       "MediaWiki\\Auth\\$class", [], "Mock$class"
-                               );
+                               $mocks[$key . $i] = $this->getMockForAbstractClass( "MediaWiki\\Auth\\$class" );
                                $mocks[$key . $i]->expects( $this->any() )->method( 'getUniqueId' )
                                        ->will( $this->returnValue( $key . $i ) );
                                $mocks[$key . $i]->expects( $this->any() )->method( 'testUserForCreation' )
@@ -2368,9 +2362,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
                $mocks = [];
                foreach ( [ 'pre', 'primary', 'secondary' ] as $key ) {
                        $class = ucfirst( $key ) . 'AuthenticationProvider';
-                       $mocks[$key] = $this->getMockForAbstractClass(
-                               "MediaWiki\\Auth\\$class", [], "Mock$class"
-                       );
+                       $mocks[$key] = $this->getMockForAbstractClass( "MediaWiki\\Auth\\$class" );
                        $mocks[$key]->expects( $this->any() )->method( 'getUniqueId' )
                                ->will( $this->returnValue( $key ) );
                }
@@ -2848,9 +2840,11 @@ class AuthManagerTest extends \MediaWikiTestCase {
                $mocks = [];
                foreach ( [ 'pre', 'primary', 'secondary' ] as $key ) {
                        $class = ucfirst( $key ) . 'AuthenticationProvider';
-                       $mocks[$key] = $this->getMockForAbstractClass(
-                               "MediaWiki\\Auth\\$class", [], "Mock$class"
-                       );
+                       $mocks[$key] = $this->getMockBuilder( "MediaWiki\\Auth\\$class" )
+                               ->setMethods( [
+                                       'getUniqueId', 'getAuthenticationRequests', 'providerAllowsAuthenticationDataChange',
+                               ] )
+                               ->getMockForAbstractClass();
                        $mocks[$key]->expects( $this->any() )->method( 'getUniqueId' )
                                ->will( $this->returnValue( $key ) );
                        $mocks[$key]->expects( $this->any() )->method( 'getAuthenticationRequests' )
@@ -2868,9 +2862,12 @@ class AuthManagerTest extends \MediaWikiTestCase {
                        PrimaryAuthenticationProvider::TYPE_LINK
                ] as $type ) {
                        $class = 'PrimaryAuthenticationProvider';
-                       $mocks["primary-$type"] = $this->getMockForAbstractClass(
-                               "MediaWiki\\Auth\\$class", [], "Mock$class"
-                       );
+                       $mocks["primary-$type"] = $this->getMockBuilder( "MediaWiki\\Auth\\$class" )
+                               ->setMethods( [
+                                       'getUniqueId', 'accountCreationType', 'getAuthenticationRequests',
+                                       'providerAllowsAuthenticationDataChange',
+                               ] )
+                               ->getMockForAbstractClass();
                        $mocks["primary-$type"]->expects( $this->any() )->method( 'getUniqueId' )
                                ->will( $this->returnValue( "primary-$type" ) );
                        $mocks["primary-$type"]->expects( $this->any() )->method( 'accountCreationType' )
@@ -2885,9 +2882,12 @@ class AuthManagerTest extends \MediaWikiTestCase {
                        $this->primaryauthMocks[] = $mocks["primary-$type"];
                }
 
-               $mocks['primary2'] = $this->getMockForAbstractClass(
-                       PrimaryAuthenticationProvider::class, [], "MockPrimaryAuthenticationProvider"
-               );
+               $mocks['primary2'] = $this->getMockBuilder( PrimaryAuthenticationProvider::class )
+                       ->setMethods( [
+                               'getUniqueId', 'accountCreationType', 'getAuthenticationRequests',
+                               'providerAllowsAuthenticationDataChange',
+                       ] )
+                       ->getMockForAbstractClass();
                $mocks['primary2']->expects( $this->any() )->method( 'getUniqueId' )
                        ->will( $this->returnValue( 'primary2' ) );
                $mocks['primary2']->expects( $this->any() )->method( 'accountCreationType' )
@@ -3138,9 +3138,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
                $mocks = [];
                foreach ( [ 'primary', 'secondary' ] as $key ) {
                        $class = ucfirst( $key ) . 'AuthenticationProvider';
-                       $mocks[$key] = $this->getMockForAbstractClass(
-                               "MediaWiki\\Auth\\$class", [], "Mock$class"
-                       );
+                       $mocks[$key] = $this->getMockForAbstractClass( "MediaWiki\\Auth\\$class" );
                        $mocks[$key]->expects( $this->any() )->method( 'getUniqueId' )
                                ->will( $this->returnValue( $key ) );
                        $mocks[$key]->expects( $this->any() )->method( 'providerAllowsPropertyChange' )
@@ -3224,8 +3222,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
        public function testAutoCreateFailOnLogin() {
                $username = self::usernameForCreation();
 
-               $mock = $this->getMockForAbstractClass(
-                       PrimaryAuthenticationProvider::class, [], "MockPrimaryAuthenticationProvider" );
+               $mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
                $mock->expects( $this->any() )->method( 'getUniqueId' )->will( $this->returnValue( 'primary' ) );
                $mock->expects( $this->any() )->method( 'beginPrimaryAuthentication' )
                        ->will( $this->returnValue( AuthenticationResponse::newPass( $username ) ) );
index 6ced540..cd48d90 100644 (file)
@@ -67,18 +67,20 @@ class LoadBalancerTest extends MediaWikiTestCase {
                $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_REPLICA also gets the master' );
                $this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on replica" );
 
-               $dbwAuto = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTO );
-               $this->assertFalse( $dbwAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTO" );
+               $dbwAuto = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTOCOMMIT );
+               $this->assertFalse(
+                       $dbwAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTOCOMMIT" );
                $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on master" );
-               $this->assertNotEquals( $dbw, $dbwAuto, "CONN_TRX_AUTO uses separate connection" );
+               $this->assertNotEquals( $dbw, $dbwAuto, "CONN_TRX_AUTOCOMMIT uses separate connection" );
 
-               $dbrAuto = $lb->getConnection( DB_REPLICA, [], false, $lb::CONN_TRX_AUTO );
-               $this->assertFalse( $dbrAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTO" );
+               $dbrAuto = $lb->getConnection( DB_REPLICA, [], false, $lb::CONN_TRX_AUTOCOMMIT );
+               $this->assertFalse(
+                       $dbrAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTOCOMMIT" );
                $this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on replica" );
-               $this->assertNotEquals( $dbr, $dbrAuto, "CONN_TRX_AUTO uses separate connection" );
+               $this->assertNotEquals( $dbr, $dbrAuto, "CONN_TRX_AUTOCOMMIT uses separate connection" );
 
-               $dbwAuto2 = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTO );
-               $this->assertEquals( $dbwAuto2, $dbwAuto, "CONN_TRX_AUTO reuses connections" );
+               $dbwAuto2 = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTOCOMMIT );
+               $this->assertEquals( $dbwAuto2, $dbwAuto, "CONN_TRX_AUTOCOMMIT reuses connections" );
 
                $lb->closeAll();
        }
@@ -135,18 +137,20 @@ class LoadBalancerTest extends MediaWikiTestCase {
                $this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on replica" );
                $this->assertWriteForbidden( $dbr );
 
-               $dbwAuto = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTO );
-               $this->assertFalse( $dbwAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTO" );
+               $dbwAuto = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTOCOMMIT );
+               $this->assertFalse(
+                       $dbwAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTOCOMMIT" );
                $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on master" );
-               $this->assertNotEquals( $dbw, $dbwAuto, "CONN_TRX_AUTO uses separate connection" );
+               $this->assertNotEquals( $dbw, $dbwAuto, "CONN_TRX_AUTOCOMMIT uses separate connection" );
 
-               $dbrAuto = $lb->getConnection( DB_REPLICA, [], false, $lb::CONN_TRX_AUTO );
-               $this->assertFalse( $dbrAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTO" );
+               $dbrAuto = $lb->getConnection( DB_REPLICA, [], false, $lb::CONN_TRX_AUTOCOMMIT );
+               $this->assertFalse(
+                       $dbrAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTOCOMMIT" );
                $this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on replica" );
-               $this->assertNotEquals( $dbr, $dbrAuto, "CONN_TRX_AUTO uses separate connection" );
+               $this->assertNotEquals( $dbr, $dbrAuto, "CONN_TRX_AUTOCOMMIT uses separate connection" );
 
-               $dbwAuto2 = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTO );
-               $this->assertEquals( $dbwAuto2, $dbwAuto, "CONN_TRX_AUTO reuses connections" );
+               $dbwAuto2 = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTOCOMMIT );
+               $this->assertEquals( $dbwAuto2, $dbwAuto, "CONN_TRX_AUTOCOMMIT reuses connections" );
 
                $lb->closeAll();
        }
index 14e2e27..4c0ca04 100644 (file)
@@ -158,7 +158,8 @@ class KafkaHandlerTest extends MediaWikiTestCase {
                        ->method( 'send' )
                        ->will( $this->returnValue( true ) );
                // evil hax
-               TestingAccessWrapper::newFromObject( $mockMethod )->matcher->parametersMatcher =
+               $matcher = TestingAccessWrapper::newFromObject( $mockMethod )->matcher;
+               TestingAccessWrapper::newFromObject( $matcher )->parametersMatcher =
                        new \PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters( [
                                [ $this->anything(), $this->anything(), [ 'words' ] ],
                                [ $this->anything(), $this->anything(), [ 'lines' ] ]
index 0625edd..64dde77 100644 (file)
@@ -387,7 +387,7 @@ class JobQueueTest extends MediaWikiTestCase {
 class JobQueueDBSingle extends JobQueueDB {
        protected function getDB( $index ) {
                $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
-               // Override to not use CONN_TRX_AUTO so that we see the same temporary `job` table
+               // Override to not use CONN_TRX_AUTOCOMMIT so that we see the same temporary `job` table
                return $lb->getConnection( $index, [], $this->wiki );
        }
 }
index bc9d9ea..c3cddc6 100644 (file)
@@ -69,7 +69,7 @@ class DBConnRefTest extends PHPUnit\Framework\TestCase {
 
                $lb->expects( $this->once() )
                        ->method( 'getConnection' )
-                       ->with( DB_MASTER, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTO )
+                       ->with( DB_MASTER, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTOCOMMIT )
                        ->willReturnCallback(
                                function () {
                                        return $this->getDatabaseMock();
@@ -78,7 +78,7 @@ class DBConnRefTest extends PHPUnit\Framework\TestCase {
 
                $ref = new DBConnRef(
                        $lb,
-                       [ DB_MASTER, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTO ]
+                       [ DB_MASTER, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTOCOMMIT ]
                );
 
                $this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
index 378935c..93192d0 100644 (file)
@@ -138,12 +138,15 @@ class DatabaseMysqlBaseTest extends PHPUnit\Framework\TestCase {
                        $db->listViews( '' ) );
        }
 
+       /**
+        * @covers Wikimedia\Rdbms\MySQLMasterPos
+        */
        public function testBinLogName() {
                $pos = new MySQLMasterPos( "db1052.2424/4643", 1 );
 
-               $this->assertEquals( "db1052", $pos->binlog );
+               $this->assertEquals( "db1052", $pos->getLogName() );
                $this->assertEquals( "db1052.2424", $pos->getLogFile() );
-               $this->assertEquals( [ 2424, 4643 ], $pos->pos );
+               $this->assertEquals( [ 2424, 4643 ], $pos->getLogPosition() );
        }
 
        /**
@@ -198,20 +201,20 @@ class DatabaseMysqlBaseTest extends PHPUnit\Framework\TestCase {
                        ],
                        // MySQL GTID style
                        [
-                               new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:23', $now ),
-                               new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:24', $now ),
+                               new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-23', $now ),
+                               new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-24', $now ),
                                true,
                                false
                        ],
                        [
-                               new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', $now ),
-                               new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:100', $now ),
+                               new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-99', $now ),
+                               new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ),
                                true,
                                false
                        ],
                        [
-                               new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', $now ),
-                               new MySQLMasterPos( '1E11FA47-71CA-11E1-9E33-C80AA9429562:100', $now ),
+                               new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-99', $now ),
+                               new MySQLMasterPos( '1E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ),
                                false,
                                false
                        ],
@@ -329,17 +332,17 @@ class DatabaseMysqlBaseTest extends PHPUnit\Framework\TestCase {
                        ],
                        [
                                new MySQLMasterPos(
-                                       '2E11FA47-71CA-11E1-9E33-C80AA9429562:5,' .
-                                       '3E11FA47-71CA-11E1-9E33-C80AA9429562:99,' .
-                                       '7E11FA47-71CA-11E1-9E33-C80AA9429562:30',
+                                       '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-5,' .
+                                       '3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99,' .
+                                       '7E11FA47-71CA-11E1-9E33-C80AA9429562:1-30',
                                        1
                                ),
                                new MySQLMasterPos(
-                                       '1E11FA47-71CA-11E1-9E33-C80AA9429562:100,' .
-                                       '3E11FA47-71CA-11E1-9E33-C80AA9429562:66',
+                                       '1E11FA47-71CA-11E1-9E33-C80AA9429562:30-100,' .
+                                       '3E11FA47-71CA-11E1-9E33-C80AA9429562:30-66',
                                        1
                                ),
-                               [ '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ]
+                               [ '3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99' ]
                        ]
                ];
        }
@@ -398,6 +401,160 @@ class DatabaseMysqlBaseTest extends PHPUnit\Framework\TestCase {
                ];
        }
 
+       /**
+        * @dataProvider provideGtidData
+        * @covers Wikimedia\Rdbms\MySQLMasterPos
+        * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getReplicaPos
+        * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getMasterPos
+        */
+       public function testServerGtidTable( $gtable, $rBLtable, $mBLtable, $rGTIDs, $mGTIDs ) {
+               $db = $this->getMockBuilder( DatabaseMysqli::class )
+                       ->disableOriginalConstructor()
+                       ->setMethods( [
+                               'useGTIDs',
+                               'getServerGTIDs',
+                               'getServerRoleStatus',
+                               'getServerId',
+                               'getServerUUID'
+                       ] )
+                       ->getMock();
+
+               $db->method( 'useGTIDs' )->willReturn( true );
+               $db->method( 'getServerGTIDs' )->willReturn( $gtable );
+               $db->method( 'getServerRoleStatus' )->willReturnCallback(
+                       function ( $role ) use ( $rBLtable, $mBLtable ) {
+                               if ( $role === 'SLAVE' ) {
+                                       return $rBLtable;
+                               } elseif ( $role === 'MASTER' ) {
+                                       return $mBLtable;
+                               }
+
+                               return null;
+                       }
+               );
+               $db->method( 'getServerId' )->willReturn( 1 );
+               $db->method( 'getServerUUID' )->willReturn( '2E11FA47-71CA-11E1-9E33-C80AA9429562' );
+
+               if ( is_array( $rGTIDs ) ) {
+                       $this->assertEquals( $rGTIDs, $db->getReplicaPos()->getGTIDs() );
+               } else {
+                       $this->assertEquals( false, $db->getReplicaPos() );
+               }
+               if ( is_array( $mGTIDs ) ) {
+                       $this->assertEquals( $mGTIDs, $db->getMasterPos()->getGTIDs() );
+               } else {
+                       $this->assertEquals( false, $db->getMasterPos() );
+               }
+       }
+
+       public static function provideGtidData() {
+               return [
+                       // MariaDB
+                       [
+                               [
+                                       'gtid_domain_id' => 100,
+                                       'gtid_current_pos' => '100-13-77',
+                                       'gtid_binlog_pos' => '100-13-77',
+                                       'gtid_slave_pos' => null // master
+                               ],
+                               [
+                                       'Relay_Master_Log_File' => 'host.1600',
+                                       'Exec_Master_Log_Pos' => '77'
+                               ],
+                               [
+                                       'File' => 'host.1600',
+                                       'Position' => '77'
+                               ],
+                               [],
+                               [ '100' => '100-13-77' ]
+                       ],
+                       [
+                               [
+                                       'gtid_domain_id' => 100,
+                                       'gtid_current_pos' => '100-13-77',
+                                       'gtid_binlog_pos' => '100-13-77',
+                                       'gtid_slave_pos' => '100-13-77' // replica
+                               ],
+                               [
+                                       'Relay_Master_Log_File' => 'host.1600',
+                                       'Exec_Master_Log_Pos' => '77'
+                               ],
+                               [],
+                               [ '100' => '100-13-77' ],
+                               [ '100' => '100-13-77' ]
+                       ],
+                       [
+                               [
+                                       'gtid_current_pos' => '100-13-77',
+                                       'gtid_binlog_pos' => '100-13-77',
+                                       'gtid_slave_pos' => '100-13-77' // replica
+                               ],
+                               [
+                                       'Relay_Master_Log_File' => 'host.1600',
+                                       'Exec_Master_Log_Pos' => '77'
+                               ],
+                               [],
+                               [ '100' => '100-13-77' ],
+                               [ '100' => '100-13-77' ]
+                       ],
+                       // MySQL
+                       [
+                               [
+                                       'gtid_executed' => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77'
+                               ],
+                               [
+                                       'Relay_Master_Log_File' => 'host.1600',
+                                       'Exec_Master_Log_Pos' => '77'
+                               ],
+                               [], // only a replica
+                               [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
+                                       => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77' ],
+                               // replica/master use same var
+                               [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
+                                       => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77' ],
+                       ],
+                       [
+                               [
+                                       'gtid_executed' => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-49,' .
+                                               '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77'
+                               ],
+                               [
+                                       'Relay_Master_Log_File' => 'host.1600',
+                                       'Exec_Master_Log_Pos' => '77'
+                               ],
+                               [], // only a replica
+                               [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
+                                       => '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77' ],
+                               // replica/master use same var
+                               [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
+                                       => '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77' ],
+                       ],
+                       [
+                               [
+                                       'gtid_executed' => null, // not enabled?
+                                       'gtid_binlog_pos' => null
+                               ],
+                               [
+                                       'Relay_Master_Log_File' => 'host.1600',
+                                       'Exec_Master_Log_Pos' => '77'
+                               ],
+                               [], // only a replica
+                               [], // binlog fallback
+                               false
+                       ],
+                       [
+                               [
+                                       'gtid_executed' => null, // not enabled?
+                                       'gtid_binlog_pos' => null
+                               ],
+                               [], // no replication
+                               [], // no replication
+                               false,
+                               false
+                       ]
+               ];
+       }
+
        /**
         * @covers Wikimedia\Rdbms\MySQLMasterPos
         */
index 929ff0f..5dc7a96 100644 (file)
@@ -6,6 +6,7 @@
 class VersionCheckerTest extends PHPUnit\Framework\TestCase {
 
        use MediaWikiCoversValidator;
+       use PHPUnit4And6Compat;
 
        /**
         * @dataProvider provideCheck
index 5118218..aeaa1ae 100644 (file)
@@ -347,7 +347,7 @@ class ChangesListSpecialPageTest extends AbstractChangesListSpecialPageTestCase
                $user = $this->getTestSysop()->getUser();
                $this->assertConditions(
                        [ # expected
-                               'rc_patrolled = 0',
+                               'rc_patrolled' => 0,
                        ],
                        [
                                'hidepatrolled' => 1,
@@ -361,7 +361,7 @@ class ChangesListSpecialPageTest extends AbstractChangesListSpecialPageTestCase
                $user = $this->getTestSysop()->getUser();
                $this->assertConditions(
                        [ # expected
-                               'rc_patrolled != 0',
+                               'rc_patrolled' => [ 1, 2 ],
                        ],
                        [
                                'hideunpatrolled' => 1,
@@ -371,6 +371,30 @@ class ChangesListSpecialPageTest extends AbstractChangesListSpecialPageTestCase
                );
        }
 
+       public function testRcReviewStatusFilter() {
+               $user = $this->getTestSysop()->getUser();
+               $this->assertConditions(
+                       [ #expected
+                               'rc_patrolled' => 1,
+                       ],
+                       [
+                               'reviewStatus' => 'manual'
+                       ],
+                       "rc conditions: reviewStatus=manual",
+                       $user
+               );
+               $this->assertConditions(
+                       [ #expected
+                               'rc_patrolled' => [ 0, 2 ],
+                       ],
+                       [
+                               'reviewStatus' => 'unpatrolled;auto'
+                       ],
+                       "rc conditions: reviewStatus=unpatrolled;auto",
+                       $user
+               );
+       }
+
        public function testRcHideminorFilter() {
                $this->assertConditions(
                        [ # expected
index f06a353..52b1433 100644 (file)
@@ -12,7 +12,7 @@
 class BatchRowUpdateTest extends MediaWikiTestCase {
 
        public function testWriterBasicFunctionality() {
-               $db = $this->mockDb();
+               $db = $this->mockDb( [ 'update' ] );
                $writer = new BatchRowWriter( $db, 'echo_event' );
 
                $updates = [
@@ -36,17 +36,13 @@ class BatchRowUpdateTest extends MediaWikiTestCase {
        }
 
        public function testReaderBasicIterate() {
-               $db = $this->mockDb();
                $batchSize = 2;
-               $reader = new BatchRowIterator( $db, 'some_table', 'id_field', $batchSize );
-
                $response = $this->genSelectResult( $batchSize, /*numRows*/ 5, function () {
                        static $i = 0;
                        return [ 'id_field' => ++$i ];
                } );
-               $db->expects( $this->exactly( count( $response ) ) )
-                       ->method( 'select' )
-                       ->will( $this->consecutivelyReturnFromSelect( $response ) );
+               $db = $this->mockDbConsecutiveSelect( $response );
+               $reader = new BatchRowIterator( $db, 'some_table', 'id_field', $batchSize );
 
                $pos = 0;
                foreach ( $reader as $rows ) {
@@ -130,7 +126,7 @@ class BatchRowUpdateTest extends MediaWikiTestCase {
        public function testReaderSetFetchColumns(
                $message, array $columns, array $primaryKeys, array $fetchColumns
        ) {
-               $db = $this->mockDb();
+               $db = $this->mockDb( [ 'select' ] );
                $db->expects( $this->once() )
                        ->method( 'select' )
                        // only testing second parameter of Database::select
@@ -202,7 +198,7 @@ class BatchRowUpdateTest extends MediaWikiTestCase {
        }
 
        protected function mockDbConsecutiveSelect( array $retvals ) {
-               $db = $this->mockDb();
+               $db = $this->mockDb( [ 'select', 'addQuotes' ] );
                $db->expects( $this->any() )
                        ->method( 'select' )
                        ->will( $this->consecutivelyReturnFromSelect( $retvals ) );
@@ -238,11 +234,12 @@ class BatchRowUpdateTest extends MediaWikiTestCase {
                return $res;
        }
 
-       protected function mockDb() {
+       protected function mockDb( $methods = [] ) {
                // @TODO: mock from Database
                // FIXME: the constructor normally sets mAtomicLevels and mSrvCache
                $databaseMysql = $this->getMockBuilder( Wikimedia\Rdbms\DatabaseMysqli::class )
                        ->disableOriginalConstructor()
+                       ->setMethods( array_merge( [ 'isOpen', 'getApproximateLagStatus' ], $methods ) )
                        ->getMock();
                $databaseMysql->expects( $this->any() )
                        ->method( 'isOpen' )